Skip to content

Commit

Permalink
fix(babel): rename minify* to chunk* (like minifyOptions to `ch…
Browse files Browse the repository at this point in the history
…unkOptions`)
  • Loading branch information
sastan committed Nov 14, 2019
1 parent b2abc38 commit 6ed57cc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 47 deletions.
8 changes: 4 additions & 4 deletions src/constant/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export const REGENERATOR_RUNTIME_NAME_1 = `${BABEL_RUNTIME_PREFIX_1}regenerator/
export const REGENERATOR_RUNTIME_NAME_2 = `${BABEL_RUNTIME_PREFIX_2}regenerator/index.js`;
export const BABEL_EXAMPLE_HELPERS = [`${BABEL_RUNTIME_PREFIX_1}helpers/esm/typeof.js`, `${BABEL_RUNTIME_PREFIX_2}helpers/esm/typeof.js`];

export const BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES = [];
export const BABEL_CHUNK_BLACKLIST_PRESET_NAMES = [];

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

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

export const BABEL_MINIFY_PLUGIN_NAMES = [
export const BABEL_CHUNK_PLUGIN_NAMES = [
"babel-plugin-transform-minify-booleans",
"babel-plugin-minify-builtins",
"babel-plugin-transform-inline-consecutive-adds",
Expand Down
16 changes: 8 additions & 8 deletions src/plugin/typescript-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
let babelConfig: ((filename: string) => IBabelConfig) | undefined;

/**
* If babel is 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 chunk presets/plugins has been passed, this config will be used
* @type {boolean}
*/
let babelMinifyConfig: ((filename: string) => IBabelConfig) | undefined;
let babelChunkConfig: ((filename: string) => IBabelConfig) | undefined;

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

/**
* The (Incremental) LanguageServiceHost to use
Expand Down Expand Up @@ -211,8 +211,8 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
rollupInputOptions
});
babelConfig = babelConfigResult.config;
babelMinifyConfig = babelConfigResult.minifyConfig;
hasBabelMinifyOptions = babelConfigResult.hasMinifyOptions;
babelChunkConfig = babelConfigResult.chunkConfig;
hasBabelChunkOptions = babelConfigResult.hasChunkOptions;
}

SUPPORTED_EXTENSIONS = getSupportedExtensions(
Expand Down Expand Up @@ -278,10 +278,10 @@ export default function typescriptRollupPlugin(pluginInputOptions: Partial<Types
*/
async renderChunk(this: PluginContext, code: string, chunk: RenderedChunk): Promise<{code: string; map: SourceMap} | null> {
// Don't proceed if there is no minification config
if (!hasBabelMinifyOptions || babelMinifyConfig == null) return null;
if (!hasBabelChunkOptions || babelChunkConfig == null) return null;

const transpilationResult = await transformAsync(code, {
...babelMinifyConfig(chunk.fileName),
...babelChunkConfig(chunk.fileName),
filename: chunk.fileName,
filenameRelative: ensureRelative(cwd, chunk.fileName)
});
Expand Down
4 changes: 2 additions & 2 deletions src/util/get-babel-config/get-babel-config-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import {IBabelConfig} from "../../plugin/i-babel-options";

export interface GetBabelConfigResult {
config(filename: string): IBabelConfig;
minifyConfig: ((filename: string) => IBabelConfig) | undefined;
hasMinifyOptions: boolean;
chunkConfig: ((filename: string) => IBabelConfig) | undefined;
hasChunkOptions: boolean;
}
61 changes: 28 additions & 33 deletions src/util/get-babel-config/get-babel-config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {IBabelConfigItem} from "../../plugin/i-babel-options";
import {isBabelPluginTransformRuntime, isBabelPresetEnv, isYearlyBabelPreset} from "../path/path-util";
import {
BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES,
BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES,
BABEL_MINIFY_PLUGIN_NAMES,
BABEL_MINIFY_PRESET_NAMES,
BABEL_CHUNK_BLACKLIST_PLUGIN_NAMES,
BABEL_CHUNK_BLACKLIST_PRESET_NAMES,
BABEL_CHUNK_PLUGIN_NAMES,
BABEL_CHUNK_PRESET_NAMES,
FORCED_BABEL_PLUGIN_TRANSFORM_RUNTIME_OPTIONS,
FORCED_BABEL_PRESET_ENV_OPTIONS,
FORCED_BABEL_YEARLY_PRESET_OPTIONS
Expand All @@ -30,7 +30,7 @@ function getBabelItemId(item: IBabelConfigItem | IBabelPlugin): string {
// 2 & 3) full module name
if (key.startsWith("/") || key.startsWith("@") || key.startsWith("babel-plugin-")) return key;

// add prefix to match keys defined in BABEL_MINIFY_PLUGIN_NAMES
// add prefix to match keys defined in BABEL_CHUNK_PLUGIN_NAMES
return `babel-plugin-${key}`;
}

Expand All @@ -39,14 +39,14 @@ function getBabelItemId(item: IBabelConfigItem | IBabelPlugin): string {
* @param {(IBabelConfigItem | IBabelPlugin)[]} userItems
* @param {(IBabelConfigItem | IBabelPlugin)[]} defaultItems
* @param {(IBabelConfigItem | IBabelPlugin)[]} [forcedItems]
* @param {boolean} [useMinifyOptions]
* @param {boolean} [useChunkOptions]
* @returns {{}[]}
*/
function combineConfigItems(
userItems: (IBabelConfigItem | IBabelPlugin)[],
defaultItems: (IBabelConfigItem | IBabelPlugin)[] = [],
forcedItems: (IBabelConfigItem | IBabelPlugin)[] = [],
useMinifyOptions: boolean = false
useChunkOptions: boolean = false
): {}[] {
const namesInUserItems = new Set(userItems.map(getBabelItemId));
const namesInForcedItems = new Set(forcedItems.map(getBabelItemId));
Expand All @@ -71,26 +71,23 @@ function combineConfigItems(
]
// Filter out those options that do not apply depending on whether or not to apply minification
.filter(configItem =>
useMinifyOptions
? configItemIsAllowedDuringMinification(getBabelItemId(configItem))
: configItemIsAllowedDuringNoMinification(getBabelItemId(configItem))
useChunkOptions ? configItemIsAllowedForChunk(getBabelItemId(configItem)) : configItemIsAllowedForTransform(getBabelItemId(configItem))
)
);
}

/**
* Returns true if the given configItem is related to minification
* Returns true if the given configItem is related to chunk transform
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsMinificationRelated(id: string): boolean {
function configItemIsChunkRelated(id: string): boolean {
return (
(/\bminify\b/.test(id) ||
BABEL_MINIFY_PRESET_NAMES.some(preset => id.includes(preset)) ||
BABEL_MINIFY_PLUGIN_NAMES.some(plugin => id.includes(plugin))) &&
BABEL_CHUNK_PRESET_NAMES.some(preset => id.includes(preset)) ||
BABEL_CHUNK_PLUGIN_NAMES.some(plugin => id.includes(plugin))) &&
!(
BABEL_MINIFICATION_BLACKLIST_PLUGIN_NAMES.some(preset => id.includes(preset)) ||
BABEL_MINIFICATION_BLACKLIST_PRESET_NAMES.some(plugin => id.includes(plugin))
BABEL_CHUNK_BLACKLIST_PLUGIN_NAMES.some(preset => id.includes(preset)) || BABEL_CHUNK_BLACKLIST_PRESET_NAMES.some(plugin => id.includes(plugin))
)
);
}
Expand All @@ -104,17 +101,17 @@ function configItemIsSyntaxRelated(id: string): boolean {
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsAllowedDuringMinification(id: string): boolean {
return configItemIsSyntaxRelated(id) || configItemIsMinificationRelated(id);
function configItemIsAllowedForChunk(id: string): boolean {
return configItemIsSyntaxRelated(id) || configItemIsChunkRelated(id);
}

/**
* Returns true if the given configItem is allowed when not applying minification
* @param {string} resolved
* @returns {boolean}
*/
function configItemIsAllowedDuringNoMinification(id: string): boolean {
return configItemIsSyntaxRelated(id) || !configItemIsMinificationRelated(id);
function configItemIsAllowedForTransform(id: string): boolean {
return configItemIsSyntaxRelated(id) || !configItemIsChunkRelated(id);
}

/**
Expand All @@ -134,7 +131,7 @@ export function getBabelConfig({
const resolvedConfig = findBabelConfig({cwd, babelConfig});

if (noBabelConfigCustomization === true) {
const loadOptionsForFilename = (filename: string, useMinifyOptions: boolean = false) => {
const loadOptionsForFilename = (filename: string, useChunkOptions: boolean = false) => {
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
Expand Down Expand Up @@ -170,14 +167,14 @@ export function getBabelConfig({

return {
...options,
plugins: combineConfigItems(options.plugins, [], [], useMinifyOptions)
plugins: combineConfigItems(options.plugins, [], [], useChunkOptions)
};
};

return {
config: filename => loadOptionsForFilename(filename, false),
minifyConfig: filename => loadOptionsForFilename(filename, true),
hasMinifyOptions: true
chunkConfig: filename => loadOptionsForFilename(filename, true),
hasChunkOptions: true
};
}

Expand Down Expand Up @@ -288,8 +285,8 @@ export function getBabelConfig({
delete combined.sourceMap;
}

// Combine the partial config with the default and forced options for the minifyConfig
const minifyCombined = {
// Combine the partial config with the default and forced options for the chunkConfig
const chunkCombined = {
...combined,
presets: combineConfigItems(
options.presets,
Expand All @@ -306,18 +303,16 @@ export function getBabelConfig({
};

const finalConfigFileOption = config == null ? configFileOption : {configFile: config};
const finalMinifyConfigFileOption = config == null ? configFileOption : {configFile: `${config}.minify`};
const finalchunkConfigFileOption = config == null ? configFileOption : {configFile: `${config}.minify`};

// Normalize the options
return {
config: filename => loadOptions({...combined, filename, ...finalConfigFileOption}),
// Only return the minify config if it includes at least one plugin or preset
minifyConfig:
minifyCombined.plugins.length < 1 && minifyCombined.presets.length < 1
chunkConfig:
chunkCombined.plugins.length < 1 && chunkCombined.presets.length < 1
? undefined
: filename => loadOptions({...minifyCombined, filename, ...finalMinifyConfigFileOption}),
hasMinifyOptions:
[...minifyCombined.plugins.filter(configItemIsMinificationRelated), ...minifyCombined.presets.filter(configItemIsMinificationRelated)].length >
0
: filename => loadOptions({...chunkCombined, filename, ...finalchunkConfigFileOption}),
hasChunkOptions: [...chunkCombined.plugins.filter(configItemIsChunkRelated), ...chunkCombined.presets.filter(configItemIsChunkRelated)].length > 0
};
}

0 comments on commit 6ed57cc

Please sign in to comment.