vue.config.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const CompressionWebpackPlugin = require('compression-webpack-plugin');
  2. const ThemeColorReplacer = require('webpack-theme-color-replacer');
  3. const forElementUI = require('webpack-theme-color-replacer/forElementUI');
  4. const themeUtil = require('webpack-theme-color-replacer/themeUtil');
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const path = require('path'); // 确保引入 path 模块
  7. const productionGzipExtensions = ['js', 'css'];
  8. module.exports = {
  9. publicPath: '/',
  10. outputDir: 'dist', // 确保输出目录为 dist
  11. lintOnSave: true,
  12. productionSourceMap: false,
  13. chainWebpack: (config) => {
  14. const entry = config.entry('app');
  15. entry.add('babel-polyfill').add('classlist-polyfill').end();
  16. // 自定义换肤
  17. config
  18. .plugin('webpack-theme-color-replacer')
  19. .use(ThemeColorReplacer)
  20. .tap((options) => {
  21. options[0] = {
  22. matchColors: [
  23. ...forElementUI.getElementUISeries('#409EFF'), // element-ui 主色系列
  24. '#0cdd3a', // 自定义颜色
  25. '#c655dd'
  26. ], // 需要全 CSS 查找的颜色数组,支持 RGB 和 HSL。
  27. changeSelector: themeUtil.changeSelector,
  28. fileName: 'css/theme-colors-[contenthash:8].css', // 输出 CSS 文件名,支持 [contenthash] 和 [hash]。
  29. injectCss: false, // 将 CSS 文本注入到 JS 文件中,不需要再下载 `theme-colors-xxx.css`。
  30. isJsUgly: process.env.NODE_ENV !== 'development'
  31. };
  32. return options;
  33. });
  34. },
  35. css: {
  36. loaderOptions: {
  37. css: {
  38. url: {
  39. filter: (url, resourcePath) => {
  40. // 如果 URL 以 '/' 开头,则不让 Webpack 处理
  41. if (url.startsWith('/')) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. }
  47. }
  48. }
  49. },
  50. configureWebpack: (config) => {
  51. if (process.env.NODE_ENV === 'production') {
  52. return {
  53. plugins: [
  54. new CompressionWebpackPlugin({
  55. // 修改 filename 配置,确保包含原始文件的扩展名,避免命名冲突
  56. filename: '[path][base].gz', // 使用 [base] 包含文件名及扩展名,如 index.js.gz
  57. algorithm: 'gzip',
  58. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  59. threshold: 1024, // 仅压缩大于1KB的文件
  60. minRatio: 0.99, // 压缩率低于0.99时启用
  61. deleteOriginalAssets: false // 建议保留原始文件,避免删除后可能导致的问题
  62. }),
  63. new MiniCssExtractPlugin({
  64. ignoreOrder: true // 忽略 CSS 顺序警告
  65. })
  66. ],
  67. // 可选:优化输出路径,确保所有文件输出到统一目录,避免路径冲突
  68. output: {
  69. filename: 'js/[name].[contenthash].js',
  70. chunkFilename: 'js/[name].[contenthash].js'
  71. },
  72. // 可选:增加 stats 以便调试
  73. stats: 'normal'
  74. };
  75. }
  76. },
  77. // 配置转发代理
  78. devServer: {
  79. client: {
  80. overlay: false
  81. },
  82. allowedHosts: 'all', // 替代 disableHostCheck: true,允许所有主机访问
  83. port: 8080,
  84. proxy: {
  85. '/': {
  86. target: process.env.VUE_APP_URL,
  87. ws: false, // 需要 WebSocket 时设置为 true
  88. pathRewrite: {
  89. '^/': '/'
  90. }
  91. }
  92. // 3.5 以后不需要再配置
  93. }
  94. }
  95. };