vue.config.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  2. const cdnDependencies = require('./dependencies-cdn')
  3. const HtmlInjectConfigPlugin = require('./plugins/html-inject-config-plugin')
  4. const path = require('path')
  5. // 拼接路径
  6. const resolve = dir => path.join(__dirname, dir)
  7. // 增加环境变量
  8. process.env.VUE_APP_VERSION = require('./package.json').version
  9. // 基础路径 注意发布之前要先修改这里
  10. const publicPath = process.env.VUE_APP_PUBLIC_PATH || '/'
  11. // If your port is set to 80,
  12. // use administrator privileges to execute the command line.
  13. // For example, Mac: sudo npm run
  14. // You can change the port by the following method:
  15. // port = 9999 npm run dev OR npm run dev --port = 9999
  16. const port = process.env.port || process.env.npm_config_port || 9999 // dev port
  17. // 是否启动Gzip
  18. const enableGzip = process.env.VUE_APP_GZ === 'true'
  19. // 是否启动CDN
  20. const enableCDN = process.env.VUE_APP_CDN === 'true'
  21. // =========CDN 处理=============
  22. // 设置不参与构建的库
  23. const externals = {}
  24. let cdn = {}
  25. if (enableCDN) {
  26. cdnDependencies.forEach(page => { externals[page.name] = page.library })
  27. // 引入文件的 cdn 链接
  28. cdn = {
  29. css: cdnDependencies.map(e => e.css).filter(e => e),
  30. js: cdnDependencies.map(e => e.js).filter(e => e)
  31. }
  32. }
  33. // ======================
  34. module.exports = {
  35. // 根据你的实际情况更改这里
  36. publicPath,
  37. lintOnSave: true,
  38. devServer: {
  39. publicPath, // 和 publicPath 保持一致
  40. port
  41. },
  42. css: {
  43. loaderOptions: {
  44. // 设置 scss 公用变量文件
  45. sass: {
  46. /* sass-loader 8.0语法 */
  47. // prependData: '@import \'~@/styles/public.scss\';',
  48. /* sass-loader 9.0写法,感谢github用户 shaonialife*/
  49. additionalData(content, loaderContext) {
  50. const { resourcePath, rootContext } = loaderContext
  51. const relativePath = path.relative(rootContext, resourcePath)
  52. if (
  53. relativePath.replace(/\\/g, '/') !== 'src/assets/styles/index.scss'
  54. ) {
  55. return '@import "~@/assets/styles/index.scss";' + content
  56. }
  57. return content
  58. }
  59. }
  60. }
  61. },
  62. configureWebpack: config => {
  63. const configNew = {}
  64. if (process.env.NODE_ENV === 'production') {
  65. configNew.externals = externals
  66. if (enableGzip) {
  67. configNew.plugins = [
  68. // gzip
  69. new CompressionWebpackPlugin({
  70. filename: '[path].gz[query]',
  71. test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
  72. threshold: 10240,
  73. minRatio: 0.8,
  74. deleteOriginalAssets: false
  75. })
  76. ]
  77. }
  78. }
  79. if (process.env.NODE_ENV === 'development') {
  80. // 关闭 host check,方便使用 ngrok 之类的内网转发工具
  81. configNew.devServer = {
  82. disableHostCheck: true
  83. }
  84. }
  85. return configNew
  86. },
  87. // 默认情况下 babel-loader 会忽略所有 node_modules 中的文件。如果你想要通过 Babel 显式转译一个依赖,可以在这个选项中列出来。
  88. transpileDependencies: [
  89. 'signature_pad'
  90. ],
  91. // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
  92. chainWebpack: config => {
  93. /**
  94. * 添加 CDN 参数到 htmlWebpackPlugin 配置中
  95. */
  96. config.plugin('html').tap(args => {
  97. args[0].cdn = process.env.NODE_ENV === 'production' && enableCDN ? cdn : []
  98. return args
  99. })
  100. // 在html文件注入配置文件
  101. config.plugin('HtmlInjectConfigPlugin').use(HtmlInjectConfigPlugin, [publicPath])
  102. /**
  103. * 删除懒加载模块的 prefetch preload,降低带宽压力
  104. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
  105. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
  106. * 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
  107. */
  108. config.plugins
  109. .delete('prefetch')
  110. .delete('preload')
  111. // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
  112. config.resolve
  113. .symlinks(true)
  114. config
  115. // 开发环境 sourcemap 不包含列信息
  116. .when(process.env.NODE_ENV === 'development',
  117. config => config.devtool('cheap-source-map')
  118. )
  119. config.when(process.env.NODE_ENV !== 'development',
  120. (config) => {
  121. config.performance.set('hints', false)
  122. config.devtool('none')
  123. config.optimization.splitChunks({
  124. chunks: 'all',
  125. cacheGroups: {
  126. libs: {
  127. name: 'chunk-libs',
  128. test: /[\\/]node_modules[\\/]/,
  129. priority: 10,
  130. chunks: 'initial'
  131. },
  132. elementUI: {
  133. name: 'chunk-vant',
  134. priority: 20,
  135. test: /[\\/]node_modules[\\/]_?vant(.*)/
  136. },
  137. commons: {
  138. name: 'chunk-commons',
  139. test: resolve('src/components'),
  140. minChunks: 3,
  141. priority: 5,
  142. reuseExistingChunk: true
  143. }
  144. }
  145. })
  146. })
  147. // markdown
  148. config.module
  149. .rule('md')
  150. .test(/\.md$/)
  151. .use('text-loader')
  152. .loader('text-loader')
  153. .end()
  154. // image exclude
  155. const imagesRule = config.module.rule('images')
  156. imagesRule
  157. .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
  158. .exclude
  159. .add(resolve('src/assets/svg-icons/icons'))
  160. .end()
  161. // 重新设置 alias
  162. config.resolve.alias
  163. .set('@api', resolve('src/api'))
  164. .set('vue$', 'vue/dist/vue.esm.js')
  165. // 分析工具
  166. if (process.env.npm_config_report) {
  167. config
  168. .plugin('webpack-bundle-analyzer')
  169. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  170. }
  171. },
  172. // 不输出 map 文件
  173. productionSourceMap: false,
  174. // i18n
  175. pluginOptions: {
  176. i18n: {
  177. locale: 'zh-CN',
  178. fallbackLocale: 'zh-CN',
  179. localeDir: 'locales',
  180. enableInSFC: true
  181. }
  182. }
  183. }