You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.1 KiB

  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. function resolve(dir) {
  5. return path.join(__dirname, dir)
  6. }
  7. const name = defaultSettings.title || 'LE-IT' // page title
  8. // If your port is set to 80,
  9. // use administrator privileges to execute the command line.
  10. // For example, Mac: sudo npm run
  11. // You can change the port by the following method:
  12. // port = 9527 npm run dev OR npm run dev --port = 9527
  13. const port = process.env.port || process.env.npm_config_port || 9090 // dev port
  14. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  15. const productionGzipExtensions = ['js', 'css']
  16. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  17. module.exports = {
  18. /**
  19. * You will need to set publicPath if you plan to deploy your site under a sub path,
  20. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  21. * then publicPath should be set to "/bar/".
  22. * In most cases please use '/' !!!
  23. * Detail: https://cli.vuejs.org/config/#publicpath
  24. */
  25. publicPath: '/',
  26. outputDir: 'dist',
  27. assetsDir: 'static',
  28. lintOnSave: true,
  29. devServer: {
  30. port: port,
  31. overlay: {
  32. warnings: true,
  33. errors: true
  34. }
  35. },
  36. configureWebpack: {
  37. name: name,
  38. resolve: {
  39. alias: {
  40. '@': resolve('src')
  41. }
  42. }
  43. },
  44. chainWebpack(config) {
  45. config.plugin('compression-webpack-plugin').use(
  46. new CompressionWebpackPlugin({
  47. filename: '[path][base].gz',
  48. algorithm: 'gzip',
  49. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'), // 匹配文件名
  50. threshold: 10240, // 对10K以上的数据进行压缩
  51. minRatio: 0.8,
  52. deleteOriginalAssets: false // 是否删除源文件
  53. })
  54. )
  55. // set svg-sprite-loader
  56. config.module
  57. .rule('svg')
  58. .exclude.add(resolve('src/icons'))
  59. .end()
  60. config.module
  61. .rule('icons')
  62. .test(/\.svg$/)
  63. .include.add(resolve('src/icons'))
  64. .end()
  65. .use('svg-sprite-loader')
  66. .loader('svg-sprite-loader')
  67. .options({
  68. symbolId: 'icon-[name]'
  69. })
  70. .end()
  71. // set preserveWhitespace
  72. config.module
  73. .rule('vue')
  74. .use('vue-loader')
  75. .loader('vue-loader')
  76. .tap(options => {
  77. options.compilerOptions.preserveWhitespace = true
  78. return options
  79. })
  80. .end()
  81. config
  82. // https://webpack.js.org/configuration/devtool/#development
  83. .when(process.env.NODE_ENV === 'development',
  84. config => config.devtool('cheap-source-map')
  85. )
  86. config
  87. .when(process.env.NODE_ENV !== 'development',
  88. config => {
  89. config
  90. .plugin('ScriptExtHtmlWebpackPlugin')
  91. .after('html')
  92. .use('script-ext-html-webpack-plugin', [{
  93. // `runtime` must same as runtimeChunk name. default is `runtime`
  94. inline: /runtime\..*\.js$/
  95. }])
  96. .end()
  97. config
  98. .optimization.splitChunks({
  99. chunks: 'all',
  100. cacheGroups: {
  101. libs: {
  102. name: 'chunk-libs',
  103. test: /[\\/]node_modules[\\/]/,
  104. priority: 10,
  105. chunks: 'initial' // only package third parties that are initially dependent
  106. },
  107. elementUI: {
  108. name: 'chunk-elementUI', // split elementUI into a single package
  109. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  110. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  111. },
  112. commons: {
  113. name: 'chunk-commons',
  114. test: resolve('src/components'), // can customize your rules
  115. minChunks: 3, // minimum common number
  116. priority: 5,
  117. reuseExistingChunk: true
  118. }
  119. }
  120. })
  121. config.optimization.runtimeChunk('single')
  122. }
  123. )
  124. },
  125. productionSourceMap: false
  126. }