forked from terkelg/facon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.js
executable file
·30 lines (24 loc) · 997 Bytes
/
builder.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
const fs = require('fs');
const mkdir = require('mkdirplz');
const sizer = require('gzip-size');
const pretty = require('pretty-bytes');
const { minify } = require('terser');
const pkg = require('./package');
let data = fs.readFileSync('src/index.js', 'utf8');
// The legend Luke did it again.
// https://github.com/lukeed
mkdir('dist').then(() => {
// Copy as is for ESM
fs.writeFileSync(pkg.module, data);
// Mutate exports for CJS
data = data.replace(/export default/, 'module.exports =');
fs.writeFileSync(pkg.main, data);
// Minify & print gzip-size
let { code } = minify(data, { toplevel:true });
console.log(`> gzip size: ${pretty(sizer.sync(code))}`);
// Write UMD bundle
let name = pkg.name;
let UMD = `!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.${name}=t()}(this,function(){`;
UMD += code.replace(/module.exports=/, 'return ') + '});';
fs.writeFileSync(pkg.unpkg, UMD);
});