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

feat: support moduleMap bundle options #27

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
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
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