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
3 changes: 2 additions & 1 deletion packages/babel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ The plugin automatically configures Babel's parser for `.jsx`, `.ts`, and `.tsx`
### `include`

- **Type:** `string | RegExp | (string | RegExp)[]`
- **Default:** `/\.(?:[jt]sx?|[cm][jt]s)(?:$|\?)/`

If specified, only files matching the pattern will be processed.
Only files matching the pattern will be processed.

### `exclude`

Expand Down
52 changes: 25 additions & 27 deletions packages/babel/src/filter.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { describe, expect, test } from 'vitest'
import { calculatePluginFilters } from './filter.ts'
import { resolveOptions } from './options.ts'
import { resolveOptions, DEFAULT_INCLUDE, DEFAULT_EXCLUDE } from './options.ts'
import type { RolldownBabelPreset } from './rolldownPreset.ts'

const DEFAULT_EXCLUDE = [/[/\\]node_modules[/\\]/]

function makeRolldownPreset(
filter: RolldownBabelPreset['rolldown']['filter'],
): RolldownBabelPreset {
Expand All @@ -16,10 +14,10 @@ function makeRolldownPreset(

describe('calculatePluginFilters', () => {
describe('baseFilter basics', () => {
test('default options produce exclude-only id filter', () => {
test('default options produce default id filter', () => {
const { transformFilter } = calculatePluginFilters(resolveOptions({}))
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})

Expand All @@ -41,7 +39,7 @@ describe('calculatePluginFilters', () => {
const customExclude = [/vendor/]
const { transformFilter } = calculatePluginFilters(resolveOptions({ exclude: customExclude }))
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: customExclude },
id: { include: DEFAULT_INCLUDE, exclude: customExclude },
})
})

Expand All @@ -66,14 +64,14 @@ describe('calculatePluginFilters', () => {
resolveOptions({ exclude: [() => true, /vendor/] }),
)
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: undefined },
id: { include: DEFAULT_INCLUDE, exclude: undefined },
})
})

test('exclude with only function entries means exclude nothing', () => {
const { transformFilter } = calculatePluginFilters(resolveOptions({ exclude: [() => true] }))
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: undefined },
id: { include: DEFAULT_INCLUDE, exclude: undefined },
})
})
})
Expand All @@ -88,7 +86,7 @@ describe('calculatePluginFilters', () => {
)
// Preset filter is ignored; only baseFilter is returned
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})

Expand All @@ -110,14 +108,14 @@ describe('calculatePluginFilters', () => {
test('returns baseFilter when presets is undefined', () => {
const { transformFilter } = calculatePluginFilters(resolveOptions({ presets: undefined }))
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})

test('returns baseFilter when presets is empty', () => {
const { transformFilter } = calculatePluginFilters(resolveOptions({ presets: [] }))
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})
})
Expand All @@ -128,7 +126,7 @@ describe('calculatePluginFilters', () => {
resolveOptions({ presets: [() => ({ plugins: [] })] }),
)
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})

Expand All @@ -139,7 +137,7 @@ describe('calculatePluginFilters', () => {
}),
)
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})

Expand All @@ -150,7 +148,7 @@ describe('calculatePluginFilters', () => {
}
const { transformFilter } = calculatePluginFilters(resolveOptions({ presets: [preset] }))
expect(transformFilter).toStrictEqual({
id: { include: undefined, exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})
})
Expand All @@ -163,7 +161,7 @@ describe('calculatePluginFilters', () => {
}),
)
expect(transformFilter).toStrictEqual({
id: { include: [/\.tsx$/], exclude: DEFAULT_EXCLUDE },
id: { include: DEFAULT_INCLUDE, exclude: DEFAULT_EXCLUDE },
})
})

Expand All @@ -174,7 +172,7 @@ describe('calculatePluginFilters', () => {
}),
)
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/, /\.jsx$/],
include: DEFAULT_INCLUDE,
exclude: DEFAULT_EXCLUDE,
})
})
Expand All @@ -186,7 +184,7 @@ describe('calculatePluginFilters', () => {
}),
)
expect(transformFilter.id).toStrictEqual({
include: ['**/*.tsx'],
include: DEFAULT_INCLUDE,
exclude: DEFAULT_EXCLUDE,
})
})
Expand All @@ -198,7 +196,7 @@ describe('calculatePluginFilters', () => {
}),
)
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/, /\.jsx$/],
include: DEFAULT_INCLUDE,
exclude: DEFAULT_EXCLUDE,
})
})
Expand All @@ -215,7 +213,7 @@ describe('calculatePluginFilters', () => {
)
// Single preset: its exclude is the intersection (just itself), combined with user exclude
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/],
include: DEFAULT_INCLUDE,
exclude: [...DEFAULT_EXCLUDE, /test/],
})
})
Expand All @@ -231,7 +229,7 @@ describe('calculatePluginFilters', () => {
)
// First preset has no id → id matches everything → no preset id include
expect(transformFilter.id).toStrictEqual({
include: undefined,
include: DEFAULT_INCLUDE,
exclude: DEFAULT_EXCLUDE,
})
})
Expand All @@ -248,7 +246,7 @@ describe('calculatePluginFilters', () => {
)
// No include → match-all, but exclude is kept (intersection of one preset)
expect(transformFilter.id).toStrictEqual({
include: undefined,
include: DEFAULT_INCLUDE,
exclude: [...DEFAULT_EXCLUDE, /test/],
})
})
Expand Down Expand Up @@ -366,14 +364,14 @@ describe('calculatePluginFilters', () => {
})
})

test('preset id includes used when user has no include', () => {
test('default include takes priority over preset id includes', () => {
const { transformFilter } = calculatePluginFilters(
resolveOptions({
presets: [makeRolldownPreset({ id: /\.tsx$/ })],
}),
)
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/],
include: DEFAULT_INCLUDE,
exclude: DEFAULT_EXCLUDE,
})
})
Expand All @@ -387,7 +385,7 @@ describe('calculatePluginFilters', () => {
}),
)
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/],
include: DEFAULT_INCLUDE,
exclude: customExclude,
})
})
Expand All @@ -407,7 +405,7 @@ describe('calculatePluginFilters', () => {
)
// No shared excludes across presets → only user exclude remains
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/, /\.jsx$/],
include: DEFAULT_INCLUDE,
exclude: DEFAULT_EXCLUDE,
})
})
Expand All @@ -427,7 +425,7 @@ describe('calculatePluginFilters', () => {
)
// /test/ is in both presets' excludes → kept; /vendor/ and /dist/ are not shared → dropped
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/, /\.jsx$/],
include: DEFAULT_INCLUDE,
exclude: [...DEFAULT_EXCLUDE, /test/],
})
})
Expand All @@ -447,7 +445,7 @@ describe('calculatePluginFilters', () => {
)
// Second preset has no excludes → intersection is empty → only user exclude
expect(transformFilter.id).toStrictEqual({
include: [/\.tsx$/, /\.jsx$/],
include: DEFAULT_INCLUDE,
exclude: DEFAULT_EXCLUDE,
})
})
Expand Down
15 changes: 13 additions & 2 deletions packages/babel/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ export interface InnerTransformOptions extends Pick<
export interface PluginOptions extends Omit<InnerTransformOptions, 'include' | 'exclude'> {
/**
* If specified, only files matching the pattern will be processed by babel.
* @default `/\.(?:[jt]sx?|[cm][jt]s)(?:$|\?)/`
*
* Note that this option receives the syntax supported by babel instead of picomatch.
* @see https://babeljs.io/docs/options#matchpattern
*/
include?: InnerTransformOptions['include']

/**
* If any of patterns match, babel will not process the file.
* @default `/[\/\\]node_modules[\/\\]/`
*
* Note that this option receives the syntax supported by babel instead of picomatch.
* @see https://babeljs.io/docs/options#matchpattern
*/
exclude?: InnerTransformOptions['exclude']

Expand All @@ -62,12 +69,16 @@ export interface PluginOptions extends Omit<InnerTransformOptions, 'include' | '
}

export type ResolvedPluginOptions = PluginOptions &
Required<Pick<PluginOptions, 'exclude' | 'sourceMap'>>
Required<Pick<PluginOptions, 'include' | 'exclude' | 'sourceMap'>>

export const DEFAULT_INCLUDE = [/\.(?:[jt]sx?|[cm][jt]s)(?:$|\?)/]
export const DEFAULT_EXCLUDE = [/[/\\]node_modules[/\\]/]

export function resolveOptions(options: PluginOptions): ResolvedPluginOptions {
return {
...options,
exclude: options.exclude ?? [/[/\\]node_modules[/\\]/],
include: options.include ?? DEFAULT_INCLUDE,
exclude: options.exclude ?? DEFAULT_EXCLUDE,
sourceMap: options.sourceMap ?? true,
}
}
Expand Down
Loading