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

fix: correct type for loaderContext.importModule #8766

Merged
merged 2 commits into from
Dec 20, 2024
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
14 changes: 9 additions & 5 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,13 @@ export type ImportFunctionName = string;
// @public
export type ImportMetaName = string;

// @public (undocumented)
interface ImportModuleOptions {
baseUri?: string;
layer?: string;
publicPath?: PublicPath;
}

// @public
export type Incremental = {
make?: boolean;
Expand Down Expand Up @@ -3356,12 +3363,9 @@ export interface LoaderContext<OptionsType = {}> {
getResolve(options: Resolve): ((context: string, request: string, callback: ResolveCallback) => void) | ((context: string, request: string) => Promise<string | false | undefined>);
// (undocumented)
hot?: boolean;
importModule<T = any>(request: string, options: ImportModuleOptions | undefined, callback: (err?: null | Error, exports?: T) => any): void;
// (undocumented)
importModule(request: string, options: {
layer?: string;
publicPath?: PublicPath;
baseUri?: string;
}, callback: (err?: Error, res?: any) => void): void;
importModule<T = any>(request: string, options?: ImportModuleOptions): Promise<T>;
// (undocumented)
loaderIndex: number;
loaders: LoaderObject[];
Expand Down
40 changes: 37 additions & 3 deletions packages/rspack/src/config/adapterRuleUse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ interface LoaderExperiments {
emitDiagnostic(diagnostic: Diagnostic): void;
}

export interface ImportModuleOptions {
/**
* Specify a layer in which this module is placed/compiled
*/
layer?: string;
/**
* The public path used for the built modules
*/
publicPath?: PublicPath;
/**
* Target base uri
*/
baseUri?: string;
}

export interface LoaderContext<OptionsType = {}> {
version: 2;
resource: string;
Expand Down Expand Up @@ -173,11 +188,30 @@ export interface LoaderContext<OptionsType = {}> {
getContextDependencies(): string[];
getMissingDependencies(): string[];
addBuildDependency(file: string): void;
importModule(

/**
* Compile and execute a module at the build time.
* This is an alternative lightweight solution for the child compiler.
* `importModule` will return a Promise if no callback is provided.
*
* @example
* ```ts
* const modulePath = path.resolve(__dirname, 'some-module.ts');
* const moduleExports = await this.importModule(modulePath, {
* // optional options
* });
* ```
*/
importModule<T = any>(
request: string,
options: { layer?: string; publicPath?: PublicPath; baseUri?: string },
callback: (err?: Error, res?: any) => void
options: ImportModuleOptions | undefined,
callback: (err?: null | Error, exports?: T) => any
): void;
importModule<T = any>(
request: string,
options?: ImportModuleOptions
): Promise<T>;

fs: any;
/**
* This is an experimental API and maybe subject to change.
Expand Down
5 changes: 4 additions & 1 deletion packages/rspack/src/loader-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ export async function runLoaders(
missingDependencies.length = 0;
context.cacheable = true;
};

loaderContext.importModule = function importModule(
this: LoaderContext,
request,
userOptions,
callback
Expand Down Expand Up @@ -490,7 +492,8 @@ export async function runLoaders(
}
}
);
};
} as LoaderContext["importModule"];

Object.defineProperty(loaderContext, "resource", {
enumerable: true,
get: () => {
Expand Down
Loading