vue.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const path = require('path');
  2. const StyleLintPlugin = require('stylelint-webpack-plugin');
  3. function resolve(dir) {
  4. return path.join(__dirname, './', dir);
  5. }
  6. const name = '全球汉语教学平台后台管理系统';
  7. const NODE_ENV = process.env.NODE_ENV;
  8. const port = process.env.port || process.env.npm_config_port || 7979;
  9. let proxy = {};
  10. if (NODE_ENV === 'development') {
  11. proxy = {
  12. [process.env.VUE_APP_BASE_API]: {
  13. target: 'http://gcls.helxsoft.cn/',
  14. changeOrigin: true,
  15. pathRewrite: {
  16. ['^' + process.env.VUE_APP_BASE_API]: ''
  17. }
  18. }
  19. };
  20. }
  21. // 配置项说明 https://cli.vuejs.org/config/
  22. module.exports = {
  23. publicPath: NODE_ENV === 'development' ? '/' : './',
  24. outputDir: 'dist',
  25. assetsDir: 'static',
  26. lintOnSave: false,
  27. runtimeCompiler: true,
  28. productionSourceMap: false,
  29. devServer: {
  30. port,
  31. open: true, // 默认false true自动打开网页
  32. overlay: {
  33. warnings: false,
  34. errors: true
  35. },
  36. proxy
  37. },
  38. css: {
  39. loaderOptions: {
  40. scss: {
  41. // 为 scss 配置共享全局变量
  42. additionalData: '@import "./src/styles/variables.scss";'
  43. }
  44. }
  45. },
  46. configureWebpack: {
  47. name,
  48. // 配置路径别名
  49. resolve: {
  50. alias: {
  51. '@': resolve('src')
  52. }
  53. },
  54. devtool: NODE_ENV === 'development' ? 'source-map' : '',
  55. // stylelint 配置
  56. plugins: [
  57. new StyleLintPlugin({
  58. files: ['**/*.{vue,htm,html,css,sss,less,scss,sass}'],
  59. fix: true, // 是否自动修复
  60. cache: true // 是否缓存
  61. })
  62. ]
  63. },
  64. chainWebpack(config) {
  65. // 启用预加载,提高首屏加载速度
  66. config.plugin('preload').tap(() => [
  67. {
  68. rel: 'preload',
  69. // 忽略 runtime.js
  70. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  71. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  72. include: 'initial'
  73. }
  74. ]);
  75. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  76. // config.plugins.delete('prefetch');
  77. // 设置 svg-sprite-loader
  78. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  79. config.module
  80. .rule('icons')
  81. .test(/\.svg$/)
  82. .include.add(resolve('src/icons'))
  83. .end()
  84. .use('svg-sprite-loader')
  85. .loader('svg-sprite-loader')
  86. .options({
  87. symbolId: 'icon-[name]'
  88. })
  89. .end();
  90. config.when(NODE_ENV !== 'development', () => {
  91. config
  92. .plugin('ScriptExtHtmlWebpackPlugin')
  93. .after('html')
  94. .use('script-ext-html-webpack-plugin', [
  95. {
  96. // `runtime`必须与runtimeChunk名称相同。默认是“运行时”
  97. inline: /runtime\..*\.js$/
  98. }
  99. ])
  100. .end();
  101. config.optimization.splitChunks({
  102. chunks: 'all',
  103. cacheGroups: {
  104. libs: {
  105. name: 'chunk-libs',
  106. test: /[\\/]node_modules[\\/]/,
  107. priority: 10,
  108. chunks: 'initial' // 仅打包最初依赖的第三方
  109. },
  110. elementUI: {
  111. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  112. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  113. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  114. },
  115. commons: {
  116. name: 'chunk-commons',
  117. test: resolve('src/components'), // 可以自定义规则
  118. minChunks: 3, // 最小公用数
  119. priority: 5,
  120. reuseExistingChunk: true
  121. }
  122. }
  123. });
  124. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  125. config.optimization.runtimeChunk('single');
  126. });
  127. }
  128. };