Skip to content

Commit

Permalink
revert: "feat(webpack): use loader options, improve compactiblity wit…
Browse files Browse the repository at this point in the history
…h webpack 5 & rspack (#279)"

This reverts commit a8817bd.
  • Loading branch information
sxzz committed Mar 14, 2023
1 parent 8d57cd3 commit 166ef6f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ export interface UnpluginContext {
error(message: any): void
warn(message: any): void
}

declare module 'webpack' {
interface Compiler {
$unpluginContext: Record<string, ResolvedUnpluginOptions>
}
}
27 changes: 19 additions & 8 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export function getWebpackPlugin<UserOptions = {}>(
return (userOptions?: UserOptions) => {
return {
apply(compiler: WebpackCompiler) {
const injected = compiler.$unpluginContext || {}
compiler.$unpluginContext = injected

const meta: UnpluginContextMeta = {
framework: 'webpack',
webpack: {
Expand All @@ -46,26 +49,34 @@ export function getWebpackPlugin<UserOptions = {}>(
},
) 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>()

// transform hook
if (plugin.transform) {
const loaders: RuleSetUseItem[] = [{
loader: TRANSFORM_LOADER,
options: { plugin },
ident: plugin.name,
const useLoader: RuleSetUseItem[] = [{
loader: `${TRANSFORM_LOADER}?unpluginName=${encodeURIComponent(plugin.name)}`,
}]
const useNone: RuleSetUseItem[] = []
compiler.options.module.rules.unshift({
enforce: plugin.enforce,
use: (data: { resource: string | null; resourceQuery: string }) => {
if (data.resource == null)
return []
return useNone

const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || ''))
if (!plugin.transformInclude || plugin.transformInclude(id))
return loaders
return useLoader

return []
return useNone
},
})
}
Expand Down Expand Up @@ -162,7 +173,7 @@ export function getWebpackPlugin<UserOptions = {}>(
use: [{
loader: LOAD_LOADER,
options: {
plugin,
unpluginName: plugin.name,
},
}],
})
Expand Down
4 changes: 2 additions & 2 deletions src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { normalizeAbsolutePath } from '../../utils'

export default async function load(this: LoaderContext<any>, source: string, map: any) {
const callback = this.async()
const { plugin } = this.query

const { unpluginName } = this.query
const plugin = this._compiler?.$unpluginContext[unpluginName]
let id = this.resource

if (!plugin?.load || !id)
Expand Down
12 changes: 11 additions & 1 deletion src/webpack/loaders/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import { createContext } from '../context'

export default async function transform(this: LoaderContext<any>, source: string, map: any) {
const callback = this.async()
const { plugin } = this.query

let unpluginName
if (typeof this.query === 'string') {
const query = new URLSearchParams(this.query)
unpluginName = query.get('unpluginName')
}
else {
unpluginName = this.query.unpluginName
}

const plugin = this._compiler?.$unpluginContext[unpluginName]

if (!plugin?.transform)
return callback(null, source, map)
Expand Down

0 comments on commit 166ef6f

Please sign in to comment.