Skip to content

Commit d42ec45

Browse files
authored
fix(babel): exclude rolldown runtime module by default (#57)
1 parent 7504f5f commit d42ec45

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

packages/babel/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ Note that this option receives [the syntax supported by babel](https://babeljs.i
4646
### `exclude`
4747

4848
- **Type:** `string | RegExp | (string | RegExp)[]`
49-
- **Default:** `/[\/\\]node_modules[\/\\]/`
49+
- **Default:** `/[\/\\]node_modules[\/\\]|\0rolldown\/runtime\.js/`
5050

51-
Files matching the pattern will be skipped.
51+
Files matching the pattern will be skipped. The default also excludes Rolldown's runtime helper module (`\0rolldown/runtime.js`) so that Babel does not transform Rolldown's internal runtime code. If you override `exclude`, you are responsible for re-adding any of these entries you still want to skip.
5252

5353
Note that this option receives [the syntax supported by babel](https://babeljs.io/docs/options#matchpattern) instead of picomatch.
5454

packages/babel/src/index.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,49 @@ test('exclude allows transformation for non-matching files', async () => {
148148
expect(result.code).toContain('const result = true')
149149
})
150150

151+
test('default exclude skips rolldown virtual runtime module', async () => {
152+
const seenIds: string[] = []
153+
const trackingPlugin: babel.PluginItem = (): babel.PluginObject => ({
154+
visitor: {
155+
Program(_p, state) {
156+
seenIds.push(state.file.opts.filename ?? '<unknown>')
157+
},
158+
},
159+
})
160+
161+
// A CJS dep forces rolldown to emit its `\0rolldown/runtime.js` helper
162+
// region (for `__toESM` / `__commonJSMin`) into the chunk.
163+
const files: Record<string, string> = {
164+
'entry.js': `import dep from './dep.js'\nexport const result = dep.value`,
165+
'dep.js': `module.exports = { value: 42 }`,
166+
}
167+
const bundle = await rolldown({
168+
input: 'entry.js',
169+
plugins: [
170+
{
171+
name: 'virtual',
172+
resolveId(id) {
173+
if (id in files) return id
174+
if (id === './dep.js') return 'dep.js'
175+
},
176+
load(id) {
177+
if (id in files) return files[id]
178+
},
179+
},
180+
babelPlugin({ plugins: [trackingPlugin] }),
181+
],
182+
})
183+
const { output } = await bundle.generate()
184+
const chunk = output.find((o) => o.type === 'chunk')
185+
assert(chunk, 'expected a chunk in output')
186+
187+
// Sanity: rolldown must have actually emitted its runtime region, otherwise
188+
// the assertion below would be vacuous.
189+
expect(chunk.code).toContain('rolldown/runtime.js')
190+
191+
expect(seenIds).not.toContainEqual(expect.stringMatching(/rolldown[/\\]runtime\.js/))
192+
})
193+
151194
test('include activates config only for matching files', async () => {
152195
const result = await build('foo.js', 'export const result = foo', {
153196
include: [/\.js$/],

packages/babel/src/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export type ResolvedPluginOptions = PluginOptions &
8383
Required<Pick<PluginOptions, 'include' | 'exclude' | 'sourceMap'>>
8484

8585
export const DEFAULT_INCLUDE = [/\.(?:[jt]sx?|[cm][jt]s)(?:$|\?)/]
86-
export const DEFAULT_EXCLUDE = [/[/\\]node_modules[/\\]/]
86+
// oxlint-disable-next-line no-control-regex
87+
export const DEFAULT_EXCLUDE = [/[/\\]node_modules[/\\]|^\0rolldown\/runtime\.js$/]
8788

8889
export function resolveOptions(options: PluginOptions): ResolvedPluginOptions {
8990
return {

0 commit comments

Comments
 (0)