-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
91 lines (86 loc) · 2.37 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
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": true}] */
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const { uglify } = require('rollup-plugin-uglify');
const replace = require('rollup-plugin-replace');
const commonjs = require('rollup-plugin-commonjs');
const resolve = require('rollup-plugin-node-resolve');
const autoExternal = require('rollup-plugin-auto-external');
const pkg = require('./package.json');
const NAME = pkg.name
.split(/-|_/)
.filter(x => x)
.map(s => s.charAt(0).toUpperCase() + s.slice(1))
.join('');
function createOptions(format, outputPath, minify) {
return {
inputOptions: {
input: pkg.source,
plugins: [
babel({
exclude: 'node_modules/**'
}),
resolve({
jsnext: true,
main: true,
browser: format === 'umd'
}),
commonjs({
include: /node_modules/
}),
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
format === 'umd' ? null : autoExternal(),
minify
? uglify({
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false
}
})
: null
].filter(x => x)
},
outputOptions: {
file: outputPath,
format,
name: NAME,
indent: false,
exports: 'named',
globals: {
react: 'React'
}
}
};
}
function generateMinPath(p) {
const min = '.min';
const pos = p.lastIndexOf('.');
if (pos === -1) return `${p}${min}`;
return `${p.substr(0, pos)}${min}${p.substr(pos)}`;
}
(async () => {
const { module, main, 'umd:main': umd } = pkg;
await Promise.all(
[
{ format: 'es', outputPath: module },
{ format: 'cjs', outputPath: main },
{ format: 'umd', outputPath: umd },
{ format: 'umd', outputPath: generateMinPath(umd), minify: true }
]
.filter(o => o.outputPath)
.map(async build => {
const { format, outputPath, minify } = build;
const { inputOptions, outputOptions } = createOptions(
format,
outputPath,
minify
);
const bundle = await rollup.rollup(inputOptions);
await bundle.write(outputOptions);
})
);
})();