Skip to content

Commit 19c95b1

Browse files
authored
refactor!: drop webpack 4 (#440)
* refactor: drop webpack 4 * fix types * fix lockfile
1 parent 2ebf5f5 commit 19c95b1

File tree

12 files changed

+24
-114
lines changed

12 files changed

+24
-114
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"@rspack/core": "^1.1.4",
5656
"@types/fs-extra": "^11.0.4",
5757
"@types/node": "^22.10.1",
58-
"@types/webpack-sources": "^3.2.3",
5958
"bumpp": "^9.8.1",
6059
"esbuild": "^0.24.0",
6160
"esbuild-plugin-copy": "^2.1.1",

pnpm-lock.yaml

Lines changed: 0 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rspack/index.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ export function getRspackPlugin<UserOptions = Record<string, never>>(
3232
// In the loader we strip the made up prefix path again
3333
const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), 'node_modules/.virtual')
3434

35-
const injected = compiler.$unpluginContext || {}
36-
compiler.$unpluginContext = injected
37-
3835
const meta: UnpluginContextMeta = {
3936
framework: 'rspack',
4037
rspack: {
@@ -51,17 +48,6 @@ export function getRspackPlugin<UserOptions = Record<string, never>>(
5148
},
5249
) as ResolvedUnpluginOptions
5350

54-
// inject context object to share with loaders
55-
injected[plugin.name] = plugin
56-
57-
compiler.hooks.thisCompilation.tap(plugin.name, (compilation) => {
58-
if (typeof compilation.hooks.childCompiler === 'undefined')
59-
throw new Error('`compilation.hooks.childCompiler` only support by @rspack/core>=0.4.1')
60-
compilation.hooks.childCompiler.tap(plugin.name, (childCompiler) => {
61-
childCompiler.$unpluginContext = injected
62-
})
63-
})
64-
6551
const externalModules = new Set<string>()
6652

6753
// resolveId hook
@@ -141,7 +127,7 @@ export function getRspackPlugin<UserOptions = Record<string, never>>(
141127
use: [{
142128
loader: LOAD_LOADER,
143129
options: {
144-
unpluginName: plugin.name,
130+
plugin,
145131
},
146132
}],
147133
type: 'javascript/auto',

src/rspack/loaders/load.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { LoaderContext } from '@rspack/core'
2+
import type { ResolvedUnpluginOptions } from '../../types'
23
import { normalizeAbsolutePath } from '../../utils'
34
import { createBuildContext, createContext } from '../context'
45
import { decodeVirtualModuleId, isVirtualModuleId } from '../utils'
56

67
export default async function load(this: LoaderContext, source: string, map: any) {
78
const callback = this.async()
8-
const { unpluginName } = this.query as { unpluginName: string }
9-
const plugin = this._compiler?.$unpluginContext[unpluginName]
10-
let id = this.resource
9+
const { plugin } = this.query as { plugin: ResolvedUnpluginOptions }
1110

11+
let id = this.resource
1212
if (!plugin?.load || !id)
1313
return callback(null, source, map)
1414

src/rspack/loaders/transform.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { LoaderContext } from '@rspack/core'
2+
import type { ResolvedUnpluginOptions } from '../../types'
23
import { createBuildContext, createContext } from '../context'
34

45
export default async function transform(
@@ -7,22 +8,11 @@ export default async function transform(
78
map: any,
89
) {
910
const callback = this.async()
10-
11-
let unpluginName: string
12-
if (typeof this.query === 'string') {
13-
const query = new URLSearchParams(this.query)
14-
unpluginName = query.get('unpluginName')!
15-
}
16-
else {
17-
unpluginName = (this.query as { unpluginName: string }).unpluginName
18-
}
19-
20-
const id = this.resource
21-
const plugin = this._compiler?.$unpluginContext[unpluginName]
22-
11+
const { plugin } = this.query as { plugin: ResolvedUnpluginOptions }
2312
if (!plugin?.transform)
2413
return callback(null, source, map)
2514

15+
const id = this.resource
2616
const context = createContext(this)
2717
const res = await plugin.transform.call(
2818
Object.assign(

src/types.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,3 @@ export interface UnpluginContext {
165165
error: (message: string | UnpluginMessage) => void
166166
warn: (message: string | UnpluginMessage) => void
167167
}
168-
169-
declare module 'webpack' {
170-
interface Compiler {
171-
$unpluginContext: Record<string, ResolvedUnpluginOptions>
172-
}
173-
}
174-
175-
declare module '@rspack/core' {
176-
interface Compiler {
177-
$unpluginContext: Record<string, ResolvedUnpluginOptions>
178-
}
179-
}

src/utils.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,10 @@ export function transformUse(
4848
const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || ''))
4949
if (!plugin.transformInclude || plugin.transformInclude(id)) {
5050
return [{
51-
loader: `${transformLoader}?unpluginName=${encodeURIComponent(plugin.name)}`,
51+
loader: transformLoader,
52+
options: { plugin },
53+
ident: plugin.name,
5254
}]
5355
}
5456
return []
5557
}
56-
57-
export function resolveQuery(query: string | { unpluginName: string }) {
58-
if (typeof query === 'string') {
59-
return new URLSearchParams(query).get('unpluginName')!
60-
}
61-
else {
62-
return query.unpluginName
63-
}
64-
}

src/webpack/context.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { Compilation, Compiler, LoaderContext } from 'webpack'
22
import type { UnpluginBuildContext, UnpluginContext, UnpluginMessage } from '../types'
33
import { Buffer } from 'buffer'
4-
import { createRequire } from 'module'
54
import { resolve } from 'path'
65
import process from 'process'
76
import { Parser } from 'acorn'
7+
import * as webpack from 'webpack'
88

99
interface ContextOptions {
1010
addWatchFile: (file: string) => void
@@ -22,16 +22,9 @@ export function contextOptionsFromCompilation(compilation: Compilation): Context
2222
}
2323
}
2424

25-
export function getSource(fileSource: string | Uint8Array) {
26-
// Create a require function to load webpack-sources as webpack in order to maintain compatibility.
27-
const webpackRequire = createRequire(require.resolve('webpack'))
28-
const RawSource = (webpackRequire('webpack-sources') as typeof import('webpack-sources')).RawSource
29-
30-
return new RawSource(
31-
typeof fileSource === 'string'
32-
? fileSource
33-
// Converting to string to support Webpack 4's RawSource.
34-
: Buffer.from(fileSource.buffer).toString('utf-8'),
25+
export function getSource(fileSource: string | Uint8Array): webpack.sources.RawSource {
26+
return new webpack.sources.RawSource(
27+
typeof fileSource === 'string' ? fileSource : Buffer.from(fileSource.buffer),
3528
)
3629
}
3730

@@ -55,7 +48,7 @@ export function createBuildContext(options: ContextOptions, compiler: Compiler,
5548
throw new Error('unplugin/webpack: emitFile outside supported hooks (buildStart, buildEnd, load, transform, watchChange)')
5649
compilation.emitAsset(
5750
outFileName,
58-
getSource(emittedFile.source) as import('webpack').sources.Source,
51+
getSource(emittedFile.source),
5952
)
6053
}
6154
},

src/webpack/index.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
2626
// In the loader we strip the made up prefix path again
2727
const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), '_virtual_')
2828

29-
const injected = compiler.$unpluginContext || {}
30-
compiler.$unpluginContext = injected
31-
3229
const meta: UnpluginContextMeta = {
3330
framework: 'webpack',
3431
webpack: {
@@ -46,15 +43,6 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
4643
},
4744
) as ResolvedUnpluginOptions
4845

49-
// inject context object to share with loaders
50-
injected[plugin.name] = plugin
51-
52-
compiler.hooks.thisCompilation.tap(plugin.name, (compilation) => {
53-
compilation.hooks.childCompiler.tap(plugin.name, (childCompiler) => {
54-
childCompiler.$unpluginContext = injected
55-
})
56-
})
57-
5846
const externalModules = new Set<string>()
5947

6048
// resolveId hook
@@ -170,7 +158,7 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
170158
use: [{
171159
loader: LOAD_LOADER,
172160
options: {
173-
unpluginName: plugin.name,
161+
plugin,
174162
},
175163
}],
176164
type: 'javascript/auto',

src/webpack/loaders/load.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { LoaderContext } from 'webpack'
2-
import { normalizeAbsolutePath, resolveQuery } from '../../utils'
2+
import type { ResolvedUnpluginOptions } from '../../types'
3+
import { normalizeAbsolutePath } from '../../utils'
34
import { createBuildContext, createContext } from '../context'
45

5-
export default async function load(this: LoaderContext<{ unpluginName: string }>, source: string, map: any) {
6+
export default async function load(this: LoaderContext<any>, source: string, map: any) {
67
const callback = this.async()
7-
8-
const unpluginName = resolveQuery(this.query)
9-
const plugin = this._compiler?.$unpluginContext[unpluginName]
8+
const { plugin } = this.query as { plugin: ResolvedUnpluginOptions }
109
let id = this.resource
1110

1211
if (!plugin?.load || !id)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy