UNPKG

3.57 kBJavaScriptView Raw
1// @ts-check
2const babelCore = require('@babel/core');
3const path = require('path');
4const fs = require('fs');
5const babelPresetMinify =
6 require('babel-preset-minify')({}, {simplifyComparisons: false});
7
8/*
9 * There doesn't seem to be documentation on what helpers are available, or
10 * which helpers are required for which transforms. The
11 * source is here:
12 * https://github.com/babel/babel/tree/master/packages/babel-helpers
13 *
14 * You can usually tell what the helpers are used for by searching the babel
15 * source to find out which plugin packages make an `addHelper` call for it.
16 *
17 * All helpers are listed here, with some commented out, so it's clear what
18 * we've excluded.
19 */
20const mainHelpers = [
21
22 // __proto__ assignment
23 'defaults',
24 'extends',
25
26 // es2015 classes
27 'assertThisInitialized',
28 'classCallCheck',
29 'construct',
30 'createClass',
31 'get',
32 'getPrototypeOf',
33 'inherits',
34 //'inheritsLoose',
35 'possibleConstructorReturn',
36 'set',
37 'setPrototypeOf',
38 'superPropBase',
39 'isNativeFunction',
40 'wrapNativeSuper',
41
42 // es2015 array-spread
43 'slicedToArray',
44 //'slicedToArrayLoose',
45 'toArray',
46 'toConsumableArray',
47 'arrayWithoutHoles',
48 'arrayWithHoles',
49 'iterableToArray',
50 'iterableToArrayLimit',
51 //'iterableToArrayLimitLoose',
52 'nonIterableSpread',
53 'nonIterableRest',
54
55 // es2015 instanceof
56 'instanceof',
57
58 // es2015 arrow-functions
59 'newArrowCheck',
60
61 // es2015 typeof-symbol
62 'typeof',
63
64 // es2015 computed-properties
65 'defineEnumerableProperties',
66 'defineProperty',
67
68 // es2015 block-scoping
69 'readOnlyError',
70 'temporalRef',
71 'temporalUndefined',
72
73 // es2015 destructuring
74 'objectDestructuringEmpty',
75 'objectWithoutProperties',
76 'objectWithoutPropertiesLoose',
77
78 // es2015 template-literals
79 'taggedTemplateLiteral',
80 //'taggedTemplateLiteralLoose',
81
82 // es2017 async-to-generator
83 'asyncToGenerator',
84
85 // es2018 proposal-async-generator-functions
86 'AsyncGenerator',
87 'AwaitValue',
88 'asyncGeneratorDelegate',
89 'asyncIterator',
90 'awaitAsyncGenerator',
91 'wrapAsyncGenerator',
92
93 // es2018 proposal-object-rest-spread
94 'objectSpread',
95 'toPropertyKey',
96
97 // proposal-function-sent
98 //'skipFirstGeneratorNext',
99
100 // proposal-class-properties
101 //'classNameTDZError',
102
103 // proposal-decorators
104 //'applyDecoratedDescriptor',
105 //'initializerDefineProperty',
106 //'initializerWarningHelper',
107
108 // react-inline-elements
109 //'jsx',
110];
111
112/**
113 * Babel helpers needed only for ES module transformation. We bundle these with
114 * the require.js AMD module loader instead so that the AMD transform does not
115 * depend on loading all Babel helpers.
116 */
117const amdHelpers = [
118 'interopRequireDefault',
119 'interopRequireWildcard',
120];
121
122minifyAndWriteJs(
123 // @ts-ignore
124 babelCore.buildExternalHelpers([...mainHelpers, ...amdHelpers]),
125 'babel-helpers-full.min.js');
126
127minifyAndWriteJs(
128 // @ts-ignore
129 babelCore.buildExternalHelpers(amdHelpers),
130 'babel-helpers-amd.min.js');
131
132const dir = path.dirname(require.resolve('regenerator-runtime'));
133const js = fs.readFileSync(path.join(dir, 'runtime.js'), 'utf-8');
134minifyAndWriteJs(js, 'regenerator-runtime.min.js');
135
136/**
137 * @param {string} js
138 * @param {string} filename
139 */
140function minifyAndWriteJs(js, filename) {
141 const minified =
142 // @ts-ignore
143 babelCore.transform(js, {presets: [babelPresetMinify]}).code;
144 const libDir = path.join(__dirname, '..', 'lib');
145 try {
146 fs.mkdirSync(libDir);
147 } catch (e) { /* don't care */ }
148 fs.writeFileSync(path.join(libDir, filename), minified, {encoding: 'utf-8'});
149}