Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 抽离 mergeTsconfigPathAliasToConfig 供内部组件库调用 #755

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 39 additions & 27 deletions src/builder/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Loading