From 0bd053d4f9e6ab1f991fac3b8fc34c40ac8feab1 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 15 Jul 2022 23:11:08 +0800 Subject: [PATCH 1/2] fix: externalize workspace relative import when bundle config --- packages/vite/src/node/config.ts | 33 ++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 28fe2cdebb8e58..e79da48b76e551 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -900,13 +900,42 @@ async function bundleConfigFile( { name: 'externalize-deps', setup(build) { - build.onResolve({ filter: /.*/ }, (args) => { - const id = args.path + build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => { + // bundle all relative paths if (id[0] !== '.' && !path.isAbsolute(id)) { return { external: true } } + // if the file is outside of a vite project, 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 + if (id.startsWith('..')) { + 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 relative import after bundled as a single vite config + path: path.relative(path.dirname(fileName), idFsPath), + external: true + } + } + } + } }) } }, From 763a568728e1d895056329d85ed72d26648a0186 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 15 Jul 2022 23:28:41 +0800 Subject: [PATCH 2/2] fix: check for all other imports --- packages/vite/src/node/config.ts | 34 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index e79da48b76e551..6b9195f17927ed 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -901,14 +901,14 @@ async function bundleConfigFile( name: 'externalize-deps', setup(build) { build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => { - // bundle all relative paths + // externalize bare imports if (id[0] !== '.' && !path.isAbsolute(id)) { return { external: true } } - // if the file is outside of a vite project, make sure that the we can - // also access it's third-party dependencies. externalize if not. + // 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) @@ -918,21 +918,19 @@ async function bundleConfigFile( // ├─ foo-project/ // │ ├─ utils.js --------> external (has own node_modules) // │ ├─ package.json - if (id.startsWith('..')) { - 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 relative import after bundled as a single vite config - path: path.relative(path.dirname(fileName), idFsPath), - external: true - } + 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 } } }