Skip to content

Commit

Permalink
feat: support moduleMap bundle options (shrinktofit#27)
Browse files Browse the repository at this point in the history
* feat: support moduleMap bundle options

* publish
  • Loading branch information
PPpro authored Oct 12, 2023
1 parent 44bf8c1 commit ad9596b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tfig",
"version": "3.3.1",
"version": "3.3.2",
"description": "Yet another tool to generate .d.ts bundle.",
"main": "build/gift.js",
"types": "build/gift.d.ts",
Expand Down
24 changes: 24 additions & 0 deletions source/gift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface IOptions {
*/
targetModule: string;
}>;

/**
* The module map, whose key is module name and value is module path.
*/
moduleMap?: Record<string, string>;
}

export interface IBundleResult {
Expand Down Expand Up @@ -130,9 +135,28 @@ export function rollupTypes(options: IOptions) {

function createProgram(): ts.Program {
const tscOptions = createTscOptions();
const host = ts.createCompilerHost(tscOptions);
host.resolveModuleNames = (moduleNames, importer): (ts.ResolvedModule | undefined)[] => {
const resolvedModules: (ts.ResolvedModule | undefined)[] = [];
for (const moduleName of moduleNames) {
if (options.moduleMap && moduleName in options.moduleMap) {
resolvedModules.push({resolvedFileName: options.moduleMap[moduleName]});
} else {
const resolvedRes = ts.resolveModuleName(moduleName, importer, tscOptions, host);
if (resolvedRes.resolvedModule) {
resolvedModules.push({resolvedFileName: resolvedRes.resolvedModule.resolvedFileName});
} else {
// Cannot resolve module, treat it as external.
resolvedModules.push(undefined);
}
}
}
return resolvedModules;
};
return ts.createProgram({
rootNames: inputs,
options: tscOptions,
host,
});
}

Expand Down

0 comments on commit ad9596b

Please sign in to comment.