forked from PokeMMOZone/pokemmo-calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundler.js
executable file
·59 lines (53 loc) · 2.08 KB
/
bundler.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
'use strict';
const fs = require('fs');
const path = require('path');
const babel = require('@babel/core');
const terser = require('terser');
const config = {
compact: false,
plugins: [
['@babel/plugin-transform-typescript', {'isTSX': true}],
['@babel/plugin-proposal-class-properties', {'loose': true}],
['@babel/plugin-proposal-optional-chaining', {'loose': true}],
['@babel/plugin-proposal-object-rest-spread', {'loose': true, 'useBuiltIns': true}],
'@babel/plugin-proposal-optional-catch-binding',
'@babel/plugin-transform-arrow-functions',
['@babel/plugin-transform-block-scoping', {'throwIfClosureRequired': true}],
['@babel/plugin-transform-classes', {'loose': true}],
['@babel/plugin-transform-computed-properties', {'loose': true}],
['@babel/plugin-transform-destructuring', {'loose': true, 'useBuiltIns': true}],
['@babel/plugin-transform-for-of', {'assumeArray': true}],
'@babel/plugin-transform-literals',
'@babel/plugin-transform-parameters',
'@babel/plugin-transform-shorthand-properties',
['@babel/plugin-transform-spread', {'loose': true}],
['@babel/plugin-transform-template-literals', {'loose': true}],
'@babel/plugin-transform-member-expression-literals',
'@babel/plugin-transform-property-literals'
],
}
class Bundler {
constructor(dirname) {
this.built = path.resolve(dirname, 'dist');
}
read(f, b = 0, e = 0) {
try {
const data = fs.readFileSync(path.join(this.built, f), 'utf8');
if (e) return data.split('\n').slice(b, -e).join('\n') + '\n';
if (b) return data.split('\n').slice(b).join('\n') + '\n';
return data + '\n';
} catch (err) {
if (err.code === 'ENOENT') {
console.error(`Missing file 'dist/${f}' - did you run \`npm run compile\`?`);
process.exit(1);
}
throw err;
}
}
async bundle(bundled, output = 'production.min.js') {
bundled = babel.transformSync(bundled, config).code;
bundled = (await terser.minify(bundled)).code;
fs.writeFileSync(path.join(this.built, output), bundled);
}
}
module.exports = Bundler;