-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathesbuild.mjs
More file actions
111 lines (95 loc) · 3.25 KB
/
esbuild.mjs
File metadata and controls
111 lines (95 loc) · 3.25 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
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
import path from "path"
import esbuild from "esbuild"
import browserslist from "browserslist"
import { resolveToEsbuildTarget } from "esbuild-plugin-browserslist"
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
const PROVIDER_CONTENTS = `
import nodeCreateHash from "create-hash"
import {
createCipheriv as nodeCreateCipheriv,
createDecipheriv as nodeCreateDecipheriv,
} from "browserify-cipher"
export const createHash = (algo) => {
const hash = nodeCreateHash(algo)
if (!hash) throw new Error("Cannot create hash for algorithm '" + algo + "'")
return hash
}
export const createCipheriv = (algo, key, iv) => {
const cipher = nodeCreateCipheriv(algo, key, iv)
if (!cipher) throw new Error("Cannot create cipher for algorithm '" + algo + "'")
return cipher
}
export const createDecipheriv = (algo, key, iv) => {
const decipher = nodeCreateDecipheriv(algo, key, iv)
if (!decipher) throw new Error("Cannot create decipher for algorithm '" + algo + "'")
return decipher
}
export default {
createHash,
createCipheriv,
createDecipheriv,
}
`
const INDEX_CONTENTS = `
// make Buffer polyfill available
globalThis.Buffer = Buffer;
// re-export TeaVM bundle for CryptoBrief
import { init, setLocale, execute } from "./target/teavm/cryptobrief-web.js"
export { init, setLocale, execute }
export default { init, setLocale, execute }
import { LOG_LEVEL } from "./target/teavm/cryptobrief-web.js"
export const LOG_LEVEL_RESULT = LOG_LEVEL.RESULT;
export const LOG_LEVEL_ERROR = LOG_LEVEL.ERROR;
export const LOG_LEVEL_WARNING = LOG_LEVEL.WARNING;
`
function inlineFilePlugin(contents, filename, extension = "js") {
return {
name: "inlineFilePlugin " + filename + "." + extension,
setup(build) {
const resolveRe = new RegExp(`^${filename}\.${extension}$`)
const pathRe = new RegExp(`(?:.*/|^)${filename}(?:\.${extension})?$`)
build.onResolve({ filter: resolveRe }, (args) => {
// dummy path in the current root for inline file,
// mark as inline for content insertion
return {
path: path.resolve(args.resolveDir, `./${filename}.${extension}`),
pluginData: Object.assign(args.pluginData || {}, {
isInline: true,
}),
}
})
build.onLoad({ filter: pathRe }, (args) => {
// only load inline contents, if file is marked as such
if (!args.pluginData?.isInline) return
return { contents, loader: 'js' }
})
}
}
}
console.log("Generate CommonJS dist/cryptobrief-web.js ...")
const target = resolveToEsbuildTarget(browserslist(), {
printUnknownTargets: false,
});
await esbuild.build({
entryPoints: ["index.mjs"],
outfile: "dist/cryptobrief-web.js",
bundle: true,
minify: true,
sourcemap: "linked",
format: "esm",
target: target,
plugins: [
// inline entrypoint
inlineFilePlugin(INDEX_CONTENTS, "index", "mjs"),
// crypto API wrapper
inlineFilePlugin(PROVIDER_CONTENTS, "cryptoProvider", "mjs"),
// polyfills for the crypto API
NodeModulesPolyfillPlugin(),
NodeGlobalsPolyfillPlugin({
buffer: true,
process: false,
}),
]
})
console.log("⚡Done! ⚡")