vue.config.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const path = require('path')
  2. const resolve = (dir) => {
  3. return path.join(__dirname, dir)
  4. }
  5. //读取npm指令 process.argv 获取⾃定义参数
  6. let argvs = process.argv.filter((e) => e.includes('='))
  7. for (const iterator of argvs) {
  8. let diyArg = iterator.split('=')
  9. process.env['VUE_APP_' + diyArg[0].slice(2)] = diyArg[1]
  10. }
  11. process.env.VUE_APP_ENV = process.env.VUE_APP_ENV || process.env.NODE_ENV
  12. const env = require('./env')
  13. const config = require('./src/config')
  14. process.env.VUE_APP_TITLE = config.SYSTEM_NAME
  15. process.env.VUE_APP_PACKETTIME = new Date().toLocaleString()
  16. // 项目部署基础
  17. // 默认情况下,我们假设你的应用将被部署在域的根目录下,
  18. // 例如:https://www.my-app.com/
  19. // 默认:'/'
  20. // 如果您的应用程序部署在子路径中,则需要在这指定子路径
  21. // 例如:https://www.my-app.com/my-app/
  22. // 需要将它改为'/my-app/'
  23. let pages = {
  24. index: {
  25. // page 的入口
  26. entry: 'src/main.js',
  27. // 模板来源
  28. template: 'public/index.html',
  29. // 在 dist/index.html 的输出
  30. filename: 'index.html',
  31. // 当使用 title 选项时,
  32. // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
  33. // title: 'Index Page',
  34. // 在这个页面中包含的块,默认情况下会包含
  35. // 提取出来的通用 chunk 和 vendor chunk。
  36. chunks: ['chunk-libs', 'chunk-vant', 'chunk-commons', 'runtime', 'index'],
  37. },
  38. subpage: {
  39. entry: 'src/main.js',
  40. template: 'public/index-wx.html',
  41. filename: 'index-wx.html',
  42. chunks: ['chunk-libs', 'chunk-vant', 'chunk-commons', 'runtime', 'index'],
  43. },
  44. }
  45. let setting = {
  46. publicPath: env.BASE_URL || '/',
  47. outputDir: 'dist', // 打包名称
  48. // 打包时不生成.map文件
  49. productionSourceMap: false,
  50. pages,
  51. // 如果你不需要使用eslint,把lintOnSave设为false即可
  52. lintOnSave: false,
  53. // 这里写你调用接口的基础路径,来解决跨域,如果设置了代理,那你本地开发环境的axios的baseUrl要写为 '' ,即空字符串
  54. devServer: {
  55. host: '0.0.0.0',
  56. port: 8082,
  57. // 自动打开浏览器
  58. open: true,
  59. proxy: {
  60. // detail: https://cli.vuejs.org/config/#devserver-proxy
  61. ['/api']: {
  62. target: env.BASE_API,
  63. changeOrigin: true,
  64. pathRewrite: {
  65. ['^/api']: '',
  66. },
  67. },
  68. },
  69. disableHostCheck: true,
  70. },
  71. pluginOptions: {
  72. 'style-resources-loader': {
  73. preProcessor: 'less',
  74. patterns: [path.resolve(__dirname, './src/styles/varibles.less')],
  75. },
  76. },
  77. css: {
  78. loaderOptions: {
  79. less: {
  80. modifyVars: {
  81. // 直接覆盖变量
  82. // 或者可以通过 less 文件覆盖(文件路径为绝对路径)
  83. hack: `true; @import "${path.resolve(__dirname, './src/styles/varibles.less')}";`,
  84. },
  85. },
  86. },
  87. },
  88. configureWebpack: {
  89. resolve: {
  90. alias: {
  91. '@': resolve('src'),
  92. },
  93. },
  94. },
  95. chainWebpack(config) {
  96. // set svg-sprite-loader
  97. config.module
  98. .rule('svg')
  99. .exclude.add(resolve('src/assets/icons'))
  100. .end()
  101. config.module
  102. .rule('icons')
  103. .test(/\.svg$/)
  104. .include.add(resolve('src/assets/icons'))
  105. .end()
  106. .use('svg-sprite-loader')
  107. .loader('svg-sprite-loader')
  108. .options({
  109. symbolId: 'icon-[name]',
  110. })
  111. .end()
  112. config.when(process.env.NODE_ENV !== 'development', (config) => {
  113. Object.keys(pages).forEach((element) => {
  114. config.plugin('preload-' + element).tap(() => [
  115. {
  116. rel: 'preload',
  117. // to ignore runtime.js
  118. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/, /subpage\./],
  119. include: 'initial',
  120. },
  121. ])
  122. config
  123. .plugin('ScriptExtHtmlWebpackPlugin')
  124. .after('html-' + element)
  125. .use('script-ext-html-webpack-plugin', [
  126. {
  127. // `runtime` must same as runtimeChunk name. default is `runtime`
  128. inline: /runtime\..*\.js$/,
  129. },
  130. ])
  131. .end()
  132. config.plugins.delete('prefetch-' + element)
  133. })
  134. // config.plugin('preload').tap(() => [
  135. // {
  136. // rel: 'preload',
  137. // // to ignore runtime.js
  138. // fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  139. // include: 'initial',
  140. // },
  141. // ])
  142. // config.plugins.delete('prefetch')
  143. // config
  144. // .plugin('ScriptExtHtmlWebpackPlugin')
  145. // .after('html')
  146. // .use('script-ext-html-webpack-plugin', [
  147. // {
  148. // // `runtime` must same as runtimeChunk name. default is `runtime`
  149. // inline: /runtime\..*\.js$/,
  150. // },
  151. // ])
  152. // .end()
  153. config.optimization.minimizer('terser').tap((options) => {
  154. options[0].terserOptions.compress.drop_console = false
  155. options[0].terserOptions.compress.drop_debugger = true // 默认true
  156. return options
  157. })
  158. config.optimization.splitChunks({
  159. chunks: 'all',
  160. // minSize: 60 * 1024,
  161. // maxSize: 1024 * 1024,
  162. cacheGroups: {
  163. alioss: {
  164. name: 'chunk-ali-oss', // split vant into a single package
  165. priority: 30, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  166. test: /[\\/]node_modules[\\/]_?ali-oss(.*)/, // in order to adapt to cnpm
  167. },
  168. vant: {
  169. name: 'chunk-vant', // split vant into a single package
  170. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  171. test: /[\\/]node_modules[\\/]_?vant(.*)/, // in order to adapt to cnpm
  172. },
  173. libs: {
  174. name: 'chunk-libs',
  175. test: /[\\/]node_modules[\\/]/,
  176. priority: 10,
  177. chunks: 'initial', // only package third parties that are initially dependent
  178. enforce: true,
  179. },
  180. // commons: {
  181. // name: 'chunk-commons',
  182. // test: resolve('src/components'), // can customize your rules
  183. // minChunks: 3, // minimum common number
  184. // priority: 5,
  185. // reuseExistingChunk: true,
  186. // },
  187. },
  188. })
  189. config.optimization.runtimeChunk('single')
  190. process.env.npm_config_report &&
  191. config
  192. .plugin('webpack-bundle-analyzer')
  193. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin, [
  194. {
  195. analyzerMode: 'static',
  196. },
  197. ])
  198. .end()
  199. })
  200. },
  201. }
  202. process.env.NODE_ENV === 'development' && delete setting.pages
  203. module.exports = { ...setting }