-
Notifications
You must be signed in to change notification settings - Fork 5
/
esbuild.js
187 lines (155 loc) · 4.83 KB
/
esbuild.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
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
//-- ----------------------------------------
// Reporter Plugin
//-- ----------------------------------------
function reporterPlugin() {
return {
name: 'measure-time',
setup(build) {
let timeStart = null;
build.onStart(() => {
timeStart = Date.now()
console.log(`\x1b[47m\x1b[30m GO \x1b[0m \x1b[37m New build process started \x1b[0m`);
});
build.onEnd((result) => {
let seconds = ((Date.now() - timeStart) / 1000).toFixed(2);
if (result.errors.length > 0) {
console.log(`\x1b[41m\x1b[30m ERRO \x1b[0m \x1b[31m An critical error occurred in ${seconds}s \x1b[0m`);
} else if (result.warnings.length > 0) {
console.log(`\x1b[43m\x1b[30m WARN \x1b[0m \x1b[33m An warning occurred in ${seconds}s \x1b[0m`);
} else {
console.log(`\x1b[42m\x1b[30m COOL \x1b[0m \x1b[32m Successfully build in ${seconds}s \x1b[0m`);
}
});
}
};
}
//-- ----------------------------------------
// esbuild Configuration - Assets
//-- ----------------------------------------
async function assets(options = {}) {
const entryPoints = {};
const plugins = [reporterPlugin()];
// EntryPoint CSS
if (options.bundleCss) {
entryPoints['css/newshub.min'] = 'resources/scss/theme.scss';
plugins.push(require('esbuild-sass-plugin').sassPlugin({ }));
}
// EntryPoint JavaScript
if (options.bundleJs) {
entryPoints['js/newshub.min'] = 'resources/ts/theme.ts';
}
// Create esbuild context
const context = await esbuild.context({
bundle: true,
entryPoints,
external: [
'*.gif',
'*.jpg',
'*.jpeg',
'*.png',
'*.svg',
'*.woff',
'*.woff2',
'/resources/ts/vendors/*'
],
format: 'iife',
outdir: 'assets',
globalName: 'NewsHub',
minify: true,
sourcemap: true,
target: "es2019",
plugins
});
// Bundle or Watch
if (options.watch) {
await context.watch();
} else {
await context.rebuild();
process.exit(0);
}
}
//-- ----------------------------------------
// esbuild Configuration - Vendors
//-- ----------------------------------------
async function vendors(options = {}) {
const entryPoints = {};
const plugins = [reporterPlugin()];
// Parse Vendors
fs.readdirSync(
path.join(process.cwd(), 'resources', 'ts', 'vendors')
).forEach(file => {
let basename = file.slice(0, file.lastIndexOf('.'));
entryPoints[`${basename}.min`] = `resources/ts/vendors/${file}`;
});
// Create esbuild context
const context = await esbuild.context({
bundle: true,
entryPoints,
external: [],
format: 'iife',
keepNames: true,
outdir: 'assets/js/vendors',
minify: true,
sourcemap: true,
target: "es2019",
plugins
});
// Bundle & Exit
await context.rebuild();
process.exit(0);
}
//-- ----------------------------------------
// Parse CLI Options
//-- ----------------------------------------
function parse(args) {
const options = {
bundleCss: true,
bundleJs: true,
include: null,
vendors: false,
watch: false
};
while (args.length > 0) {
let arg = args.shift();
if (arg === '--no-css') {
options.bundleCss = false;
} else if (arg === '--no-js') {
options.bundleJs = false;
} else if (arg === '--vendors') {
options.vendors = true;
} else if (arg === '--watch' || args === '-w') {
options.watch = true;
} else if (arg.indexOf('--folder') === 0) {
let folder = null;
if (arg === '--folder' && args[0].indexOf('--') < 0) {
folder = args.shift();
} else if (arg.length > 8 && arg[8] === '=') {
folder = arg.slice(9)
} else {
throw new Error(`Invalid CLI arguments / syntax near part "${arg}"!`);
}
if (folder) {
if (options.include === null) {
options.include = [];
}
options.include.push(folder);
}
}
}
return options;
}
//-- ----------------------------------------
// Main CLI Handler
//-- ----------------------------------------
function main() {
const args = parse(process.argv.slice(2));
if (args.vendors) {
vendors(args);
} else {
assets(args);
}
}
main();