vue.config.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = '全球中文学习系统 教学系统 Web端';
  7. const NODE_ENV = process.env.NODE_ENV;
  8. const port = process.env.port || process.env.npm_config_port || 7878;
  9. let proxy = {};
  10. if (NODE_ENV === 'development') {
  11. proxy = {
  12. '/api': {
  13. target: 'http://localhost:20088/GCLSLearnWebSI/',
  14. changeOrigin: true,
  15. pathRewrite: {
  16. '^/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: port,
  31. open: true, //默认false true自动打开网页
  32. overlay: {
  33. warnings: false,
  34. errors: true
  35. },
  36. proxy: proxy
  37. },
  38. css: {
  39. loaderOptions: {
  40. scss: {
  41. // 为 scss 配置共享全局变量
  42. additionalData: '@import "./src/styles/variables.scss";'
  43. }
  44. }
  45. },
  46. configureWebpack: {
  47. name: 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: false, // 是否自动修复
  60. cache: true, // 是否缓存
  61. emitErrors: true,
  62. failOnError: false
  63. })
  64. ]
  65. },
  66. chainWebpack(config) {
  67. // 启用预加载,提高首屏加载速度
  68. config.plugin('preload').tap(() => [
  69. {
  70. rel: 'preload',
  71. // 忽略 runtime.js
  72. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  73. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  74. include: 'initial'
  75. }
  76. ]);
  77. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  78. // config.plugins.delete('prefetch');
  79. // 设置 svg-sprite-loader
  80. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  81. config.module
  82. .rule('icons')
  83. .test(/\.svg$/)
  84. .include.add(resolve('src/icons'))
  85. .end()
  86. .use('svg-sprite-loader')
  87. .loader('svg-sprite-loader')
  88. .options({
  89. symbolId: 'icon-[name]'
  90. })
  91. .end();
  92. config.when(NODE_ENV !== 'development', () => {
  93. config
  94. .plugin('ScriptExtHtmlWebpackPlugin')
  95. .after('html')
  96. .use('script-ext-html-webpack-plugin', [
  97. {
  98. // `runtime`必须与runtimeChunk名称相同。默认是“运行时”
  99. inline: /runtime\..*\.js$/
  100. }
  101. ])
  102. .end();
  103. config.optimization.splitChunks({
  104. chunks: 'all',
  105. cacheGroups: {
  106. libs: {
  107. name: 'chunk-libs',
  108. test: /[\\/]node_modules[\\/]/,
  109. priority: 10,
  110. chunks: 'initial' // 仅打包最初依赖的第三方
  111. },
  112. elementUI: {
  113. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  114. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  115. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  116. },
  117. commons: {
  118. name: 'chunk-commons',
  119. test: resolve('src/components'), // 可以自定义规则
  120. minChunks: 3, // 最小公用数
  121. priority: 5,
  122. reuseExistingChunk: true
  123. }
  124. }
  125. });
  126. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  127. config.optimization.runtimeChunk('single');
  128. });
  129. }
  130. };