Skip to content

Commit 367920b

Browse files
authored
fix(resolve): always use module condition (#13370)
1 parent 7792515 commit 367920b

File tree

12 files changed

+67
-2
lines changed

12 files changed

+67
-2
lines changed

packages/vite/src/node/plugins/resolve.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1105,8 +1105,6 @@ function resolveExportsOrImports(
11051105
return options.isProduction
11061106
case 'development':
11071107
return !options.isProduction
1108-
case 'module':
1109-
return !options.isRequire
11101108
}
11111109
return true
11121110
})

playground/resolve/__tests__/resolve.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ test('Respect exports to take precedence over mainFields', async () => {
6767
expect(await page.textContent('.exports-with-module')).toMatch('[success]')
6868
})
6969

70+
test('import and require resolve using module condition', async () => {
71+
expect(await page.textContent('.exports-with-module-condition')).toMatch(
72+
'[success]',
73+
)
74+
expect(
75+
await page.textContent('.exports-with-module-condition-required'),
76+
).toMatch('[success]')
77+
})
78+
7079
test('implicit dir/index.js', async () => {
7180
expect(await page.textContent('.index')).toMatch('[success]')
7281
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable import/no-commonjs */
2+
const { msg } = require('@vitejs/test-resolve-exports-with-module-condition')
3+
module.exports = { msg }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@vitejs/test-resolve-exports-with-module-condition-required",
3+
"private": true,
4+
"version": "1.0.0",
5+
"main": "index.cjs",
6+
"dependencies": {
7+
"@vitejs/test-resolve-exports-with-module-condition": "link:../exports-with-module-condition"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msg = '[success] exports with module condition (index.esm.js)'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable import/no-commonjs */
2+
module.exports.msg = '[fail] exports with module condition (index.js)'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msg = '[fail] exports with module condition (index.mjs)'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@vitejs/test-resolve-exports-with-module-condition",
3+
"private": true,
4+
"version": "1.0.0",
5+
"exports": {
6+
"module": "./index.esm.js",
7+
"import": "./index.mjs",
8+
"require": "./index.js"
9+
}
10+
}

playground/resolve/index.html

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ <h2>Exports with legacy fallback</h2>
3636
<h2>Exports with module</h2>
3737
<p class="exports-with-module">fail</p>
3838

39+
<h2>
40+
Both import and require resolve using module condition (avoids dual package
41+
hazard)
42+
</h2>
43+
<p class="exports-with-module-condition">fail</p>
44+
<p class="exports-with-module-condition-required">fail</p>
45+
3946
<h2>Resolving top level with imports field</h2>
4047
<p class="imports-top-level">fail</p>
4148

@@ -214,6 +221,14 @@ <h2>resolve package that contains # in path</h2>
214221
import { msg as exportsWithModule } from '@vitejs/test-resolve-exports-with-module'
215222
text('.exports-with-module', exportsWithModule)
216223

224+
import { msg as exportsWithModuleCondition } from '@vitejs/test-resolve-exports-with-module-condition'
225+
import { msg as exportsWithModuleConditionRequired } from '@vitejs/test-resolve-exports-with-module-condition-required'
226+
text('.exports-with-module-condition', exportsWithModuleCondition)
227+
text(
228+
'.exports-with-module-condition-required',
229+
exportsWithModuleConditionRequired,
230+
)
231+
217232
// imports field
218233
import { msg as importsTopLevel } from '#top-level'
219234
text('.imports-top-level', importsTopLevel)

playground/resolve/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
"@vitejs/test-resolve-exports-legacy-fallback": "link:./exports-legacy-fallback",
3535
"@vitejs/test-resolve-exports-path": "link:./exports-path",
3636
"@vitejs/test-resolve-exports-with-module": "link:./exports-with-module",
37+
"@vitejs/test-resolve-exports-with-module-condition": "link:./exports-with-module-condition",
38+
"@vitejs/test-resolve-exports-with-module-condition-required": "link:./exports-with-module-condition-required",
3739
"@vitejs/test-resolve-linked": "workspace:*",
3840
"@vitejs/test-resolve-imports-pkg": "link:./imports-path/other-pkg",
3941
"@vitejs/test-resolve-sharp-dir": "link:./sharp-dir"

playground/resolve/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export default defineConfig({
102102
],
103103
optimizeDeps: {
104104
include: [
105+
'@vitejs/test-resolve-exports-with-module-condition-required',
105106
'@vitejs/test-require-pkg-with-module-field',
106107
'@vitejs/test-resolve-sharp-dir',
107108
],

pnpm-lock.yaml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)