-
Notifications
You must be signed in to change notification settings - Fork 2
/
esbuild.js
executable file
·193 lines (171 loc) · 4.87 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
188
189
190
191
192
193
import esbuild from 'esbuild';
import yargs from 'yargs';
import { globbySync } from 'globby';
import { glslify } from 'esbuild-plugin-glslify';
import path from 'path';
import copy from 'copy';
import server from 'live-server';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function liveServer(options = {}) {
const defaultParams = {
file: 'index.html',
host: '0.0.0.0',
logLevel: 2,
open: false,
port: 8080,
root: '.',
wait: 200,
};
const params = Object.assign({}, defaultParams, options);
let running = false;
return {
start() {
if (!running) {
running = true;
server.start(params);
console.log(`live-server running on ${params.port}`);
}
},
};
}
function pluginIgnoreImports(options) {
const config = Object.assign({
ignoreFilter: /.*/,
allowFilePatterns: [ /.(?:glsl|css)$/ ],
allowFolderPatterns: [],
}, options);
function shouldAllow(args) {
if (args.kind === 'entry-point') {
return true;
}
for (const pattern of config.allowFolderPatterns) {
if (args.resolveDir.match(pattern)) {
return true;
}
}
for (const pattern of config.allowFilePatterns) {
if (args.path.match(pattern)) {
return true;
}
}
return false;
}
return {
name: 'ignore-imports',
setup(build) {
build.onResolve({ filter: config.ignoreFilter }, args => {
if (!shouldAllow(args)) {
return {
path: args.path,
external: true,
};
}
return undefined;
});
}
}
}
function getExamplesBuild(watch) {
return {
entryPoints: [ 'examples/src/mod.ts' ],
bundle: true,
outdir: 'build/examples/examples',
target: 'es2019',
format: 'esm',
sourcemap: true,
watch: Boolean(watch),
plugins: [
glslify({
transform: ['glslify-import'],
}),
],
};
}
function getLibBuild(watch) {
const input = [];
globbySync([
path.join('src/', '/**/*.{ts,js}'),
`!${path.join('src/', '/**/*.d.ts')}`,
]).forEach(file => {
input.push(file);
});
return {
entryPoints: input,
bundle: true,
outdir: 'build/lib/',
target: 'es2019',
format: 'esm',
sourcemap: true,
watch: Boolean(watch),
plugins: [
pluginIgnoreImports(),
glslify({
transform: ['glslify-import'],
}),
],
};
}
function getDistBuild(watch) {
return {
entryPoints: [ 'src/mod.ts' ],
bundle: true,
outdir: 'build/dist/',
target: 'es2019',
format: 'esm',
sourcemap: false,
minify: true,
watch: Boolean(watch),
plugins: [
glslify({
transform: ['glslify-import'],
}),
],
};
}
async function main(options) {
const promises = [];
try {
if (options.examples || options.all) {
promises.push(esbuild.build(getExamplesBuild(options.watch)));
}
if (options.lib || options.all) {
promises.push(esbuild.build(getLibBuild(options.watch)));
}
if (options.dist || options.all) {
promises.push(esbuild.build(getDistBuild(options.watch)));
}
await Promise.all(promises);
if (options.examples) {
copy('examples/static/**/*', 'build/examples/', function(err) {
if (err) {
// sorry future Dario, this is going to be an issue one day, you should throw!
console.error(err);
}
});
}
if (options['dev-server']) {
const server = liveServer({
port: 8090,
host: '0.0.0.0',
root: path.resolve(__dirname, 'examples/static/'),
file: 'index.html',
open: false,
wait: 500,
// proxy: [['/api', 'http://127.0.0.1:8080']], // not needed for now, used to proxy to the server API
watch: [
path.resolve(__dirname, 'examples/static'),
path.resolve(__dirname, 'build/examples/'),
],
mount: [
['/examples', path.resolve(__dirname, 'build/examples/examples')],
],
});
server.start();
}
} catch (e) {
console.error(e);
process.exit(1);
}
}
main(yargs(process.argv).argv);