Skip to content

Commit

Permalink
fix(config): resolve externalized specifier with internal resolver (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Oct 28, 2022
1 parent e2ea6af commit b15d21c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 116 deletions.
82 changes: 0 additions & 82 deletions packages/vite/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]>
>
> 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
Expand Down
1 change: 0 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 35 additions & 21 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,6 +25,7 @@ import {
createDebugger,
createFilter,
dynamicImport,
isBuiltin,
isExternalUrl,
isObject,
lookupFile,
Expand All @@ -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'
Expand Down Expand Up @@ -957,36 +961,46 @@ 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
}

// partial deno support as `npm:` does not work with esbuild
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 }
}
)
}
Expand Down
46 changes: 41 additions & 5 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down
21 changes: 14 additions & 7 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 b15d21c

Please sign in to comment.