Skip to content

Commit

Permalink
pref: reuse virtual module instance if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 27, 2021
1 parent a33bbe3 commit 7962ad9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ export function getWebpackPlugin<UserOptions = {}> (

// resolveId hook
if (plugin.resolveId) {
const virtualModule = new VirtualModulesPlugin()
plugin.__vfs = virtualModule
let vfs = compiler.options.plugins.find(i => i instanceof VirtualModulesPlugin) as VirtualModulesPlugin
if (!vfs) {
vfs = new VirtualModulesPlugin()
compiler.options.plugins.push(vfs)
}
plugin.__vfs = vfs

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

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

// load hook
if (plugin.load) {
if (plugin.__vfs) {
compiler.options.plugins.push(plugin.__vfs)
}

compiler.options.module.rules.push({
include () {
return true
Expand Down

0 comments on commit 7962ad9

Please sign in to comment.