feat: 优化getConfigFile和hardSourePlugin

1. getConfigFile函数直接拿到所有配置文件,包含env和local
2. hardSourcePlugin会监听configFile的变化,更新缓存
This commit is contained in:
万纯 2021-02-05 11:43:24 +08:00
parent 4dfe9e5647
commit d5bfc61b01
5 changed files with 56 additions and 57 deletions

View File

@ -130,41 +130,9 @@ export default class Config {
getUserConfig() {
const configFile = this.getConfigFile();
this.configFile = configFile;
// 潜在问题:
// .local 和 .env 的配置必须有 configFile 才有效
if (configFile) {
let envConfigFile;
if (process.env.FES_ENV) {
const envConfigFileName = this.addAffix(
configFile,
process.env.FES_ENV
);
const fileNameWithoutExt = envConfigFileName.replace(
extname(envConfigFileName),
''
);
envConfigFile = getFile({
base: this.cwd,
fileNameWithoutExt,
type: 'javascript'
}).filename;
if (!envConfigFile) {
throw new Error(
`get user config failed, ${envConfigFile} does not exist, but process.env.FES_ENV is set to ${process.env.FES_ENV}.`
);
}
}
const files = [
configFile,
envConfigFile,
this.localConfig && this.addAffix(configFile, 'local')
]
.filter(f => !!f)
.map(f => join(this.cwd, f))
.filter(f => existsSync(f));
if (configFile.length > 0) {
// clear require cache and set babel register
const requireDeps = files.reduce((memo, file) => {
const requireDeps = configFile.reduce((memo, file) => {
memo = memo.concat(parseRequireDeps(file));
return memo;
}, []);
@ -175,7 +143,7 @@ export default class Config {
});
// require config and merge
return this.mergeConfig(...this.requireConfigs(files));
return this.mergeConfig(...this.requireConfigs(configFile));
}
return {};
}
@ -201,8 +169,41 @@ export default class Config {
getConfigFile() {
// TODO: support custom config file
const configFile = CONFIG_FILES.find(f => existsSync(join(this.cwd, f)));
return configFile ? winPath(configFile) : null;
let configFile = CONFIG_FILES.find(f => existsSync(join(this.cwd, f)));
if (!configFile) return [];
configFile = winPath(configFile);
let envConfigFile;
// 潜在问题:
// .local 和 .env 的配置必须有 configFile 才有效
if (process.env.FES_ENV) {
const envConfigFileName = this.addAffix(
configFile,
process.env.FES_ENV
);
const fileNameWithoutExt = envConfigFileName.replace(
extname(envConfigFileName),
''
);
envConfigFile = getFile({
base: this.cwd,
fileNameWithoutExt,
type: 'javascript'
}).filename;
if (!envConfigFile) {
throw new Error(
`get user config failed, ${envConfigFile} does not exist, but process.env.FES_ENV is set to ${process.env.FES_ENV}.`
);
}
}
const files = [
configFile,
envConfigFile,
this.localConfig && this.addAffix(configFile, 'local')
]
.filter(f => !!f)
.map(f => join(this.cwd, f))
.filter(f => existsSync(f));
return files;
}
getWatchFilesAndDirectories() {

View File

@ -265,6 +265,7 @@ export default class Service extends EventEmitter {
'paths',
'cwd',
'pkg',
'configInstance',
'userConfig',
'config',
'env',

View File

@ -9,30 +9,22 @@ export default (api) => {
});
api.chainWebpack((webpackConfig) => {
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const { winPath } = require('@umijs/utils');
const crypto = require('crypto');
const path = require('path');
const { toString } = require('webpack-chain');
const cwd = api.cwd;
if (api.env === 'development') {
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
const { winPath } = require('@umijs/utils');
const path = require('path');
const cwd = api.cwd;
const configFiles = (api.configInstance.configFile || []).map(item => path.relative(cwd, item));
webpackConfig
.plugin('hardSource')
.use(HardSourceWebpackPlugin, [{
cacheDirectory: winPath(`${cwd}/.cache/hard-source/[confighash]`),
configHash: (config) => {
const hardSourcePlugin = config.plugins.find(
({ constructor }) => constructor.name === 'HardSourceWebpackPlugin'
);
const cacheDir = hardSourcePlugin.getCachePath();
const context = path.resolve(process.cwd(), config.context);
const clone = Object.assign({}, config, {
context: path.relative(cacheDir, context)
});
return crypto
.createHash('sha256')
.update(toString(clone))
.digest('hex');
environmentHash: {
root: cwd,
files: ['package-lock.json', 'yarn.lock'].concat(
Array.isArray(configFiles) ? configFiles : [configFiles]
)
},
...api.config.hardSource || {}
}]);

View File

@ -4,7 +4,7 @@
export default {
base: '/foo/',
define: {
__DEV__: true
__DEV__: false
},
publicPath: '/',
access: {

View File

@ -0,0 +1,5 @@
export default {
// define: {
// __DEV__: true
// },
}