Skip to content

Commit

Permalink
Fix condition matching (#658)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Feb 15, 2025
1 parent dd38370 commit 53d1011
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export async function collectEntriesFromParsedExports(
}

if (!entries[entryExportPath]) {
// Create a new entry
entries[entryExportPath] = {
source: sourceFile,
name: normalizedExportPath,
Expand Down Expand Up @@ -408,7 +409,6 @@ export async function collectSourceEntriesFromExportPaths(
const exportPath = sourceFilenameToExportFullPath(file)
const isEsmPkg = isESModulePackage(pkg.type)

// const specialItems: [string, string][] = []
const specialExportType = getSpecialExportTypeFromSourcePath(file)
const normalizedExportPath = stripSpecialCondition(exportPath)
const isSpecialExport = specialExportType !== 'default'
Expand Down
16 changes: 3 additions & 13 deletions src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {
normalizePath,
} from './utils'
import { baseNameWithoutExtension } from './util/file-path'
import {
BINARY_TAG,
dtsExtensionsMap,
runtimeExportConventions,
} from './constants'
import { BINARY_TAG, dtsExtensionsMap } from './constants'
import { OutputOptions } from 'rollup'

export function getPackageTypings(pkg: PackageMetadata) {
Expand Down Expand Up @@ -75,12 +71,7 @@ function collectExportPath(
exportValue,
exportCondition,
]
addToExportDistMap(
exportToDist,
currentPath,
[outputConditionPair],
runtimeExportConventions.has(exportType) ? exportType : undefined,
)
addToExportDistMap(exportToDist, currentPath, [outputConditionPair])
} else {
exportInfo.push([exportValue, exportCondition])
}
Expand Down Expand Up @@ -124,10 +115,9 @@ function addToExportDistMap(
exportToDist: ParsedExportsInfo,
exportPath: string,
outputConditionPairs: [string, string][],
specialExportType?: string,
) {
const fullPath = mapExportFullPath(exportPath)
// + (specialExportType ? '.' + specialExportType : '')

const existingExportInfo = exportToDist.get(fullPath)
if (!existingExportInfo) {
exportToDist.set(fullPath, outputConditionPairs)
Expand Down
43 changes: 18 additions & 25 deletions src/plugins/alias-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OutputOptions, Plugin } from 'rollup'
import { Entries } from '../types'
import { Entries, ParsedExportCondition } from '../types'
import { posix } from 'path'
import { posixRelativify } from '../lib/format'
import { getSpecialExportTypeFromConditionNames } from '../entries'
Expand Down Expand Up @@ -55,8 +55,10 @@ function findJsBundlePathCallback(
const isMatchedFormat = hasFormatCond ? conditionNames.has(formatCond) : true

const isMatchedConditionWithFormat =
conditionNames.has(specialCondition) ||
(!conditionNames.has('default') && hasNoSpecialCondition(conditionNames))
// Has matched special condition
(specialCondition !== 'default' && conditionNames.has(specialCondition)) ||
// Match normal condition
hasNoSpecialCondition(conditionNames)

const match =
isMatchedConditionWithFormat &&
Expand All @@ -77,7 +79,8 @@ function findJsBundlePathCallback(
return (
isMatchedFormat &&
!isTypesCondName &&
(conditionNames.has(specialCondition) ||
((specialCondition !== 'default' &&
conditionNames.has(specialCondition)) ||
fallback.some((name) => conditionNames.has(name)))
)
}
Expand Down Expand Up @@ -108,7 +111,7 @@ function findTypesFileCallback({
// Alias entry key to dist bundle path
export function aliasEntries({
entry: sourceFilePath,
conditionNames,
exportCondition,
entries,
isESMPkg,
format,
Expand All @@ -119,16 +122,22 @@ export function aliasEntries({
entries: Entries
format: OutputOptions['format']
isESMPkg: boolean
conditionNames: Set<string>
exportCondition: ParsedExportCondition
dts: boolean
cwd: string
}): Plugin {
const currentConditionNames = new Set(
Object.keys(exportCondition.export)[0].split('.'),
)

// <imported source file path>: <relative path to source's bundle>
const sourceToRelativeBundleMap = new Map<string, string>()
const specialCondition =
getSpecialExportTypeFromConditionNames(conditionNames)
let specialCondition = getSpecialExportTypeFromConditionNames(
currentConditionNames,
)
for (const [, exportCondition] of Object.entries(entries)) {
const exportDistMaps = exportCondition.export

const exportMapEntries = Object.entries(exportDistMaps).map(
([composedKey, bundlePath]) => {
const conditionNames = new Set(composedKey.split('.'))
Expand Down Expand Up @@ -160,23 +169,7 @@ export function aliasEntries({
})?.bundlePath
}
} else {
const orderedExportConditions = exportMapEntries.sort((condA, condB) => {
const bHasSpecialCond = condB.conditionNames.has(specialCondition)
const aHasSpecialCond = condA.conditionNames.has(specialCondition)
if (bHasSpecialCond || aHasSpecialCond) {
const specialCompare =
Number(bHasSpecialCond) - Number(aHasSpecialCond)
if (specialCompare !== 0) {
return specialCompare
}
}

// Always put default condition at the end.
return (
Number(condA.isDefaultCondition) - Number(condB.isDefaultCondition)
)
})
matchedBundlePath = orderedExportConditions.find((item) => {
matchedBundlePath = exportMapEntries.find((item) => {
return findJsBundlePathCallback(item, specialCondition, isESMPkg)
})?.bundlePath
}
Expand Down
3 changes: 1 addition & 2 deletions src/rollup/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,12 @@ export async function buildInputConfig(
: 'esm'
: bundleConfig.format

const currentConditionNames = Object.keys(exportCondition.export)[0]
const aliasPlugin = aliasEntries({
entry,
entries,
format: aliasFormat,
isESMPkg: isESModulePackage(pkg.type),
conditionNames: new Set(currentConditionNames.split('.')),
exportCondition,
dts,
cwd,
})
Expand Down
10 changes: 5 additions & 5 deletions test/integration/exports-order/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ describe('integration exports-order', () => {
export { foo };
"
`)
// FIXME: the cjs alias is wrong

expect(contents['index.cjs']).toMatchInlineSnapshot(`
"var a_edgeLight_js = require('./a.edge-light.js');
"var a_cjs = require('./a.cjs');
Object.keys(a_edgeLight_js).forEach(function (k) {
Object.keys(a_cjs).forEach(function (k) {
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
enumerable: true,
get: function () { return a_edgeLight_js[k]; }
get: function () { return a_cjs[k]; }
});
});
"
Expand All @@ -65,7 +65,7 @@ describe('integration exports-order', () => {
"
`)
expect(contents['index.js']).toMatchInlineSnapshot(`
"export * from './a.edge-light.js';
"export * from './a.js';
"
`)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { describe, it } from 'vitest'
import { assertFilesContent, createJob } from '../../testing-utils'

describe('integration - shared-module-with-suffix', () => {
// TODO: this is not available as browser cannot be the fallback condition
// Until later we can use chunk split to create shared entry then it will be easier.
describe.skip('integration - shared-module-with-suffix', () => {
const { distDir } = createJob({
directory: __dirname,
})
Expand Down

0 comments on commit 53d1011

Please sign in to comment.