Skip to content

Commit

Permalink
refactor: drop webpack 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 28, 2024
1 parent d702641 commit e4be2d2
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 91 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 1 addition & 15 deletions src/rspack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ export function getRspackPlugin<UserOptions = Record<string, never>>(
// 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: {
Expand All @@ -51,17 +48,6 @@ export function getRspackPlugin<UserOptions = Record<string, never>>(
},
) 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<string>()

// resolveId hook
Expand Down Expand Up @@ -141,7 +127,7 @@ export function getRspackPlugin<UserOptions = Record<string, never>>(
use: [{
loader: LOAD_LOADER,
options: {
unpluginName: plugin.name,
plugin,
},
}],
type: 'javascript/auto',
Expand Down
5 changes: 2 additions & 3 deletions src/rspack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 2 additions & 13 deletions src/rspack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 0 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,3 @@ export interface UnpluginContext {
error: (message: string | UnpluginMessage) => void
warn: (message: string | UnpluginMessage) => void
}

declare module 'webpack' {
interface Compiler {
$unpluginContext: Record<string, ResolvedUnpluginOptions>
}
}

declare module '@rspack/core' {
interface Compiler {
$unpluginContext: Record<string, ResolvedUnpluginOptions>
}
}
13 changes: 3 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
17 changes: 5 additions & 12 deletions src/webpack/context.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
)
}

Expand All @@ -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),
)
}
},
Expand Down
14 changes: 1 addition & 13 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
// 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: {
Expand All @@ -46,15 +43,6 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
},
) 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<string>()

// resolveId hook
Expand Down Expand Up @@ -170,7 +158,7 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
use: [{
loader: LOAD_LOADER,
options: {
unpluginName: plugin.name,
plugin,
},
}],
type: 'javascript/auto',
Expand Down
8 changes: 3 additions & 5 deletions src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
@@ -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<any>, 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)
Expand Down
7 changes: 2 additions & 5 deletions src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
@@ -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<any>, 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)

Expand Down
3 changes: 1 addition & 2 deletions test/unit-tests/resolve-id/resolve-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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$/),
Expand Down

0 comments on commit e4be2d2

Please sign in to comment.