Skip to content

Commit 0de16c3

Browse files
committed
chore: reduce code duplication
1 parent 96254b5 commit 0de16c3

File tree

1 file changed

+61
-78
lines changed

1 file changed

+61
-78
lines changed

packages/babel-plugin/src/utils/state-manager.js

Lines changed: 61 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,6 @@ export default class StateManager {
629629
return manualAliases ? this.normalizeAliases(manualAliases) : null;
630630
}
631631

632-
let packageAliases = {};
633-
let tsconfigAliases = {};
634-
let denoAliases = {};
635-
636632
const pkgInfo = this.getPackageNameAndPath(this.filename);
637633
if (!pkgInfo) {
638634
return manualAliases ? this.normalizeAliases(manualAliases) : null;
@@ -653,86 +649,73 @@ export default class StateManager {
653649
return basePath;
654650
});
655651

656-
// Load aliases from package.json imports field
657-
try {
658-
const packageJsonPath = path.join(projectDir, 'package.json');
659-
if (fs.existsSync(packageJsonPath)) {
660-
const rawConfig: mixed = JSON5.parse(
661-
fs.readFileSync(packageJsonPath, 'utf8'),
662-
);
663-
if (!isPackageJSON(rawConfig)) {
664-
throw new Error('Invalid package.json format');
665-
}
666-
const packageJson: PackageJSON = rawConfig as $FlowFixMe;
667-
668-
// Handle Node.js native imports
669-
const imports = packageJson.imports;
670-
if (imports && typeof imports === 'object') {
671-
packageAliases = Object.fromEntries(
672-
Object.entries(imports)
673-
.filter(([key]) => key.startsWith('#'))
674-
.map(([key, value]) => [key.slice(1), resolveAliasPaths(value)]),
675-
);
676-
}
677-
}
678-
} catch (err) {
679-
console.warn('Failed to load aliases from package.json:', err.message);
680-
}
681-
682-
// Load aliases from tsconfig.json
683-
try {
684-
const tsconfigPath = path.join(projectDir, 'tsconfig.json');
685-
if (fs.existsSync(tsconfigPath)) {
686-
const rawConfig: mixed = JSON5.parse(
687-
fs.readFileSync(tsconfigPath, 'utf8'),
688-
);
689-
if (!isTSConfig(rawConfig)) {
690-
throw new Error('Invalid tsconfig.json format');
691-
}
692-
const tsconfig: TSConfig = rawConfig as $FlowFixMe;
693-
const tsConfigAliasPaths = tsconfig.compilerOptions?.paths;
694-
if (tsConfigAliasPaths != null && isImportsObject(tsConfigAliasPaths)) {
695-
tsconfigAliases = Object.fromEntries(
696-
Object.entries(tsConfigAliasPaths).map(([key, value]) => [
697-
key,
698-
resolveAliasPaths(value),
699-
]),
700-
);
701-
}
702-
}
703-
} catch (err) {
704-
console.warn('Failed to load aliases from tsconfig.json:', err.message);
705-
}
706-
707-
// Load aliases from deno.json
708-
try {
709-
const denoConfigPath = path.join(projectDir, 'deno.json');
710-
if (fs.existsSync(denoConfigPath)) {
711-
const rawConfig: mixed = JSON5.parse(
712-
fs.readFileSync(denoConfigPath, 'utf8'),
713-
);
714-
if (!isDenoConfig(rawConfig)) {
715-
throw new Error('Invalid deno.json format');
652+
const [packageAliases, tsconfigAliases, denoAliases] = [
653+
[
654+
'package.json',
655+
(rawConfig: mixed) => {
656+
if (!isPackageJSON(rawConfig)) {
657+
throw new Error('Invalid package.json format');
658+
}
659+
return rawConfig.imports;
660+
},
661+
],
662+
[
663+
'tsconfig.json',
664+
(rawConfig: mixed) => {
665+
if (!isTSConfig(rawConfig)) {
666+
throw new Error('Invalid tsconfig.json format');
667+
}
668+
const config = rawConfig as $FlowFixMe;
669+
return config.compilerOptions?.paths;
670+
},
671+
],
672+
[
673+
'deno.json',
674+
(rawConfig: mixed) => {
675+
if (!isDenoConfig(rawConfig)) {
676+
throw new Error('Invalid deno.json format');
677+
}
678+
return rawConfig.imports;
679+
},
680+
],
681+
].map(
682+
([fileName, getConfig]): $ReadOnly<{
683+
[string]: string | $ReadOnlyArray<string>,
684+
}> => {
685+
try {
686+
const filePath = path.join(projectDir, fileName);
687+
if (fs.existsSync(filePath)) {
688+
const rawConfig: mixed = JSON5.parse(
689+
fs.readFileSync(filePath, 'utf8'),
690+
);
691+
const config = getConfig(rawConfig);
692+
693+
// Handle Node.js native imports
694+
if (isImportsObject(config)) {
695+
return Object.fromEntries(
696+
Object.entries(config).map(([k, v]) => [
697+
k,
698+
resolveAliasPaths(v as $FlowFixMe),
699+
]),
700+
) as $ReadOnly<{ [string]: $ReadOnlyArray<string> }>;
701+
}
702+
}
703+
return {};
704+
} catch (err) {
705+
console.warn(`Failed to load aliases from ${fileName}`, err.message);
716706
}
717-
const denoConfig: DenoConfig = rawConfig as $FlowFixMe;
718-
if (denoConfig.imports) {
719-
denoAliases = Object.fromEntries(
720-
Object.entries(denoConfig.imports).map(([key, value]) => {
721-
return [key, resolveAliasPaths(value)];
722-
}),
723-
);
724-
}
725-
}
726-
} catch (err) {
727-
console.warn('Failed to load aliases from deno.json:', err.message);
728-
}
707+
return {};
708+
},
709+
);
729710

730711
// Merge aliases in priority: manual > package.json > tsconfig.json > deno.json
731-
const mergedAliases = {
712+
const mergedAliases: {
713+
[string]: string | $ReadOnlyArray<string>,
714+
} = {
732715
...denoAliases,
733716
...tsconfigAliases,
734717
...packageAliases,
735-
...(manualAliases || {}),
718+
...manualAliases,
736719
};
737720

738721
return Object.keys(mergedAliases).length > 0

0 commit comments

Comments
 (0)