Skip to content

Commit

Permalink
fix: externalize workspace relative import when bundle config (#9140)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jul 15, 2022
1 parent a52b45e commit 5a8a3ab
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,13 +900,40 @@ async function bundleConfigFile(
{
name: 'externalize-deps',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
const id = args.path
build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => {
// externalize bare imports
if (id[0] !== '.' && !path.isAbsolute(id)) {
return {
external: true
}
}
// bundle the rest and make sure that the we can also access
// it's third-party dependencies. externalize if not.
// monorepo/
// ├─ package.json
// ├─ utils.js -----------> bundle (share same node_modules)
// ├─ vite-project/
// │ ├─ vite.config.js --> entry
// │ ├─ package.json
// ├─ foo-project/
// │ ├─ utils.js --------> external (has own node_modules)
// │ ├─ package.json
const idFsPath = path.resolve(path.dirname(importer), id)
const idPkgPath = lookupFile(idFsPath, [`package.json`], {
pathOnly: true
})
if (idPkgPath) {
const idPkgDir = path.dirname(idPkgPath)
// if this file needs to go up one or more directory to reach the vite config,
// that means it has it's own node_modules (e.g. foo-project)
if (path.relative(idPkgDir, fileName).startsWith('..')) {
return {
// normalize actual import after bundled as a single vite config
path: idFsPath,
external: true
}
}
}
})
}
},
Expand Down

0 comments on commit 5a8a3ab

Please sign in to comment.