vue.config.js 5.0 KB

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