vue.config.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/config/index.js')
  4. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  5. const resolve = dir => path.join(__dirname, dir)
  6. // page title
  7. const name = defaultSettings.title || 'vue mobile template'
  8. // const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)
  9. const IS_PROD = false
  10. // externals
  11. // const externals = {
  12. // vue: 'Vue',
  13. // 'vue-router': 'VueRouter',
  14. // vuex: 'Vuex',
  15. // vant: 'vant',
  16. // axios: 'axios'
  17. // }
  18. // CDN外链,会插入到index.html中
  19. // const cdn = {
  20. // // 开发环境
  21. // dev: {
  22. // css: [],
  23. // js: []
  24. // },
  25. // // 生产环境
  26. // build: {
  27. // css: ['https://cdn.jsdelivr.net/npm/vant@2.4.7/lib/index.css'],
  28. // js: [
  29. // 'https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js',
  30. // 'https://cdn.jsdelivr.net/npm/vue-router@3.1.5/dist/vue-router.min.js',
  31. // 'https://cdn.jsdelivr.net/npm/axios@0.19.2/dist/axios.min.js',
  32. // 'https://cdn.jsdelivr.net/npm/vuex@3.1.2/dist/vuex.min.js',
  33. // 'https://cdn.jsdelivr.net/npm/vant@2.4.7/lib/index.min.js'
  34. // ]
  35. // }
  36. // }
  37. module.exports = {
  38. publicPath: './', // 署应用包时的基本 URL。 vue-router hash 模式使用
  39. // publicPath: '/app/', //署应用包时的基本 URL。 vue-router history模式使用
  40. outputDir: 'dist', // 生产环境构建文件的目录
  41. assetsDir: 'static', // outputDir的静态资源(js、css、img、fonts)目录
  42. // lintOnSave: !IS_PROD,
  43. lintOnSave: false,
  44. productionSourceMap: false, // 如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建。
  45. devServer: {
  46. port: 9020, // 端口
  47. open: true, // 启动后打开浏览器
  48. overlay: {
  49. // 当出现编译器错误或警告时,在浏览器中显示全屏覆盖层
  50. warnings: false,
  51. errors: true
  52. }
  53. // proxy: {
  54. // //配置跨域
  55. // '/api': {
  56. // target: "https://test.xxx.com",
  57. // // ws:true,
  58. // changOrigin:true,
  59. // pathRewrite:{
  60. // '^/api':'/'
  61. // }
  62. // }
  63. // }
  64. },
  65. css: {
  66. extract: IS_PROD, // 是否将组件中的 CSS 提取至一个独立的 CSS 文件中 (而不是动态注入到 JavaScript 中的 inline 代码)。
  67. sourceMap: false,
  68. loaderOptions: {
  69. scss: {
  70. // 向全局sass样式传入共享的全局变量, $src可以配置图片cdn前缀
  71. // 详情: https://cli.vuejs.org/guide/css.html#passing-options-to-pre-processor-loaders
  72. prependData: `
  73. @import "assets/css/mixin.scss";
  74. @import "assets/css/variables.scss";
  75. $cdn: "${defaultSettings.$cdn}";
  76. `
  77. }
  78. }
  79. },
  80. configureWebpack: config => {
  81. config.name = name
  82. // 为生产环境修改配置...
  83. // if (IS_PROD) {
  84. // // externals
  85. // config.externals = externals
  86. // }
  87. },
  88. chainWebpack: config => {
  89. config.plugins.delete('preload') // TODO: need test
  90. config.plugins.delete('prefetch') // TODO: need test
  91. // 别名 alias
  92. config.resolve.alias
  93. .set('@', resolve('src'))
  94. .set('assets', resolve('src/assets'))
  95. .set('api', resolve('src/api'))
  96. .set('views', resolve('src/views'))
  97. .set('components', resolve('src/components'))
  98. /**
  99. * 添加CDN参数到htmlWebpackPlugin配置中
  100. */
  101. // config.plugin('html').tap(args => {
  102. // if (IS_PROD) {
  103. // args[0].cdn = cdn.build
  104. // } else {
  105. // args[0].cdn = cdn.dev
  106. // }
  107. // return args
  108. // })
  109. config.module
  110. .rule('language')
  111. .test(/\.(js|vue)$/)
  112. .use('language-hk-loader')
  113. .loader('language-hk-loader')
  114. .end()
  115. /**
  116. * 设置保留空格
  117. */
  118. config.module
  119. .rule('vue')
  120. .use('vue-loader')
  121. .loader('vue-loader')
  122. .tap(options => {
  123. options.compilerOptions.preserveWhitespace = true
  124. return options
  125. })
  126. .end()
  127. /**
  128. * 打包分析
  129. */
  130. if (IS_PROD) {
  131. config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
  132. {
  133. analyzerMode: 'static'
  134. }
  135. ])
  136. }
  137. config
  138. // https://webpack.js.org/configuration/devtool/#development
  139. .when(!IS_PROD, config => config.devtool('cheap-source-map'))
  140. config.when(IS_PROD, config => {
  141. config
  142. .plugin('ScriptExtHtmlWebpackPlugin')
  143. .after('html')
  144. .use('script-ext-html-webpack-plugin', [
  145. {
  146. // 将 runtime 作为内联引入不单独存在
  147. inline: /runtime\..*\.js$/
  148. }
  149. ])
  150. .end()
  151. config.optimization.splitChunks({
  152. chunks: 'all',
  153. cacheGroups: {
  154. // cacheGroups 下可以可以配置多个组,每个组根据test设置条件,符合test条件的模块
  155. commons: {
  156. name: 'chunk-commons',
  157. test: resolve('src/components'),
  158. minChunks: 3, // 被至少用三次以上打包分离
  159. priority: 5, // 优先级
  160. reuseExistingChunk: true // 表示是否使用已有的 chunk,如果为 true 则表示如果当前的 chunk 包含的模块已经被抽取出去了,那么将不会重新生成新的。
  161. },
  162. node_vendors: {
  163. name: 'chunk-libs',
  164. chunks: 'initial', // 只打包初始时依赖的第三方
  165. test: /[\\/]node_modules[\\/]/,
  166. priority: 10
  167. },
  168. vantUI: {
  169. name: 'chunk-vantUI', // 单独将 vantUI 拆包
  170. priority: 20, // 数字大权重到,满足多个 cacheGroups 的条件时候分到权重高的
  171. test: /[\\/]node_modules[\\/]_?vant(.*)/
  172. }
  173. }
  174. })
  175. config.optimization.runtimeChunk('single')
  176. })
  177. }
  178. }