vue.config.js 8.2 KB

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