Skip to content

Commit

Permalink
fix(resolve): always use module condition (#13370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Jun 7, 2023
1 parent 7792515 commit 367920b
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 2 deletions.
2 changes: 0 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -1105,8 +1105,6 @@ function resolveExportsOrImports(
return options.isProduction
case 'development':
return !options.isProduction
case 'module':
return !options.isRequire
}
return true
})
Expand Down
9 changes: 9 additions & 0 deletions playground/resolve/__tests__/resolve.spec.ts
Expand Up @@ -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]')
})
Expand Down
@@ -0,0 +1,3 @@
/* eslint-disable import/no-commonjs */
const { msg } = require('@vitejs/test-resolve-exports-with-module-condition')
module.exports = { msg }
@@ -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"
}
}
@@ -0,0 +1 @@
export const msg = '[success] exports with module condition (index.esm.js)'
2 changes: 2 additions & 0 deletions 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)'
1 change: 1 addition & 0 deletions playground/resolve/exports-with-module-condition/index.mjs
@@ -0,0 +1 @@
export const msg = '[fail] exports with module condition (index.mjs)'
10 changes: 10 additions & 0 deletions 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"
}
}
15 changes: 15 additions & 0 deletions playground/resolve/index.html
Expand Up @@ -36,6 +36,13 @@ <h2>Exports with legacy fallback</h2>
<h2>Exports with module</h2>
<p class="exports-with-module">fail</p>

<h2>
Both import and require resolve using module condition (avoids dual package
hazard)
</h2>
<p class="exports-with-module-condition">fail</p>
<p class="exports-with-module-condition-required">fail</p>

<h2>Resolving top level with imports field</h2>
<p class="imports-top-level">fail</p>

Expand Down Expand Up @@ -214,6 +221,14 @@ <h2>resolve package that contains # in path</h2>
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)
Expand Down
2 changes: 2 additions & 0 deletions playground/resolve/package.json
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/vite.config.js
Expand Up @@ -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',
],
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 367920b

Please sign in to comment.