Skip to content

Commit

Permalink
fix: correct type for loaderContext.importModule (#8766)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Dec 20, 2024
1 parent e5a53a5 commit 7099641
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
14 changes: 9 additions & 5 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,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 @@ -3349,12 +3356,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

0 comments on commit 7099641

Please sign in to comment.