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 @@
fail
+fail
+fail
+fail
@@ -214,6 +221,14 @@