Skip to content

Commit

Permalink
feat(babel): pass rollup context as this context into override config…
Browse files Browse the repository at this point in the history
… function (#784)

* pass rollup context as this context into override config function

* PR Review improvements
  • Loading branch information
KingSora authored Feb 6, 2021
1 parent cbfd779 commit 7e1be64
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/babel/src/transformCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function transformCode(

let transformOptions = !overrides.config
? config.options
: await overrides.config.call(this, config, {
: await overrides.config.call(ctx, config, {
code: inputCode,
customOptions
});
Expand Down
15 changes: 15 additions & 0 deletions packages/babel/test/as-input-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,17 @@ test('works with minified bundled helpers', async (t) => {
});

test('supports customizing the loader', async (t) => {
const expectedRollupContextKeys = ['getCombinedSourcemap', 'getModuleIds', 'emitFile', 'resolve'];
const customBabelPlugin = createBabelInputPluginFactory(() => {
return {
config(cfg) {
t.true(typeof this === 'object', 'override config this context is rollup context');
expectedRollupContextKeys.forEach((key) => {
t.true(
Object.keys(this).includes(key),
`override config this context is rollup context with key ${key}`
);
});
return {
...cfg.options,
plugins: [
Expand All @@ -387,6 +395,13 @@ test('supports customizing the loader', async (t) => {
};
},
result(result) {
t.true(typeof this === 'object', 'override result this context is rollup context');
expectedRollupContextKeys.forEach((key) => {
t.true(
Object.keys(this).includes(key),
`override result this context is rollup context with key ${key}`
);
});
return {
...result,
code: `${result.code}\n// Generated by some custom loader`
Expand Down
15 changes: 15 additions & 0 deletions packages/babel/test/as-output-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,17 @@ export default getResult;
});

test('supports customizing the loader', async (t) => {
const expectedRollupContextKeys = ['getModuleIds', 'emitFile', 'resolve', 'parse'];
const customBabelPlugin = createBabelOutputPluginFactory(() => {
return {
config(cfg) {
t.true(typeof this === 'object', 'override config this context is rollup context');
expectedRollupContextKeys.forEach((key) => {
t.true(
Object.keys(this).includes(key),
`override config this context is rollup context with key ${key}`
);
});
return Object.assign({}, cfg.options, {
plugins: [
...(cfg.options.plugins || []),
Expand All @@ -279,6 +287,13 @@ test('supports customizing the loader', async (t) => {
});
},
result(result) {
t.true(typeof this === 'object', 'override result this context is rollup context');
expectedRollupContextKeys.forEach((key) => {
t.true(
Object.keys(this).includes(key),
`override result this context is rollup context with key ${key}`
);
});
return Object.assign({}, result, {
code: `${result.code}\n// Generated by some custom loader`
});
Expand Down
45 changes: 31 additions & 14 deletions packages/babel/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin } from 'rollup';
import { Plugin, PluginContext, TransformPluginContext } from 'rollup';
import { FilterPattern } from '@rollup/pluginutils';
import * as babelCore from '@babel/core';

Expand Down Expand Up @@ -52,28 +52,45 @@ export type RollupBabelCustomOutputPluginOptions = (
customOptions: Record<string, any>;
pluginOptions: RollupBabelOutputPluginOptions;
};
export type RollupBabelCustomPluginConfig = (
export interface RollupBabelCustomPluginConfigOptions {
code: string;
customOptions: Record<string, any>;
}
export interface RollupBabelCustomPluginResultOptions {
code: string;
customOptions: Record<string, any>;
config: babelCore.PartialConfig;
transformOptions: babelCore.TransformOptions;
}
export type RollupBabelCustomInputPluginConfig = (
this: TransformPluginContext,
cfg: babelCore.PartialConfig,
options: RollupBabelCustomPluginConfigOptions
) => babelCore.TransformOptions;
export type RollupBabelCustomInputPluginResult = (
this: TransformPluginContext,
result: babelCore.BabelFileResult,
options: RollupBabelCustomPluginResultOptions
) => babelCore.BabelFileResult;
export type RollupBabelCustomOutputPluginConfig = (
this: PluginContext,
cfg: babelCore.PartialConfig,
options: { code: string; customOptions: Record<string, any> }
options: RollupBabelCustomPluginConfigOptions
) => babelCore.TransformOptions;
export type RollupBabelCustomPluginResult = (
export type RollupBabelCustomOutputPluginResult = (
this: PluginContext,
result: babelCore.BabelFileResult,
options: {
code: string;
customOptions: Record<string, any>;
config: babelCore.PartialConfig;
transformOptions: babelCore.TransformOptions;
}
options: RollupBabelCustomPluginResultOptions
) => babelCore.BabelFileResult;
export interface RollupBabelCustomInputPlugin {
options?: RollupBabelCustomInputPluginOptions;
config?: RollupBabelCustomPluginConfig;
result?: RollupBabelCustomPluginResult;
config?: RollupBabelCustomInputPluginConfig;
result?: RollupBabelCustomInputPluginResult;
}
export interface RollupBabelCustomOutputPlugin {
options?: RollupBabelCustomOutputPluginOptions;
config?: RollupBabelCustomPluginConfig;
result?: RollupBabelCustomPluginResult;
config?: RollupBabelCustomOutputPluginConfig;
result?: RollupBabelCustomOutputPluginResult;
}
export type RollupBabelCustomInputPluginBuilder = (
babel: typeof babelCore
Expand Down

0 comments on commit 7e1be64

Please sign in to comment.