diff --git a/package.json b/package.json index ac578817..155b10e6 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "@rspack/core": "^1.1.4", "@types/fs-extra": "^11.0.4", "@types/node": "^22.10.1", - "@types/webpack-sources": "^3.2.3", "bumpp": "^9.8.1", "esbuild": "^0.24.0", "esbuild-plugin-copy": "^2.1.1", diff --git a/src/rspack/index.ts b/src/rspack/index.ts index 3b76bbc7..2ddc5734 100644 --- a/src/rspack/index.ts +++ b/src/rspack/index.ts @@ -32,9 +32,6 @@ export function getRspackPlugin>( // In the loader we strip the made up prefix path again const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), 'node_modules/.virtual') - const injected = compiler.$unpluginContext || {} - compiler.$unpluginContext = injected - const meta: UnpluginContextMeta = { framework: 'rspack', rspack: { @@ -51,17 +48,6 @@ export function getRspackPlugin>( }, ) as ResolvedUnpluginOptions - // inject context object to share with loaders - injected[plugin.name] = plugin - - compiler.hooks.thisCompilation.tap(plugin.name, (compilation) => { - if (typeof compilation.hooks.childCompiler === 'undefined') - throw new Error('`compilation.hooks.childCompiler` only support by @rspack/core>=0.4.1') - compilation.hooks.childCompiler.tap(plugin.name, (childCompiler) => { - childCompiler.$unpluginContext = injected - }) - }) - const externalModules = new Set() // resolveId hook @@ -141,7 +127,7 @@ export function getRspackPlugin>( use: [{ loader: LOAD_LOADER, options: { - unpluginName: plugin.name, + plugin, }, }], type: 'javascript/auto', diff --git a/src/rspack/loaders/load.ts b/src/rspack/loaders/load.ts index 3bbb31c0..f4a05f63 100644 --- a/src/rspack/loaders/load.ts +++ b/src/rspack/loaders/load.ts @@ -5,10 +5,9 @@ import { decodeVirtualModuleId, isVirtualModuleId } from '../utils' export default async function load(this: LoaderContext, source: string, map: any) { const callback = this.async() - const { unpluginName } = this.query as { unpluginName: string } - const plugin = this._compiler?.$unpluginContext[unpluginName] - let id = this.resource + const { plugin } = this.query as any + let id = this.resource if (!plugin?.load || !id) return callback(null, source, map) diff --git a/src/rspack/loaders/transform.ts b/src/rspack/loaders/transform.ts index a142a2ae..451109f2 100644 --- a/src/rspack/loaders/transform.ts +++ b/src/rspack/loaders/transform.ts @@ -7,22 +7,11 @@ export default async function transform( map: any, ) { const callback = this.async() - - let unpluginName: string - if (typeof this.query === 'string') { - const query = new URLSearchParams(this.query) - unpluginName = query.get('unpluginName')! - } - else { - unpluginName = (this.query as { unpluginName: string }).unpluginName - } - - const id = this.resource - const plugin = this._compiler?.$unpluginContext[unpluginName] - + const { plugin } = this.query as any if (!plugin?.transform) return callback(null, source, map) + const id = this.resource const context = createContext(this) const res = await plugin.transform.call( Object.assign( diff --git a/src/types.ts b/src/types.ts index 6648f1d0..4e4d2d86 100644 --- a/src/types.ts +++ b/src/types.ts @@ -159,15 +159,3 @@ export interface UnpluginContext { error: (message: string | UnpluginMessage) => void warn: (message: string | UnpluginMessage) => void } - -declare module 'webpack' { - interface Compiler { - $unpluginContext: Record - } -} - -declare module '@rspack/core' { - interface Compiler { - $unpluginContext: Record - } -} diff --git a/src/utils.ts b/src/utils.ts index 767ffc52..a4ee2882 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -58,17 +58,10 @@ export function transformUse( const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || '')) if (!plugin.transformInclude || plugin.transformInclude(id)) { return [{ - loader: `${transformLoader}?unpluginName=${encodeURIComponent(plugin.name)}`, + loader: transformLoader, + options: { plugin }, + ident: plugin.name, }] } return [] } - -export function resolveQuery(query: string | { unpluginName: string }) { - if (typeof query === 'string') { - return new URLSearchParams(query).get('unpluginName')! - } - else { - return query.unpluginName - } -} diff --git a/src/webpack/context.ts b/src/webpack/context.ts index 8c5d1da2..407c812f 100644 --- a/src/webpack/context.ts +++ b/src/webpack/context.ts @@ -1,10 +1,10 @@ import type { Compilation, Compiler, LoaderContext } from 'webpack' import type { UnpluginBuildContext, UnpluginContext, UnpluginMessage } from '../types' import { Buffer } from 'buffer' -import { createRequire } from 'module' import { resolve } from 'path' import process from 'process' import { Parser } from 'acorn' +import * as webpack from 'webpack' interface ContextOptions { addWatchFile: (file: string) => void @@ -22,16 +22,9 @@ export function contextOptionsFromCompilation(compilation: Compilation): Context } } -export function getSource(fileSource: string | Uint8Array) { - // Create a require function to load webpack-sources as webpack in order to maintain compatibility. - const webpackRequire = createRequire(require.resolve('webpack')) - const RawSource = (webpackRequire('webpack-sources') as typeof import('webpack-sources')).RawSource - - return new RawSource( - typeof fileSource === 'string' - ? fileSource - // Converting to string to support Webpack 4's RawSource. - : Buffer.from(fileSource.buffer).toString('utf-8'), +export function getSource(fileSource: string | Uint8Array): webpack.sources.RawSource { + return new webpack.sources.RawSource( + typeof fileSource === 'string' ? fileSource : Buffer.from(fileSource.buffer), ) } @@ -55,7 +48,7 @@ export function createBuildContext(options: ContextOptions, compiler: Compiler, throw new Error('unplugin/webpack: emitFile outside supported hooks (buildStart, buildEnd, load, transform, watchChange)') compilation.emitAsset( outFileName, - getSource(emittedFile.source) as import('webpack').sources.Source, + getSource(emittedFile.source), ) } }, diff --git a/src/webpack/index.ts b/src/webpack/index.ts index e215c962..5fa84532 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -26,9 +26,6 @@ export function getWebpackPlugin>( // In the loader we strip the made up prefix path again const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), '_virtual_') - const injected = compiler.$unpluginContext || {} - compiler.$unpluginContext = injected - const meta: UnpluginContextMeta = { framework: 'webpack', webpack: { @@ -46,15 +43,6 @@ export function getWebpackPlugin>( }, ) as ResolvedUnpluginOptions - // inject context object to share with loaders - injected[plugin.name] = plugin - - compiler.hooks.thisCompilation.tap(plugin.name, (compilation) => { - compilation.hooks.childCompiler.tap(plugin.name, (childCompiler) => { - childCompiler.$unpluginContext = injected - }) - }) - const externalModules = new Set() // resolveId hook @@ -170,7 +158,7 @@ export function getWebpackPlugin>( use: [{ loader: LOAD_LOADER, options: { - unpluginName: plugin.name, + plugin, }, }], type: 'javascript/auto', diff --git a/src/webpack/loaders/load.ts b/src/webpack/loaders/load.ts index 1bbd28db..9868612e 100644 --- a/src/webpack/loaders/load.ts +++ b/src/webpack/loaders/load.ts @@ -1,12 +1,10 @@ import type { LoaderContext } from 'webpack' -import { normalizeAbsolutePath, resolveQuery } from '../../utils' +import { normalizeAbsolutePath } from '../../utils' import { createBuildContext, createContext } from '../context' -export default async function load(this: LoaderContext<{ unpluginName: string }>, source: string, map: any) { +export default async function load(this: LoaderContext, source: string, map: any) { const callback = this.async() - - const unpluginName = resolveQuery(this.query) - const plugin = this._compiler?.$unpluginContext[unpluginName] + const { plugin } = this.query let id = this.resource if (!plugin?.load || !id) diff --git a/src/webpack/loaders/transform.ts b/src/webpack/loaders/transform.ts index 9f0ae5e5..ff1ec390 100644 --- a/src/webpack/loaders/transform.ts +++ b/src/webpack/loaders/transform.ts @@ -1,13 +1,10 @@ import type { LoaderContext } from 'webpack' -import { resolveQuery } from '../../utils' import { createBuildContext, createContext } from '../context' -export default async function transform(this: LoaderContext<{ unpluginName: string }>, source: string, map: any) { +export default async function transform(this: LoaderContext, source: string, map: any) { const callback = this.async() - const unpluginName = resolveQuery(this.query) - const plugin = this._compiler?.$unpluginContext[unpluginName] - + const { plugin } = this.query if (!plugin?.transform) return callback(null, source, map) diff --git a/test/unit-tests/resolve-id/resolve-id.test.ts b/test/unit-tests/resolve-id/resolve-id.test.ts index 13327752..d179d0ef 100644 --- a/test/unit-tests/resolve-id/resolve-id.test.ts +++ b/test/unit-tests/resolve-id/resolve-id.test.ts @@ -26,8 +26,7 @@ function createResolveIdHook(): Mock { } function checkResolveIdHook(resolveIdCallback: Mock): void { - if (!process.env.IS_WEBPACK_4) // Webpack 4 has different set of assertions - expect.assertions(4 * (1 + propsToTest.length * 2)) + expect.assertions(4 * (1 + propsToTest.length * 2)) expect(resolveIdCallback).toHaveBeenCalledWith( expect.stringMatching(/(?:\/|\\)entry\.js$/),