Skip to content

Commit dc3d0e7

Browse files
feat(babel): allow excluding manual chunks when transforming generated code (#1906)
* feat(babel): add includeChunks/excludeChunks to filter generated-code transforms by manual chunk name; docs + tests * chore(babel): fix lint in as-output-plugin test (avoid explicit undefined; disable consistent-return for manualChunks) * ci(workflows): avoid 'git pull' divergence error; use explicit fetch refspecs and default_branch; update Windows to windows-latest * chore(babel): remove superfluous "new:" inline comment in options destructure --------- Co-authored-by: CharlieHelps <charlie@charlielabs.ai>
1 parent 764910a commit dc3d0e7

File tree

7 files changed

+108
-8
lines changed

7 files changed

+108
-8
lines changed

.github/workflows/node-windows.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ on:
1313

1414
jobs:
1515
build:
16-
runs-on: windows-2019
16+
runs-on: windows-latest
1717

1818
strategy:
1919
matrix:
@@ -30,7 +30,15 @@ jobs:
3030
fetch-depth: 2
3131

3232
- name: Update Master
33-
run: git pull --force --no-tags origin master:master
33+
shell: bash
34+
run: |
35+
# Ensure the remote-tracking ref exists and is up-to-date without invoking an implicit merge
36+
set -euo pipefail
37+
# Ensure the remote-tracking ref and local branch ref are up-to-date without invoking an implicit merge
38+
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
39+
git fetch --no-tags --force origin \
40+
"+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}" \
41+
"+refs/heads/${DEFAULT_BRANCH}:refs/heads/${DEFAULT_BRANCH}"
3442
3543
- name: Setup Node
3644
uses: actions/setup-node@v3

.github/workflows/release.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ jobs:
2525

2626
- name: Update Master
2727
run: |
28-
git pull --force --no-tags origin master:master
29-
git checkout master
28+
set -euo pipefail
29+
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
30+
# Avoid implicit merges; update the remote-tracking ref explicitly
31+
git fetch --no-tags --force origin "+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}"
32+
git checkout "${DEFAULT_BRANCH}"
3033
git fetch --tags
3134
3235
- name: Setup Node
@@ -86,10 +89,12 @@ jobs:
8689

8790
- name: Push Release and Cleanup
8891
run: |
92+
set -euo pipefail
93+
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
8994
pnpm lint:docs
9095
git checkout .npmrc
9196
git add . && git commit --amend --no-edit
92-
git pull origin master --no-edit
97+
git pull origin "${DEFAULT_BRANCH}" --no-edit
9398
git rebase
94-
git push origin HEAD:master
95-
git push origin HEAD:master --tags
99+
git push origin HEAD:"${DEFAULT_BRANCH}"
100+
git push origin HEAD:"${DEFAULT_BRANCH}" --tags

.github/workflows/validate.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ jobs:
2828
fetch-depth: 2
2929

3030
- name: Update Master
31-
run: git pull --force --no-tags origin master:master
31+
run: |
32+
# Ensure the remote-tracking ref exists and is up-to-date without invoking an implicit merge
33+
set -euo pipefail
34+
# Ensure the remote-tracking ref and local branch ref are up-to-date without invoking an implicit merge
35+
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
36+
git fetch --no-tags --force origin \
37+
"+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}" \
38+
"+refs/heads/${DEFAULT_BRANCH}:refs/heads/${DEFAULT_BRANCH}"
3239
3340
- name: Setup Node
3441
uses: actions/setup-node@v3

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: 15 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,8 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
234237
/* eslint-disable no-unused-vars */
235238
const {
236239
allowAllFormats,
240+
includeChunks,
241+
excludeChunks,
237242
exclude,
238243
extensions,
239244
externalHelpers,
@@ -243,6 +248,16 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
243248
...babelOptions
244249
} = unpackOutputPluginOptions(pluginOptionsWithOverrides, outputOptions);
245250
/* eslint-enable no-unused-vars */
251+
// If includeChunks/excludeChunks are specified, filter by chunk.name
252+
if (includeChunks != null || excludeChunks != null) {
253+
if (!chunkNameFilter) {
254+
chunkNameFilter = createFilter(includeChunks, excludeChunks, { resolve: false });
255+
}
256+
if (!chunkNameFilter(chunk.name)) {
257+
// Skip transforming this chunk
258+
return null;
259+
}
260+
}
246261

247262
return transformCode(code, babelOptions, overrides, customOptions, this);
248263
}

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+
// eslint-disable-next-line consistent-return
392+
manualChunks(id) {
393+
// Place the dependency into a named manual chunk
394+
if (id.endsWith(`${nodePath.sep}chunks${nodePath.sep}dep.js`)) return 'vendor';
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)