Skip to content

Commit

Permalink
feat: allow to opt-out of default babel plugins and presets
Browse files Browse the repository at this point in the history
  • Loading branch information
sastan committed Nov 6, 2019
1 parent 1762264 commit a1fd26f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ Type: `string | Partial<IBabelInputOptions>`

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<CompilerOptions> | Partial<Record<keyof CompilerOptions, string | number | boolean>> | ParsedCommandLine | TsConfigResolver | TsConfigResolverWithFileName`
Expand Down Expand Up @@ -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 |
Expand Down
1 change: 1 addition & 0 deletions src/plugin/i-typescript-plugin-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface ITypescriptPluginTypescriptOptions extends ITypescriptPluginBas

export interface ITypescriptPluginBabelOptions extends ITypescriptPluginBaseOptions {
transpiler: "babel";
noBabelConfigCustomization?: boolean;
babelConfig?: string | Partial<IBabelInputOptions>;
}

Expand Down
1 change: 1 addition & 0 deletions src/plugin/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
const babelConfigResult = getBabelConfig({
cwd,
babelConfig: pluginOptions.babelConfig,
noBabelConfigCustomization: pluginOptions.noBabelConfigCustomization,
forcedOptions: getForcedBabelOptions({cwd, pluginOptions, rollupInputOptions, browserslist: computedBrowserslist}),
defaultOptions: getDefaultBabelOptions({pluginOptions, rollupInputOptions, browserslist: computedBrowserslist}),
browserslist: computedBrowserslist,
Expand Down
2 changes: 2 additions & 0 deletions src/util/get-babel-config/get-babel-config-options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ITypescriptPluginBabelOptions} from "../../plugin/i-typescript-plugin-options";
import {IGetForcedBabelOptionsResult} from "../get-forced-babel-options/i-get-forced-babel-options-result";
import {IGetDefaultBabelOptionsResult} from "../get-default-babel-options/i-get-default-babel-options-result";
import {InputOptions} from "rollup";
Expand All @@ -8,4 +9,5 @@ export interface GetBabelConfigOptions extends FindBabelConfigOptions {
rollupInputOptions: InputOptions;
forcedOptions?: IGetForcedBabelOptionsResult;
defaultOptions?: IGetDefaultBabelOptionsResult;
noBabelConfigCustomization?: ITypescriptPluginBabelOptions["noBabelConfigCustomization"];
}
36 changes: 36 additions & 0 deletions src/util/get-babel-config/get-babel-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,49 @@ function configItemIsAllowedDuringNoMinification({file: {resolved}}: IBabelConfi
export function getBabelConfig({
babelConfig,
cwd,
noBabelConfigCustomization,
forcedOptions = {},
defaultOptions = {},
browserslist,
rollupInputOptions
}: GetBabelConfigOptions): GetBabelConfigResult {
const resolvedConfig = findBabelConfig({cwd, babelConfig});

if (noBabelConfigCustomization === true) {
return {
config: filename => {
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"
Expand Down
1 change: 1 addition & 0 deletions src/util/plugin-options/get-plugin-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function getPluginOptions(options: Partial<TypescriptPluginOptions>): Typ
return {
...baseOptions,
...("babelConfig" in options ? {babelConfig: options.babelConfig} : {}),
...("noBabelConfigCustomization" in options ? {noBabelConfigCustomization: options.noBabelConfigCustomization} : {}),
transpiler: "babel"
};
}
Expand Down

0 comments on commit a1fd26f

Please sign in to comment.