vue.config.js 2.9 KB

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