vue.config.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 IS_PRODUCTION = process.env.NODE_ENV === 'production';
  8. // 当前场景
  9. const SCENE = process.env.SCENE;
  10. if (SCENE != null) {
  11. console.log('当前场景:', SCENE);
  12. }
  13. const ENV = getEnv();
  14. module.exports = defineConfig({
  15. transpileDependencies: true,
  16. lintOnSave: false,
  17. publicPath: IS_PRODUCTION ? config.publicPath : '/',
  18. outputDir: IS_PRODUCTION && !!SCENE ? `build/${SCENE}` : 'build',
  19. // 根据场景隔离
  20. assetsDir: path.posix.join(config.assetsDir, SCENE || ''),
  21. configureWebpack: {
  22. devtool: 'source-map',
  23. resolve: {
  24. symlinks: false,
  25. alias: {
  26. '@': path.join(__dirname, 'src'),
  27. },
  28. },
  29. plugins: [
  30. new NodePolyfillPlugin(),
  31. // 扩展 process.env
  32. new webpack.DefinePlugin(ENV.define),
  33. ],
  34. },
  35. chainWebpack: (webpackConfig) => {
  36. if (SCENE != null) {
  37. const extensions = webpackConfig.resolve.extensions.values();
  38. for (let i = extensions.length - 1; i >= 0; i--) {
  39. webpackConfig.resolve.extensions.prepend(`.${SCENE}${extensions[i]}`);
  40. }
  41. }
  42. webpackConfig.module
  43. .rule('json')
  44. .test(/(.*)\.tr$/)
  45. .use('json-loader')
  46. .loader('json-loader')
  47. .end();
  48. webpackConfig.module.rules.delete('svg'); // 删除默认svg配置
  49. webpackConfig.module
  50. .rule('svg-sprite-loader')
  51. .test(/\.svg$/)
  52. .use('svg-sprite-loader')
  53. .loader('svg-sprite-loader')
  54. .options({
  55. symbolId: 'icon-[name]',
  56. })
  57. .end();
  58. if (IS_PRODUCTION) {
  59. webpackConfig.plugin('loadshReplace').use(new LodashModuleReplacementPlugin());
  60. }
  61. },
  62. });
  63. /**
  64. * 初始化环境变量
  65. */
  66. function getEnv() {
  67. const constants = config.constants;
  68. // 扩展内置变量
  69. for (const key in constants) {
  70. process.env[`VUE_APP_${key}`] = constants[key];
  71. }
  72. // 内置变量
  73. const vueVariables = ['NODE_ENV', 'BASE_URL'];
  74. const variableNames = [...vueVariables, 'SCENE'];
  75. const variables = [];
  76. for (const key in process.env) {
  77. if (key.startsWith('VUE_APP_')) {
  78. variableNames.push(key);
  79. }
  80. }
  81. variableNames.forEach((name) => {
  82. variables.push(`${name}: "${process.env[name]}"`);
  83. });
  84. return {
  85. variableNames,
  86. variables,
  87. // 用于 definePlugin
  88. define: variableNames
  89. .filter((name) => {
  90. return !name.startsWith('VUE_APP_') && !vueVariables.includes(name);
  91. })
  92. .reduce((prev, cur) => {
  93. prev[`process.env.${cur}`] = JSON.stringify(process.env[cur]);
  94. return prev;
  95. }, {}),
  96. };
  97. }