vue.config.js 3.9 KB

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