From b15d21ca65df8cd06a3139e461e49817dc763325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Fri, 28 Oct 2022 17:07:27 +0900 Subject: [PATCH] fix(config): resolve externalized specifier with internal resolver (#10683) --- packages/vite/LICENSE.md | 82 ----------------------- packages/vite/package.json | 1 - packages/vite/src/node/config.ts | 56 ++++++++++------ packages/vite/src/node/plugins/resolve.ts | 46 +++++++++++-- pnpm-lock.yaml | 21 ++++-- 5 files changed, 90 insertions(+), 116 deletions(-) diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index 749d149e2e39b8..042a589f01d17c 100644 --- a/packages/vite/LICENSE.md +++ b/packages/vite/LICENSE.md @@ -1651,88 +1651,6 @@ Repository: git+https://github.com/css-modules/icss-utils.git --------------------------------------- -## import-meta-resolve -License: MIT -By: Titus Wormer -Repository: wooorm/import-meta-resolve - -> (The MIT License) -> -> Copyright (c) 2021 Titus Wormer -> -> Permission is hereby granted, free of charge, to any person obtaining -> a copy of this software and associated documentation files (the -> 'Software'), to deal in the Software without restriction, including -> without limitation the rights to use, copy, modify, merge, publish, -> distribute, sublicense, and/or sell copies of the Software, and to -> permit persons to whom the Software is furnished to do so, subject to -> the following conditions: -> -> The above copyright notice and this permission notice shall be -> included in all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, -> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -> -> --- -> -> This is a derivative work based on: -> . -> Which is licensed: -> -> """ -> Copyright Node.js contributors. All rights reserved. -> -> Permission is hereby granted, free of charge, to any person obtaining a copy -> of this software and associated documentation files (the "Software"), to -> deal in the Software without restriction, including without limitation the -> rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -> sell copies of the Software, and to permit persons to whom the Software is -> furnished to do so, subject to the following conditions: -> -> The above copyright notice and this permission notice shall be included in -> all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -> IN THE SOFTWARE. -> """ -> -> This license applies to parts of Node.js originating from the -> https://github.com/joyent/node repository: -> -> """ -> Copyright Joyent, Inc. and other Node contributors. All rights reserved. -> Permission is hereby granted, free of charge, to any person obtaining a copy -> of this software and associated documentation files (the "Software"), to -> deal in the Software without restriction, including without limitation the -> rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -> sell copies of the Software, and to permit persons to whom the Software is -> furnished to do so, subject to the following conditions: -> -> The above copyright notice and this permission notice shall be included in -> all copies or substantial portions of the Software. -> -> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -> IN THE SOFTWARE. -> """ - ---------------------------------------- - ## inflight License: ISC By: Isaac Z. Schlueter diff --git a/packages/vite/package.json b/packages/vite/package.json index 55db233e3fc0bb..73f1ff11f293e2 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -96,7 +96,6 @@ "etag": "^1.8.1", "fast-glob": "^3.2.12", "http-proxy": "^1.18.1", - "import-meta-resolve": "^2.1.0", "json5": "^2.2.1", "launch-editor-middleware": "^2.6.0", "magic-string": "^0.26.7", diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index ea9de372ed5fee..0019cc0ddebe2a 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -8,7 +8,6 @@ import type { Alias, AliasOptions } from 'dep-types/alias' import aliasPlugin from '@rollup/plugin-alias' import { build } from 'esbuild' import type { RollupOptions } from 'rollup' -import { resolve as importMetaResolve } from 'import-meta-resolve' import type { HookHandler, Plugin } from './plugin' import type { BuildOptions, @@ -26,6 +25,7 @@ import { createDebugger, createFilter, dynamicImport, + isBuiltin, isExternalUrl, isObject, lookupFile, @@ -48,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, + InternalResolveOptionsWithOverrideConditions, + 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' @@ -957,11 +961,31 @@ async function bundleConfigFile( { name: 'externalize-deps', setup(build) { + const options: InternalResolveOptionsWithOverrideConditions = { + root: path.dirname(fileName), + isBuild: true, + isProduction: true, + isRequire: !isESM, + preferRelative: false, + tryIndex: true, + mainFields: [], + browserField: false, + conditions: [], + overrideConditions: ['node'], + dedupe: [], + extensions: DEFAULT_EXTENSIONS, + preserveSymlinks: false + } + // externalize bare imports build.onResolve( { filter: /^[^.].*/ }, async ({ path: id, importer, kind }) => { - if (kind === 'entry-point' || path.isAbsolute(id)) { + if ( + kind === 'entry-point' || + path.isAbsolute(id) || + isBuiltin(id) + ) { return } @@ -969,24 +993,14 @@ async function bundleConfigFile( if (id.startsWith('npm:')) { return { external: true } } - - const resolveWithRequire = - kind === 'require-call' || - kind === 'require-resolve' || - (kind === 'import-statement' && !isESM) - - let resolved: string - if (resolveWithRequire) { - const require = createRequire(importer) - resolved = require.resolve(id) - } else { - resolved = await importMetaResolve( - id, - pathToFileURL(importer).href - ) + let idFsPath = tryNodeResolve(id, importer, options, false)?.id + if (idFsPath && (isESM || kind === 'dynamic-import')) { + idFsPath = pathToFileURL(idFsPath).href + } + return { + path: idFsPath, + external: true } - - return { path: resolved, external: true } } ) } diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 1d7c86a301d2b1..fcbd7644b6d857 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -580,12 +580,21 @@ function tryResolveFile( } } +export type InternalResolveOptionsWithOverrideConditions = + InternalResolveOptions & { + /** + * @deprecated In future, `conditions` will work like this. + * @internal + */ + overrideConditions?: string[] + } + export const idToPkgMap = new Map() export function tryNodeResolve( id: string, importer: string | null | undefined, - options: InternalResolveOptions, + options: InternalResolveOptionsWithOverrideConditions, targetWeb: boolean, depsOptimizer?: DepsOptimizer, ssr?: boolean, @@ -1031,17 +1040,44 @@ function packageEntryFailure(id: string, details?: string) { ) } +const conditionalConditions = new Set(['production', 'development', 'module']) + function resolveExports( pkg: PackageData['data'], key: string, - options: InternalResolveOptions, + options: InternalResolveOptionsWithOverrideConditions, targetWeb: boolean ) { - const conditions = [options.isProduction ? 'production' : 'development'] - if (!options.isRequire) { + const overrideConditions = options.overrideConditions + ? new Set(options.overrideConditions) + : undefined + + const conditions = [] + if ( + (!overrideConditions || overrideConditions.has('production')) && + options.isProduction + ) { + conditions.push('production') + } + if ( + (!overrideConditions || overrideConditions.has('development')) && + !options.isProduction + ) { + conditions.push('development') + } + if ( + (!overrideConditions || overrideConditions.has('module')) && + !options.isRequire + ) { conditions.push('module') } - if (options.conditions.length > 0) { + if (options.overrideConditions) { + conditions.push( + ...options.overrideConditions.filter((condition) => + conditionalConditions.has(condition) + ) + ) + } else if (options.conditions.length > 0) { conditions.push(...options.conditions) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2a94d585cb5690..05160f4d6d3ab6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -241,7 +241,6 @@ importers: fast-glob: ^3.2.12 fsevents: ~2.3.2 http-proxy: ^1.18.1 - import-meta-resolve: ^2.1.0 json5: ^2.2.1 launch-editor-middleware: ^2.6.0 magic-string: ^0.26.7 @@ -307,7 +306,6 @@ importers: etag: 1.8.1 fast-glob: 3.2.12 http-proxy: 1.18.1_debug@4.3.4 - import-meta-resolve: 2.1.0 json5: 2.2.1 launch-editor-middleware: 2.6.0 magic-string: 0.26.7 @@ -5540,6 +5538,19 @@ packages: peerDependenciesMeta: debug: optional: true + dev: false + + /follow-redirects/1.15.0_debug@4.3.4: + resolution: {integrity: sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==} + engines: {node: '>=4.0'} + peerDependencies: + debug: '*' + peerDependenciesMeta: + debug: + optional: true + dependencies: + debug: 4.3.4 + dev: true /form-data/4.0.0: resolution: {integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==} @@ -5934,7 +5945,7 @@ packages: engines: {node: '>=8.0.0'} dependencies: eventemitter3: 4.0.7 - follow-redirects: 1.15.0 + follow-redirects: 1.15.0_debug@4.3.4 requires-port: 1.0.0 transitivePeerDependencies: - debug @@ -6012,10 +6023,6 @@ packages: engines: {node: '>=8'} dev: true - /import-meta-resolve/2.1.0: - resolution: {integrity: sha512-yG9pxkWJVTy4cmRsNWE3ztFdtFuYIV8G4N+cbCkO8b+qngkLyIUhxQFuZ0qJm67+0nUOxjMPT7nfksPKza1v2g==} - dev: true - /imurmurhash/0.1.4: resolution: {integrity: sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==} engines: {node: '>=0.8.19'}