|
1 |
| -import * as esbuild from "esbuild"; |
2 |
| -import * as process from "process"; |
3 |
| -import { readFileSync } from "fs"; |
4 |
| -import { umdWrapper } from "esbuild-plugin-umd-wrapper"; |
5 |
| -import yargs from "yargs"; |
6 |
| -import { hideBin } from "yargs/helpers"; |
| 1 | +import { writeFileSync } from "fs"; |
| 2 | +import { buildWatch, isProduction } from "@hpcc-js/esbuild-plugins"; |
7 | 3 |
|
8 |
| -const myYargs = yargs(hideBin(process.argv)); |
9 |
| -myYargs |
10 |
| - .usage("Usage: node esbuild.mjs [options]") |
11 |
| - .demandCommand(0, 0) |
12 |
| - .example("node esbuild.mjs --watch", "Bundle and watch for changes") |
13 |
| - .option("mode", { |
14 |
| - alias: "m", |
15 |
| - describe: "Build mode", |
16 |
| - choices: ["development", "production"], |
17 |
| - default: "production" |
18 |
| - }) |
19 |
| - .option("w", { |
20 |
| - alias: "watch", |
21 |
| - describe: "Watch for changes", |
22 |
| - type: "boolean" |
23 |
| - }) |
24 |
| - .help("h") |
25 |
| - .alias("h", "help") |
26 |
| - .epilog("https://github.com/hpcc-systems/hpcc-js-wasm") |
27 |
| - ; |
28 |
| -const argv = await myYargs.argv; |
29 |
| -const isDevelopment = argv.mode === "development"; |
30 |
| -const isProduction = !isDevelopment; |
31 |
| -const isWatch = argv.watch; |
32 |
| - |
33 |
| -// plugins --- |
34 |
| -const excludeSourceMapPlugin = ({ filter }) => ({ |
35 |
| - name: 'excludeSourceMapPlugin', |
36 |
| - |
37 |
| - setup(build) { |
38 |
| - build.onLoad({ filter }, (args) => { |
39 |
| - return { |
40 |
| - contents: |
41 |
| - readFileSync(args.path, 'utf8') + |
42 |
| - '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==', |
43 |
| - loader: 'default', |
44 |
| - }; |
45 |
| - }); |
46 |
| - }, |
47 |
| -}); |
48 |
| - |
49 |
| -const esbuildProblemMatcherPlugin = ({ filter }) => ({ |
50 |
| - name: "esbuild-problem-matcher", |
51 |
| - |
52 |
| - setup(build) { |
53 |
| - build.onStart(() => { |
54 |
| - console.log("[watch] build started"); |
55 |
| - }); |
56 |
| - build.onEnd((result) => { |
57 |
| - result.errors.forEach(({ text, location }) => { |
58 |
| - console.error(`✘ [ERROR] ${text}`); |
59 |
| - console.error(` ${location.file}:${location.line}:${location.column}:`); |
60 |
| - }); |
61 |
| - console.log("[watch] build finished"); |
62 |
| - }); |
63 |
| - }, |
64 |
| -}); |
65 |
| - |
66 |
| -// helpers --- |
67 |
| -function build(config) { |
68 |
| - isDevelopment && console.log("Start: ", config.entryPoints[0], config.outfile); |
69 |
| - return esbuild.build({ |
70 |
| - ...config, |
| 4 | +export function nodeTpl(input, output, format = "esm", external = []) { |
| 5 | + return buildWatch({ |
| 6 | + entryPoints: [input], |
| 7 | + outfile: `${output}.js`, |
| 8 | + platform: "node", |
| 9 | + target: "node20", |
| 10 | + format, |
| 11 | + bundle: true, |
| 12 | + minify: isProduction, |
71 | 13 | sourcemap: "linked",
|
72 |
| - plugins: [ |
73 |
| - ...config.plugins ?? [], |
74 |
| - ] |
75 |
| - }).then(() => { |
76 |
| - isDevelopment && console.log("Stop: ", config.entryPoints[0], config.outfile); |
| 14 | + external |
77 | 15 | });
|
78 | 16 | }
|
79 | 17 |
|
80 |
| -function watch(config) { |
81 |
| - return esbuild.context({ |
82 |
| - ...config, |
83 |
| - sourcemap: "linked", |
84 |
| - plugins: [ |
85 |
| - ...config.plugins ?? [], |
86 |
| - { |
87 |
| - name: "rebuild-notify", |
88 |
| - setup(build) { |
89 |
| - build.onEnd(result => { |
90 |
| - console.log(`Built ${config.outfile}`); |
91 |
| - }); |
92 |
| - }, |
93 |
| - } |
94 |
| - ] |
95 |
| - }).then(ctx => { |
96 |
| - return ctx.watch(); |
97 |
| - }); |
| 18 | +function replaceAfterTransform() { |
| 19 | + return { |
| 20 | + name: "replace-after-transform", |
| 21 | + setup(build) { |
| 22 | + build.onEnd((result) => { |
| 23 | + result?.outputFiles?.forEach(file => { |
| 24 | + if (file.path.endsWith(".js")) { |
| 25 | + const contents = file.text.replace(/"use strict";/g, ""); |
| 26 | + if (contents.indexOf("use strict") >= 0) { |
| 27 | + console.error("use strict: ", file.path); |
| 28 | + } |
| 29 | + writeFileSync(file.path, contents, { encoding: "utf8" }); |
| 30 | + } else { |
| 31 | + writeFileSync(file.path, file.contents, { encoding: "binary" }); |
| 32 | + } |
| 33 | + }); |
| 34 | + }); |
| 35 | + } |
| 36 | + }; |
98 | 37 | }
|
99 | 38 |
|
100 |
| -function browserTpl(input, output, format, globalName = "", external = []) { |
101 |
| - return (isWatch ? watch : build)({ |
| 39 | +export function browserTpl(input, output, format = "esm", globalName = undefined, external = []) { |
| 40 | + return buildWatch({ |
102 | 41 | entryPoints: [input],
|
103 |
| - outfile: `${output}.${format === "esm" ? "js" : "umd.js"}`, |
| 42 | + outfile: `${output}.js`, |
104 | 43 | platform: "browser",
|
105 | 44 | target: "es2022",
|
106 | 45 | format,
|
107 | 46 | globalName,
|
108 | 47 | bundle: true,
|
109 | 48 | minify: isProduction,
|
| 49 | + sourcemap: "linked", |
110 | 50 | external,
|
111 |
| - plugins: format === "umd" ? [umdWrapper()] : [] |
| 51 | + write: false, |
| 52 | + plugins: [replaceAfterTransform()], |
112 | 53 | });
|
113 | 54 | }
|
114 | 55 |
|
115 |
| -function browserBoth(input, output, globalName = undefined, external = []) { |
116 |
| - return Promise.all([ |
117 |
| - browserTpl(input, output, "esm", globalName, external), |
118 |
| - browserTpl(input, output, "umd", globalName, external) |
119 |
| - ]); |
120 |
| -} |
121 |
| - |
122 |
| -function nodeTpl(input, output, format, external = []) { |
123 |
| - return (isWatch ? watch : build)({ |
124 |
| - entryPoints: [input], |
125 |
| - outfile: `${output}.${format === "esm" ? "mjs" : "js"}`, |
126 |
| - platform: "node", |
127 |
| - target: "node20", |
128 |
| - format, |
129 |
| - bundle: true, |
130 |
| - minify: isProduction, |
131 |
| - external |
132 |
| - }); |
133 |
| -} |
134 |
| - |
135 |
| -function nodeBoth(input, output, external = []) { |
136 |
| - return Promise.all([ |
137 |
| - nodeTpl(input, output, "esm", external), |
138 |
| - nodeTpl(input, output, "cjs", external) |
139 |
| - ]); |
140 |
| -} |
141 |
| - |
142 |
| -function bothTpl(input, output, globalName = undefined, external = []) { |
143 |
| - return Promise.all([ |
144 |
| - browserBoth(input, output, globalName, external), |
145 |
| - nodeTpl(input, output, "cjs", external) |
146 |
| - ]); |
147 |
| -} |
148 |
| - |
149 | 56 | // config ---
|
150 | 57 | nodeTpl("src/extension.ts", "dist/extension", "cjs", ["vscode"]);
|
151 |
| -nodeTpl("src/notebook/renderers/wuRenderer.tsx", "dist/notebook/renderers", "cjs", ["vscode", "fs", "path", "os"]); |
152 |
| -nodeTpl("src/notebook/renderers/ojsRenderer.ts", "dist/notebook/renderers", "cjs", ["vscode", "fs", "path", "os"]); |
153 |
| - |
| 58 | +nodeTpl("src/notebook/renderers/wuRenderer.tsx", "dist/notebook/renderers/wuRenderer", "cjs", ["vscode", "fs", "path", "os"]); |
| 59 | +nodeTpl("src/notebook/renderers/ojsRenderer.ts", "dist/notebook/renderers/ojsRenderer", "cjs", ["vscode", "fs", "path", "os"]); |
154 | 60 | nodeTpl("src/debugger.ts", "dist/debugger", "cjs", ["vscode"]);
|
155 | 61 |
|
156 |
| -browserTpl("src/eclwatch.tsx", "dist/eclwatch", "esm", undefined, ["vscode", "fs", "path", "os"]); |
157 |
| -browserTpl("src/web-extension.ts", "dist-web/extension.js", "esm", undefined, ["vscode", "fs", "path", "os"]); |
| 62 | +const ctx = await browserTpl("src/eclwatch.tsx", "dist/eclwatch", "iife"); |
| 63 | +// if (isServe) { |
| 64 | +// ctx.serve({ servedir: "." }).then(({ host, port }) => { |
| 65 | +// console.log(`http://localhost:${port}`); |
| 66 | +// }); |
| 67 | +// } |
| 68 | +// browserTpl("src/web-extension.ts", "dist-web/extension", "esm", undefined, ["vscode", "fs", "path", "os"]); |
| 69 | + |
0 commit comments