Skip to content

Commit 64f19b9

Browse files
authoredOct 20, 2022
fix(config): don't use module condition (import.meta.resolve) (fixes #10430) (#10528)
1 parent f328f61 commit 64f19b9

File tree

12 files changed

+152
-33
lines changed

12 files changed

+152
-33
lines changed
 

‎packages/vite/LICENSE.md

+82
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,88 @@ Repository: git+https://github.com/css-modules/icss-utils.git
16511651
16521652
---------------------------------------
16531653

1654+
## import-meta-resolve
1655+
License: MIT
1656+
By: Titus Wormer
1657+
Repository: wooorm/import-meta-resolve
1658+
1659+
> (The MIT License)
1660+
>
1661+
> Copyright (c) 2021 Titus Wormer <mailto:tituswormer@gmail.com>
1662+
>
1663+
> Permission is hereby granted, free of charge, to any person obtaining
1664+
> a copy of this software and associated documentation files (the
1665+
> 'Software'), to deal in the Software without restriction, including
1666+
> without limitation the rights to use, copy, modify, merge, publish,
1667+
> distribute, sublicense, and/or sell copies of the Software, and to
1668+
> permit persons to whom the Software is furnished to do so, subject to
1669+
> the following conditions:
1670+
>
1671+
> The above copyright notice and this permission notice shall be
1672+
> included in all copies or substantial portions of the Software.
1673+
>
1674+
> THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
1675+
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1676+
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1677+
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
1678+
> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
1679+
> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1680+
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1681+
>
1682+
> ---
1683+
>
1684+
> This is a derivative work based on:
1685+
> <https://github.com/nodejs/node>.
1686+
> Which is licensed:
1687+
>
1688+
> """
1689+
> Copyright Node.js contributors. All rights reserved.
1690+
>
1691+
> Permission is hereby granted, free of charge, to any person obtaining a copy
1692+
> of this software and associated documentation files (the "Software"), to
1693+
> deal in the Software without restriction, including without limitation the
1694+
> rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1695+
> sell copies of the Software, and to permit persons to whom the Software is
1696+
> furnished to do so, subject to the following conditions:
1697+
>
1698+
> The above copyright notice and this permission notice shall be included in
1699+
> all copies or substantial portions of the Software.
1700+
>
1701+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1702+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1703+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1704+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1705+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1706+
> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1707+
> IN THE SOFTWARE.
1708+
> """
1709+
>
1710+
> This license applies to parts of Node.js originating from the
1711+
> https://github.com/joyent/node repository:
1712+
>
1713+
> """
1714+
> Copyright Joyent, Inc. and other Node contributors. All rights reserved.
1715+
> Permission is hereby granted, free of charge, to any person obtaining a copy
1716+
> of this software and associated documentation files (the "Software"), to
1717+
> deal in the Software without restriction, including without limitation the
1718+
> rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1719+
> sell copies of the Software, and to permit persons to whom the Software is
1720+
> furnished to do so, subject to the following conditions:
1721+
>
1722+
> The above copyright notice and this permission notice shall be included in
1723+
> all copies or substantial portions of the Software.
1724+
>
1725+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1726+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1727+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1728+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1729+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1730+
> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1731+
> IN THE SOFTWARE.
1732+
> """
1733+
1734+
---------------------------------------
1735+
16541736
## inflight
16551737
License: ISC
16561738
By: Isaac Z. Schlueter

‎packages/vite/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@
8888
"cors": "^2.8.5",
8989
"cross-spawn": "^7.0.3",
9090
"debug": "^4.3.4",
91+
"dep-types": "link:./src/types",
9192
"dotenv": "^14.3.2",
9293
"dotenv-expand": "^5.1.0",
9394
"es-module-lexer": "^1.0.3",
9495
"estree-walker": "^3.0.1",
9596
"etag": "^1.8.1",
9697
"fast-glob": "^3.2.12",
9798
"http-proxy": "^1.18.1",
99+
"import-meta-resolve": "^2.1.0",
98100
"json5": "^2.2.1",
99101
"launch-editor-middleware": "^2.6.0",
100102
"magic-string": "^0.26.7",
@@ -118,7 +120,6 @@
118120
"strip-literal": "^0.4.2",
119121
"tsconfck": "^2.0.1",
120122
"tslib": "^2.4.0",
121-
"dep-types": "link:./src/types",
122123
"types": "link:./types",
123124
"ufo": "^0.8.5",
124125
"ws": "^8.9.0"

‎packages/vite/src/node/config.ts

+30-29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Alias, AliasOptions } from 'dep-types/alias'
88
import aliasPlugin from '@rollup/plugin-alias'
99
import { build } from 'esbuild'
1010
import type { RollupOptions } from 'rollup'
11+
import { resolve as importMetaResolve } from 'import-meta-resolve'
1112
import type { HookHandler, Plugin } from './plugin'
1213
import type {
1314
BuildOptions,
@@ -25,7 +26,6 @@ import {
2526
createDebugger,
2627
createFilter,
2728
dynamicImport,
28-
isBuiltin,
2929
isExternalUrl,
3030
isObject,
3131
lookupFile,
@@ -49,7 +49,7 @@ import {
4949
ENV_ENTRY
5050
} from './constants'
5151
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
52-
import { resolvePlugin, tryNodeResolve } from './plugins/resolve'
52+
import { resolvePlugin } from './plugins/resolve'
5353
import type { LogLevel, Logger } from './logger'
5454
import { createLogger } from './logger'
5555
import type { DepOptimizationConfig, DepOptimizationOptions } from './optimizer'
@@ -949,6 +949,7 @@ async function bundleConfigFile(
949949
platform: 'node',
950950
bundle: true,
951951
format: isESM ? 'esm' : 'cjs',
952+
mainFields: ['main'],
952953
sourcemap: 'inline',
953954
metafile: true,
954955
define: {
@@ -960,38 +961,38 @@ async function bundleConfigFile(
960961
{
961962
name: 'externalize-deps',
962963
setup(build) {
963-
const options: InternalResolveOptions = {
964-
root: path.dirname(fileName),
965-
isBuild: true,
966-
isProduction: true,
967-
isRequire: !isESM,
968-
preferRelative: false,
969-
tryIndex: true,
970-
mainFields: [],
971-
browserField: false,
972-
conditions: [],
973-
dedupe: [],
974-
extensions: DEFAULT_EXTENSIONS,
975-
preserveSymlinks: false
976-
}
977-
978-
build.onResolve({ filter: /.*/ }, ({ path: id, importer, kind }) => {
979-
// externalize bare imports
980-
if (id[0] !== '.' && !path.isAbsolute(id) && !isBuiltin(id)) {
981-
// partial deno support as `npm:` does not work in `tryNodeResolve`
964+
// externalize bare imports
965+
build.onResolve(
966+
{ filter: /^[^.].*/ },
967+
async ({ path: id, importer, kind }) => {
968+
if (kind === 'entry-point' || path.isAbsolute(id)) {
969+
return
970+
}
971+
972+
// partial deno support as `npm:` does not work with esbuild
982973
if (id.startsWith('npm:')) {
983974
return { external: true }
984975
}
985-
let idFsPath = tryNodeResolve(id, importer, options, false)?.id
986-
if (idFsPath && (isESM || kind === 'dynamic-import')) {
987-
idFsPath = pathToFileURL(idFsPath).href
988-
}
989-
return {
990-
path: idFsPath,
991-
external: true
976+
977+
const resolveWithRequire =
978+
kind === 'require-call' ||
979+
kind === 'require-resolve' ||
980+
(kind === 'import-statement' && !isESM)
981+
982+
let resolved: string
983+
if (resolveWithRequire) {
984+
const require = createRequire(importer)
985+
resolved = require.resolve(id)
986+
} else {
987+
resolved = await importMetaResolve(
988+
id,
989+
pathToFileURL(importer).href
990+
)
992991
}
992+
993+
return { path: resolved, external: true }
993994
}
994-
})
995+
)
995996
}
996997
},
997998
{

‎playground/config/__tests__/load.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { expect, it } from 'vitest'
55
it('loadConfigFromFile', async () => {
66
const { config } = await loadConfigFromFile(
77
{} as any,
8-
resolve(__dirname, '../packages/entry/vite.config.ts')
8+
resolve(__dirname, '../packages/entry/vite.config.ts'),
9+
resolve(__dirname, '../packages/entry')
910
)
1011
expect(config).toMatchInlineSnapshot(`
1112
{
@@ -19,6 +20,7 @@ it('loadConfigFromFile', async () => {
1920
4,
2021
],
2122
],
23+
"moduleCondition": "require condition",
2224
}
2325
`)
2426
})
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"name": "@vite/test-config-entry"
2+
"name": "@vite/test-config-entry",
3+
"dependencies": {
4+
"@vite/test-config-plugin-module-condition": "link:../plugin-module-condition"
5+
}
36
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import moduleCondition from '@vite/test-config-plugin-module-condition'
12
import { array } from '../siblings/foo'
23

34
export default {
4-
array
5+
array,
6+
moduleCondition
57
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'require condition'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const str: string
2+
export default str
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'import condition'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'module condition'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@vite/test-config-plugin-module-condition",
3+
"exports": {
4+
".": {
5+
"types": "./index.d.ts",
6+
"module": "./module.mjs",
7+
"import": "./index.mjs",
8+
"require": "./index.cjs"
9+
}
10+
}
11+
}

‎pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.