forked from mdbootstrap/TW-Elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
96 lines (86 loc) · 1.91 KB
/
build.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fs = require('fs');
const path = require('path');
const uglify = require('uglify-js');
const rollup = require('rollup');
const buble = require('rollup-plugin-buble');
const version = require('./package.json').version;
const banner =
`/*!
* perfect-scrollbar v${version}
* (c) ${new Date().getFullYear()} Hyunje Jun
* @license MIT
*/`;
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist')
}
const resolve = _path => path.resolve(__dirname, _path);
const entry = resolve('src/index.js');
build([
{
dest: resolve('dist/perfect-scrollbar.js'),
format: 'umd',
},
{
dest: resolve('dist/perfect-scrollbar.min.js'),
format: 'umd',
},
{
dest: resolve('dist/perfect-scrollbar.common.js'),
format: 'cjs'
},
{
dest: resolve('dist/perfect-scrollbar.esm.js'),
format: 'es'
}
]);
function build(builds) {
let built = 0;
const total = builds.length;
const next = () => {
buildEntry(builds[built]).then(() => {
built++;
if (built < total) {
next();
}
}).catch((e) => console.log(e));
}
next();
}
function buildEntry(opts) {
const isProd = /min\.js$/.test(opts.dest);
return rollup
.rollup({
input: entry,
plugins: [buble()],
})
.then(bundle => bundle.generate({
file: opts.dest,
format: opts.format,
name: 'PerfectScrollbar',
banner,
}))
.then(({code}) => {
if (isProd) {
code = (banner ? banner + '\n' : '') + uglify.minify(code, {
output: {
ascii_only: true
},
compress: {
pure_funcs: ['makeMap']
}
}).code;
}
return write(opts.dest, code);
})
}
function write(dest, code) {
return new Promise((resolve, reject) => {
fs.writeFile(dest, code, err => {
if (err) {
return reject(err);
}
console.log(path.relative(process.cwd(), dest));
resolve();
});
});
}