From 6c08ef7db52b07b1bfb8874ff149c2b4f3b882e7 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 27 Sep 2022 10:06:14 +0800 Subject: [PATCH 1/8] fix(config): resolve implicit deps as absolute path --- packages/vite/src/node/config.ts | 54 ++++++++++++++------------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 09c6e00adf9d6e..c0ac1c6ad7c01d 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -31,7 +31,8 @@ import { mergeAlias, mergeConfig, normalizeAlias, - normalizePath + normalizePath, + resolveFrom } from './utils' import { createPluginHookUtils, @@ -47,8 +48,12 @@ import { DEFAULT_MAIN_FIELDS, ENV_ENTRY } from './constants' -import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve' -import { resolvePlugin } from './plugins/resolve' +import type { + InternalResolveOptions, + ResolveOptions} 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' @@ -967,40 +972,29 @@ async function bundleConfigFile( { name: 'externalize-deps', setup(build) { + 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 }) => { // externalize bare imports if (id[0] !== '.' && !path.isAbsolute(id)) { return { + path: tryNodeResolve(id, importer, options, false)?.id, 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 - } - } - } }) } }, From 3f235770e00ecf036e7e1dc37b49ec220344f49f Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 29 Sep 2022 17:01:23 +0800 Subject: [PATCH 2/8] test: add test --- packages/vite/src/node/config.ts | 11 +++----- playground/config/__tests__/load.spec.ts | 24 +++++++++++++++++ playground/config/__tests__/serve.ts | 3 +++ playground/config/packages/entry/package.json | 3 +++ .../config/packages/entry/vite.config.ts | 5 ++++ playground/config/packages/siblings/foo.ts | 3 +++ .../config/packages/siblings/package.json | 7 +++++ pnpm-lock.yaml | 27 ++++++++++++++++++- 8 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 playground/config/__tests__/load.spec.ts create mode 100644 playground/config/__tests__/serve.ts create mode 100644 playground/config/packages/entry/package.json create mode 100644 playground/config/packages/entry/vite.config.ts create mode 100644 playground/config/packages/siblings/foo.ts create mode 100644 playground/config/packages/siblings/package.json diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index c0ac1c6ad7c01d..8261fbed688ca5 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -31,8 +31,7 @@ import { mergeAlias, mergeConfig, normalizeAlias, - normalizePath, - resolveFrom + normalizePath } from './utils' import { createPluginHookUtils, @@ -48,12 +47,8 @@ import { DEFAULT_MAIN_FIELDS, ENV_ENTRY } from './constants' -import type { - InternalResolveOptions, - ResolveOptions} from './plugins/resolve'; -import { - resolvePlugin -, tryNodeResolve } from './plugins/resolve' +import type { InternalResolveOptions, ResolveOptions } 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' diff --git a/playground/config/__tests__/load.spec.ts b/playground/config/__tests__/load.spec.ts new file mode 100644 index 00000000000000..e25ac224673a66 --- /dev/null +++ b/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, + ], + ], + } + `) +}) diff --git a/playground/config/__tests__/serve.ts b/playground/config/__tests__/serve.ts new file mode 100644 index 00000000000000..e8959c0a1eda19 --- /dev/null +++ b/playground/config/__tests__/serve.ts @@ -0,0 +1,3 @@ +export function serve() { + return +} diff --git a/playground/config/packages/entry/package.json b/playground/config/packages/entry/package.json new file mode 100644 index 00000000000000..c251a034716150 --- /dev/null +++ b/playground/config/packages/entry/package.json @@ -0,0 +1,3 @@ +{ + "name": "@vite/test-config-entry" +} diff --git a/playground/config/packages/entry/vite.config.ts b/playground/config/packages/entry/vite.config.ts new file mode 100644 index 00000000000000..a53828cb84cfd8 --- /dev/null +++ b/playground/config/packages/entry/vite.config.ts @@ -0,0 +1,5 @@ +import { array } from '../siblings/foo' + +export default { + array +} diff --git a/playground/config/packages/siblings/foo.ts b/playground/config/packages/siblings/foo.ts new file mode 100644 index 00000000000000..78a8912131faed --- /dev/null +++ b/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) diff --git a/playground/config/packages/siblings/package.json b/playground/config/packages/siblings/package.json new file mode 100644 index 00000000000000..4cbdc81d2100ea --- /dev/null +++ b/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" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ab25134fb26652..fc6c549d24a485 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -388,6 +388,22 @@ importers: playground/cli-module: specifiers: {} + playground/config/packages/entry: + specifiers: + '@types/lodash-es': ^4.17.6 + lodash-es: ^4.17.21 + devDependencies: + '@types/lodash-es': 4.17.6 + lodash-es: 4.17.21 + + playground/config/packages/siblings: + specifiers: + '@types/lodash': ^4.14.186 + lodash: ^4.17.21 + devDependencies: + '@types/lodash': 4.14.186 + lodash: 4.17.21 + playground/css: specifiers: css-dep: link:./css-dep @@ -2516,6 +2532,16 @@ packages: resolution: {integrity: sha512-1YXyYH83h6We1djyoUEqTlVyQtCfJAFXELSKW2ZRtjHD4hQ82CC4lvrv5D0l0FLcKBaiPbXyi3MpMsI9ZRgKsw==} dev: true + /@types/lodash-es/4.17.6: + resolution: {integrity: sha512-R+zTeVUKDdfoRxpAryaQNRKk3105Rrgx2CFRClIgRGaqDTdjsm8h6IYA8ir584W3ePzkZfst5xIgDwYrlh9HLg==} + dependencies: + '@types/lodash': 4.14.186 + dev: true + + /@types/lodash/4.14.186: + resolution: {integrity: sha512-eHcVlLXP0c2FlMPm56ITode2AgLMSa6aJ05JTTbYbI+7EMkCEE5qk2E41d5g2lCVTqRe0GnnRFurmlCsDODrPw==} + dev: true + /@types/micromatch/4.0.2: resolution: {integrity: sha512-oqXqVb0ci19GtH0vOA/U2TmHTcRY9kuZl4mqUxe0QmJAlIW13kzhuK5pi1i9+ngav8FjpSb9FVS/GE00GLX1VA==} dependencies: @@ -6184,7 +6210,6 @@ packages: /lodash-es/4.17.21: resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==} - dev: false /lodash.camelcase/4.3.0: resolution: {integrity: sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==} From 271c9b47029edc2c35f4b3ff9484ddd5fd8c5971 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 29 Sep 2022 17:03:28 +0800 Subject: [PATCH 3/8] chore: update lock --- pnpm-lock.yaml | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fc6c549d24a485..8349499bf4272b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -389,12 +389,7 @@ importers: specifiers: {} playground/config/packages/entry: - specifiers: - '@types/lodash-es': ^4.17.6 - lodash-es: ^4.17.21 - devDependencies: - '@types/lodash-es': 4.17.6 - lodash-es: 4.17.21 + specifiers: {} playground/config/packages/siblings: specifiers: @@ -2532,12 +2527,6 @@ packages: resolution: {integrity: sha512-1YXyYH83h6We1djyoUEqTlVyQtCfJAFXELSKW2ZRtjHD4hQ82CC4lvrv5D0l0FLcKBaiPbXyi3MpMsI9ZRgKsw==} dev: true - /@types/lodash-es/4.17.6: - resolution: {integrity: sha512-R+zTeVUKDdfoRxpAryaQNRKk3105Rrgx2CFRClIgRGaqDTdjsm8h6IYA8ir584W3ePzkZfst5xIgDwYrlh9HLg==} - dependencies: - '@types/lodash': 4.14.186 - dev: true - /@types/lodash/4.14.186: resolution: {integrity: sha512-eHcVlLXP0c2FlMPm56ITode2AgLMSa6aJ05JTTbYbI+7EMkCEE5qk2E41d5g2lCVTqRe0GnnRFurmlCsDODrPw==} dev: true @@ -6210,6 +6199,7 @@ packages: /lodash-es/4.17.21: resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==} + dev: false /lodash.camelcase/4.3.0: resolution: {integrity: sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==} From 71192f96456e4f3e2ef1c2569672f4c15c6348cc Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 29 Sep 2022 17:29:45 +0800 Subject: [PATCH 4/8] chore: use url in ESM --- packages/vite/src/node/config.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 8261fbed688ca5..3c1baf3ccd8399 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -985,8 +985,11 @@ async function bundleConfigFile( build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => { // externalize bare imports if (id[0] !== '.' && !path.isAbsolute(id)) { + const idFsPath = tryNodeResolve(id, importer, options, false)?.id + const idPath = + isESM && idFsPath ? pathToFileURL(idFsPath).href : idFsPath return { - path: tryNodeResolve(id, importer, options, false)?.id, + path: idPath, external: true } } From 1e28de6363ec00a012a4b40de74e4c4c7f661a96 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 1 Oct 2022 16:19:44 +0800 Subject: [PATCH 5/8] chore: try --- packages/vite/src/node/config.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 3c1baf3ccd8399..2cf716dc38b529 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1,5 +1,6 @@ import fs from 'node:fs' import path from 'node:path' +import pathPosix from 'node:path/posix' import { parse as parseUrl, pathToFileURL } from 'node:url' import { performance } from 'node:perf_hooks' import { createRequire } from 'node:module' @@ -984,7 +985,7 @@ async function bundleConfigFile( build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => { // externalize bare imports - if (id[0] !== '.' && !path.isAbsolute(id)) { + if (id[0] !== '.' && !isAbsolute(id)) { const idFsPath = tryNodeResolve(id, importer, options, false)?.id const idPath = isESM && idFsPath ? pathToFileURL(idFsPath).href : idFsPath @@ -1114,3 +1115,7 @@ export function isDepsOptimizerEnabled( (command === 'serve' && disabled === 'dev') ) } + +function isAbsolute(id: string) { + return path.isAbsolute(id) || pathPosix.isAbsolute(id) +} From 759eaab82d8c0d38369e72778239f91a8f2be5e3 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 3 Oct 2022 12:16:05 +0800 Subject: [PATCH 6/8] chore: update --- packages/vite/src/node/config.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 2cf716dc38b529..d775a376f7bd18 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1,6 +1,5 @@ import fs from 'node:fs' import path from 'node:path' -import pathPosix from 'node:path/posix' import { parse as parseUrl, pathToFileURL } from 'node:url' import { performance } from 'node:perf_hooks' import { createRequire } from 'node:module' @@ -1117,5 +1116,5 @@ export function isDepsOptimizerEnabled( } function isAbsolute(id: string) { - return path.isAbsolute(id) || pathPosix.isAbsolute(id) + return path.isAbsolute(id) || path.posix.isAbsolute(id) } From 73e70459fe2b357d8c55f9fdec3746a98da9a6f8 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 3 Oct 2022 13:11:57 +0800 Subject: [PATCH 7/8] chore: try --- packages/vite/src/node/config.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index d775a376f7bd18..0300248ff4c1ec 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -987,7 +987,11 @@ async function bundleConfigFile( if (id[0] !== '.' && !isAbsolute(id)) { const idFsPath = tryNodeResolve(id, importer, options, false)?.id const idPath = - isESM && idFsPath ? pathToFileURL(idFsPath).href : idFsPath + isESM && idFsPath + ? pathToFileURL(idFsPath).href + : idFsPath + ? normalizePath(idFsPath) + : undefined return { path: idPath, external: true From 16390b4bec2b36487dee851d155f3700e04ff199 Mon Sep 17 00:00:00 2001 From: BjornLuG Date: Mon, 3 Oct 2022 14:22:23 +0800 Subject: [PATCH 8/8] fix: handle windows issue --- packages/vite/src/node/config.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 0300248ff4c1ec..5fded668f295d0 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -982,18 +982,15 @@ async function bundleConfigFile( preserveSymlinks: false } - build.onResolve({ filter: /.*/ }, ({ path: id, importer }) => { + build.onResolve({ filter: /.*/ }, ({ path: id, importer, kind }) => { // externalize bare imports if (id[0] !== '.' && !isAbsolute(id)) { - const idFsPath = tryNodeResolve(id, importer, options, false)?.id - const idPath = - isESM && idFsPath - ? pathToFileURL(idFsPath).href - : idFsPath - ? normalizePath(idFsPath) - : undefined + let idFsPath = tryNodeResolve(id, importer, options, false)?.id + if (idFsPath && (isESM || kind === 'dynamic-import')) { + idFsPath = pathToFileURL(idFsPath).href + } return { - path: idPath, + path: idFsPath, external: true } }