vue.config.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. const fs = require('fs');
  2. const path = require('path')
  3. const webpack = require('webpack')
  4. const createThemeColorReplacerPlugin = require('./config/plugin.config')
  5. // gzip 压缩
  6. const compressionWebpackPlugin = require('compression-webpack-plugin');
  7. // js,css 压缩
  8. const TerserPlugin = require('terser-webpack-plugin');
  9. // 版本自动生成
  10. const WebpackVersionPlugin = require('webpack-version-plugin');
  11. const versionConfig = require(resolve('public/version.json'));
  12. function resolve (dir) {
  13. return path.join(__dirname, dir)
  14. }
  15. /**
  16. * check production or preview(pro.loacg.com only)
  17. * @returns {boolean}
  18. */
  19. function isProd () {
  20. return process.env.NODE_ENV === 'production'
  21. }
  22. // 项目部署基础
  23. // 默认情况下,我们假设你的应用将被部署在域的根目录下,
  24. // 例如:https://www.my-app.com/
  25. // 默认:'/'
  26. // 如果您的应用程序部署在子路径中,则需要在这指定子路径
  27. // 例如:https://www.foobar.com/my-app/
  28. // 需要将它改为'/my-app/'
  29. const BASE_URL = isProd ? '/' : '/'
  30. const assetsCDN = {
  31. css: [],
  32. // https://unpkg.com/browse/vue@2.6.10/
  33. js: [
  34. '//vuepkg.oss-cn-beijing.aliyuncs.com/vue.min.js',
  35. '//vuepkg.oss-cn-beijing.aliyuncs.com/vue-router.min.js',
  36. '//vuepkg.oss-cn-beijing.aliyuncs.com/vuex.min.js',
  37. '//vuepkg.oss-cn-beijing.aliyuncs.com/axios.min.js',
  38. // '//vuepkg.oss-cn-beijing.aliyuncs.com/lodash.min.js',
  39. '//vuepkg.oss-cn-beijing.aliyuncs.com/echarts.min.js'
  40. ]
  41. }
  42. // webpack build externals
  43. const prodExternals = {
  44. vue: 'Vue',
  45. 'vue-router': 'VueRouter',
  46. vuex: 'Vuex',
  47. axios: 'axios',
  48. echarts: 'echarts',
  49. // lodash : {
  50. // commonjs: "lodash",
  51. // amd: "lodash",
  52. // root: "_" // indicates global variable
  53. // },
  54. 'mockjs2': 'Mock'
  55. }
  56. // vue.config.js
  57. const vueConfig = {
  58. runtimeCompiler: true, // 动态编译组件
  59. publicPath: BASE_URL,
  60. configureWebpack: {
  61. optimization: isProd() ? {
  62. minimize: true,
  63. minimizer: [new TerserPlugin({
  64. test: /\.js(\?.*)?$/i, //匹配参与压缩的文件
  65. parallel: true, //使用多进程并发运行
  66. terserOptions: { //Terser 压缩配置
  67. output:{comments: false},
  68. compress: {//console删除
  69. pure_funcs: ["console.log"]
  70. }
  71. },
  72. extractComments: true, //将注释剥离到单独的文件中
  73. })],
  74. }:{},
  75. // webpack plugins
  76. plugins: [
  77. // Ignore all locale files of moment.js
  78. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  79. new compressionWebpackPlugin({
  80. //[file] 会被替换成原始资源。[path] 会被替换成原始资源的路径, [query] 会被替换成查询字符串。默认值是 "[path].gz[query]"。
  81. filename: '[path].gz[query]', // 提示compression-webpack-plugin@3.0.0的话asset改为filename
  82. //可以是 function(buf, callback) 或者字符串。对于字符串来说依照 zlib 的算法(或者 zopfli 的算法)。默认值是 "gzip"。
  83. algorithm: 'gzip',
  84. //所有匹配该正则的资源都会被处理。默认值是全部资源。
  85. test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
  86. //只有大小大于该值的资源会被处理。单位是 bytes。默认值是 0。
  87. threshold: 10240,
  88. //只有压缩率小于这个值的资源才会被处理。默认值是 0.8。
  89. minRatio: 0.8
  90. }),
  91. new WebpackVersionPlugin({
  92. // You must set the cb option
  93. cb: function(hashMap) {
  94. if(!isProd()){
  95. versionConfig.version = new Date().getTime();
  96. fs.writeFileSync(resolve('./public/version.json'), JSON.stringify(versionConfig, null, 2));
  97. }
  98. }
  99. })
  100. ],
  101. // if prod is on, add externals
  102. externals: isProd() ? prodExternals : {}
  103. },
  104. chainWebpack: (config) => {
  105. config.resolve.alias
  106. .set('@$', resolve('src'))
  107. const svgRule = config.module.rule('svg')
  108. svgRule.uses.clear()
  109. svgRule
  110. .oneOf('inline')
  111. .resourceQuery(/inline/)
  112. .use('vue-svg-icon-loader')
  113. .loader('vue-svg-icon-loader')
  114. .end()
  115. .end()
  116. .oneOf('external')
  117. .use('file-loader')
  118. .loader('file-loader')
  119. .options({
  120. name: 'assets/[name].[hash:8].[ext]'
  121. })
  122. // if prod is on
  123. // assets require on cdn
  124. if (isProd()) {
  125. config.plugin('html').tap(args => {
  126. args[0].cdn = assetsCDN
  127. return args
  128. })
  129. }
  130. config.optimization && config.optimization.splitChunks({
  131. // 拆包配置
  132. chunks: 'all', //三选一:"initial" 初始化,"all"(默认就是all),"async"(动态加载)
  133. minSize: 30000, // 形成一个新代码块最小的体积,只有 >= minSize 的bundle会被拆分出来 30000
  134. maxSize: 0, //拆分之前最大的数值,默认为0,即不做限制
  135. minChunks: 1, //引入次数,如果为2 那么一个资源最少被引用两次才可以被拆分出来
  136. maxAsyncRequests: 5, // 按需加载的最大并行请求数
  137. maxInitialRequests: 4, // 一个入口最大并行请求数
  138. automaticNameDelimiter: '~', // 文件名的连接符
  139. name: true,
  140. cacheGroups: {
  141. // node_modules模块包
  142. vendors: {
  143. test: /[\\/]node_modules[\\/]/,
  144. name: 'chunk-vendors',
  145. // name(module) {
  146. // const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
  147. // return `chunk.${packageName.replace('@', '')}`;
  148. // },
  149. chunks: 'all',
  150. priority: -10,
  151. },
  152. // UI库单独拆包
  153. antDesign: {
  154. name: 'chunk-antDesign',
  155. priority: 19, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  156. test: /[\\/]node_modules[\\/]_?@ant-design(.*)/
  157. },
  158. antDesignVue: {
  159. name: 'chunk-antDesignVue',
  160. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  161. test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/
  162. },
  163. // 共享模块
  164. common: {
  165. name: 'chunk-common',
  166. minChunks: 2,
  167. maxSize: 1024, //拆分之前最大的数值,默认为0,即不做限制
  168. priority: -20,
  169. reuseExistingChunk: true
  170. }
  171. }
  172. });
  173. // 分析打包大小
  174. if (process.env.npm_config_report) {
  175. config.plugin('webpack-bundle-analyzer')
  176. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  177. .end();
  178. }
  179. },
  180. css: {
  181. sourceMap: false,
  182. loaderOptions: {
  183. less: {
  184. modifyVars: {
  185. // less vars锛宑ustomize ant design theme
  186. 'primary-color': process.env.VUE_APP_THEME_COLOR,
  187. 'link-color': process.env.VUE_APP_THEME_COLOR
  188. // 'border-radius-base': '4px'
  189. },
  190. // do not remove this line
  191. javascriptEnabled: true
  192. }
  193. }
  194. },
  195. devServer: {
  196. // development server port 8000
  197. port: 8000,
  198. disableHostCheck: true,
  199. // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
  200. proxy: {
  201. '/api': {
  202. // target: 'http://192.168.0.103:8503/qpls-md',
  203. target: 'http://p.iscm.360arrow.com/qpls-md',
  204. // ws: false,
  205. ws: true,
  206. changeOrigin: true,
  207. pathRewrite: {
  208. '^/api': ''
  209. }
  210. }
  211. },
  212. watchOptions: {
  213. ignored: [/node_modules/, /public/],
  214. }
  215. },
  216. // disable source map in production
  217. productionSourceMap: false,
  218. assetsDir: 'static',
  219. lintOnSave: undefined,
  220. // babel-loader no-ignore node_modules/*
  221. transpileDependencies: []
  222. }
  223. // preview.pro.loacg.com only do not use in your production;
  224. if (process.env.VUE_APP_PREVIEW === 'true') {
  225. console.log('VUE_APP_PREVIEW', true)
  226. // add `ThemeColorReplacer` plugin to webpack plugins
  227. vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
  228. }
  229. module.exports = vueConfig