vue.config.js 5.8 KB

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