Skip to content

Commit 5a8a3ab

Browse files
authored
fix: externalize workspace relative import when bundle config (#9140)
1 parent a52b45e commit 5a8a3ab

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

packages/vite/src/node/config.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -900,13 +900,40 @@ async function bundleConfigFile(
900900
{
901901
name: 'externalize-deps',
902902
setup(build) {
903-
build.onResolve({ filter: /.*/ }, (args) => {
904-
const id = args.path
903+
build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => {
904+
// externalize bare imports
905905
if (id[0] !== '.' && !path.isAbsolute(id)) {
906906
return {
907907
external: true
908908
}
909909
}
910+
// bundle the rest and make sure that the we can also access
911+
// it's third-party dependencies. externalize if not.
912+
// monorepo/
913+
// ├─ package.json
914+
// ├─ utils.js -----------> bundle (share same node_modules)
915+
// ├─ vite-project/
916+
// │ ├─ vite.config.js --> entry
917+
// │ ├─ package.json
918+
// ├─ foo-project/
919+
// │ ├─ utils.js --------> external (has own node_modules)
920+
// │ ├─ package.json
921+
const idFsPath = path.resolve(path.dirname(importer), id)
922+
const idPkgPath = lookupFile(idFsPath, [`package.json`], {
923+
pathOnly: true
924+
})
925+
if (idPkgPath) {
926+
const idPkgDir = path.dirname(idPkgPath)
927+
// if this file needs to go up one or more directory to reach the vite config,
928+
// that means it has it's own node_modules (e.g. foo-project)
929+
if (path.relative(idPkgDir, fileName).startsWith('..')) {
930+
return {
931+
// normalize actual import after bundled as a single vite config
932+
path: idFsPath,
933+
external: true
934+
}
935+
}
936+
}
910937
})
911938
}
912939
},

0 commit comments

Comments
 (0)