Skip to content

Commit

Permalink
fix(webpack,rspack): incorrect assumption about the current working d…
Browse files Browse the repository at this point in the history
…irectory in webpack and rspack (#374)

Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
NullVoxPopuli and antfu committed Mar 29, 2024
1 parent f7e427d commit b0ec0e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/rspack/index.ts
Expand Up @@ -19,14 +19,16 @@ const LOAD_LOADER = resolve(
__DEV__ ? '../../dist/rspack/loaders/load.js' : 'rspack/loaders/load',
)

const VIRTUAL_MODULE_PREFIX = resolve(process.cwd(), '_virtual_')

export function getRspackPlugin<UserOptions = Record<string, never>>(
factory: UnpluginFactory<UserOptions>,
): UnpluginInstance<UserOptions>['rspack'] {
return (userOptions?: UserOptions): RspackPluginInstance => {
return {
apply(compiler) {
// 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)
// In the loader we strip the made up prefix path again
const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), '_virtual_')

const injected = compiler.$unpluginContext || {}
compiler.$unpluginContext = injected

Expand Down
9 changes: 4 additions & 5 deletions src/webpack/index.ts
Expand Up @@ -16,17 +16,16 @@ const LOAD_LOADER = resolve(
__dirname,
__DEV__ ? '../../dist/webpack/loaders/load' : 'webpack/loaders/load',
)

// 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)
// In the loader we strip the made up prefix path again
const VIRTUAL_MODULE_PREFIX = resolve(process.cwd(), '_virtual_')

export function getWebpackPlugin<UserOptions = Record<string, never>>(
factory: UnpluginFactory<UserOptions>,
): UnpluginInstance<UserOptions>['webpack'] {
return (userOptions?: UserOptions) => {
return {
apply(compiler: WebpackCompiler) {
// 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)
// In the loader we strip the made up prefix path again
const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), '_virtual_')

const injected = compiler.$unpluginContext || {}
compiler.$unpluginContext = injected

Expand Down

0 comments on commit b0ec0e0

Please sign in to comment.