vue.config.js 3.1 KB

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