From 64f19b945da3d692127aaa6273eb78a07e28ee84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 20 Oct 2022 15:10:28 +0900 Subject: [PATCH] fix(config): don't use module condition (`import.meta.resolve`) (fixes #10430) (#10528) --- packages/vite/LICENSE.md | 82 +++++++++++++++++++ packages/vite/package.json | 3 +- packages/vite/src/node/config.ts | 59 ++++++------- playground/config/__tests__/load.spec.ts | 4 +- playground/config/packages/entry/package.json | 5 +- .../config/packages/entry/vite.config.ts | 4 +- .../plugin-module-condition/index.cjs | 1 + .../plugin-module-condition/index.d.ts | 2 + .../plugin-module-condition/index.mjs | 1 + .../plugin-module-condition/module.mjs | 1 + .../plugin-module-condition/package.json | 11 +++ pnpm-lock.yaml | 12 +++ 12 files changed, 152 insertions(+), 33 deletions(-) create mode 100644 playground/config/packages/plugin-module-condition/index.cjs create mode 100644 playground/config/packages/plugin-module-condition/index.d.ts create mode 100644 playground/config/packages/plugin-module-condition/index.mjs create mode 100644 playground/config/packages/plugin-module-condition/module.mjs create mode 100644 playground/config/packages/plugin-module-condition/package.json diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index 41f21cba4cd801..68244430b2e8a5 100644 --- a/packages/vite/LICENSE.md +++ b/packages/vite/LICENSE.md @@ -1651,6 +1651,88 @@ 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 cd8a1dbcf5290f..6bcd5b4b0164d2 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -88,6 +88,7 @@ "cors": "^2.8.5", "cross-spawn": "^7.0.3", "debug": "^4.3.4", + "dep-types": "link:./src/types", "dotenv": "^14.3.2", "dotenv-expand": "^5.1.0", "es-module-lexer": "^1.0.3", @@ -95,6 +96,7 @@ "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", @@ -118,7 +120,6 @@ "strip-literal": "^0.4.2", "tsconfck": "^2.0.1", "tslib": "^2.4.0", - "dep-types": "link:./src/types", "types": "link:./types", "ufo": "^0.8.5", "ws": "^8.9.0" diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 4d6db3300a8339..dff650a1b487fd 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -8,6 +8,7 @@ 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, @@ -25,7 +26,6 @@ import { createDebugger, createFilter, dynamicImport, - isBuiltin, isExternalUrl, isObject, lookupFile, @@ -49,7 +49,7 @@ import { ENV_ENTRY } from './constants' import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve' -import { resolvePlugin, tryNodeResolve } from './plugins/resolve' +import { resolvePlugin } from './plugins/resolve' import type { LogLevel, Logger } from './logger' import { createLogger } from './logger' import type { DepOptimizationConfig, DepOptimizationOptions } from './optimizer' @@ -949,6 +949,7 @@ async function bundleConfigFile( platform: 'node', bundle: true, format: isESM ? 'esm' : 'cjs', + mainFields: ['main'], sourcemap: 'inline', metafile: true, define: { @@ -960,38 +961,38 @@ async function bundleConfigFile( { name: 'externalize-deps', setup(build) { - const options: InternalResolveOptions = { - root: path.dirname(fileName), - isBuild: true, - isProduction: true, - isRequire: !isESM, - preferRelative: false, - tryIndex: true, - mainFields: [], - browserField: false, - conditions: [], - dedupe: [], - extensions: DEFAULT_EXTENSIONS, - preserveSymlinks: false - } - - build.onResolve({ filter: /.*/ }, ({ path: id, importer, kind }) => { - // externalize bare imports - if (id[0] !== '.' && !path.isAbsolute(id) && !isBuiltin(id)) { - // partial deno support as `npm:` does not work in `tryNodeResolve` + // externalize bare imports + build.onResolve( + { filter: /^[^.].*/ }, + async ({ path: id, importer, kind }) => { + if (kind === 'entry-point' || path.isAbsolute(id)) { + return + } + + // partial deno support as `npm:` does not work with esbuild if (id.startsWith('npm:')) { return { external: true } } - let idFsPath = tryNodeResolve(id, importer, options, false)?.id - if (idFsPath && (isESM || kind === 'dynamic-import')) { - idFsPath = pathToFileURL(idFsPath).href - } - return { - path: idFsPath, - 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 + ) } + + return { path: resolved, external: true } } - }) + ) } }, { diff --git a/playground/config/__tests__/load.spec.ts b/playground/config/__tests__/load.spec.ts index e25ac224673a66..3205912b8156c3 100644 --- a/playground/config/__tests__/load.spec.ts +++ b/playground/config/__tests__/load.spec.ts @@ -5,7 +5,8 @@ import { expect, it } from 'vitest' it('loadConfigFromFile', async () => { const { config } = await loadConfigFromFile( {} as any, - resolve(__dirname, '../packages/entry/vite.config.ts') + resolve(__dirname, '../packages/entry/vite.config.ts'), + resolve(__dirname, '../packages/entry') ) expect(config).toMatchInlineSnapshot(` { @@ -19,6 +20,7 @@ it('loadConfigFromFile', async () => { 4, ], ], + "moduleCondition": "require condition", } `) }) diff --git a/playground/config/packages/entry/package.json b/playground/config/packages/entry/package.json index c251a034716150..287aa6c698618b 100644 --- a/playground/config/packages/entry/package.json +++ b/playground/config/packages/entry/package.json @@ -1,3 +1,6 @@ { - "name": "@vite/test-config-entry" + "name": "@vite/test-config-entry", + "dependencies": { + "@vite/test-config-plugin-module-condition": "link:../plugin-module-condition" + } } diff --git a/playground/config/packages/entry/vite.config.ts b/playground/config/packages/entry/vite.config.ts index a53828cb84cfd8..9b865e5fa3d66a 100644 --- a/playground/config/packages/entry/vite.config.ts +++ b/playground/config/packages/entry/vite.config.ts @@ -1,5 +1,7 @@ +import moduleCondition from '@vite/test-config-plugin-module-condition' import { array } from '../siblings/foo' export default { - array + array, + moduleCondition } diff --git a/playground/config/packages/plugin-module-condition/index.cjs b/playground/config/packages/plugin-module-condition/index.cjs new file mode 100644 index 00000000000000..082d40b7f30a06 --- /dev/null +++ b/playground/config/packages/plugin-module-condition/index.cjs @@ -0,0 +1 @@ +module.exports = 'require condition' diff --git a/playground/config/packages/plugin-module-condition/index.d.ts b/playground/config/packages/plugin-module-condition/index.d.ts new file mode 100644 index 00000000000000..e04fab0f58f539 --- /dev/null +++ b/playground/config/packages/plugin-module-condition/index.d.ts @@ -0,0 +1,2 @@ +const str: string +export default str diff --git a/playground/config/packages/plugin-module-condition/index.mjs b/playground/config/packages/plugin-module-condition/index.mjs new file mode 100644 index 00000000000000..a4386771448945 --- /dev/null +++ b/playground/config/packages/plugin-module-condition/index.mjs @@ -0,0 +1 @@ +export default 'import condition' diff --git a/playground/config/packages/plugin-module-condition/module.mjs b/playground/config/packages/plugin-module-condition/module.mjs new file mode 100644 index 00000000000000..6361ab1059bd2d --- /dev/null +++ b/playground/config/packages/plugin-module-condition/module.mjs @@ -0,0 +1 @@ +export default 'module condition' diff --git a/playground/config/packages/plugin-module-condition/package.json b/playground/config/packages/plugin-module-condition/package.json new file mode 100644 index 00000000000000..38df99751d9e3e --- /dev/null +++ b/playground/config/packages/plugin-module-condition/package.json @@ -0,0 +1,11 @@ +{ + "name": "@vite/test-config-plugin-module-condition", + "exports": { + ".": { + "types": "./index.d.ts", + "module": "./module.mjs", + "import": "./index.mjs", + "require": "./index.cjs" + } + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 713af3da8d2bb1..09c4c4eb626b0b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -241,6 +241,7 @@ 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 @@ -306,6 +307,7 @@ 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 @@ -392,6 +394,12 @@ importers: url: 0.11.0 playground/config/packages/entry: + specifiers: + '@vite/test-config-plugin-module-condition': link:../plugin-module-condition + dependencies: + '@vite/test-config-plugin-module-condition': link:../plugin-module-condition + + playground/config/packages/plugin-module-condition: specifiers: {} playground/config/packages/siblings: @@ -5910,6 +5918,10 @@ 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'}