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,10 +130,51 @@ export default class Config {
getUserConfig() {
const configFile = this.getConfigFile();
this.configFile = configFile;
if (configFile.length > 0) {
// clear require cache and set babel register
const requireDeps = configFile.reduce((memo, file) => {
memo = memo.concat(parseRequireDeps(file));
return memo;
}, []);
requireDeps.forEach(cleanRequireCache);
this.service.babelRegister.setOnlyMap({
key: 'config',
value: requireDeps
});
// require config and merge
return this.mergeConfig(...this.requireConfigs(configFile));
}
return {};
}
addAffix(file, affix) {
const ext = extname(file);
return file.replace(new RegExp(`${ext}$`), `.${affix}${ext}`);
}
requireConfigs(configFiles) {
// eslint-disable-next-line
return configFiles.map((f) => compatESModuleRequire(require(f)));
}
mergeConfig(...configs) {
let ret = {};
for (const config of configs) {
// TODO: 精细化处理,比如处理 dotted config key
ret = deepmerge(ret, config);
}
return ret;
}
getConfigFile() {
// TODO: support custom config file
let configFile = CONFIG_FILES.find(f => existsSync(join(this.cwd, f)));
if (!configFile) return [];
configFile = winPath(configFile);
let envConfigFile;
// 潜在问题:
// .local 和 .env 的配置必须有 configFile 才有效
if (configFile) {
let envConfigFile;
if (process.env.FES_ENV) {
const envConfigFileName = this.addAffix(
configFile,
@ -162,47 +203,7 @@ export default class Config {
.filter(f => !!f)
.map(f => join(this.cwd, f))
.filter(f => existsSync(f));
// clear require cache and set babel register
const requireDeps = files.reduce((memo, file) => {
memo = memo.concat(parseRequireDeps(file));
return memo;
}, []);
requireDeps.forEach(cleanRequireCache);
this.service.babelRegister.setOnlyMap({
key: 'config',
value: requireDeps
});
// require config and merge
return this.mergeConfig(...this.requireConfigs(files));
}
return {};
}
addAffix(file, affix) {
const ext = extname(file);
return file.replace(new RegExp(`${ext}$`), `.${affix}${ext}`);
}
requireConfigs(configFiles) {
// eslint-disable-next-line
return configFiles.map((f) => compatESModuleRequire(require(f)));
}
mergeConfig(...configs) {
let ret = {};
for (const config of configs) {
// TODO: 精细化处理,比如处理 dotted config key
ret = deepmerge(ret, config);
}
return ret;
}
getConfigFile() {
// TODO: support custom config file
const configFile = CONFIG_FILES.find(f => existsSync(join(this.cwd, f)));
return configFile ? winPath(configFile) : null;
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) => {
if (api.env === 'development') {
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 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
// },
}