From d5bfc61b01d28cd0da50198299fafa7eb3393ee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E7=BA=AF?= Date: Fri, 5 Feb 2021 11:43:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=20=E4=BC=98=E5=8C=96getConfigFile?= =?UTF-8?q?=E5=92=8ChardSourePlugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. getConfigFile函数直接拿到所有配置文件,包含env和local 2. hardSourcePlugin会监听configFile的变化,更新缓存 --- packages/fes-compiler/src/config/index.js | 75 ++++++++++--------- packages/fes-compiler/src/service/index.js | 1 + .../src/plugins/features/hardSource.js | 30 +++----- packages/fes-template/.fes.js | 2 +- packages/fes-template/.fes.local.js | 5 ++ 5 files changed, 56 insertions(+), 57 deletions(-) create mode 100644 packages/fes-template/.fes.local.js diff --git a/packages/fes-compiler/src/config/index.js b/packages/fes-compiler/src/config/index.js index 7ce22819..ea8d6a60 100644 --- a/packages/fes-compiler/src/config/index.js +++ b/packages/fes-compiler/src/config/index.js @@ -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() { diff --git a/packages/fes-compiler/src/service/index.js b/packages/fes-compiler/src/service/index.js index 6e8a0e95..91277b34 100644 --- a/packages/fes-compiler/src/service/index.js +++ b/packages/fes-compiler/src/service/index.js @@ -265,6 +265,7 @@ export default class Service extends EventEmitter { 'paths', 'cwd', 'pkg', + 'configInstance', 'userConfig', 'config', 'env', diff --git a/packages/fes-preset-built-in/src/plugins/features/hardSource.js b/packages/fes-preset-built-in/src/plugins/features/hardSource.js index 59e1d132..465eb318 100644 --- a/packages/fes-preset-built-in/src/plugins/features/hardSource.js +++ b/packages/fes-preset-built-in/src/plugins/features/hardSource.js @@ -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 || {} }]); diff --git a/packages/fes-template/.fes.js b/packages/fes-template/.fes.js index e9a7d3f3..db77aa6f 100644 --- a/packages/fes-template/.fes.js +++ b/packages/fes-template/.fes.js @@ -4,7 +4,7 @@ export default { base: '/foo/', define: { - __DEV__: true + __DEV__: false }, publicPath: '/', access: { diff --git a/packages/fes-template/.fes.local.js b/packages/fes-template/.fes.local.js new file mode 100644 index 00000000..62fab3c1 --- /dev/null +++ b/packages/fes-template/.fes.local.js @@ -0,0 +1,5 @@ +export default { + // define: { + // __DEV__: true + // }, +} \ No newline at end of file