diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 4b28e810ab2c6e..1c689c8220e37f 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -1101,8 +1101,6 @@ function resolveExportsOrImports( return options.isProduction case 'development': return !options.isProduction - case 'module': - return !options.isRequire } return true }) diff --git a/playground/resolve/__tests__/resolve.spec.ts b/playground/resolve/__tests__/resolve.spec.ts index fa274846b4d869..90d02a761258e6 100644 --- a/playground/resolve/__tests__/resolve.spec.ts +++ b/playground/resolve/__tests__/resolve.spec.ts @@ -67,6 +67,15 @@ test('Respect exports to take precedence over mainFields', async () => { expect(await page.textContent('.exports-with-module')).toMatch('[success]') }) +test('import and require resolve using module condition', async () => { + expect(await page.textContent('.exports-with-module-condition')).toMatch( + '[success]', + ) + expect( + await page.textContent('.exports-with-module-condition-required'), + ).toMatch('[success]') +}) + test('implicit dir/index.js', async () => { expect(await page.textContent('.index')).toMatch('[success]') }) diff --git a/playground/resolve/exports-with-module-condition-required/index.js b/playground/resolve/exports-with-module-condition-required/index.js new file mode 100644 index 00000000000000..b06508a52c8cc2 --- /dev/null +++ b/playground/resolve/exports-with-module-condition-required/index.js @@ -0,0 +1,3 @@ +/* eslint-disable import/no-commonjs */ +const { msg } = require('@vitejs/test-resolve-exports-with-module-condition') +module.exports = { msg } diff --git a/playground/resolve/exports-with-module-condition-required/package.json b/playground/resolve/exports-with-module-condition-required/package.json new file mode 100644 index 00000000000000..3920a875778ba1 --- /dev/null +++ b/playground/resolve/exports-with-module-condition-required/package.json @@ -0,0 +1,9 @@ +{ + "name": "@vitejs/test-resolve-exports-with-module-condition-required", + "private": true, + "version": "1.0.0", + "main": "index.js", + "dependencies": { + "@vitejs/test-resolve-exports-with-module-condition": "link:../exports-with-module-condition" + } +} diff --git a/playground/resolve/exports-with-module-condition/index.esm.js b/playground/resolve/exports-with-module-condition/index.esm.js new file mode 100644 index 00000000000000..f1e0b54ee0c7ac --- /dev/null +++ b/playground/resolve/exports-with-module-condition/index.esm.js @@ -0,0 +1 @@ +export const msg = '[success] exports with module condition (index.esm.js)' diff --git a/playground/resolve/exports-with-module-condition/index.js b/playground/resolve/exports-with-module-condition/index.js new file mode 100644 index 00000000000000..6465a8c55084c3 --- /dev/null +++ b/playground/resolve/exports-with-module-condition/index.js @@ -0,0 +1,2 @@ +/* eslint-disable import/no-commonjs */ +module.exports.msg = '[fail] exports with module condition (index.js)' diff --git a/playground/resolve/exports-with-module-condition/index.mjs b/playground/resolve/exports-with-module-condition/index.mjs new file mode 100644 index 00000000000000..1696a05425e35f --- /dev/null +++ b/playground/resolve/exports-with-module-condition/index.mjs @@ -0,0 +1 @@ +export const msg = '[fail] exports with module condition (index.mjs)' diff --git a/playground/resolve/exports-with-module-condition/package.json b/playground/resolve/exports-with-module-condition/package.json new file mode 100644 index 00000000000000..92a8f2dd2d1f80 --- /dev/null +++ b/playground/resolve/exports-with-module-condition/package.json @@ -0,0 +1,10 @@ +{ + "name": "@vitejs/test-resolve-exports-with-module-condition", + "private": true, + "version": "1.0.0", + "exports": { + "module": "./index.esm.js", + "import": "./index.mjs", + "require": "./index.js" + } +} diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 3aba3224c4822c..64d9306266a68d 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -36,6 +36,13 @@

Exports with legacy fallback

Exports with module

fail

+

+ Both import and require resolve using module condition (avoids dual package + hazard) +

+

fail

+

fail

+

Resolving top level with imports field

fail

@@ -214,6 +221,14 @@

resolve package that contains # in path

import { msg as exportsWithModule } from '@vitejs/test-resolve-exports-with-module' text('.exports-with-module', exportsWithModule) + import { msg as exportsWithModuleCondition } from '@vitejs/test-resolve-exports-with-module-condition' + import { msg as exportsWithModuleConditionRequired } from '@vitejs/test-resolve-exports-with-module-condition-required' + text('.exports-with-module-condition', exportsWithModuleCondition) + text( + '.exports-with-module-condition-required', + exportsWithModuleConditionRequired, + ) + // imports field import { msg as importsTopLevel } from '#top-level' text('.imports-top-level', importsTopLevel) diff --git a/playground/resolve/package.json b/playground/resolve/package.json index 3e4a231a05582f..6e3a7d50b86a63 100644 --- a/playground/resolve/package.json +++ b/playground/resolve/package.json @@ -34,6 +34,8 @@ "@vitejs/test-resolve-exports-legacy-fallback": "link:./exports-legacy-fallback", "@vitejs/test-resolve-exports-path": "link:./exports-path", "@vitejs/test-resolve-exports-with-module": "link:./exports-with-module", + "@vitejs/test-resolve-exports-with-module-condition": "link:./exports-with-module-condition", + "@vitejs/test-resolve-exports-with-module-condition-required": "link:./exports-with-module-condition-required", "@vitejs/test-resolve-linked": "workspace:*", "@vitejs/test-resolve-imports-pkg": "link:./imports-path/other-pkg", "@vitejs/test-resolve-sharp-dir": "link:./sharp-dir" diff --git a/playground/resolve/vite.config.js b/playground/resolve/vite.config.js index 5f2ba3e2e70df7..f4d923d1a05ae0 100644 --- a/playground/resolve/vite.config.js +++ b/playground/resolve/vite.config.js @@ -102,6 +102,7 @@ export default defineConfig({ ], optimizeDeps: { include: [ + '@vitejs/test-resolve-exports-with-module-condition-required', '@vitejs/test-require-pkg-with-module-field', '@vitejs/test-resolve-sharp-dir', ], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e0c939e96c82cf..47b461f31bcd95 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -271,7 +271,7 @@ importers: version: 15.0.2(rollup@3.21.0) '@rollup/plugin-typescript': specifier: ^11.1.0 - version: 11.1.0(rollup@3.21.0)(tslib@2.5.0)(typescript@5.0.2) + version: 11.1.0(rollup@3.21.0)(tslib@2.5.0) '@rollup/pluginutils': specifier: ^5.0.2 version: 5.0.2(rollup@3.21.0) @@ -403,7 +403,7 @@ importers: version: 1.0.1 tsconfck: specifier: ^2.1.1 - version: 2.1.1(typescript@5.0.2) + version: 2.1.1 tslib: specifier: ^2.5.0 version: 2.5.0 @@ -467,7 +467,7 @@ importers: version: 1.62.0 tailwindcss: specifier: ^3.3.1 - version: 3.3.1(ts-node@10.9.1) + version: 3.3.1 playground/build-old: {} @@ -1024,6 +1024,12 @@ importers: '@vitejs/test-resolve-exports-with-module': specifier: link:./exports-with-module version: link:exports-with-module + '@vitejs/test-resolve-exports-with-module-condition': + specifier: link:./exports-with-module-condition + version: link:exports-with-module-condition + '@vitejs/test-resolve-exports-with-module-condition-required': + specifier: link:./exports-with-module-condition-required + version: link:exports-with-module-condition-required '@vitejs/test-resolve-imports-pkg': specifier: link:./imports-path/other-pkg version: link:imports-path/other-pkg @@ -1085,6 +1091,14 @@ importers: playground/resolve/exports-with-module: {} + playground/resolve/exports-with-module-condition: {} + + playground/resolve/exports-with-module-condition-required: + dependencies: + '@vitejs/test-resolve-exports-with-module-condition': + specifier: link:../exports-with-module-condition + version: link:../exports-with-module-condition + playground/resolve/imports-path/other-pkg: {} playground/resolve/inline-package: {} @@ -1331,16 +1345,16 @@ importers: devDependencies: '@vitejs/plugin-vue': specifier: ^4.1.0 - version: 4.1.0(vite@packages+vite)(vue@3.2.47) + version: 4.1.0(vue@3.2.47) ts-node: specifier: ^10.9.1 - version: 10.9.1(@types/node@18.15.12)(typescript@5.0.2) + version: 10.9.1 playground/tailwind-sourcemap: dependencies: tailwindcss: specifier: ^3.3.1 - version: 3.3.1(ts-node@10.9.1) + version: 3.3.1 playground/transform-plugin: {} @@ -3461,6 +3475,25 @@ packages: rollup: 3.21.0 dev: true + /@rollup/plugin-typescript@11.1.0(rollup@3.21.0)(tslib@2.5.0): + resolution: {integrity: sha512-86flrfE+bSHB69znnTV6kVjkncs2LBMhcTCyxWgRxLyfXfQrxg4UwlAqENnjrrxnSNS/XKCDJCl8EkdFJVHOxw==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^2.14.0||^3.0.0 + tslib: '*' + typescript: '>=3.7.0' + peerDependenciesMeta: + rollup: + optional: true + tslib: + optional: true + dependencies: + '@rollup/pluginutils': 5.0.2(rollup@3.21.0) + resolve: 1.22.2 + rollup: 3.21.0 + tslib: 2.5.0 + dev: true + /@rollup/plugin-typescript@11.1.0(rollup@3.21.0)(tslib@2.5.0)(typescript@5.0.2): resolution: {integrity: sha512-86flrfE+bSHB69znnTV6kVjkncs2LBMhcTCyxWgRxLyfXfQrxg4UwlAqENnjrrxnSNS/XKCDJCl8EkdFJVHOxw==} engines: {node: '>=14.0.0'} @@ -3692,6 +3725,7 @@ packages: /@types/node@18.15.12: resolution: {integrity: sha512-Wha1UwsB3CYdqUm2PPzh/1gujGCNtWVUYF0mB00fJFoR4gTyWTDPjSm+zBF787Ahw8vSGgBja90MkgFwvB86Dg==} + dev: true /@types/node@18.15.5: resolution: {integrity: sha512-Ark2WDjjZO7GmvsyFFf81MXuGTA/d6oP38anyxWOL6EREyBKAxKoFHwBhaZxCfLRLpO8JgVXwqOwSwa7jRcjew==} @@ -3891,6 +3925,16 @@ packages: vue: 3.2.47 dev: true + /@vitejs/plugin-vue@4.1.0(vue@3.2.47): + resolution: {integrity: sha512-++9JOAFdcXI3lyer9UKUV4rfoQ3T1RN8yDqoCLar86s0xQct5yblxAE+yWgRnU5/0FOlVCpTZpYSBV/bGWrSrQ==} + engines: {node: ^14.18.0 || >=16.0.0} + peerDependencies: + vite: ^4.0.0 + vue: ^3.2.25 + dependencies: + vue: 3.2.47 + dev: true + /@vitejs/release-scripts@1.2.0: resolution: {integrity: sha512-HdLOS/BQtavOis+VvT1517y7a5jYEMuBIybnL1F1Pd7vIXkFYPdo0vSKtnDBILOHhFT27SYist4BFkppDvsBnA==} dependencies: @@ -4331,7 +4375,7 @@ packages: /axios@1.3.6: resolution: {integrity: sha512-PEcdkk7JcdPiMDkvM4K6ZBRYq9keuVJsToxm2zQIM70Qqo2WHTdJZMXcG9X+RmRp2VPNUQC8W1RAGbgt6b1yMg==} dependencies: - follow-redirects: 1.15.0(debug@4.3.4) + follow-redirects: 1.15.0 form-data: 4.0.0 proxy-from-env: 1.1.0 transitivePeerDependencies: @@ -6137,7 +6181,7 @@ packages: resolution: {integrity: sha512-WIWGi2L3DyTUvUrwRKgGi9TwxQMUEqPOPQBVi71R96jZXJdFskXEmf54BoZaS1kknGODoIGASGEzBUYdyMCBJg==} dev: true - /follow-redirects@1.15.0(debug@4.3.4): + /follow-redirects@1.15.0: resolution: {integrity: sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==} engines: {node: '>=4.0'} peerDependencies: @@ -6145,8 +6189,6 @@ packages: peerDependenciesMeta: debug: optional: true - dependencies: - debug: 4.3.4 /for-each@0.3.3: resolution: {integrity: sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==} @@ -6579,7 +6621,7 @@ packages: engines: {node: '>=8.0.0'} dependencies: eventemitter3: 4.0.7 - follow-redirects: 1.15.0(debug@4.3.4) + follow-redirects: 1.15.0 requires-port: 1.0.0 transitivePeerDependencies: - debug @@ -8208,6 +8250,22 @@ packages: camelcase-css: 2.0.1 postcss: 8.4.23 + /postcss-load-config@3.1.4(postcss@8.4.23): + resolution: {integrity: sha512-6DiM4E7v4coTE4uzA8U//WhtPwyhiim3eyjEMFCnUpzbrkK9wJHgKDT2mR+HbtSrd/NubVaYTOpSpjUl8NQeRg==} + engines: {node: '>= 10'} + peerDependencies: + postcss: '>=8.0.9' + ts-node: '>=9.0.0' + peerDependenciesMeta: + postcss: + optional: true + ts-node: + optional: true + dependencies: + lilconfig: 2.1.0 + postcss: 8.4.23 + yaml: 1.10.2 + /postcss-load-config@3.1.4(postcss@8.4.23)(ts-node@10.9.1): resolution: {integrity: sha512-6DiM4E7v4coTE4uzA8U//WhtPwyhiim3eyjEMFCnUpzbrkK9wJHgKDT2mR+HbtSrd/NubVaYTOpSpjUl8NQeRg==} engines: {node: '>= 10'} @@ -8222,8 +8280,9 @@ packages: dependencies: lilconfig: 2.1.0 postcss: 8.4.23 - ts-node: 10.9.1(@types/node@18.15.12)(typescript@5.0.2) + ts-node: 10.9.1 yaml: 1.10.2 + dev: false /postcss-load-config@4.0.1(postcss@8.4.23): resolution: {integrity: sha512-vEJIc8RdiBRu3oRAI0ymerOn+7rPuMvRXslTvZUKZonDHFIczxztIyJ1urxM1x9JXEikvpWWTUUqal5j/8QgvA==} @@ -9413,6 +9472,38 @@ packages: resolution: {integrity: sha512-8ftwWd+XnQtZ/aGbatrN4QFNGrKJzmbtixW+ODpci7pyoTajg4sonPP8aFLESAcuVxaC1FyDESt+SpfFCH9rZQ==} dev: false + /tailwindcss@3.3.1: + resolution: {integrity: sha512-Vkiouc41d4CEq0ujXl6oiGFQ7bA3WEhUZdTgXAhtKxSy49OmKs8rEfQmupsfF0IGW8fv2iQkp1EVUuapCFrZ9g==} + engines: {node: '>=12.13.0'} + hasBin: true + dependencies: + arg: 5.0.2 + chokidar: 3.5.3(patch_hash=dzxbf3kgof5pdmbsyih2x43sq4) + color-name: 1.1.4 + didyoumean: 1.2.2 + dlv: 1.1.3 + fast-glob: 3.2.12 + glob-parent: 6.0.2 + is-glob: 4.0.3 + jiti: 1.18.2 + lilconfig: 2.1.0 + micromatch: 4.0.5 + normalize-path: 3.0.0 + object-hash: 3.0.0 + picocolors: 1.0.0 + postcss: 8.4.23 + postcss-import: 14.1.0(postcss@8.4.23) + postcss-js: 4.0.0(postcss@8.4.23) + postcss-load-config: 3.1.4(postcss@8.4.23) + postcss-nested: 6.0.0(postcss@8.4.23) + postcss-selector-parser: 6.0.11 + postcss-value-parser: 4.2.0 + quick-lru: 5.1.1 + resolve: 1.22.2 + sucrase: 3.32.0 + transitivePeerDependencies: + - ts-node + /tailwindcss@3.3.1(ts-node@10.9.1): resolution: {integrity: sha512-Vkiouc41d4CEq0ujXl6oiGFQ7bA3WEhUZdTgXAhtKxSy49OmKs8rEfQmupsfF0IGW8fv2iQkp1EVUuapCFrZ9g==} engines: {node: '>=12.13.0'} @@ -9444,6 +9535,7 @@ packages: sucrase: 3.32.0 transitivePeerDependencies: - ts-node + dev: false /tar@6.1.11: resolution: {integrity: sha512-an/KZQzQUkZCkuoAA64hM92X0Urb6VpRhAFllDzz44U2mcD5scmT3zBc4VgVpkugF580+DQn8eAFSyoQt0tznA==} @@ -9589,7 +9681,7 @@ packages: /ts-interface-checker@0.1.13: resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==} - /ts-node@10.9.1(@types/node@18.15.12)(typescript@5.0.2): + /ts-node@10.9.1: resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==} hasBin: true peerDependencies: @@ -9608,18 +9700,16 @@ packages: '@tsconfig/node12': 1.0.9 '@tsconfig/node14': 1.0.1 '@tsconfig/node16': 1.0.2 - '@types/node': 18.15.12 acorn: 8.8.2 acorn-walk: 8.2.0(acorn@8.8.2) arg: 4.1.3 create-require: 1.1.1 diff: 4.0.2 make-error: 1.3.6 - typescript: 5.0.2 v8-compile-cache-lib: 3.0.1 yn: 3.1.1 - /tsconfck@2.1.1(typescript@5.0.2): + /tsconfck@2.1.1: resolution: {integrity: sha512-ZPCkJBKASZBmBUNqGHmRhdhM8pJYDdOXp4nRgj/O0JwUwsMq50lCDRQP/M5GBNAA0elPrq4gAeu4dkaVCuKWww==} engines: {node: ^14.13.1 || ^16 || >=18} hasBin: true @@ -9628,8 +9718,6 @@ packages: peerDependenciesMeta: typescript: optional: true - dependencies: - typescript: 5.0.2 dev: true /tsconfig-paths@3.14.1: @@ -9746,6 +9834,7 @@ packages: resolution: {integrity: sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==} engines: {node: '>=12.20'} hasBin: true + dev: true /typescript@5.0.4: resolution: {integrity: sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==}