Skip to content

Commit 1c33089

Browse files
himself65huozhi
andauthored
fix: runtime fallback (#590)
Co-authored-by: Jiachi Liu <[email protected]>
1 parent cf109bc commit 1c33089

File tree

6 files changed

+59
-3
lines changed

6 files changed

+59
-3
lines changed

src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ export const runtimeExportConventions = new Set([
3131
// Browser only
3232
'browser',
3333
])
34+
export const runtimeExportConventionsFallback = new Map<string, string[]>([
35+
// ESM only runtime
36+
['workerd', ['import', 'default']],
37+
['edge-light', ['import', 'default']],
38+
['browser', ['import', 'default']],
39+
40+
// it could be CJS or ESM
41+
['electron', ['default']],
42+
['react-server', ['default']],
43+
['react-native', ['default']],
44+
['node', ['default']],
45+
['deno', ['default']],
46+
['bun', ['default']],
47+
])
3448
export const optimizeConventions = new Set(['development', 'production'])
3549
export const specialExportConventions = new Set([
3650
...runtimeExportConventions,

src/plugins/alias-plugin.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { Entries } from '../types'
33
import { posix } from 'path'
44
import { relativify } from '../lib/format'
55
import { getSpecialExportTypeFromConditionNames } from '../entries'
6-
import { specialExportConventions } from '../constants'
6+
import {
7+
specialExportConventions,
8+
runtimeExportConventionsFallback,
9+
} from '../constants'
710

811
function hasNoSpecialCondition(conditionNames: Set<string>) {
912
return [...conditionNames].every(
@@ -36,12 +39,21 @@ function findJsBundlePathCallback(
3639
conditionNames.has(specialCondition) ||
3740
(!conditionNames.has('default') && hasNoSpecialCondition(conditionNames))
3841

39-
return (
42+
const match =
4043
isMatchedConditionWithFormat &&
4144
!isTypesCondName &&
4245
hasBundle &&
4346
isMatchedFormat
44-
)
47+
if (!match) {
48+
const fallback = runtimeExportConventionsFallback.get(specialCondition)
49+
if (!fallback) {
50+
return false
51+
} else {
52+
return fallback.some((name) => conditionNames.has(name))
53+
}
54+
} else {
55+
return match
56+
}
4557
}
4658

4759
function findTypesFileCallback({
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { readFile } from 'fs/promises'
2+
import { createIntegrationTest } from '../utils'
3+
4+
describe('integration import fallback', () => {
5+
it('should handle import fallback', async () => {
6+
await createIntegrationTest(
7+
{
8+
directory: __dirname,
9+
},
10+
async ({ distDir }) => {
11+
const aEdgeLightFile = `${distDir}/a.edge-light.js`
12+
const content = await readFile(aEdgeLightFile, { encoding: 'utf-8' })
13+
expect(content).toMatch(/import '.\/b.js'/)
14+
},
15+
)
16+
})
17+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"type": "module",
3+
"exports": {
4+
"./a": {
5+
"edge-light": "./dist/a.edge-light.js"
6+
},
7+
"./b": {
8+
"default": "./dist/b.js"
9+
}
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../b'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('this is b')

0 commit comments

Comments
 (0)