11import type { ResolvePluginInstance , Resolver } from 'webpack'
2+ import type VirtualModulesPluginType from 'webpack-virtual-modules'
23import type { ResolvedUnpluginOptions , UnpluginContext , UnpluginContextMeta , UnpluginFactory , UnpluginInstance , WebpackCompiler } from '../types'
34import fs from 'node:fs'
45import { resolve } from 'node:path'
56import process from 'node:process'
6- import VirtualModulesPlugin from 'webpack-virtual-modules'
77import { normalizeObjectHook } from '../utils/filter'
88import { toArray } from '../utils/general'
99import { normalizeAbsolutePath , transformUse } from '../utils/webpack-like'
@@ -24,6 +24,9 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
2424 factory : UnpluginFactory < UserOptions > ,
2525) : UnpluginInstance < UserOptions > [ 'webpack' ] {
2626 return ( userOptions ?: UserOptions ) => {
27+ // eslint-disable-next-line ts/no-require-imports
28+ const VirtualModulesPlugin : typeof VirtualModulesPluginType = require ( 'webpack-virtual-modules' )
29+
2730 return {
2831 apply ( compiler : WebpackCompiler ) {
2932 // We need the prefix of virtual modules to be an absolute path so webpack let's us load them (even if it's made up)
@@ -51,7 +54,7 @@ export function getWebpackPlugin<UserOptions = Record<string, never>>(
5154
5255 // resolveId hook
5356 if ( plugin . resolveId ) {
54- let vfs = compiler . options . plugins . find ( i => i instanceof VirtualModulesPlugin ) as VirtualModulesPlugin
57+ let vfs = compiler . options . plugins . find ( i => i instanceof VirtualModulesPlugin )
5558 if ( ! vfs ) {
5659 vfs = new VirtualModulesPlugin ( )
5760 compiler . options . plugins . push ( vfs )
0 commit comments