vue.config.js 7.5 KB

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