1 | import {transform} from 'babel-core';
|
2 |
|
3 |
|
4 | import manglePlugin from 'babel-plugin-transform-mangle';
|
5 | import evaluatePlugin from 'babel-plugin-transform-evaluate';
|
6 | import conditionalsPlugin from 'babel-plugin-transform-conditionals';
|
7 | import removeDebugger from 'babel-plugin-transform-remove-debugger';
|
8 | import removeConsole from 'babel-plugin-transform-remove-console';
|
9 | import deadCodeElimination from 'babel-plugin-transform-dead-code-elimination';
|
10 | import memberExpressionLiterals from 'babel-plugin-transform-member-expression-literals';
|
11 | import mergeSiblingVariables from 'babel-plugin-transform-merge-sibling-variables';
|
12 | import minifyBooleans from 'babel-plugin-transform-minify-booleans';
|
13 | import propertyLiterals from 'babel-plugin-transform-property-literals';
|
14 | import simplifyComparisonOperators from 'babel-plugin-transform-simplify-comparison-operators';
|
15 | import undefinedToVoid from 'babel-plugin-transform-undefined-to-void';
|
16 |
|
17 | Error.stackTraceLimit = Infinity;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export 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 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | let minifyPlugins = [];
|
63 |
|
64 | |
65 |
|
66 |
|
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 |
|
82 |
|
83 | minifyPlugins = minifyPlugins.concat(plugins);
|
84 |
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 | if (dead_code) {
|
91 | passes.push({plugins: [deadCodeElimination]});
|
92 | }
|
93 | if (conditionals) {
|
94 | passes.push({plugins: [conditionalsPlugin]});
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 | passes = passes.concat(presets);
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 | if (mangle) {
|
108 | passes.push({
|
109 | plugins: [
|
110 | [manglePlugin, {
|
111 | keep_fnames,
|
112 | mangle_globals
|
113 | }]
|
114 | ]
|
115 | });
|
116 | }
|
117 |
|
118 |
|
119 |
|
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 | }
|