Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: externalize workspace relative import when bundle config #9140

Merged
merged 2 commits into from Jul 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 29 additions & 2 deletions packages/vite/src/node/config.ts
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