vue.config.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. '/api': {
  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://112.51.248.191:7070',
  45. // target: '192.168.2.6:7070',
  46. ws: true,
  47. changeOrigin: true,
  48. pathRewrite: {
  49. '^/api': ''
  50. }
  51. }
  52. }
  53. },
  54. css: {
  55. loaderOptions: {
  56. // 设置 scss 公用变量文件
  57. sass: {
  58. prependData: '@import \'~@/assets/style/public.scss\';'
  59. }
  60. }
  61. },
  62. pages,
  63. configureWebpack: config => {
  64. const configNew = {}
  65. if (process.env.NODE_ENV === 'production') {
  66. configNew.externals = externals
  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. return configNew
  79. },
  80. // 默认设置: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-service/lib/config/base.js
  81. chainWebpack: config => {
  82. /**
  83. * 添加 CDN 参数到 htmlWebpackPlugin 配置中
  84. * 已适配多页
  85. */
  86. const htmlPluginNames = chain(pages).keys().map(page => 'html-' + page).value()
  87. const targetHtmlPluginNames = htmlPluginNames.length ? htmlPluginNames : ['html']
  88. each(targetHtmlPluginNames, name => {
  89. config.plugin(name).tap(options => {
  90. set(options, '[0].cdn', process.env.NODE_ENV === 'production' ? cdn : [])
  91. return options
  92. })
  93. })
  94. /**
  95. * 删除懒加载模块的 prefetch preload,降低带宽压力
  96. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#prefetch
  97. * https://cli.vuejs.org/zh/guide/html-and-static-assets.html#preload
  98. * 而且预渲染时生成的 prefetch 标签是 modern 版本的,低版本浏览器是不需要的
  99. */
  100. config.plugins
  101. .delete('prefetch')
  102. .delete('preload')
  103. // 解决 cli3 热更新失效 https://github.com/vuejs/vue-cli/issues/1559
  104. config.resolve
  105. .symlinks(true)
  106. config
  107. .plugin('theme-color-replacer')
  108. .use(ThemeColorReplacer, [{
  109. fileName: 'css/theme-colors.[contenthash:8].css',
  110. matchColors: [
  111. ...forElementUI.getElementUISeries(process.env.VUE_APP_ELEMENT_COLOR) // Element-ui主色系列
  112. ],
  113. 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.
  114. changeSelector: forElementUI.changeSelector
  115. }])
  116. config.plugin('provide').use(webpack.ProvidePlugin,[{
  117. $:"jquery",
  118. jquery:"jquery",
  119. jQuery:"jquery",
  120. 'window.jQuery':"jquery",
  121. }])
  122. config
  123. // 开发环境 sourcemap 不包含列信息
  124. .when(process.env.NODE_ENV === 'development',
  125. config => config.devtool('cheap-source-map')
  126. )
  127. // 预览环境构建 vue-loader 添加 filename
  128. .when(
  129. process.env.VUE_APP_SCOURCE_LINK === 'TRUE',
  130. config => VueFilenameInjector(config, {
  131. propName: process.env.VUE_APP_SOURCE_VIEWER_PROP_NAME
  132. })
  133. )
  134. // markdown
  135. config.module
  136. .rule('md')
  137. .test(/\.md$/)
  138. .use('text-loader')
  139. .loader('text-loader')
  140. .end()
  141. // svg
  142. const svgRule = config.module.rule('svg')
  143. svgRule.uses.clear()
  144. svgRule
  145. .include
  146. .add(resolve('src/assets/svg-icons/icons'))
  147. .end()
  148. .use('svg-sprite-loader')
  149. .loader('svg-sprite-loader')
  150. .options({
  151. symbolId: 'd2-[name]'
  152. })
  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. // 分析工具
  165. if (process.env.npm_config_report) {
  166. config
  167. .plugin('webpack-bundle-analyzer')
  168. .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  169. }
  170. },
  171. // 不输出 map 文件
  172. productionSourceMap: false,
  173. // i18n
  174. pluginOptions: {
  175. i18n: {
  176. locale: 'zh-chs',
  177. fallbackLocale: 'en',
  178. localeDir: 'locales',
  179. enableInSFC: true
  180. }
  181. }
  182. }