Skip to content

Commit

Permalink
fix(webpack): loader for load hook only enabled for resolveId result
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 27, 2021
1 parent 7962ad9 commit 4ff7681
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface UnpluginOptions {
export interface ResolvedUnpluginOptions extends UnpluginOptions {
// injected internal objects
__vfs?: VirtualModulesPlugin
__vfsModules?: Set<string>
__virtualModulePrefix: string
}

Expand Down
10 changes: 6 additions & 4 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function getWebpackPlugin<UserOptions = {}> (
vfs = new VirtualModulesPlugin()
compiler.options.plugins.push(vfs)
}
plugin.__vfsModules = new Set()
plugin.__vfs = vfs

const resolver = {
Expand All @@ -88,7 +89,8 @@ export function getWebpackPlugin<UserOptions = {}> (
// we treat it as a virtual module
if (!fs.existsSync(resolved)) {
resolved = plugin.__virtualModulePrefix + request.request
vfs.writeModule(resolved, '')
plugin.__vfs!.writeModule(resolved, '')
plugin.__vfsModules!.add(resolved)
}

// construt the new request
Expand All @@ -112,10 +114,10 @@ export function getWebpackPlugin<UserOptions = {}> (
}

// load hook
if (plugin.load) {
if (plugin.load && plugin.__vfsModules) {
compiler.options.module.rules.push({
include () {
return true
include (id) {
return plugin.__vfsModules!.has(id)
},
enforce: plugin.enforce,
use: [{
Expand Down

0 comments on commit 4ff7681

Please sign in to comment.