UNPKG

4.32 kBJavaScriptView Raw
1import {transform} from 'babel-core';
2
3// plugins
4import manglePlugin from 'babel-plugin-transform-mangle';
5import evaluatePlugin from 'babel-plugin-transform-evaluate';
6import conditionalsPlugin from 'babel-plugin-transform-conditionals';
7import removeDebugger from 'babel-plugin-transform-remove-debugger';
8import removeConsole from 'babel-plugin-transform-remove-console';
9import deadCodeElimination from 'babel-plugin-transform-dead-code-elimination';
10import memberExpressionLiterals from 'babel-plugin-transform-member-expression-literals';
11import mergeSiblingVariables from 'babel-plugin-transform-merge-sibling-variables';
12import minifyBooleans from 'babel-plugin-transform-minify-booleans';
13import propertyLiterals from 'babel-plugin-transform-property-literals';
14import simplifyComparisonOperators from 'babel-plugin-transform-simplify-comparison-operators';
15import undefinedToVoid from 'babel-plugin-transform-undefined-to-void';
16
17Error.stackTraceLimit = Infinity;
18/**
19 * The main function of the minifier
20 * @function
21 */
22export default function BabelMinify(inputCode, {
23 mangle = true,
24 mangle_globals = false,
25
26 dead_code = false,
27 conditionals = true,
28 evaluate = true, // eval constant expressions
29 drop_debugger = false,
30 drop_console = false,
31 properties = true,
32 join_vars = true,
33 booleans = true,
34 unsafe = true,
35 keep_fnames = false,
36
37 // number of passes
38 npasses = 1,
39
40 // passed on to babel transform to tell whether to use babelrc
41 babelrc = false,
42
43 // should there be any other plugins added to this build process
44 plugins = [],
45
46 // should there be any other presets
47 presets = [],
48
49 // if false, babel-minify can give a list of plugins to use as a preset
50 minify = true,
51} = {}) {
52
53 if (typeof inputCode !== 'string' && minify) throw new Error('Invalid Input');
54
55 /**
56 * The final list of plugins that are applied in babel transform
57 * This is the first list that's preffered in babel transform, the plugins
58 * that go into this take one pass, plugins that prefer separate passes go into
59 * the {finalPresets}
60 * @type {Array}
61 */
62 let minifyPlugins = [];
63
64 /**
65 * The final list of presets that are applied in SEPARATE passes
66 * @type {Array}
67 */
68 let passes = [];
69
70 evaluate && minifyPlugins.push(evaluatePlugin);
71 drop_debugger && minifyPlugins.push(removeDebugger);
72 drop_console && minifyPlugins.push(removeConsole);
73 properties && minifyPlugins.push(memberExpressionLiterals);
74 properties && minifyPlugins.push(propertyLiterals);
75 join_vars && minifyPlugins.push(mergeSiblingVariables);
76 booleans && minifyPlugins.push(minifyBooleans);
77 unsafe && minifyPlugins.push(undefinedToVoid);
78 unsafe && minifyPlugins.push(simplifyComparisonOperators);
79
80 /**
81 * Append all user passed plugins to minifyPlugins
82 */
83 minifyPlugins = minifyPlugins.concat(plugins);
84
85 /**
86 * Things that remove code or replace code in a major way,
87 * we just use then in separate presets to enable them to be
88 * under separate passes
89 */
90 if (dead_code) {
91 passes.push({plugins: [deadCodeElimination]});
92 }
93 if (conditionals) {
94 passes.push({plugins: [conditionalsPlugin]});
95 }
96
97 /**
98 * Append all user passed presets to passes
99 */
100 passes = passes.concat(presets);
101
102 /**
103 * Keep mangler to be in the last of the presets
104 * I don't know why clearly, but mangler seems to disrupt everything, so
105 * I just keep it as the last pass
106 */
107 if (mangle) {
108 passes.push({
109 plugins: [
110 [manglePlugin, {
111 keep_fnames,
112 mangle_globals
113 }]
114 ]
115 });
116 }
117
118 // if minify is false, return the plugins list to be used elsewhere
119 // maybe move this to a separate file later
120 if (!minify) return { plugins: minifyPlugins, presets: passes };
121
122 let result = {code: inputCode};
123
124 while (npasses-- > 0) {
125 result = transform(result.code, {
126 babelrc,
127 comments: false,
128 compact: true,
129 minified: true,
130 passPerPreset: true,
131 presets: passes,
132 plugins: minifyPlugins
133 });
134 }
135
136 return result.code;
137}