vue.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const path = require('path');
  2. const Setting = require('./src/setting.env');
  3. // 引入打包分析文件
  4. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  5. // 引入Gzip压缩文件
  6. const CompressionPlugin = require('compression-webpack-plugin');
  7. // 引入js打包工具
  8. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  9. const resolve = (dir) => {
  10. return path.join(__dirname, dir);
  11. };
  12. // 项目部署基础
  13. // 默认情况下,我们假设你的应用将被部署在域的根目录下,
  14. // 例如:https://www.my-app.com/
  15. // 默认:'/'
  16. // 如果您的应用程序部署在子路径中,则需要在这指定子路径
  17. // 例如:https://www.foobar.com/my-app/
  18. // 需要将它改为'/my-app/'
  19. // iview-admin线上演示打包路径: https://file.iviewui.com/admin-dist/
  20. const BASE_URL = process.env.NODE_ENV === 'production' ? '/' : '/';
  21. const env = process.env.NODE_ENV;
  22. module.exports = {
  23. // Project deployment base
  24. // By default we assume your app will be deployed at the root of a domain,
  25. // e.g. https://www.my-app.com/
  26. // If your app is deployed at a sub-path, you will need to specify that
  27. // sub-path here. For example, if your app is deployed at
  28. // https://www.foobar.com/my-app/
  29. // then change this to '/my-app/'
  30. outputDir: Setting.outputDir,
  31. runtimeCompiler: true,
  32. productionSourceMap: false, //关闭生产环境下的SourceMap映射文件
  33. baseUrl: BASE_URL,
  34. // tweak internal webpack configuration.
  35. // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
  36. // 如果你不需要使用eslint,把lintOnSave设为false即可
  37. lintOnSave: false,
  38. // 打包优化
  39. // 打包优化
  40. configureWebpack: (config) => {
  41. const pluginsPro = [new BundleAnalyzerPlugin()];
  42. pluginsPro.push(
  43. new CompressionPlugin({
  44. algorithm: 'gzip',
  45. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  46. minRatio: 0.8, // 压缩率小于1才会压缩
  47. threshold: 10240, // 对超过10k的数据压缩
  48. deleteOriginalAssets: false, // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  49. }),
  50. );
  51. pluginsPro.push(
  52. // js文件压缩
  53. new UglifyJsPlugin({
  54. uglifyOptions: {
  55. compress: {
  56. drop_debugger: true,
  57. drop_console: true, //生产环境自动删除console
  58. pure_funcs: ['console.log'], //移除console
  59. },
  60. },
  61. sourceMap: false,
  62. parallel: true, //使用多进程并行运行来提高构建速度。默认并发运行数:os.cpus().length - 1。
  63. }),
  64. );
  65. if (process.env.NODE_ENV === 'production') {
  66. config.plugins = [...config.plugins, ...pluginsPro];
  67. }
  68. if (process.env.NODE_ENV === 'production') {
  69. // 开启分离js
  70. // config.optimization = {
  71. // runtimeChunk: 'single',
  72. // splitChunks: {
  73. // chunks: 'all',
  74. // maxInitialRequests: Infinity,
  75. // minSize: 20000,
  76. // cacheGroups: {
  77. // vendor: {
  78. // test: /[\\/]node_modules[\\/]/,
  79. // name(module) {
  80. // // get the name. E.g. node_modules/packageName/not/this/part.js
  81. // // or node_modules/packageName
  82. // const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
  83. // // npm package names are URL-safe, but some servers don't like @ symbols
  84. // return `npm.${packageName.replace('@', '')}`;
  85. // },
  86. // },
  87. // },
  88. // },
  89. // };
  90. }
  91. },
  92. chainWebpack: (config) => {
  93. config.resolve.alias
  94. .set('@', resolve('src')) // key,value自行定义,比如.set('@@', resolve('src/components'))
  95. .set('_c', resolve('src/components'));
  96. // 使用 iView Loader
  97. config.module
  98. .rule('vue')
  99. .test(/\.vue$/)
  100. .use('iview-loader')
  101. .loader('iview-loader')
  102. .tap(() => {
  103. return Setting.iviewLoaderOptions;
  104. })
  105. .end();
  106. // 重新设置 alias
  107. config.resolve.alias.set('@api', resolve('src/api'));
  108. // node
  109. config.node.set('__dirname', true).set('__filename', true);
  110. // 判断是否需要加入模拟数据
  111. const entry = config.entry('app');
  112. if (Setting.isMock) {
  113. entry.add('@/mock').end();
  114. }
  115. },
  116. // 设为false打包时不生成.map文件
  117. productionSourceMap: false,
  118. // 这里写你调用接口的基础路径,来解决跨域,如果设置了代理,那你本地开发环境的axios的baseUrl要写为 '' ,即空字符串
  119. // devServer: {
  120. // proxy: 'localhost:3000'
  121. // }
  122. publicPath: env === 'development' ? '/admin/' : '/admin/',
  123. };