From 7e1be64d39d2e5f0368359d0a1317ee1f2f57f91 Mon Sep 17 00:00:00 2001 From: Rene Haas Date: Sat, 6 Feb 2021 01:57:55 +0100 Subject: [PATCH] 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 --- packages/babel/src/transformCode.js | 2 +- packages/babel/test/as-input-plugin.js | 15 +++++++++ packages/babel/test/as-output-plugin.js | 15 +++++++++ packages/babel/types/index.d.ts | 45 +++++++++++++++++-------- 4 files changed, 62 insertions(+), 15 deletions(-) diff --git a/packages/babel/src/transformCode.js b/packages/babel/src/transformCode.js index c648e6bda..f677ea09c 100644 --- a/packages/babel/src/transformCode.js +++ b/packages/babel/src/transformCode.js @@ -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 }); diff --git a/packages/babel/test/as-input-plugin.js b/packages/babel/test/as-input-plugin.js index 14e44e118..32109c4ba 100644 --- a/packages/babel/test/as-input-plugin.js +++ b/packages/babel/test/as-input-plugin.js @@ -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: [ @@ -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` diff --git a/packages/babel/test/as-output-plugin.js b/packages/babel/test/as-output-plugin.js index f60f433ef..c10a05e02 100644 --- a/packages/babel/test/as-output-plugin.js +++ b/packages/babel/test/as-output-plugin.js @@ -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 || []), @@ -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` }); diff --git a/packages/babel/types/index.d.ts b/packages/babel/types/index.d.ts index 602aea31b..52c9558be 100644 --- a/packages/babel/types/index.d.ts +++ b/packages/babel/types/index.d.ts @@ -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'; @@ -52,28 +52,45 @@ export type RollupBabelCustomOutputPluginOptions = ( customOptions: Record; pluginOptions: RollupBabelOutputPluginOptions; }; -export type RollupBabelCustomPluginConfig = ( +export interface RollupBabelCustomPluginConfigOptions { + code: string; + customOptions: Record; +} +export interface RollupBabelCustomPluginResultOptions { + code: string; + customOptions: Record; + 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 } + options: RollupBabelCustomPluginConfigOptions ) => babelCore.TransformOptions; -export type RollupBabelCustomPluginResult = ( +export type RollupBabelCustomOutputPluginResult = ( + this: PluginContext, result: babelCore.BabelFileResult, - options: { - code: string; - customOptions: Record; - 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