Skip to content

Commit e44dc7b

Browse files
committed
feat(babel): add includeChunks/excludeChunks to filter generated-code transforms by manual chunk name; docs + tests
1 parent c3dcdc0 commit e44dc7b

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

packages/babel/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,30 @@ export default {
200200

201201
The `include`, `exclude` and `extensions` options are ignored when using `getBabelOutputPlugin` and `createBabelOutputPluginFactory` will produce warnings, and there are a few more points to note that users should be aware of.
202202

203+
When transforming generated code, you can instead control which chunks are processed by matching their manual chunk names via the `includeChunks`/`excludeChunks` options. These patterns are matched against `chunk.name` as provided to Rollup's `renderChunk` hook and are especially useful to skip already-transpiled/minified vendor chunks:
204+
205+
```js
206+
// rollup.config.js
207+
import { getBabelOutputPlugin } from '@rollup/plugin-babel';
208+
209+
export default {
210+
input: 'main.js',
211+
manualChunks(id) {
212+
if (id.includes('big-library')) return 'vendor';
213+
},
214+
output: {
215+
format: 'es',
216+
plugins: [
217+
getBabelOutputPlugin({
218+
presets: ['@babel/preset-env'],
219+
// Do not transform the 'vendor' manual chunk
220+
excludeChunks: ['vendor']
221+
})
222+
]
223+
}
224+
};
225+
```
226+
203227
You can also run the plugin twice on the code, once when processing the input files to transpile special syntax to JavaScript and once on the output to transpile to a lower compatibility target:
204228

205229
```js

packages/babel/src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
209209
overrides
210210
);
211211

212+
// cache for chunk name filter (includeChunks/excludeChunks)
213+
let chunkNameFilter;
214+
212215
return {
213216
name: 'babel',
214217

@@ -234,6 +237,9 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
234237
/* eslint-disable no-unused-vars */
235238
const {
236239
allowAllFormats,
240+
// new: allow filtering of generated chunks by name
241+
includeChunks,
242+
excludeChunks,
237243
exclude,
238244
extensions,
239245
externalHelpers,
@@ -243,6 +249,16 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
243249
...babelOptions
244250
} = unpackOutputPluginOptions(pluginOptionsWithOverrides, outputOptions);
245251
/* eslint-enable no-unused-vars */
252+
// If includeChunks/excludeChunks are specified, filter by chunk.name
253+
if (includeChunks != null || excludeChunks != null) {
254+
if (!chunkNameFilter) {
255+
chunkNameFilter = createFilter(includeChunks, excludeChunks, { resolve: false });
256+
}
257+
if (!chunkNameFilter(chunk.name)) {
258+
// Skip transforming this chunk
259+
return null;
260+
}
261+
}
246262

247263
return transformCode(code, babelOptions, overrides, customOptions, this);
248264
}

packages/babel/test/as-output-plugin.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,34 @@ console.log(\`the answer is \${answer}\`);
378378
`
379379
);
380380
});
381+
382+
test('allows excluding manual chunks from output transform via `excludeChunks`', async (t) => {
383+
const bundle = await rollup({
384+
input: `${FIXTURES}chunks/main.js`
385+
});
386+
387+
const output = await getCode(
388+
bundle,
389+
{
390+
format: 'es',
391+
manualChunks(id) {
392+
// Place the dependency into a named manual chunk
393+
if (id.endsWith(`${nodePath.sep}chunks${nodePath.sep}dep.js`)) return 'vendor';
394+
return undefined;
395+
},
396+
plugins: [
397+
getBabelOutputPlugin({
398+
// Transform generated code but skip the 'vendor' manual chunk
399+
presets: ['@babel/env'],
400+
excludeChunks: ['vendor']
401+
})
402+
]
403+
},
404+
true
405+
);
406+
407+
const codes = output.map(({ code }) => code);
408+
// The vendor chunk should remain untransformed and contain the arrow function as-is
409+
// Debug output intentionally omitted
410+
t.true(codes.some((c) => c.includes('=> 42')));
411+
});

packages/babel/types/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ export interface RollupBabelOutputPluginOptions
4848
* @default false
4949
*/
5050
allowAllFormats?: boolean;
51+
/**
52+
* Limit transforming of generated code to specific manual chunks by name.
53+
* These patterns are matched against the `chunk.name` value in Rollup's `renderChunk` hook.
54+
*/
55+
includeChunks?: FilterPattern;
56+
/**
57+
* Exclude specific manual chunks by name from transforming the generated code.
58+
* These patterns are matched against the `chunk.name` value in Rollup's `renderChunk` hook.
59+
*/
60+
excludeChunks?: FilterPattern;
5161
}
5262

5363
export type RollupBabelCustomInputPluginOptions = (

0 commit comments

Comments
 (0)