Skip to content

Commit 7e1be64

Browse files
authored
feat(babel): pass rollup context as this context into override config function (#784)
* pass rollup context as this context into override config function * PR Review improvements
1 parent cbfd779 commit 7e1be64

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

packages/babel/src/transformCode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default async function transformCode(
1818

1919
let transformOptions = !overrides.config
2020
? config.options
21-
: await overrides.config.call(this, config, {
21+
: await overrides.config.call(ctx, config, {
2222
code: inputCode,
2323
customOptions
2424
});

packages/babel/test/as-input-plugin.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,17 @@ test('works with minified bundled helpers', async (t) => {
374374
});
375375

376376
test('supports customizing the loader', async (t) => {
377+
const expectedRollupContextKeys = ['getCombinedSourcemap', 'getModuleIds', 'emitFile', 'resolve'];
377378
const customBabelPlugin = createBabelInputPluginFactory(() => {
378379
return {
379380
config(cfg) {
381+
t.true(typeof this === 'object', 'override config this context is rollup context');
382+
expectedRollupContextKeys.forEach((key) => {
383+
t.true(
384+
Object.keys(this).includes(key),
385+
`override config this context is rollup context with key ${key}`
386+
);
387+
});
380388
return {
381389
...cfg.options,
382390
plugins: [
@@ -387,6 +395,13 @@ test('supports customizing the loader', async (t) => {
387395
};
388396
},
389397
result(result) {
398+
t.true(typeof this === 'object', 'override result this context is rollup context');
399+
expectedRollupContextKeys.forEach((key) => {
400+
t.true(
401+
Object.keys(this).includes(key),
402+
`override result this context is rollup context with key ${key}`
403+
);
404+
});
390405
return {
391406
...result,
392407
code: `${result.code}\n// Generated by some custom loader`

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,17 @@ export default getResult;
266266
});
267267

268268
test('supports customizing the loader', async (t) => {
269+
const expectedRollupContextKeys = ['getModuleIds', 'emitFile', 'resolve', 'parse'];
269270
const customBabelPlugin = createBabelOutputPluginFactory(() => {
270271
return {
271272
config(cfg) {
273+
t.true(typeof this === 'object', 'override config this context is rollup context');
274+
expectedRollupContextKeys.forEach((key) => {
275+
t.true(
276+
Object.keys(this).includes(key),
277+
`override config this context is rollup context with key ${key}`
278+
);
279+
});
272280
return Object.assign({}, cfg.options, {
273281
plugins: [
274282
...(cfg.options.plugins || []),
@@ -279,6 +287,13 @@ test('supports customizing the loader', async (t) => {
279287
});
280288
},
281289
result(result) {
290+
t.true(typeof this === 'object', 'override result this context is rollup context');
291+
expectedRollupContextKeys.forEach((key) => {
292+
t.true(
293+
Object.keys(this).includes(key),
294+
`override result this context is rollup context with key ${key}`
295+
);
296+
});
282297
return Object.assign({}, result, {
283298
code: `${result.code}\n// Generated by some custom loader`
284299
});

packages/babel/types/index.d.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Plugin } from 'rollup';
1+
import { Plugin, PluginContext, TransformPluginContext } from 'rollup';
22
import { FilterPattern } from '@rollup/pluginutils';
33
import * as babelCore from '@babel/core';
44

@@ -52,28 +52,45 @@ export type RollupBabelCustomOutputPluginOptions = (
5252
customOptions: Record<string, any>;
5353
pluginOptions: RollupBabelOutputPluginOptions;
5454
};
55-
export type RollupBabelCustomPluginConfig = (
55+
export interface RollupBabelCustomPluginConfigOptions {
56+
code: string;
57+
customOptions: Record<string, any>;
58+
}
59+
export interface RollupBabelCustomPluginResultOptions {
60+
code: string;
61+
customOptions: Record<string, any>;
62+
config: babelCore.PartialConfig;
63+
transformOptions: babelCore.TransformOptions;
64+
}
65+
export type RollupBabelCustomInputPluginConfig = (
66+
this: TransformPluginContext,
67+
cfg: babelCore.PartialConfig,
68+
options: RollupBabelCustomPluginConfigOptions
69+
) => babelCore.TransformOptions;
70+
export type RollupBabelCustomInputPluginResult = (
71+
this: TransformPluginContext,
72+
result: babelCore.BabelFileResult,
73+
options: RollupBabelCustomPluginResultOptions
74+
) => babelCore.BabelFileResult;
75+
export type RollupBabelCustomOutputPluginConfig = (
76+
this: PluginContext,
5677
cfg: babelCore.PartialConfig,
57-
options: { code: string; customOptions: Record<string, any> }
78+
options: RollupBabelCustomPluginConfigOptions
5879
) => babelCore.TransformOptions;
59-
export type RollupBabelCustomPluginResult = (
80+
export type RollupBabelCustomOutputPluginResult = (
81+
this: PluginContext,
6082
result: babelCore.BabelFileResult,
61-
options: {
62-
code: string;
63-
customOptions: Record<string, any>;
64-
config: babelCore.PartialConfig;
65-
transformOptions: babelCore.TransformOptions;
66-
}
83+
options: RollupBabelCustomPluginResultOptions
6784
) => babelCore.BabelFileResult;
6885
export interface RollupBabelCustomInputPlugin {
6986
options?: RollupBabelCustomInputPluginOptions;
70-
config?: RollupBabelCustomPluginConfig;
71-
result?: RollupBabelCustomPluginResult;
87+
config?: RollupBabelCustomInputPluginConfig;
88+
result?: RollupBabelCustomInputPluginResult;
7289
}
7390
export interface RollupBabelCustomOutputPlugin {
7491
options?: RollupBabelCustomOutputPluginOptions;
75-
config?: RollupBabelCustomPluginConfig;
76-
result?: RollupBabelCustomPluginResult;
92+
config?: RollupBabelCustomOutputPluginConfig;
93+
result?: RollupBabelCustomOutputPluginResult;
7794
}
7895
export type RollupBabelCustomInputPluginBuilder = (
7996
babel: typeof babelCore

0 commit comments

Comments
 (0)