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 committed Aug 23, 2024
1 parent 8f7e5ee commit ad57f4a
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 @@ -59,6 +60,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 ad57f4a

Please sign in to comment.