From ad57f4ac15132ac9272b98d062fb2473db8bb362 Mon Sep 17 00:00:00 2001 From: Michael James Date: Fri, 23 Aug 2024 15:07:41 -0400 Subject: [PATCH] Allow access to rspack and webpack LoaderContext in the UnpluginBuildContext that is available in the load and transform hooks --- src/rspack/context.ts | 1 + src/rspack/loaders/load.ts | 7 ++++++- src/rspack/loaders/transform.ts | 7 ++++++- src/types.ts | 1 + src/webpack/context.ts | 4 ++++ src/webpack/loaders/load.ts | 3 +++ src/webpack/loaders/transform.ts | 3 +++ 7 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/rspack/context.ts b/src/rspack/context.ts index e63247e4..9f508c94 100644 --- a/src/rspack/context.ts +++ b/src/rspack/context.ts @@ -7,6 +7,7 @@ import type { UnpluginBuildContext, UnpluginContext, UnpluginMessage } from '../ interface ContextOptions { addWatchFile: (file: string) => void getWatchFiles: () => string[] + getNativeBuildContext?: () => any } export function contextOptionsFromCompilation(compilation: Compilation): ContextOptions { diff --git a/src/rspack/loaders/load.ts b/src/rspack/loaders/load.ts index 2e9d8dc3..60699e97 100644 --- a/src/rspack/loaders/load.ts +++ b/src/rspack/loaders/load.ts @@ -19,7 +19,12 @@ export default async function load(this: LoaderContext, source: string, map: any const res = await plugin.load.call( Object.assign( {}, - this._compilation && createBuildContext(contextOptionsFromCompilation(this._compilation), this._compilation), + this._compilation && createBuildContext({ + ...contextOptionsFromCompilation(this._compilation), + getNativeBuildContext: () => { + return this + }, + }, this._compilation), context, ), normalizeAbsolutePath(id), diff --git a/src/rspack/loaders/transform.ts b/src/rspack/loaders/transform.ts index 891b7931..5a23775c 100644 --- a/src/rspack/loaders/transform.ts +++ b/src/rspack/loaders/transform.ts @@ -27,7 +27,12 @@ export default async function transform( const res = await plugin.transform.call( Object.assign( {}, - this._compilation && createBuildContext(contextOptionsFromCompilation(this._compilation), this._compilation), + this._compilation && createBuildContext({ + ...contextOptionsFromCompilation(this._compilation), + getNativeBuildContext: () => { + return this + }, + }, this._compilation), context, ), source, diff --git a/src/types.ts b/src/types.ts index 4277ebb5..0da46da3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,6 +45,7 @@ export interface UnpluginBuildContext { emitFile: (emittedFile: EmittedAsset) => void getWatchFiles: () => string[] parse: (input: string, options?: any) => AstNode + getNativeBuildContext?: () => any } export interface UnpluginOptions { diff --git a/src/webpack/context.ts b/src/webpack/context.ts index 2ec2edf6..af725777 100644 --- a/src/webpack/context.ts +++ b/src/webpack/context.ts @@ -9,6 +9,7 @@ import type { UnpluginBuildContext, UnpluginContext, UnpluginMessage } from '../ interface ContextOptions { addWatchFile: (file: string) => void getWatchFiles: () => string[] + getNativeBuildContext?: () => any } export function contextOptionsFromCompilation(compilation: Compilation): ContextOptions { @@ -59,6 +60,9 @@ export function createBuildContext(options: ContextOptions, compilation?: Compil getWatchFiles() { return options.getWatchFiles() }, + getNativeBuildContext() { + return options.getNativeBuildContext?.() + }, } } diff --git a/src/webpack/loaders/load.ts b/src/webpack/loaders/load.ts index e7e3cb38..211f9537 100644 --- a/src/webpack/loaders/load.ts +++ b/src/webpack/loaders/load.ts @@ -23,6 +23,9 @@ export default async function load(this: LoaderContext, source: string, map getWatchFiles: () => { return this.getDependencies() }, + getNativeBuildContext: () => { + return this + }, }, this._compilation), ...context }, normalizeAbsolutePath(id), ) diff --git a/src/webpack/loaders/transform.ts b/src/webpack/loaders/transform.ts index f8556c3f..9f5b81e0 100644 --- a/src/webpack/loaders/transform.ts +++ b/src/webpack/loaders/transform.ts @@ -27,6 +27,9 @@ export default async function transform(this: LoaderContext<{ unpluginName: stri getWatchFiles: () => { return this.getDependencies() }, + getNativeBuildContext: () => { + return this + }, }, this._compilation), ...context }, source, this.resource,