vue.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const webpack = require('webpack');
  5. const sass = require('sass')
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || 'wolf console' // page title
  10. // If your port is set to 80,
  11. // use administrator privileges to execute the command line.
  12. // For example, Mac: sudo npm run
  13. // You can change the port by the following method:
  14. // port = 12188 npm run dev OR npm run dev --port = 12188
  15. const port = process.env.port || 12188 // dev port
  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: process.env.NODE_ENV === 'development',
  29. productionSourceMap: false,
  30. transpileDependencies: true,
  31. css: {
  32. loaderOptions: {
  33. sass: {
  34. implementation: sass,
  35. additionalData: `
  36. @use "sass:math";
  37. `,
  38. sassOptions: {
  39. indentedSyntax: false,
  40. logger: sass.Logger.silent
  41. }
  42. }
  43. },
  44. extract: true,
  45. sourceMap: true
  46. },
  47. configureWebpack: {
  48. // provide the app's title in webpack's name field, so that
  49. // it can be accessed in index.html to inject the correct title.
  50. name: name,
  51. plugins: [
  52. new webpack.ProvidePlugin({
  53. process: 'process/browser',
  54. }),
  55. ],
  56. resolve: {
  57. alias: {
  58. '@': resolve('src'),
  59. },
  60. fallback: {
  61. path: require.resolve("path-browserify"),
  62. fs: false,
  63. crypto: require.resolve("crypto-browserify"),
  64. stream: require.resolve("stream-browserify"),
  65. },
  66. },
  67. module: {
  68. rules: [
  69. {
  70. test: /\.js$/,
  71. exclude: /node_modules/,
  72. use: {
  73. loader: 'babel-loader',
  74. options: {
  75. presets: [
  76. ['@vue/cli-plugin-babel/preset', {
  77. useBuiltIns: 'usage',
  78. corejs: 3,
  79. exclude: [] // 确保这里是空数组
  80. }]
  81. ],
  82. plugins: [
  83. '@babel/plugin-proposal-optional-chaining',
  84. '@babel/plugin-proposal-nullish-coalescing-operator'
  85. ]
  86. }
  87. }
  88. }
  89. ]
  90. },
  91. // output: {
  92. // clean: false
  93. // }
  94. },
  95. devServer: {
  96. host: '0.0.0.0',
  97. port: port, // CHANGE YOUR PORT HERE!
  98. https: false,
  99. },
  100. chainWebpack(config) {
  101. config.plugins.delete('preload') // TODO: need test
  102. config.plugins.delete('prefetch') // TODO: need test
  103. // 修正 SVG 规则
  104. config.module
  105. .rule('svg')
  106. .exclude.add(resolve('src/icons'))
  107. .end()
  108. config.module
  109. .rule('icons')
  110. .test(/\.svg$/)
  111. .include.add(resolve('src/icons'))
  112. .end()
  113. .use('svg-sprite-loader')
  114. .loader('svg-sprite-loader')
  115. .options({
  116. symbolId: 'icon-[name]'
  117. })
  118. .end()
  119. .use('svgo-loader')
  120. .loader('svgo-loader')
  121. .options({
  122. plugins: [
  123. {
  124. name: 'removeAttrs',
  125. params: { attrs: 'fill' }
  126. }
  127. ]
  128. })
  129. .end()
  130. config
  131. .when(process.env.NODE_ENV === 'development',
  132. config => config.devtool('cheap-source-map')
  133. )
  134. config
  135. .when(process.env.NODE_ENV !== 'development',
  136. config => {
  137. config
  138. .plugin('ScriptExtHtmlWebpackPlugin')
  139. .after('html')
  140. .use('script-ext-html-webpack-plugin', [{
  141. // `runtime` must same as runtimeChunk name. default is `runtime`
  142. inline: /runtime\..*\.js$/,
  143. }])
  144. .end()
  145. config
  146. .optimization.splitChunks({
  147. chunks: 'all',
  148. cacheGroups: {
  149. libs: {
  150. name: 'chunk-libs',
  151. test: /[\\/]node_modules[\\/]/,
  152. priority: 10,
  153. chunks: 'initial', // only package third parties that are initially dependent
  154. },
  155. elementUI: {
  156. name: 'chunk-elementUI', // split elementUI into a single package
  157. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  158. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  159. },
  160. commons: {
  161. name: 'chunk-commons',
  162. test: resolve('src/components'), // can customize your rules
  163. minChunks: 3, // minimum common number
  164. priority: 5,
  165. reuseExistingChunk: true,
  166. },
  167. },
  168. })
  169. // config.optimization.runtimeChunk('single')
  170. }
  171. )
  172. // 修改文件名hash长度
  173. config.output
  174. .filename('static/js/[name].[chunkhash:8].js')
  175. .chunkFilename('static/js/[name].[chunkhash:8].js');
  176. config.plugin('extract-css').tap(args => [{
  177. filename: 'static/css/[name].[contenthash:8].css',
  178. chunkFilename: 'static/css/[name].[contenthash:8].css'
  179. }]);
  180. },
  181. }