Skip to content

Commit

Permalink
feat(webpack): use loader options, improve compactiblity with webpack…
Browse files Browse the repository at this point in the history
… 5 & rspack (#279)
  • Loading branch information
sxzz authored Mar 10, 2023
1 parent 72f827e commit a8817bd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 38 deletions.
6 changes: 0 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,3 @@ export interface UnpluginContext {
error(message: any): void
warn(message: any): void
}

declare module 'webpack' {
interface Compiler {
$unpluginContext: Record<string, ResolvedUnpluginOptions>
}
}
27 changes: 8 additions & 19 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ 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 @@ -49,34 +46,26 @@ 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 useLoader: RuleSetUseItem[] = [{
loader: `${TRANSFORM_LOADER}?unpluginName=${encodeURIComponent(plugin.name)}`,
const loaders: RuleSetUseItem[] = [{
loader: TRANSFORM_LOADER,
options: { plugin },
ident: 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 useNone
return []

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

return useNone
return []
},
})
}
Expand Down Expand Up @@ -173,7 +162,7 @@ export function getWebpackPlugin<UserOptions = {}>(
use: [{
loader: LOAD_LOADER,
options: {
unpluginName: plugin.name,
plugin,
},
}],
})
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 { unpluginName } = this.query
const plugin = this._compiler?.$unpluginContext[unpluginName]
const { plugin } = this.query

let id = this.resource

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

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

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]
const { plugin } = this.query

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

0 comments on commit a8817bd

Please sign in to comment.