Skip to content

Commit

Permalink
fix(bug): fixes an issue with babel where applying minification-relat…
Browse files Browse the repository at this point in the history
…ed plugins could lead to syntax errors
  • Loading branch information
wessberg committed Mar 19, 2019
1 parent 9efc99c commit 2a91b7f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/constant/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const TYPEOF_BABEL_HELPER_NAME_2 = `${BABEL_RUNTIME_PREFIX_1}helpers/type
export const TYPEOF_BABEL_HELPER_NAME_3 = `${BABEL_RUNTIME_PREFIX_2}helpers/esm/typeof.js`;
export const TYPEOF_BABEL_HELPER_NAME_4 = `${BABEL_RUNTIME_PREFIX_2}helpers/typeof.js`;

export const BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES = [];

export const BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES = ["@babel/plugin-transform-runtime", "babel-plugin-transform-runtime"];

export const BABEL_MINIFY_PRESET_NAMES = ["babel-preset-minify"];

export const BABEL_MINIFY_PLUGIN_NAMES = [
Expand Down
15 changes: 12 additions & 3 deletions src/plugin/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
let babelConfig: IBabelConfig | undefined;

/**
* If babel are to be used, and if one or more minify presets/plugins has been passed, this config will be used
* If babel is to be used, and if one or more minify presets/plugins has been passed, this config will be used
* @type {boolean}
*/
let babelMinifyConfig: IBabelConfig | undefined;

/**
* If babel is to be used, and if one or more minify presets/plugins has been passed, this will be true
* @type {boolean}
*/
let hasBabelMinifyOptions: boolean = false;

/**
* The (Incremental) LanguageServiceHost to use
* @type {IncrementalLanguageService?}
Expand Down Expand Up @@ -158,6 +164,7 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
});
babelConfig = babelConfigResult.config;
babelMinifyConfig = babelConfigResult.minifyConfig;
hasBabelMinifyOptions = babelConfigResult.hasMinifyOptions;
}

SUPPORTED_EXTENSIONS = getSupportedExtensions(Boolean(parsedCommandLineResult.parsedCommandLine.options.allowJs), Boolean(parsedCommandLineResult.parsedCommandLine.options.resolveJsonModule));
Expand Down Expand Up @@ -192,16 +199,18 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
async renderChunk(this: PluginContext, code: string, chunk: RenderedChunk): Promise<{code: string; map: RawSourceMap} | null> {
const includesPropertyAccessExpression = code.includes(PRESERVING_PROPERTY_ACCESS_EXPRESSION);

console.log(babelMinifyConfig, hasBabelMinifyOptions);

// If the code doesn't include a PropertyAccessExpression that needs replacement, and if no additional minification should be applied, return immediately.
if (!includesPropertyAccessExpression && babelMinifyConfig == null) return null;
if (!includesPropertyAccessExpression && (!hasBabelMinifyOptions || babelMinifyConfig == null)) return null;

const updatedCode = getMagicStringContainer(code, chunk.fileName);

if (includesPropertyAccessExpression) {
updatedCode.replaceAll(PRESERVING_PROPERTY_ACCESS_EXPRESSION, "");
}

if (babelMinifyConfig == null) {
if (!hasBabelMinifyOptions || babelMinifyConfig == null) {
return updatedCode.hasModified
? {
code: updatedCode.code,
Expand Down
16 changes: 13 additions & 3 deletions src/util/get-babel-config/get-babel-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {IBabelConfigItem, IBabelInputOptions} from "../../plugin/i-babel-options
import {IGetBabelConfigOptions} from "./i-get-babel-config-options";
import {ensureAbsolute} from "../path/path-util";
import {IGetBabelConfigResult} from "./i-get-babel-config-result";
import {BABEL_MINIFY_PLUGIN_NAMES, BABEL_MINIFY_PRESET_NAMES} from "../../constant/constant";
import {BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES, BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES, BABEL_MINIFY_PLUGIN_NAMES, BABEL_MINIFY_PRESET_NAMES} from "../../constant/constant";
// @ts-ignore
import {loadOptions, loadPartialConfig} from "@babel/core";

Expand Down Expand Up @@ -42,13 +42,22 @@ function combineConfigItems(userItems: IBabelConfigItem[], defaultItems: IBabelC
);
}

/**
* Returns true if the given configItem is related to minification
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsMinificationRelated({file: {resolved}}: IBabelConfigItem): boolean {
return BABEL_MINIFY_PRESET_NAMES.some(preset => resolved.includes(preset)) || BABEL_MINIFY_PLUGIN_NAMES.some(plugin => resolved.includes(plugin));
}

/**
* Returns true if the given configItem is allowed during minification
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsAllowedDuringMinification({file: {resolved}}: IBabelConfigItem): boolean {
return BABEL_MINIFY_PRESET_NAMES.some(preset => resolved.includes(preset)) || BABEL_MINIFY_PLUGIN_NAMES.some(plugin => resolved.includes(plugin));
return BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES.every(preset => !resolved.includes(preset)) && BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES.every(plugin => !resolved.includes(plugin));
}

/**
Expand Down Expand Up @@ -125,6 +134,7 @@ export function getBabelConfig({babelConfig, cwd, forcedOptions = {}, defaultOpt
return {
config: loadOptions(combined),
// Only return the minify config if it includes at least one plugin or preset
minifyConfig: minifyCombined.plugins.length < 1 && minifyCombined.presets.length < 1 ? undefined : loadOptions(minifyCombined)
minifyConfig: minifyCombined.plugins.length < 1 && minifyCombined.presets.length < 1 ? undefined : loadOptions(minifyCombined),
hasMinifyOptions: [...minifyCombined.plugins.filter(configItemIsMinificationRelated), ...minifyCombined.presets.filter(configItemIsMinificationRelated)].length > 0
};
}
3 changes: 2 additions & 1 deletion src/util/get-babel-config/i-get-babel-config-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import {IBabelConfig} from "../../plugin/i-babel-options";

export interface IGetBabelConfigResult {
config: IBabelConfig;
minifyConfig?: IBabelConfig;
minifyConfig: IBabelConfig | undefined;
hasMinifyOptions: boolean;
}

0 comments on commit 2a91b7f

Please sign in to comment.