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
12 changes: 10 additions & 2 deletions .github/workflows/node-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:

jobs:
build:
runs-on: windows-2019
runs-on: windows-latest

strategy:
matrix:
Expand All @@ -30,7 +30,15 @@ jobs:
fetch-depth: 2

- name: Update Master
run: git pull --force --no-tags origin master:master
shell: bash
run: |
# Ensure the remote-tracking ref exists and is up-to-date without invoking an implicit merge
set -euo pipefail
# Ensure the remote-tracking ref and local branch ref are up-to-date without invoking an implicit merge
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
git fetch --no-tags --force origin \
"+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}" \
"+refs/heads/${DEFAULT_BRANCH}:refs/heads/${DEFAULT_BRANCH}"

- name: Setup Node
uses: actions/setup-node@v3
Expand Down
15 changes: 10 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ jobs:

- name: Update Master
run: |
git pull --force --no-tags origin master:master
git checkout master
set -euo pipefail
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
# Avoid implicit merges; update the remote-tracking ref explicitly
git fetch --no-tags --force origin "+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}"
git checkout "${DEFAULT_BRANCH}"
git fetch --tags

- name: Setup Node
Expand Down Expand Up @@ -86,10 +89,12 @@ jobs:

- name: Push Release and Cleanup
run: |
set -euo pipefail
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
pnpm lint:docs
git checkout .npmrc
git add . && git commit --amend --no-edit
git pull origin master --no-edit
git pull origin "${DEFAULT_BRANCH}" --no-edit
git rebase
git push origin HEAD:master
git push origin HEAD:master --tags
git push origin HEAD:"${DEFAULT_BRANCH}"
git push origin HEAD:"${DEFAULT_BRANCH}" --tags
9 changes: 8 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ jobs:
fetch-depth: 2

- name: Update Master
run: git pull --force --no-tags origin master:master
run: |
# Ensure the remote-tracking ref exists and is up-to-date without invoking an implicit merge
set -euo pipefail
# Ensure the remote-tracking ref and local branch ref are up-to-date without invoking an implicit merge
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
git fetch --no-tags --force origin \
"+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}" \
"+refs/heads/${DEFAULT_BRANCH}:refs/heads/${DEFAULT_BRANCH}"

- name: Setup Node
uses: actions/setup-node@v3
Expand Down
24 changes: 24 additions & 0 deletions packages/babel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,30 @@ export default {

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.

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:

```js
// rollup.config.js
import { getBabelOutputPlugin } from '@rollup/plugin-babel';

export default {
input: 'main.js',
manualChunks(id) {
if (id.includes('big-library')) return 'vendor';
},
output: {
format: 'es',
plugins: [
getBabelOutputPlugin({
presets: ['@babel/preset-env'],
// Do not transform the 'vendor' manual chunk
excludeChunks: ['vendor']
})
]
}
};
```

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:

```js
Expand Down
15 changes: 15 additions & 0 deletions packages/babel/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
overrides
);

// cache for chunk name filter (includeChunks/excludeChunks)
let chunkNameFilter;

return {
name: 'babel',

Expand All @@ -234,6 +237,8 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
/* eslint-disable no-unused-vars */
const {
allowAllFormats,
includeChunks,
excludeChunks,
exclude,
extensions,
externalHelpers,
Expand All @@ -243,6 +248,16 @@ function createBabelOutputPluginFactory(customCallback = returnObject) {
...babelOptions
} = unpackOutputPluginOptions(pluginOptionsWithOverrides, outputOptions);
/* eslint-enable no-unused-vars */
// If includeChunks/excludeChunks are specified, filter by chunk.name
if (includeChunks != null || excludeChunks != null) {
if (!chunkNameFilter) {
chunkNameFilter = createFilter(includeChunks, excludeChunks, { resolve: false });
}
if (!chunkNameFilter(chunk.name)) {
// Skip transforming this chunk
return null;
}
}

return transformCode(code, babelOptions, overrides, customOptions, this);
}
Expand Down
31 changes: 31 additions & 0 deletions packages/babel/test/as-output-plugin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,34 @@ console.log(\`the answer is \${answer}\`);
`
);
});

test('allows excluding manual chunks from output transform via `excludeChunks`', async (t) => {
const bundle = await rollup({
input: `${FIXTURES}chunks/main.js`
});

const output = await getCode(
bundle,
{
format: 'es',
// eslint-disable-next-line consistent-return
manualChunks(id) {
// Place the dependency into a named manual chunk
if (id.endsWith(`${nodePath.sep}chunks${nodePath.sep}dep.js`)) return 'vendor';
},
plugins: [
getBabelOutputPlugin({
// Transform generated code but skip the 'vendor' manual chunk
presets: ['@babel/env'],
excludeChunks: ['vendor']
})
]
},
true
);

const codes = output.map(({ code }) => code);
// The vendor chunk should remain untransformed and contain the arrow function as-is
// Debug output intentionally omitted
t.true(codes.some((c) => c.includes('=> 42')));
});
Comment on lines +407 to +411
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test only asserts that some output contains => 42. It doesn’t verify that other chunks were actually transformed, so it could pass even if the plugin didn’t transform any chunks. Strengthening the assertions will make the test robust and prevent false positives.

Suggestion

Strengthen the assertions to also verify that at least one non-vendor chunk was transformed (no arrow function remains). If output items include fileName, you can also target the vendor chunk explicitly:

const vendor = output.find((o) => /vendor/.test(o.fileName));
t.truthy(vendor);
t.true(vendor.code.includes('=> 42'));

const nonVendor = output.find((o) => !/vendor/.test(o.fileName));
t.truthy(nonVendor);
t.false(nonVendor.code.includes('=> 42'));

If fileName isn’t available in getCode’s return value, at minimum ensure that another chunk was transformed:

// Ensure at least one other chunk had arrow functions transformed
t.true(codes.some((c) => !c.includes('=> 42')));

Reply with "@CharlieHelps yes please" if you want me to update the test accordingly.

10 changes: 10 additions & 0 deletions packages/babel/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ export interface RollupBabelOutputPluginOptions
* @default false
*/
allowAllFormats?: boolean;
/**
* Limit transforming of generated code to specific manual chunks by name.
* These patterns are matched against the `chunk.name` value in Rollup's `renderChunk` hook.
*/
includeChunks?: FilterPattern;
/**
* Exclude specific manual chunks by name from transforming the generated code.
* These patterns are matched against the `chunk.name` value in Rollup's `renderChunk` hook.
*/
excludeChunks?: FilterPattern;
}

export type RollupBabelCustomInputPluginOptions = (
Expand Down
Loading