forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (51 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const promisify = require('util.promisify');
const fs = require('fs');
// TODO: replace by `fs.promises` after dropping NodeJS < 10 support
const readFile = promisify(fs.readFile);
const unlink = promisify(fs.unlink);
const writeFile = promisify(fs.writeFile);
const { basename, dirname, join } = require('path');
const tmpdir = require('os').tmpdir();
// TODO: replace by `mkdir` with `recursive: true` after dropping NodeJS < 10.12 support
const { sync: mkdirp } = require('mkdirp');
const webpack = promisify(require('webpack'));
const compat = require('core-js-compat');
const modulesList = require('core-js-compat/modules');
const { banner } = require('./config');
module.exports = function ({ blacklist = [], modules = modulesList.slice(), targets, filename } = {}) {
const set = new Set();
function filter(method, list) {
for (const ns of list) {
for (const name of modulesList) {
if (name === ns || name.startsWith(`${ ns }.`)) {
set[method](name);
}
}
}
}
filter('add', modules);
filter('delete', blacklist);
modules = modulesList.filter(it => set.has(it));
if (targets) modules = compat({ targets, filter: modules }).list;
const tempFile = join(tmpdir, `core-js-${ Math.random().toString(36).slice(2) }.js`);
return webpack({
mode: 'none',
node: {
global: false,
process: false,
setImmediate: false,
},
entry: modules.map(it => require.resolve(`core-js/modules/${ it }`)),
output: {
path: dirname(tempFile),
filename: basename(`./${ tempFile }`),
},
}).then(() => readFile(tempFile)).then(script => unlink(tempFile).then(() => {
script = `${ banner }\n!function (undefined) { 'use strict'; ${ script } }();`;
if (typeof filename != 'undefined') {
mkdirp(dirname(filename));
return writeFile(filename, script).then(() => script);
} return script;
}));
};