From 367920b6c75fa4a99fa6eaa40dd97ac5038843d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 7 Jun 2023 10:28:57 +0200 Subject: [PATCH] fix(resolve): always use `module` condition (#13370) --- packages/vite/src/node/plugins/resolve.ts | 2 -- playground/resolve/__tests__/resolve.spec.ts | 9 +++++++++ .../index.cjs | 3 +++ .../package.json | 9 +++++++++ .../exports-with-module-condition/index.esm.js | 1 + .../exports-with-module-condition/index.js | 2 ++ .../exports-with-module-condition/index.mjs | 1 + .../exports-with-module-condition/package.json | 10 ++++++++++ playground/resolve/index.html | 15 +++++++++++++++ playground/resolve/package.json | 2 ++ playground/resolve/vite.config.js | 1 + pnpm-lock.yaml | 14 ++++++++++++++ 12 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 playground/resolve/exports-with-module-condition-required/index.cjs create mode 100644 playground/resolve/exports-with-module-condition-required/package.json create mode 100644 playground/resolve/exports-with-module-condition/index.esm.js create mode 100644 playground/resolve/exports-with-module-condition/index.js create mode 100644 playground/resolve/exports-with-module-condition/index.mjs create mode 100644 playground/resolve/exports-with-module-condition/package.json diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index a7d898efa5a566..f89abbe267422d 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -1105,8 +1105,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.cjs b/playground/resolve/exports-with-module-condition-required/index.cjs new file mode 100644 index 00000000000000..b06508a52c8cc2 --- /dev/null +++ b/playground/resolve/exports-with-module-condition-required/index.cjs @@ -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..8e1b2969fbf7f2 --- /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.cjs", + "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 cc02aa7279696c..98a02dffad4a93 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1034,6 +1034,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 @@ -1095,6 +1101,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: {}