-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__esbuild.mjs
More file actions
85 lines (78 loc) · 2.41 KB
/
__esbuild.mjs
File metadata and controls
85 lines (78 loc) · 2.41 KB
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
import * as esbuild from 'esbuild'
import path from 'node:path'
import pkg from './package.json' assert { type: "json" }
import * as fs from 'node:fs'
const arg = process.argv[2]
console.log(`\n${pkg.name} v${pkg.version}\n`)
/**
* @type {esbuild.Plugin}
*/
const envPlugin = {
name: 'env',
setup(build) {
// Intercept import paths called "env" so esbuild doesn't attempt
// to map them to a file system location. Tag them with the "env-ns"
// namespace to reserve them for this plugin.
build.onResolve({ filter: /^env$/ }, args => ({
path: args.path,
namespace: 'env-ns',
}))
// Load paths tagged with the "env-ns" namespace and behave as if
// they point to a JSON file containing the environment variables.
build.onLoad({ filter: /.*/, namespace: 'env-ns' }, () => ({
contents: JSON.stringify(process.env),
loader: 'json',
}))
},
}
/**
* @type {esbuild.Plugin}
*/
const myPlugin = {
name: 'myPlugin',
setup(build) {
const files = {}
build.onEnd(result => {
const changed = []
// console.log('[onEnd][result]', result)
for (let out of result.outputFiles) {
if (files[out.path] !== out.hash) {
files[out.path] = out.hash
fs.writeFileSync(out.path, out.contents)
const resolvedPath = require.resolve(out.path)
console.log(`[onEnd][changed]`, out.path, out.hash, resolvedPath)
}
}
console.log(`[onEnd]`, `build ended with ${result.errors.length} errors`)
})
},
}
const ctx = await esbuild.context(/** @type {esbuild.CommonOptions} */({
preserveSymlinks: true,
entryPoints: ["./src/*.ts"],
// bundle: true,
//external: ["esbuild"],
format: 'cjs',
sourcemap: false, //'inline',
outdir: './lib',
platform: 'node',
logLevel: "debug",
plugins: [myPlugin, envPlugin],
write: false,
banner: {
js: `// ${pkg.name} v${pkg.version}`
},
define: {
CLAPP_SRC_PATH: `"${path.resolve(process.env.PWD, '../')}"`
}
}))
if (arg === '--watch') {
await ctx.watch()
console.log('Watching...')
} else {
const start = Date.now()
await ctx.rebuild()
const took = Date.now() - start
console.log(`Build done in ${took}ms`)
await ctx.dispose()
}