From fe661a55c042e1d397d1211ae41fe59f979b832e Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 11 Oct 2022 13:18:20 +0800 Subject: [PATCH 1/3] fix(config): skip resolve builtin modules --- packages/vite/src/node/config.ts | 7 ++----- playground/cli-module/package.json | 3 +++ playground/cli-module/vite.config.js | 5 +++++ pnpm-lock.yaml | 17 ++++++++++------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 6faf7d2d863f03..dd0feee665ae34 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -25,6 +25,7 @@ import { createDebugger, createFilter, dynamicImport, + isBuiltin, isExternalUrl, isObject, lookupFile, @@ -984,7 +985,7 @@ async function bundleConfigFile( build.onResolve({ filter: /.*/ }, ({ path: id, importer, kind }) => { // externalize bare imports - if (id[0] !== '.' && !isAbsolute(id)) { + if (id[0] !== '.' && !path.isAbsolute(id) && !isBuiltin(id)) { let idFsPath = tryNodeResolve(id, importer, options, false)?.id if (idFsPath && (isESM || kind === 'dynamic-import')) { idFsPath = pathToFileURL(idFsPath).href @@ -1115,7 +1116,3 @@ export function isDepsOptimizerEnabled( (command === 'serve' && disabled === 'dev') ) } - -function isAbsolute(id: string) { - return path.isAbsolute(id) || path.posix.isAbsolute(id) -} diff --git a/playground/cli-module/package.json b/playground/cli-module/package.json index 0f297d4e4d0563..30a94d2bb877ad 100644 --- a/playground/cli-module/package.json +++ b/playground/cli-module/package.json @@ -8,5 +8,8 @@ "build": "vite build", "debug": "node --inspect-brk ../../packages/vite/bin/vite", "serve": "vite preview" + }, + "devDependencies": { + "url": "^0.11.0" } } diff --git a/playground/cli-module/vite.config.js b/playground/cli-module/vite.config.js index 9629e535d58e38..c85a46ce7a37fe 100644 --- a/playground/cli-module/vite.config.js +++ b/playground/cli-module/vite.config.js @@ -1,4 +1,9 @@ import { defineConfig } from 'vite' +import { URL } from 'url' + +// make sure bundling works even if `url` refers to the locally installed +// `url` package instead of the built-in `url` nodejs module +globalThis.__test_url = URL export default defineConfig({ server: { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 000d30c1040610..10ef42fe220fc2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -383,10 +383,16 @@ importers: specifiers: {} playground/cli: - specifiers: {} + specifiers: + url: ^0.11.0 + dependencies: + url: 0.11.0 playground/cli-module: - specifiers: {} + specifiers: + url: ^0.11.0 + devDependencies: + url: 0.11.0 playground/config/packages/entry: specifiers: {} @@ -7601,8 +7607,7 @@ packages: dev: true /punycode/1.3.2: - resolution: {integrity: sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=} - dev: false + resolution: {integrity: sha512-RofWgt/7fL5wP1Y7fxE7/EmTLzQVnB0ycyibJ0OOHIlJqTNzglYFxVwETOcIoJqJmpDXJ9xImDv+Fq34F/d4Dw==} /punycode/2.1.1: resolution: {integrity: sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==} @@ -7636,7 +7641,6 @@ packages: resolution: {integrity: sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=} engines: {node: '>=0.4.x'} deprecated: The querystring API is considered Legacy. new code should use the URLSearchParams API instead. - dev: false /queue-microtask/1.2.3: resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} @@ -8907,11 +8911,10 @@ packages: dev: true /url/0.11.0: - resolution: {integrity: sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE=} + resolution: {integrity: sha512-kbailJa29QrtXnxgq+DdCEGlbTeYM2eJUxsz6vjZavrCYPMIFHMKQmSKYAIuUK2i7hgPm28a8piX5NTUtM/LKQ==} dependencies: punycode: 1.3.2 querystring: 0.2.0 - dev: false /utf8-byte-length/1.0.4: resolution: {integrity: sha512-4+wkEYLBbWxqTahEsWrhxepcoVOJ+1z5PGIjPZxRkytcdSUaNjIjBM7Xn8E+pdSuV7SzvWovBFA54FO0JSoqhA==} From afca40d8210392305d0f4ff7af6d9983b9b69b54 Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 11 Oct 2022 13:25:26 +0800 Subject: [PATCH 2/3] chore: fix lockfile --- pnpm-lock.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 10ef42fe220fc2..8c17bb951e931c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -383,10 +383,7 @@ importers: specifiers: {} playground/cli: - specifiers: - url: ^0.11.0 - dependencies: - url: 0.11.0 + specifiers: {} playground/cli-module: specifiers: From 3806a9872c21dc943366722732fd3a4459e3f6a4 Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 11 Oct 2022 15:27:35 +0800 Subject: [PATCH 3/3] chore: fix lint --- playground/cli-module/vite.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/playground/cli-module/vite.config.js b/playground/cli-module/vite.config.js index c85a46ce7a37fe..4020d4627917bb 100644 --- a/playground/cli-module/vite.config.js +++ b/playground/cli-module/vite.config.js @@ -1,5 +1,6 @@ -import { defineConfig } from 'vite' +// eslint-disable-next-line import/no-nodejs-modules import { URL } from 'url' +import { defineConfig } from 'vite' // make sure bundling works even if `url` refers to the locally installed // `url` package instead of the built-in `url` nodejs module