vue.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const { defineConfig } = require('@vue/cli-service');
  2. const webpack = require('webpack');
  3. const path = require('path');
  4. const config = require('./config');
  5. const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
  6. const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
  7. const AutoImport = require('unplugin-auto-import/webpack');
  8. const Components = require('unplugin-vue-components/webpack');
  9. const { ElementPlusResolver } = require('unplugin-vue-components/resolvers');
  10. const IS_PRODUCTION = process.env.NODE_ENV === 'production';
  11. // 当前场景
  12. const SCENE = process.env.SCENE;
  13. if (SCENE != null) {
  14. console.log('当前场景:', SCENE);
  15. }
  16. const ENV = getEnv();
  17. module.exports = defineConfig({
  18. transpileDependencies: true,
  19. lintOnSave: false,
  20. indexPath: `${SCENE}.html`,
  21. publicPath: IS_PRODUCTION ? config.publicPath : '/',
  22. outputDir: IS_PRODUCTION && !!SCENE ? `build/${SCENE}` : 'build',
  23. // 根据场景隔离
  24. assetsDir: path.posix.join(config.assetsDir, SCENE || ''),
  25. /**
  26. * 开发服务器配置
  27. */
  28. devServer: {
  29. compress: true,
  30. host: '0.0.0.0',
  31. port: process.env.PORT || 80,
  32. historyApiFallback: true,
  33. allowedHosts: 'all',
  34. },
  35. configureWebpack: {
  36. devtool: 'source-map',
  37. resolve: {
  38. symlinks: false,
  39. alias: {
  40. '@': path.join(__dirname, 'src'),
  41. },
  42. },
  43. plugins: [
  44. new NodePolyfillPlugin(),
  45. // 扩展 process.env
  46. new webpack.DefinePlugin(ENV.define),
  47. AutoImport({
  48. resolvers: [ElementPlusResolver()],
  49. }),
  50. Components({
  51. resolvers: [ElementPlusResolver()],
  52. }),
  53. ],
  54. },
  55. chainWebpack: (webpackConfig) => {
  56. if (SCENE != null) {
  57. const extensions = webpackConfig.resolve.extensions.values();
  58. for (let i = extensions.length - 1; i >= 0; i--) {
  59. webpackConfig.resolve.extensions.prepend(`.${SCENE}${extensions[i]}`);
  60. }
  61. }
  62. webpackConfig.module
  63. .rule('json')
  64. .test(/(.*)\.tr$/)
  65. .use('json-loader')
  66. .loader('json-loader')
  67. .end();
  68. webpackConfig.module.rules.delete('svg'); // 删除默认svg配置
  69. webpackConfig.module
  70. .rule('svg-sprite-loader')
  71. .test(/\.svg$/)
  72. .use('svg-sprite-loader')
  73. .loader('svg-sprite-loader')
  74. .options({
  75. symbolId: 'icon-[name]',
  76. })
  77. .end();
  78. if (IS_PRODUCTION) {
  79. webpackConfig.plugin('loadshReplace').use(new LodashModuleReplacementPlugin());
  80. }
  81. webpackConfig.plugin('html').tap((args) => {
  82. args[0].title = process.env.TITLE;
  83. return args;
  84. });
  85. },
  86. });
  87. /**
  88. * 初始化环境变量
  89. */
  90. function getEnv() {
  91. const constants = config.constants;
  92. // 扩展内置变量
  93. for (const key in constants) {
  94. process.env[`VUE_APP_${key}`] = constants[key];
  95. }
  96. // 内置变量
  97. const vueVariables = ['NODE_ENV', 'BASE_URL'];
  98. const variableNames = [...vueVariables, 'SCENE'];
  99. const variables = [];
  100. for (const key in process.env) {
  101. if (key.startsWith('VUE_APP_')) {
  102. variableNames.push(key);
  103. }
  104. }
  105. variableNames.forEach((name) => {
  106. variables.push(`${name}: "${process.env[name]}"`);
  107. });
  108. return {
  109. variableNames,
  110. variables,
  111. // 用于 definePlugin
  112. define: variableNames
  113. .filter((name) => {
  114. return !name.startsWith('VUE_APP_') && !vueVariables.includes(name);
  115. })
  116. .reduce((prev, cur) => {
  117. prev[`process.env.${cur}`] = JSON.stringify(process.env[cur]);
  118. return prev;
  119. }, {}),
  120. };
  121. }