Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure bundle fallback wont go to types #633

Merged
merged 6 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ export const runtimeExportConventionsFallback = new Map<string, string[]>([
// ESM only runtime
['workerd', ['import', 'default']],
['edge-light', ['import', 'default']],
['browser', ['import', 'default']],

// it could be CJS or ESM
// Fallback to default when unsure
['electron', ['default']],
['react-server', ['default']],
['react-native', ['default']],
['node', ['default']],
['deno', ['default']],
['bun', ['default']],
['development', ['default']],
['production', ['default']],
['browser', ['import', 'require', 'default']],
])
export const optimizeConventions = new Set(['development', 'production'])
export const specialExportConventions = new Set([
Expand Down
16 changes: 14 additions & 2 deletions src/plugins/alias-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ function findJsBundlePathCallback(
conditionNames: Set<string>
},
specialCondition: string,
) {
): boolean {
const hasBundle = bundlePath != null
const formatCond = format === 'cjs' ? 'require' : 'import'

const isTypesCondName = conditionNames.has('types')
const hasFormatCond =
conditionNames.has('import') || conditionNames.has('require')

// Check if the format condition is matched:
// if there's condition existed, check if the format condition is matched;
// if there's no condition, just return true, assuming format doesn't matter;
const isMatchedFormat = hasFormatCond ? conditionNames.has(formatCond) : true

const isMatchedConditionWithFormat =
Expand All @@ -50,7 +53,16 @@ function findJsBundlePathCallback(
if (!fallback) {
return false
} else {
return fallback.some((name) => conditionNames.has(name))
// Match its own condition first,
// e.g. when import utils.js in index.js
// In output: index.browser.js should match util.browser.js, fallback to util.js
// The last guard condition is to ensure bundle condition but not types file.
return (
isMatchedFormat &&
(conditionNames.has(specialCondition) ||
fallback.some((name) => conditionNames.has(name))) &&
!conditionNames.has('types')
)
}
} else {
return match
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/integration-test-template/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function Foo() {
return <p>Foo</p>
export function foo() {
return 'foo'
}
13 changes: 13 additions & 0 deletions test/integration/shared-module-special-condition/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "shared-module-special-condition",
"main": "./dist/index.js",
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"development": "./dist/index.development.js",
"production": "./dist/index.production.js",
"browser": "./dist/index.browser.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createJob, getFileContents } from '../../testing-utils'

describe('integration - shared-module-special-condition', () => {
const { distDir } = createJob({
directory: __dirname,
})
it('should work', async () => {
const contents = await getFileContents(distDir)
const files = [
'index.development.js',
'index.production.js',
'index.browser.js',
]
files.forEach((file) => {
expect(contents[file]).toContain('./_util.js')
expect(contents[file]).not.toContain('./_util.ts')
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sharedValue = 'shared-value-text'
5 changes: 5 additions & 0 deletions test/integration/shared-module-special-condition/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { sharedValue } from './_util'

export function index() {
return process.env.NODE_ENV + sharedValue
}
2 changes: 1 addition & 1 deletion test/testing-utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type FunctionCondition = (content: string) => Boolean

export async function getFileContents(dir: string, filePaths?: string[]) {
const results: Record<string, string> = {}
const files = filePaths || (await fsp.readdir(dir))
const files = filePaths || (await fsp.readdir(dir, { recursive: true }))
for (const file of files) {
const fullPath = path.resolve(dir, file)
const content = await fsp.readFile(fullPath, { encoding: 'utf-8' })
Expand Down
Loading