Skip to content

Commit

Permalink
fix(optimizer): empty contents onLoad for all
Browse files Browse the repository at this point in the history
  • Loading branch information
swandir committed Feb 10, 2022
1 parent 92f50a9 commit 84205e2
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -194,6 +194,14 @@ export function esbuildDepPlugin(
}
})

// Returning empty contents that will be turned by ESBuild
// into a CommonJS module exporting an empty object.
// This is what ESBuild does when bundling a dependency
// starting from a non-browser-external entrypoint.
build.onLoad({ filter: /.*/, namespace: 'browser-external' }, () => {
return { contents: '' }
})

// yarn 2 pnp compat
if (isRunningWithYarnPnp) {
build.onResolve(
Expand All @@ -212,13 +220,6 @@ export function esbuildDepPlugin(
}
)

// Without pnp esbuild is transforming modules disabled for the browser
// to empty CommonJS modules. In pnp mode we achieve the same by passing
// empty contents causing esbuild to produce an empty CommonJS module.
build.onLoad({ filter: /.*/, namespace: 'browser-external' }, () => {
return { contents: '' }
})

build.onLoad({ filter: /.*/ }, async (args) => ({
contents: await require('fs').promises.readFile(args.path),
loader: 'default'
Expand Down

0 comments on commit 84205e2

Please sign in to comment.