From a1fd26f8c8e06787f369e99e8676abd95bfa08cd Mon Sep 17 00:00:00 2001 From: Sascha Tandel Date: Wed, 6 Nov 2019 18:13:02 +0100 Subject: [PATCH] feat: allow to opt-out of default babel plugins and presets --- README.md | 8 +++++ src/plugin/i-typescript-plugin-options.ts | 1 + src/plugin/typescript-plugin.ts | 1 + .../get-babel-config-options.ts | 2 ++ src/util/get-babel-config/get-babel-config.ts | 36 +++++++++++++++++++ src/util/plugin-options/get-plugin-options.ts | 1 + 6 files changed, 49 insertions(+) diff --git a/README.md b/README.md index 268becb2..43d5630a 100644 --- a/README.md +++ b/README.md @@ -442,6 +442,12 @@ Type: `string | Partial` This option will only be respected when `"babel"` is being used as the `transpiler` and can be used to provide a [Babel config](https://babeljs.io/docs/en/options) or a path to one. +#### `noBabelConfigCustomization` + +Type: `boolean` + +If this option is `true`, **no** [default presets or plugins](#default-babel-plugins) are applied to the babel config. The [ignored/overridden Babel options](#ignoredoverridden-babel-options) are nevertheless applied. + #### `tsconfig` Type: `string | Partial | Partial> | ParsedCommandLine | TsConfigResolver | TsConfigResolverWithFileName` @@ -550,6 +556,8 @@ By default, the plugin will conditionally apply the `@babel/preset-env` preset i If you provide these presets or plugins yourself through the found or provided Babel config, _your_ config options will take precedence. +In case that this is not enough for you setting [`noBabelConfigCustomization`](#nobabelconfigcustomization) disables these defaults. + Here's table with a full overview of the specifics: | Preset/Plugin | Condition | Reason | diff --git a/src/plugin/i-typescript-plugin-options.ts b/src/plugin/i-typescript-plugin-options.ts index e9708060..481b5ff0 100644 --- a/src/plugin/i-typescript-plugin-options.ts +++ b/src/plugin/i-typescript-plugin-options.ts @@ -54,6 +54,7 @@ export interface ITypescriptPluginTypescriptOptions extends ITypescriptPluginBas export interface ITypescriptPluginBabelOptions extends ITypescriptPluginBaseOptions { transpiler: "babel"; + noBabelConfigCustomization?: boolean; babelConfig?: string | Partial; } diff --git a/src/plugin/typescript-plugin.ts b/src/plugin/typescript-plugin.ts index bee3154a..52f22444 100644 --- a/src/plugin/typescript-plugin.ts +++ b/src/plugin/typescript-plugin.ts @@ -189,6 +189,7 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial { + const partialConfig = + resolvedConfig != null && resolvedConfig.kind === "dict" + ? // If the given babelConfig is an object of input options, use that as the basis for the full config + resolvedConfig + : // Load the path to a babel config provided to the plugin if any, otherwise try to resolve it + loadPartialConfig({ + cwd, + root: cwd, + ...(resolvedConfig != null ? {configFile: resolvedConfig.path} : {babelrc: true}), + filename + }); + + const {options} = loadPartialConfig({ + ...partialConfig.options, + ...forcedOptions, + presets: partialConfig.options.presets || [], + plugins: partialConfig.options.plugins || [], + filename + }); + + // sourceMap is an alias for 'sourceMaps'. If the user provided it, make sure it is undefined. Otherwise, Babel will fail during validation + if ("sourceMap" in options) { + delete options.sourceMap; + } + + return options; + }, + minifyConfig: undefined, + hasMinifyOptions: false + }; + } + // Load a partial Babel config based on the input options const partialConfig = loadPartialConfig( resolvedConfig != null && resolvedConfig.kind === "dict" diff --git a/src/util/plugin-options/get-plugin-options.ts b/src/util/plugin-options/get-plugin-options.ts index 99ed164a..8cea847f 100644 --- a/src/util/plugin-options/get-plugin-options.ts +++ b/src/util/plugin-options/get-plugin-options.ts @@ -44,6 +44,7 @@ export function getPluginOptions(options: Partial): Typ return { ...baseOptions, ...("babelConfig" in options ? {babelConfig: options.babelConfig} : {}), + ...("noBabelConfigCustomization" in options ? {noBabelConfigCustomization: options.noBabelConfigCustomization} : {}), transpiler: "babel" }; }