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 ac6b4f54245e0f..8f2e6c167420ff 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -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: {}
@@ -3985,7 +3999,6 @@ packages:
'@vue/shared': 3.3.4
estree-walker: 2.0.2
source-map-js: 1.0.2
- dev: true
/@vue/compiler-dom@3.2.47:
resolution: {integrity: sha512-dBBnEHEPoftUiS03a4ggEig74J2YBZ2UIeyfpcRM2tavgMWo4bsEfgCGsu+uJIL/vax9S+JztH8NmQerUo7shQ==}
@@ -3998,7 +4011,6 @@ packages:
dependencies:
'@vue/compiler-core': 3.3.4
'@vue/shared': 3.3.4
- dev: true
/@vue/compiler-sfc@3.2.47:
resolution: {integrity: sha512-rog05W+2IFfxjMcFw10tM9+f7i/+FFpZJJ5XHX72NP9eC2uRD+42M3pYcQqDXVYoj74kHMSEdQ/WmCjt8JFksQ==}
@@ -4027,7 +4039,6 @@ packages:
magic-string: 0.30.0
postcss: 8.4.23
source-map-js: 1.0.2
- dev: true
/@vue/compiler-ssr@3.2.47:
resolution: {integrity: sha512-wVXC+gszhulcMD8wpxMsqSOpvDZ6xKXSVWkf50Guf/S+28hTAXPDYRTbLQ3EDkOP5Xz/+SY37YiwDquKbJOgZw==}
@@ -4040,7 +4051,6 @@ packages:
dependencies:
'@vue/compiler-dom': 3.3.4
'@vue/shared': 3.3.4
- dev: true
/@vue/devtools-api@6.4.5:
resolution: {integrity: sha512-JD5fcdIuFxU4fQyXUu3w2KpAJHzTVdN+p4iOX2lMWSHMOoQdMAcpFLZzm9Z/2nmsoZ1a96QEhZ26e50xLBsgOQ==}
@@ -4066,7 +4076,6 @@ packages:
'@vue/shared': 3.3.4
estree-walker: 2.0.2
magic-string: 0.30.0
- dev: true
/@vue/reactivity@3.2.47:
resolution: {integrity: sha512-7khqQ/75oyyg+N/e+iwV6lpy1f5wq759NdlS1fpAhFXa8VeAIKGgk2E/C4VF59lx5b+Ezs5fpp/5WsRYXQiKxQ==}
@@ -4077,7 +4086,6 @@ packages:
resolution: {integrity: sha512-kLTDLwd0B1jG08NBF3R5rqULtv/f8x3rOFByTDz4J53ttIQEDmALqKqXY0J+XQeN0aV2FBxY8nJDf88yvOPAqQ==}
dependencies:
'@vue/shared': 3.3.4
- dev: true
/@vue/runtime-core@3.2.47:
resolution: {integrity: sha512-RZxbLQIRB/K0ev0K9FXhNbBzT32H9iRtYbaXb0ZIz2usLms/D55dJR2t6cIEUn6vyhS3ALNvNthI+Q95C+NOpA==}
@@ -4090,7 +4098,6 @@ packages:
dependencies:
'@vue/reactivity': 3.3.4
'@vue/shared': 3.3.4
- dev: true
/@vue/runtime-dom@3.2.47:
resolution: {integrity: sha512-ArXrFTjS6TsDei4qwNvgrdmHtD930KgSKGhS5M+j8QxXrDJYLqYw4RRcDy1bz1m1wMmb6j+zGLifdVHtkXA7gA==}
@@ -4105,7 +4112,6 @@ packages:
'@vue/runtime-core': 3.3.4
'@vue/shared': 3.3.4
csstype: 3.1.2
- dev: true
/@vue/server-renderer@3.2.47(vue@3.2.47):
resolution: {integrity: sha512-dN9gc1i8EvmP9RCzvneONXsKfBRgqFeFZLurmHOveL7oH6HiFXJw5OGu294n1nHc/HMgTy6LulU/tv5/A7f/LA==}
@@ -4124,14 +4130,12 @@ packages:
'@vue/compiler-ssr': 3.3.4
'@vue/shared': 3.3.4
vue: 3.3.4
- dev: true
/@vue/shared@3.2.47:
resolution: {integrity: sha512-BHGyyGN3Q97EZx0taMQ+OLNuZcW3d37ZEVmEAyeoA9ERdGvm9Irc/0Fua8SNyOtV1w6BS4q25wbMzJujO9HIfQ==}
/@vue/shared@3.3.4:
resolution: {integrity: sha512-7OjdcV8vQ74eiz1TZLzZP4JwqM5fA94K6yntPS5Z25r9HDuGNzaGdgvwKYq6S+MxwF0TFRwe50fIR/MYnakdkQ==}
- dev: true
/@vueuse/core@10.1.2(vue@3.3.4):
resolution: {integrity: sha512-roNn8WuerI56A5uiTyF/TEYX0Y+VKlhZAF94unUfdhbDUI+NfwQMn4FUnUscIRUhv3344qvAghopU4bzLPNFlA==}
@@ -5222,7 +5226,6 @@ packages:
/csstype@3.1.2:
resolution: {integrity: sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==}
- dev: true
/d@1.0.1:
resolution: {integrity: sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==}
@@ -10278,7 +10281,6 @@ packages:
'@vue/runtime-dom': 3.3.4
'@vue/server-renderer': 3.3.4(vue@3.3.4)
'@vue/shared': 3.3.4
- dev: true
/vuex@4.1.0(vue@3.2.47):
resolution: {integrity: sha512-hmV6UerDrPcgbSy9ORAtNXDr9M4wlNP4pEFKye4ujJF8oqgFFuxDCdOLS3eNoRTtq5O3hoBDh9Doj1bQMYHRbQ==}
@@ -10579,7 +10581,7 @@ packages:
dependencies:
slash3: /slash@3.0.0
slash5: /slash@5.0.0
- vue: 3.2.47
+ vue: 3.3.4
dev: false
file:playground/external/dep-that-requires:
@@ -10589,7 +10591,7 @@ packages:
dependencies:
slash3: /slash@3.0.0
slash5: /slash@5.0.0
- vue: 3.2.47
+ vue: 3.3.4
dev: false
file:playground/import-assertion/import-assertion-dep: