Skip to content

Commit

Permalink
fix(config): resolve implicit deps as absolute path (#10254)
Browse files Browse the repository at this point in the history
Co-authored-by: BjornLuG <bjornlu.dev@gmail.com>
  • Loading branch information
antfu and bluwy committed Oct 3, 2022
1 parent e42c7cd commit dc140af
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 30 deletions.
57 changes: 27 additions & 30 deletions packages/vite/src/node/config.ts
Expand Up @@ -46,7 +46,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 @@ -955,40 +955,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 @@ -1110,3 +1103,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.

0 comments on commit dc140af

Please sign in to comment.