fix(config): resolve externalized specifier with internal resolver (#10683)

This commit is contained in:
翠 / green 2022-10-28 17:07:27 +09:00 committed by GitHub
parent e2ea6afd47
commit b15d21ca65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 116 deletions

View File

@ -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 <mailto:tituswormer@gmail.com>
>
> 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:
> <https://github.com/nodejs/node>.
> 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

View File

@ -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",

View File

@ -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 }
}
)
}

View File

@ -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<string, PackageData>()
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)
}

View File

@ -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'}