vue.config.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  2. const VueFilenameInjector = require('@d2-projects/vue-filename-injector')
  3. const ThemeColorReplacer = require('webpack-theme-color-replacer')
  4. const forElementUI = require('webpack-theme-color-replacer/forElementUI')
  5. const cdnDependencies = require('./dependencies-cdn')
  6. const { chain, set, each } = require('lodash')
  7. const webpack=require('webpack')
  8. // 拼接路径
  9. const resolve = dir => require('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. // 基础路径 注意发布之前要先修改这里
  14. const publicPath = /* process.env.VUE_APP_PUBLIC_PATH || */ '/'
  15. console.log(publicPath,"publicPath")
  16. // 设置不参与构建的库
  17. const externals = {}
  18. cdnDependencies.forEach(pkg => { externals[pkg.name] = pkg.library })
  19. // 引入文件的 cdn 链接
  20. const cdn = {
  21. css: cdnDependencies.map(e => e.css).filter(e => e),
  22. js: cdnDependencies.map(e => e.js).filter(e => e)
  23. }
  24. // 多页配置,默认未开启,如需要请参考 https://cli.vuejs.org/zh/config/#pages
  25. const pages = undefined
  26. // const pages = {
  27. // index: './src/main.js',
  28. // subpage: './src/subpage.js'
  29. // }
  30. module.exports = {
  31. // 根据你的实际情况更改这里
  32. publicPath,
  33. lintOnSave: true,
  34. devServer: {
  35. publicPath, // 和 publicPath 保持一致
  36. disableHostCheck: process.env.NODE_ENV === 'development', // 关闭 host check,方便使用 ngrok 之类的内网转发工具
  37. // proxy: {
  38. // '/': {
  39. // //测试
  40. // // target: 'http://101.36.160.140:31005/smart-grp-basic',
  41. // // target: 'http://172.16.0.6:7070/smart-grp-basic',
  42. // //生产
  43. // // target:"http://23.99.21.201:59887/smart-grp-basic",
  44. // target:"http://101.36.160.140:31006/smart-grp-basic",
  45. // // target: 'http://112.51.248.191:7070',
  46. // // target: '192.168.2.6:7070',
  47. // ws: true,
  48. // changeOrigin: true,
  49. // pathRewrite: {
  50. // '^/': ''
  51. // }
  52. // }
  53. // }
  54. },
  55. css: {
  56. loaderOptions: {
  57. // 设置 scss 公用变量文件
  58. sass: {
  59. prependData: '@import \'~@/assets/style/public.scss\';'
  60. }
  61. }
  62. },
  63. pages,
  64. configureWebpack: config => {
  65. const configNew = {}
  66. if (process.env.NODE_ENV === 'production') {
  67. configNew.externals = externals
  68. configNew.plugins = [
  69. // gzip
  70. new CompressionWebpackPlugin({
  71. filename: '[path].gz[query]',
  72. test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
  73. threshold: 10240,
  74. minRatio: 0.8,
  75. deleteOriginalAssets: false
  76. })
  77. ]
  78. }
  79. return configNew
  80. },
  81. // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
  82. chainWebpack: config => {
  83. /**
  84. * 添加 CDN 参数到 htmlWebpackPlugin 配置中
  85. * 已适配多页
  86. */
  87. const htmlPluginNames = chain(pages).keys().map(page => 'html-' + page).value()
  88. const targetHtmlPluginNames = htmlPluginNames.length ? htmlPluginNames : ['html']
  89. each(targetHtmlPluginNames, name => {
  90. config.plugin(name).tap(options => {
  91. set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? cdn : [])
  92. return options
  93. })
  94. })
  95. /**
  96. * 删除懒加载模块的 prefetch preload,降低带宽压力
  97. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
  98. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
  99. * 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
  100. */
  101. config.plugins
  102. .delete('prefetch')
  103. .delete('preload')
  104. // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
  105. config.resolve
  106. .symlinks(true)
  107. config
  108. .plugin('theme-color-replacer')
  109. .use(ThemeColorReplacer, [{
  110. fileName: 'css/theme-colors.[contenthash:8].css',
  111. matchColors: [
  112. ...forElementUI.getElementUISeries(process.env.VUE_APP_ELEMENT_COLOR) // Element-ui主色系列
  113. ],
  114. 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.
  115. changeSelector: forElementUI.changeSelector
  116. }])
  117. config.plugin('provide').use(webpack.ProvidePlugin,[{
  118. $:"jquery",
  119. jquery:"jquery",
  120. jQuery:"jquery",
  121. 'window.jQuery':"jquery",
  122. }])
  123. config
  124. // 开发环境 sourcemap 不包含列信息
  125. .when(process.env.NODE_ENV === 'development',
  126. config => config.devtool('cheap-source-map')
  127. )
  128. // 预览环境构建 vue-loader 添加 filename
  129. .when(
  130. process.env.VUE_APP_SCOURCE_LINK === 'TRUE',
  131. config => VueFilenameInjector(config, {
  132. propName: process.env.VUE_APP_SOURCE_VIEWER_PROP_NAME
  133. })
  134. )
  135. // markdown
  136. config.module
  137. .rule('md')
  138. .test(/\.md$/)
  139. .use('text-loader')
  140. .loader('text-loader')
  141. .end()
  142. // svg
  143. const svgRule = config.module.rule('svg')
  144. svgRule.uses.clear()
  145. svgRule
  146. .include
  147. .add(resolve('src/assets/svg-icons/icons'))
  148. .end()
  149. .use('svg-sprite-loader')
  150. .loader('svg-sprite-loader')
  151. .options({
  152. symbolId: 'd2-[name]'
  153. })
  154. .end()
  155. // image exclude
  156. const imagesRule = config.module.rule('images')
  157. imagesRule
  158. .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
  159. .exclude
  160. .add(resolve('src/assets/svg-icons/icons'))
  161. .end()
  162. // 重新设置 alias
  163. config.resolve.alias
  164. .set('@api', resolve('src/api'))
  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-chs',
  178. fallbackLocale: 'en',
  179. localeDir: 'locales',
  180. enableInSFC: true
  181. }
  182. }
  183. }