vue.config.js 3.5 KB

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