vue.config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 || 9217;
  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. plugins: [
  43. new StyleLintPlugin({
  44. files: ['**/*.{vue,htm,html,css,sss,less,scss,sass}'],
  45. fix: false, // 是否自动修复
  46. cache: true, // 是否缓存
  47. emitErrors: true,
  48. failOnError: false
  49. })
  50. ]
  51. },
  52. chainWebpack(config) {
  53. // 启用预加载,提高首屏加载速度
  54. config.plugin('preload').tap(() => [
  55. {
  56. rel: 'preload',
  57. // 忽略 runtime.js
  58. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  59. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  60. include: 'initial'
  61. }
  62. ]);
  63. // 当页面很多时,它将导致太多无意义的请求
  64. // config.plugins.delete('prefetch');
  65. // 设置 svg-sprite-loader
  66. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  67. config.module
  68. .rule('icons')
  69. .test(/\.svg$/)
  70. .include.add(resolve('src/icons'))
  71. .end()
  72. .use('svg-sprite-loader')
  73. .loader('svg-sprite-loader')
  74. .options({
  75. symbolId: 'icon-[name]'
  76. })
  77. .end();
  78. config.when(process.env.NODE_ENV !== 'development', () => {
  79. config
  80. .plugin('ScriptExtHtmlWebpackPlugin')
  81. .after('html')
  82. .use('script-ext-html-webpack-plugin', [
  83. {
  84. // `runtime`必须与runtimeChunk名称相同。默认是“运行时”
  85. inline: /runtime\..*\.js$/
  86. }
  87. ])
  88. .end();
  89. config.optimization.splitChunks({
  90. chunks: 'all',
  91. cacheGroups: {
  92. libs: {
  93. name: 'chunk-libs',
  94. test: /[\\/]node_modules[\\/]/,
  95. priority: 10,
  96. chunks: 'initial' // 仅打包最初依赖的第三方
  97. },
  98. elementUI: {
  99. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  100. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  101. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  102. },
  103. commons: {
  104. name: 'chunk-commons',
  105. test: resolve('src/components'), // 可以自定义规则
  106. minChunks: 3, // 最小公用数
  107. priority: 5,
  108. reuseExistingChunk: true
  109. }
  110. }
  111. });
  112. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  113. config.optimization.runtimeChunk('single');
  114. });
  115. }
  116. };