-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjustfile
466 lines (399 loc) · 14.5 KB
/
justfile
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
export PATH := env_var("PATH") + ":" + justfile_directory() + "/node_modules/.bin"
set export := true
_help:
@just --list
prepare:
#!/bin/bash
set -eou pipefail
if ! &>/dev/null which deno; then
>&2 echo 'Deno not found. Please install it using the steps described here: https://docs.deno.com/runtime/manual/getting_started/installation'
exit 1
fi
if ! &>/dev/null which node; then
>&2 echo 'Node not found. Please install the appropriate LTS package from here: https://nodejs.org/'
exit 1
fi
if ! &>/dev/null which jq; then
>&2 echo 'jq not found. Please install jq (https://jqlang.github.io/jq/) using your favorite package manager.'
exit 1
fi
if [ ! -e node_modules ]; then
npm ci
fi
playwright install --with-deps
_build out args='[]': prepare
#!/bin/bash
set -eou pipefail
if [ -e dist/{{ out }} ]; then
rm -rf dist/{{ out }}
fi
node <<EOF
const { build } = require("esbuild");
const path = require("path");
const args = [{
outdir: "dist/{{ out }}",
bundle: true,
minify: true,
drop: [],
}, ...$args];
const accValue = key => (acc, xs) => [...(acc[key] ?? []), ...(xs[key] ?? [])]
const combValue = key => (acc, xs) => ({...(acc[key] ?? {}), ...(xs[key] ?? {})})
const lastValue = key => (acc, xs) => xs[key] ?? acc[key]
const combine = {
entryPoints: accValue('entryPoints'),
external: accValue('external'),
alias: combValue('alias'),
polyfills: combValue('polyfills'),
define: combValue('define'),
sourcemap: lastValue('sourcemap'),
}
const resolved = args.reduce((acc, xs) => {
for (var key in xs) {
const combinator = combine[key] || lastValue(key);
acc[key] = combinator(acc, xs);
}
return acc;
}, {})
const { polyfills, ...config } = resolved;
config.plugins = [{
name: 'resolve',
setup (build) {
const items = Object.keys(polyfills).map(xs => xs.replace(/\./g, '\\.').replace(/\//g, '\\/'));
build.onResolve({ namespace: 'file', filter: /^\..*\.ts\$/g }, async args => {
if (!args.path.startsWith('.')) {
return { path: args.path, external: true }
}
const resolved = path.resolve(args.resolveDir, args.path)
const replaced = resolved.replace(process.cwd(), '.').replaceAll(path.sep, path.posix.sep)
if (!(replaced in polyfills)) {
return { path: path.resolve(args.resolveDir, args.path) }
}
const result = polyfills[replaced]
if (result[0] === '.') {
return { path: path.resolve(result) }
}
return { path: result, external: true }
})
}
}];
if (config.platform === 'browser' && config.outdir.startsWith('dist/tests/')) {
config.plugins = [].concat(config.plugins || [], [
require('esbuild-node-builtin').nodeBuiltin()
]);
}
build({
...config
});
EOF
# bsd sed vs gnu sed: the former "-i" REQUIRES an argument, the latter
# REQUIRES not having an argument
find "dist/{{ out }}" -name '*.js' | if [ $(uname) == 'Darwin' ]; then
xargs -I{} sed -i '' -e '/^((ex|im)port|} from)/s/.ts"/.js"/g' '{}'
else
xargs -I{} sed -i -e '/^((ex|im)port|} from)/s/.ts"/.js"/g' '{}'
fi
# build types (TODO: switch module target based on incoming args)
tsc --emitDeclarationOnly --project ./tsconfig.json --declaration --outDir dist/{{ out }}
build_worker out args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": false,
"entryPoints": ["src/worker.ts"],
"bundle": true,
"minify": true,
"format": "esm"
}] + .
')"
just _build {{ out }} "$config"
if [ $(uname) == 'Darwin' ]; then
flag="-b"
else
flag="-w"
fi
echo "export const WORKER_URL = new URL($(
<dist/{{ out }}/worker.js base64 ${flag} 0 |
jq -cMRS '"data:text/javascript;base64," + .'
));" > dist/{{ out }}/worker-url.ts
build_worker_node out='worker/node' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"platform": "node",
"polyfills": {
"./src/polyfills/deno-capabilities.ts": "./src/polyfills/node-capabilities.ts",
"./src/polyfills/deno-minimatch.ts": "./src/polyfills/node-minimatch.ts",
"./src/polyfills/node-fs.ts": "node:fs/promises",
"./src/polyfills/deno-wasi.ts": "./src/polyfills/node-wasi.ts",
}
}] + .
')"
just build_worker {{ out }} "$config"
build_worker_browser out='worker/browser' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"format": "esm",
"alias": {
"node:worker_threads": "./src/polyfills/worker-node-worker_threads.ts"
},
"polyfills": {
"./src/polyfills/deno-capabilities.ts": "./src/polyfills/browser-capabilities.ts",
"./src/polyfills/deno-minimatch.ts": "./src/polyfills/node-minimatch.ts",
"./src/polyfills/node-fs.ts": "./src/polyfills/browser-fs.ts",
"./src/polyfills/deno-wasi.ts": "./src/polyfills/browser-wasi.ts",
}
}] + .
')"
just build_worker {{ out }} "$config"
build_node_cjs out='cjs' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"entryPoints": ["src/mod.ts"],
"platform": "node",
"minify": false,
"polyfills": {
"./src/polyfills/deno-capabilities.ts": "./src/polyfills/node-capabilities.ts",
"./src/polyfills/deno-minimatch.ts": "./src/polyfills/node-minimatch.ts",
"./src/worker-url.ts": "./dist/worker/node/worker-url.ts",
"./src/polyfills/node-fs.ts": "node:fs/promises",
"./src/polyfills/deno-wasi.ts": "./src/polyfills/node-wasi.ts",
},
"define": {
"import.meta.url": "__filename"
}
}] + .
')"
just _build {{ out }} "$config"
echo '{"type":"commonjs"}' > dist/{{ out }}/package.json
cat > dist/{{ out }}/index.js <<EOF
const mod = require('./mod.js')
module.exports = Object.assign(mod.default, mod)
EOF
cat > dist/{{ out }}/index.d.ts <<EOF
export * from './mod.d.ts'
EOF
build_node_esm out='esm' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"entryPoints": ["src/mod.ts"],
"platform": "node",
"format": "esm",
"minify": false,
"polyfills": {
"./src/polyfills/deno-capabilities.ts": "./src/polyfills/node-capabilities.ts",
"./src/polyfills/deno-minimatch.ts": "./src/polyfills/node-minimatch.ts",
"./src/worker-url.ts": "./dist/worker/node/worker-url.ts",
"./src/polyfills/node-fs.ts": "node:fs/promises",
"./src/polyfills/deno-wasi.ts": "./src/polyfills/node-wasi.ts",
}
}] + .
')"
just _build {{ out }} "$config"
echo '{"type":"module"}' > dist/{{ out }}/package.json
build_bun out='bun' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"entryPoints": ["src/mod.ts", "src/worker.ts"],
"platform": "node",
"format": "esm",
"minify": false,
"polyfills": {
"./src/worker-url.ts": "./src/polyfills/bun-worker-url.ts",
"./src/polyfills/response-to-module.ts": "./src/polyfills/bun-response-to-module.ts",
"./src/polyfills/deno-minimatch.ts": "./src/polyfills/node-minimatch.ts",
"./src/polyfills/deno-capabilities.ts": "./src/polyfills/bun-capabilities.ts",
"./src/polyfills/node-fs.ts": "node:fs/promises",
"./src/polyfills/deno-wasi.ts": "./src/polyfills/node-wasi.ts",
}
}] + .
')"
just _build {{ out }} "$config"
echo '{"type":"module"}' > dist/{{ out }}/package.json
build_browser out='browser' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"sourcemap": true,
"entryPoints": ["src/mod.ts"],
"platform": "browser",
"define": {"global": "globalThis"},
"format": "esm",
"alias": {
"node:worker_threads": "./src/polyfills/host-node-worker_threads.ts"
},
"polyfills": {
"./src/polyfills/deno-capabilities.ts": "./src/polyfills/browser-capabilities.ts",
"./src/polyfills/deno-minimatch.ts": "./src/polyfills/node-minimatch.ts",
"./src/polyfills/node-fs.ts": "./src/polyfills/browser-fs.ts",
"./src/worker-url.ts": "./dist/worker/browser/worker-url.ts",
"./src/polyfills/deno-wasi.ts": "./src/polyfills/browser-wasi.ts",
}
}] + .
')"
just _build {{ out }} "$config"
echo '{"type":"module"}' > dist/{{ out }}/package.json
_build_node_tests:
just build_node_cjs 'tests/cjs' '[{"minify": false, "entryPoints":["src/mod.test.ts"]}]'
just build_node_esm 'tests/esm' '[{"minify": false, "entryPoints":["src/mod.test.ts"]}]'
_build_bun_tests:
just build_bun 'tests/bun' '[{"minify": false, "entryPoints":["src/mod.test.ts"], "alias": {"node:test": "tape"}}]'
_build_browser_tests out='tests/browser' args='[]':
#!/bin/bash
config="$(<<<'{{ args }}' jq -cM '
[{
"entryPoints": ["src/mod.test.ts"],
"alias": {
"node:test": "tape"
},
"minify": false
}] + .
')"
just build_browser {{ out }} "$config"
echo '{"type":"module"}' > dist/{{ out }}/package.json
echo '<html><script type="module" src="/dist/{{ out }}/mod.test.js"></script></html>' > dist/{{ out }}/index.html
build: prepare build_worker_node build_worker_browser build_browser build_node_esm build_node_cjs build_bun _build_browser_tests _build_node_tests _build_bun_tests
npm pack --pack-destination dist/
_test filter='.*':
#!/bin/bash
set -eou pipefail
just serve 8124 false &
cleanup() {
&>/dev/null curl http://localhost:8124/quit
}
trap cleanup EXIT
trap cleanup ERR
case "$(uname -s)" in
[dD]arwin)
os=macos
;;
[lL]inux*)
os=linux
;;
*)
os=windows
;;
esac
if [ "$os" = "windows" ]; then
browsers=chromium
else
browsers=all
fi
sleep 0.1
if [[ "deno" =~ {{ filter }} ]]; then deno test -A src/mod.test.ts; fi
if [[ "node-cjs" =~ {{ filter }} ]]; then node --no-warnings --test --experimental-global-webcrypto dist/tests/cjs/*.test.js; fi
if [[ "node-esm" =~ {{ filter }} ]]; then node --no-warnings --test --experimental-global-webcrypto dist/tests/esm/*.test.js; fi
if [[ "bun" =~ {{ filter }} ]]; then if &>/dev/null which bun; then bun run dist/tests/bun/*.test.js; fi; fi
if [[ "browsers" =~ {{ filter }} ]]; then playwright test --browser $browsers tests/playwright.test.js --trace retain-on-failure; fi
test: build && _test test-artifacts
bake filter='.*':
while just _test '{{ filter }}'; do true; done
test-artifacts:
#!/bin/bash
set -eou pipefail
rm -rf tests/artifacts
mkdir -p tests/artifacts
cd tests/artifacts
npm init --yes
npm i ../../dist/extism*.tgz
node --no-warnings <<EOF
const assert = require('assert')
const extism = require('@extism/extism')
assert(typeof extism === 'function')
async function main() {
const plugin = await extism('../../wasm/hello.wasm')
try {
const text = new TextDecoder().decode(
await plugin.call('run_test', 'this is a test')
)
assert.equal(text, 'Hello, world!')
} finally {
await plugin.close()
}
}
main().catch(err => {
console.error(err)
process.exit(1)
})
EOF
cat >./index.js <<EOF
import extism from '@extism/extism'
import assert from 'node:assert'
const plugin = await extism('../../wasm/hello.wasm')
try {
const text = new TextDecoder().decode(
await plugin.call('run_test', 'this is a test')
)
assert.equal(text, 'Hello, world!')
} finally {
await plugin.close()
}
EOF
node --input-type=module --no-warnings <index.js
# if &>/dev/null which bun; then bun run index.js; fi
lint *args:
eslint src tests examples $args
format:
prettier --write src/*.ts src/**/*.ts examples/*
docs:
#!/bin/bash
typedoc src/mod.ts
serve-docs: docs
python3 -m http.server 8000 -d docs/
watch-docs: prepare
watchexec -r -w types -w src -w README.md just serve-docs
bench-deno: prepare
(cd benches/deno; deno bench -A --no-check main.ts)
bench-node: build
(cd benches/node; npm ci; node --no-warnings main.js)
bench-bun: build
(cd benches/node; npm ci; bun main.js)
bench: bench-deno bench-node bench-bun
serve port='8124' logs='true':
#!/usr/bin/env node
const http = require('http')
const fs = require('fs/promises');
const path = require('path');
const server = http.createServer().listen({{ port }}, console.log);
server.on('request', async (req, res) => {
let statusCode = 200
const headers = {
'Access-Control-Allow-Origin': '*',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin'
}
const url = new URL(req.url, 'http://localhost:{{port}}');
if (url.pathname === '/quit') {
server.close()
}
let body = await fs.readFile(url.pathname.slice(1)).catch(err => {
if (err.code === 'EISDIR') {
url.pathname = path.join(url.pathname, 'index.html')
return fs.readFile(url.pathname.slice(1))
}
return null
}).catch(() => null);
if (!body) {
headers['content-type'] = 'text/html'
statusCode = 404
body = '<html>not here sorry</html>'
} else switch (path.extname(url.pathname)) {
case '.html': headers['content-type'] = 'text/html'; break
case '.wasm': headers['content-type'] = 'application/wasm'; break
case '.json': headers['content-type'] = 'application/json'; break
case '.js': headers['content-type'] = 'text/javascript'; break
}
if ({{ logs }}) {
console.log(statusCode, url.pathname, body.length)
}
headers['content-length'] = body.length
res.writeHead(statusCode, headers);
res.end(body);
});