Skip to content

Commit

Permalink
Allow access to rspack and webpack LoaderContext in the UnpluginBuild…
Browse files Browse the repository at this point in the history
…Context that is available in the load and transform hooks
  • Loading branch information
urbnjamesmi1 authored and sxzz committed Sep 9, 2024
1 parent 15c1882 commit f50c6e2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/rspack/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion src/rspack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 6 additions & 1 deletion src/rspack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface UnpluginBuildContext {
emitFile: (emittedFile: EmittedAsset) => void
getWatchFiles: () => string[]
parse: (input: string, options?: any) => AstNode
getNativeBuildContext?: () => any
}

export interface UnpluginOptions {
Expand Down
4 changes: 4 additions & 0 deletions src/webpack/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -62,6 +63,9 @@ export function createBuildContext(options: ContextOptions, compilation?: Compil
getWatchFiles() {
return options.getWatchFiles()
},
getNativeBuildContext() {
return options.getNativeBuildContext?.()
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default async function load(this: LoaderContext<any>, source: string, map
getWatchFiles: () => {
return this.getDependencies()
},
getNativeBuildContext: () => {
return this
},
}, this._compilation), ...context },
normalizeAbsolutePath(id),
)
Expand Down
3 changes: 3 additions & 0 deletions src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f50c6e2

Please sign in to comment.