From e5c42261392026eef935adf75e5653ca54390a6e Mon Sep 17 00:00:00 2001 From: Jinbao1001 Date: Wed, 8 May 2024 19:39:23 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=8A=BD=E7=A6=BB=20mergeTsconfigPath?= =?UTF-8?q?AliasToConfig=20=E4=BE=9B=E5=86=85=E9=83=A8=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=BA=93=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builder/config.ts | 66 +++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/src/builder/config.ts b/src/builder/config.ts index 5cddea80..e757ea90 100644 --- a/src/builder/config.ts +++ b/src/builder/config.ts @@ -94,6 +94,41 @@ export function convertAliasByTsconfigPaths(cwd: string) { return { bundle, bundless }; } +export function mergeTsconfigPathAliasToConfig( + userConfig: IFatherConfig, + cwd: string, + pkg: IApi['pkg'], +) { + const configs = normalizeUserConfig(userConfig, pkg); + // convert alias from tsconfig paths + const aliasFromPaths = convertAliasByTsconfigPaths(cwd); + logger.debug('Convert alias from tsconfig.json:', aliasFromPaths); + return configs.reduce( + (r, config) => { + if (config.type === IFatherBuildTypes.BUNDLE) { + config.alias = { ...aliasFromPaths.bundle, ...config.alias }; + r.bundle.push(config); + } else if (config.type === IFatherBuildTypes.BUNDLESS) { + config.alias = { ...aliasFromPaths.bundless, ...config.alias }; + // Handling file suffixes only bundless mode needs to be handled + for (let target in config.alias) { + // If the file suffix is js remove the suffix + const aPath = config.alias[target]; + config.alias![target] = aPath.replace(/\.(t|j)sx?$/, ''); + } + + r.bundless[config.format].push(config); + } + + return r; + }, + { bundle: [], bundless: { esm: [], cjs: [] } } as { + bundle: IBundleConfig[]; + bundless: { esm: IBundlessConfig[]; cjs: IBundlessConfig[] }; + }, + ); +} + /** * normalize user config to bundler configs * @param userConfig config from user @@ -362,33 +397,10 @@ export function createConfigProviders( } = { bundless: {} }; const configs = normalizeUserConfig(userConfig, pkg); - // convert alias from tsconfig paths - const aliasFromPaths = convertAliasByTsconfigPaths(cwd); - logger.debug('Convert alias from tsconfig.json:', aliasFromPaths); - - const { bundle, bundless } = configs.reduce( - (r, config) => { - if (config.type === IFatherBuildTypes.BUNDLE) { - config.alias = { ...aliasFromPaths.bundle, ...config.alias }; - r.bundle.push(config); - } else if (config.type === IFatherBuildTypes.BUNDLESS) { - config.alias = { ...aliasFromPaths.bundless, ...config.alias }; - // Handling file suffixes only bundless mode needs to be handled - for (let target in config.alias) { - // If the file suffix is js remove the suffix - const aPath = config.alias[target]; - config.alias![target] = aPath.replace(/\.(t|j)sx?$/, ''); - } - - r.bundless[config.format].push(config); - } - - return r; - }, - { bundle: [], bundless: { esm: [], cjs: [] } } as { - bundle: IBundleConfig[]; - bundless: { esm: IBundlessConfig[]; cjs: IBundlessConfig[] }; - }, + const { bundle, bundless } = mergeTsconfigPathAliasToConfig( + userConfig, + cwd, + pkg, ); if (bundle.length) {