Skip to content

Commit c882cf0

Browse files
authored
fix(babel): use substring matching for string code filter (#7)
1 parent 8b10c9b commit c882cf0

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

packages/babel/src/rolldownPreset.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ describe('createBabelOptionsConverter', () => {
148148
expect(convert(makeCtx({ code: 'const x = 1' })).presets).toStrictEqual([])
149149
})
150150

151+
test('string include uses substring matching', () => {
152+
const convert = createBabelOptionsConverter(
153+
resolveOptions({
154+
presets: [makeRolldownPreset(presetA, { code: 'import React' })],
155+
}),
156+
)
157+
expect(convert(makeCtx({ code: 'import React from "react"' })).presets).toStrictEqual([
158+
presetA,
159+
])
160+
expect(convert(makeCtx({ code: 'const x = 1' })).presets).toStrictEqual([])
161+
})
162+
151163
test('object with include and exclude', () => {
152164
const convert = createBabelOptionsConverter(
153165
resolveOptions({

packages/babel/src/rolldownPreset.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,29 @@ export type PresetConversionContext = {
3333
type StringOrRegExp = string | RegExp
3434
type Matcher = (value: string) => boolean
3535

36-
function compilePattern(pattern: StringOrRegExp): Matcher {
36+
function compilePattern(pattern: StringOrRegExp, isPath: boolean): Matcher {
3737
if (pattern instanceof RegExp) {
3838
return (value) => pattern.test(value)
3939
}
40-
return picomatch(pattern)
40+
if (isPath) {
41+
return picomatch(pattern)
42+
}
43+
return (value) => value.includes(pattern)
4144
}
4245

43-
function compilePatterns(patterns: StringOrRegExp[]): Matcher {
44-
const matchers = patterns.map(compilePattern)
46+
function compilePatterns(patterns: StringOrRegExp[], isPath: boolean): Matcher {
47+
const matchers = patterns.map((p) => compilePattern(p, isPath))
4548
return (value) => matchers.some((m) => m(value))
4649
}
4750

4851
/**
4952
* Pre-compile a GeneralHookFilter into a single matcher function.
5053
* Returns undefined when the filter matches everything.
5154
*/
52-
function compileGeneralHookFilter(filter: GeneralHookFilter | undefined): Matcher | undefined {
55+
function compileGeneralHookFilter(
56+
filter: GeneralHookFilter | undefined,
57+
isPath: boolean,
58+
): Matcher | undefined {
5359
if (filter == null) return undefined
5460

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

67-
const includeMatcher = include ? compilePatterns(include) : undefined
68-
const excludeMatcher = exclude ? compilePatterns(exclude) : undefined
73+
const includeMatcher = include ? compilePatterns(include, isPath) : undefined
74+
const excludeMatcher = exclude ? compilePatterns(exclude, isPath) : undefined
6975

7076
if (includeMatcher && excludeMatcher) {
7177
return (value) => !excludeMatcher(value) && includeMatcher(value)
@@ -95,9 +101,9 @@ export function compilePresetFilter(
95101
filter: RolldownBabelPreset['rolldown']['filter'],
96102
): CompiledPresetFilter | undefined {
97103
if (!filter) return undefined
98-
const matchId = compileGeneralHookFilter(filter.id)
104+
const matchId = compileGeneralHookFilter(filter.id, true)
99105
const matchModuleType = compileModuleTypeFilter(filter.moduleType)
100-
const matchCode = compileGeneralHookFilter(filter.code)
106+
const matchCode = compileGeneralHookFilter(filter.code, false)
101107
if (!matchId && !matchModuleType && !matchCode) return undefined
102108
return (ctx) =>
103109
(!matchId || matchId(ctx.id)) &&

0 commit comments

Comments
 (0)