-
Notifications
You must be signed in to change notification settings - Fork 736
/
build.js
209 lines (182 loc) · 4.33 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint import/no-extraneous-dependencies: off */
/* eslint no-console: off */
const rollup = require("rollup"),
rollupBabel = require("rollup-plugin-babel"),
rollupMinify = require("rollup-plugin-babel-minify"),
rollupNode = require("@rollup/plugin-node-resolve"),
rollupCommonJS = require("@rollup/plugin-commonjs"),
UglifyES = require("uglify-es"),
fs = require("fs");
// For some reason, the minifier is currently producing total giberrish, at least for the global build.
// I've disabled it for now, and will simply uglify externally.
const TRUST_MINIFY = false;
function rollupInputOpts(opts) {
const presetOpts = {
modules: false,
loose: true
};
if (opts.target) {
presetOpts.targets = opts.target;
}
const inputOpts = {
input: opts.src || "./src/luxon.js",
onwarn: warning => {
// I don't care about these for now
if (warning.code !== "CIRCULAR_DEPENDENCY") {
console.warn(`(!) ${warning.message}`);
}
},
plugins: [
rollupNode(),
rollupCommonJS({
include: "node_modules/**"
})
]
};
if (opts.compile || typeof opts.compile === "undefined") {
inputOpts.plugins.push(
rollupBabel({
babelrc: false,
presets: [["@babel/preset-env", presetOpts]]
})
);
}
if (opts.minify && TRUST_MINIFY) {
inputOpts.plugins.push(
rollupMinify({
comments: false,
mangle: {
topLevel: !opts.global
}
})
);
}
return inputOpts;
}
function rollupOutputOpts(dest, opts) {
const outputOpts = {
file: `build/${dest}/${opts.filename || "luxon.js"}`,
format: opts.format,
sourcemap: true
};
if (opts.name) {
outputOpts.name = opts.name;
}
return outputOpts;
}
async function babelAndRollup(dest, opts) {
const inputOpts = rollupInputOpts(opts),
outputOpts = rollupOutputOpts(dest, opts),
bundle = await rollup.rollup(inputOpts);
await bundle.write(outputOpts);
}
async function buildLibrary(dest, opts) {
console.log("Building", dest);
const promises = [babelAndRollup(dest, opts)];
if (opts.minify && TRUST_MINIFY) {
promises.push(
babelAndRollup(
dest,
Object.assign({}, opts, {
minify: true,
filename: "luxon.min.js"
})
)
);
}
await Promise.all(promises);
if (opts.minify && !TRUST_MINIFY) {
const code = fs.readFileSync(`build/${dest}/luxon.js`, "utf8"),
ugly = UglifyES.minify(code, {
toplevel: !opts.global,
output: {
comments: false
},
sourceMap: {
filename: `build/${dest}/luxon.js`
}
});
if (ugly.error) {
console.error("Error uglifying", ugly.error);
} else {
fs.writeFileSync(`build/${dest}/luxon.min.js`, ugly.code);
fs.writeFileSync(`build/${dest}/luxon.min.js.map`, ugly.map);
}
}
console.log("Built", dest);
}
const browsersOld = "last 2 major versions";
async function global() {
await buildLibrary("global", {
format: "iife",
global: true,
name: "luxon",
target: browsersOld,
minify: true
});
}
async function globalFilled() {
await buildLibrary("global-filled", {
format: "iife",
global: true,
name: "luxon",
target: browsersOld,
src: "./src/luxonFilled.js",
minify: true
});
}
async function amd() {
await buildLibrary("amd", {
format: "amd",
name: "luxon",
target: browsersOld,
minify: true
});
}
async function amdFilled() {
await buildLibrary("amd-filled", {
format: "amd",
name: "luxon",
target: browsersOld,
src: "./src/luxonFilled.js",
minify: true
});
}
async function node() {
await buildLibrary("node", { format: "cjs", target: "node 6" });
}
async function cjsBrowser() {
await buildLibrary("cjs-browser", { format: "cjs", target: browsersOld });
}
async function es6() {
await buildLibrary("es6", {
format: "es",
compile: false,
minify: true
});
}
async function globalEs6() {
await buildLibrary("global-es6", {
format: "iife",
name: "luxon",
compile: false,
global: true
});
}
async function buildAll() {
await Promise.all([
node(),
cjsBrowser(),
es6(),
amd(),
amdFilled(),
global(),
globalEs6(),
globalFilled()
]);
}
module.exports = {
buildAll,
buildNode: node,
buildGlobal: global
};