Skip to content
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
12 changes: 12 additions & 0 deletions packages/babel/src/rolldownPreset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ describe('createBabelOptionsConverter', () => {
expect(convert(makeCtx({ code: 'const x = 1' })).presets).toStrictEqual([])
})

test('string include uses substring matching', () => {
const convert = createBabelOptionsConverter(
resolveOptions({
presets: [makeRolldownPreset(presetA, { code: 'import React' })],
}),
)
expect(convert(makeCtx({ code: 'import React from "react"' })).presets).toStrictEqual([
presetA,
])
expect(convert(makeCtx({ code: 'const x = 1' })).presets).toStrictEqual([])
})

test('object with include and exclude', () => {
const convert = createBabelOptionsConverter(
resolveOptions({
Expand Down
24 changes: 15 additions & 9 deletions packages/babel/src/rolldownPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,29 @@ export type PresetConversionContext = {
type StringOrRegExp = string | RegExp
type Matcher = (value: string) => boolean

function compilePattern(pattern: StringOrRegExp): Matcher {
function compilePattern(pattern: StringOrRegExp, isPath: boolean): Matcher {
if (pattern instanceof RegExp) {
return (value) => pattern.test(value)
}
return picomatch(pattern)
if (isPath) {
return picomatch(pattern)
}
return (value) => value.includes(pattern)
}

function compilePatterns(patterns: StringOrRegExp[]): Matcher {
const matchers = patterns.map(compilePattern)
function compilePatterns(patterns: StringOrRegExp[], isPath: boolean): Matcher {
const matchers = patterns.map((p) => compilePattern(p, isPath))
return (value) => matchers.some((m) => m(value))
}

/**
* Pre-compile a GeneralHookFilter into a single matcher function.
* Returns undefined when the filter matches everything.
*/
function compileGeneralHookFilter(filter: GeneralHookFilter | undefined): Matcher | undefined {
function compileGeneralHookFilter(
filter: GeneralHookFilter | undefined,
isPath: boolean,
): Matcher | undefined {
if (filter == null) return undefined

let include: StringOrRegExp[] | undefined
Expand All @@ -64,8 +70,8 @@ function compileGeneralHookFilter(filter: GeneralHookFilter | undefined): Matche
exclude = filter.exclude != null ? arrayify(filter.exclude) : undefined
}

const includeMatcher = include ? compilePatterns(include) : undefined
const excludeMatcher = exclude ? compilePatterns(exclude) : undefined
const includeMatcher = include ? compilePatterns(include, isPath) : undefined
const excludeMatcher = exclude ? compilePatterns(exclude, isPath) : undefined

if (includeMatcher && excludeMatcher) {
return (value) => !excludeMatcher(value) && includeMatcher(value)
Expand Down Expand Up @@ -95,9 +101,9 @@ export function compilePresetFilter(
filter: RolldownBabelPreset['rolldown']['filter'],
): CompiledPresetFilter | undefined {
if (!filter) return undefined
const matchId = compileGeneralHookFilter(filter.id)
const matchId = compileGeneralHookFilter(filter.id, true)
const matchModuleType = compileModuleTypeFilter(filter.moduleType)
const matchCode = compileGeneralHookFilter(filter.code)
const matchCode = compileGeneralHookFilter(filter.code, false)
if (!matchId && !matchModuleType && !matchCode) return undefined
return (ctx) =>
(!matchId || matchId(ctx.id)) &&
Expand Down