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(config): resolve implicit deps as absolute path #10254

Merged
merged 9 commits into from Oct 3, 2022
Merged
Show file tree
Hide file tree
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
57 changes: 27 additions & 30 deletions packages/vite/src/node/config.ts
Expand Up @@ -48,7 +48,7 @@ import {
ENV_ENTRY
} from './constants'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import { resolvePlugin } from './plugins/resolve'
import { resolvePlugin, tryNodeResolve } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
import type { DepOptimizationConfig, DepOptimizationOptions } from './optimizer'
Expand Down Expand Up @@ -967,40 +967,33 @@ async function bundleConfigFile(
{
name: 'externalize-deps',
setup(build) {
build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => {
const options: InternalResolveOptions = {
root: path.dirname(fileName),
isBuild: true,
isProduction: true,
isRequire: !isESM,
preferRelative: false,
tryIndex: true,
mainFields: DEFAULT_MAIN_FIELDS,
browserField: false,
conditions: [],
dedupe: [],
extensions: DEFAULT_EXTENSIONS,
preserveSymlinks: false
}

build.onResolve({ filter: /.*/ }, ({ path: id, importer, kind }) => {
// externalize bare imports
if (id[0] !== '.' && !path.isAbsolute(id)) {
if (id[0] !== '.' && !isAbsolute(id)) {
let idFsPath = tryNodeResolve(id, importer, options, false)?.id
if (idFsPath && (isESM || kind === 'dynamic-import')) {
idFsPath = pathToFileURL(idFsPath).href
}
return {
path: idFsPath,
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: isESM ? pathToFileURL(idFsPath).href : idFsPath,
external: true
}
}
}
})
}
},
Expand Down Expand Up @@ -1122,3 +1115,7 @@ export function isDepsOptimizerEnabled(
(command === 'serve' && disabled === 'dev')
)
}

function isAbsolute(id: string) {
return path.isAbsolute(id) || path.posix.isAbsolute(id)
}
24 changes: 24 additions & 0 deletions playground/config/__tests__/load.spec.ts
@@ -0,0 +1,24 @@
import { resolve } from 'node:path'
import { loadConfigFromFile } from 'vite'
import { expect, it } from 'vitest'

it('loadConfigFromFile', async () => {
const { config } = await loadConfigFromFile(
{} as any,
resolve(__dirname, '../packages/entry/vite.config.ts')
)
expect(config).toMatchInlineSnapshot(`
{
"array": [
[
1,
3,
],
[
2,
4,
],
],
}
`)
})
3 changes: 3 additions & 0 deletions playground/config/__tests__/serve.ts
@@ -0,0 +1,3 @@
export function serve() {
return
}
3 changes: 3 additions & 0 deletions playground/config/packages/entry/package.json
@@ -0,0 +1,3 @@
{
"name": "@vite/test-config-entry"
}
5 changes: 5 additions & 0 deletions playground/config/packages/entry/vite.config.ts
@@ -0,0 +1,5 @@
import { array } from '../siblings/foo'

export default {
array
}
3 changes: 3 additions & 0 deletions playground/config/packages/siblings/foo.ts
@@ -0,0 +1,3 @@
import { partition } from 'lodash'

export const array = partition([1, 2, 3, 4], (n) => n % 2)
7 changes: 7 additions & 0 deletions playground/config/packages/siblings/package.json
@@ -0,0 +1,7 @@
{
"name": "@vite/test-config-sibling",
"devDependencies": {
"@types/lodash": "^4.14.186",
"lodash": "^4.17.21"
}
}
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.