forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (54 loc) · 1.87 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
59
60
61
62
63
64
65
66
'use strict';
const { promisify } = require('util');
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 { dirname, join } = require('path');
const tmpdir = require('os').tmpdir();
// TODO: replace by `mkdir` with `recursive: true` after dropping NodeJS < 10.12 support
const mkdirp = promisify(require('mkdirp'));
const webpack = promisify(require('webpack'));
const compat = require('core-js-compat/compat');
const modulesList = require('core-js-compat/modules');
const { banner } = require('./config');
module.exports = async 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 tempFileName = `core-js-${ Math.random().toString(36).slice(2) }.js`;
const tempFile = join(tmpdir, tempFileName);
await webpack({
mode: 'none',
node: {
global: false,
process: false,
setImmediate: false,
},
entry: modules.map(it => require.resolve(`core-js/modules/${ it }`)),
output: {
path: tmpdir,
filename: tempFileName,
},
});
const file = await readFile(tempFile);
await unlink(tempFile);
const script = `${ banner }\n!function (undefined) { 'use strict'; ${ file } }();`;
if (typeof filename != 'undefined') {
await mkdirp(dirname(filename));
await writeFile(filename, script);
}
return script;
};