1- import { writeFileSync } from "fs" ;
2- import { buildWatch , isProduction } from "@hpcc-js/esbuild-plugins" ;
3- import { aliasPath } from "esbuild-plugin-alias-path" ;
1+ import * as esbuild from "esbuild" ;
2+ import copyStaticFiles from "esbuild-copy-static-files" ;
3+ import process from "node:process" ;
4+ import path from "node:path" ;
5+ import { writeFileSync } from "node:fs" ;
46
5- export function nodeTpl ( input , output , format = "esm" , external = [ ] ) {
6- return buildWatch ( {
7- entryPoints : [ input ] ,
8- outfile : `${ output } .js` ,
9- platform : "node" ,
10- target : "node20" ,
11- format,
7+ const outputDirectory = "dist" ;
8+ const production = process . argv . includes ( "--production" ) ;
9+ const watch = process . argv . includes ( "--watch" ) ;
10+
11+ async function main ( entryPoint , platform , format , plugins = [ ] ) {
12+ const ctx = await esbuild . context ( {
13+ entryPoints : [ entryPoint ] ,
14+ outdir : outputDirectory ,
1215 bundle : true ,
13- minify : isProduction ,
14- sourcemap : "linked" ,
15- external
16+ format,
17+ minify : production ,
18+ sourcemap : ! production ? "linked" : false ,
19+ platform,
20+ target : platform === "node" ? "node20" : "es2022" ,
21+ external : [ "vscode" , "fs" , "path" , "os" ] ,
22+ // mainFields: platform === "node" ? ["main", "module"] : undefined, // https://github.com/microsoft/node-jsonc-parser/issues/57
23+ logLevel : production ? "silent" : "info" ,
24+ write : ! plugins . some ( p => p . name === "replace-after-transform" ) ,
25+ plugins : [
26+ ...plugins ,
27+ esbuildProblemMatcherPlugin ,
28+ ] ,
1629 } ) ;
30+ if ( watch ) {
31+ await ctx . watch ( ) ;
32+ } else {
33+ await ctx . rebuild ( ) ;
34+ await ctx . dispose ( ) ;
35+ }
1736}
1837
38+ const esbuildProblemMatcherPlugin = {
39+ name : "esbuild-problem-matcher" ,
40+
41+ setup ( build ) {
42+ build . onStart ( ( ) => {
43+ console . log ( "[watch] build started" ) ;
44+ } ) ;
45+ build . onEnd ( ( result ) => {
46+ result . errors . forEach ( ( { text, location } ) => {
47+ console . error ( `✘ [ERROR] ${ text } ` ) ;
48+ console . error ( ` ${ location . file } :${ location . line } :${ location . column } :` ) ;
49+ } ) ;
50+ console . log ( "[watch] build finished" ) ;
51+ } ) ;
52+ } ,
53+ } ;
54+
1955function replaceAfterTransform ( ) {
2056 return {
2157 name : "replace-after-transform" ,
2258 setup ( build ) {
2359 build . onEnd ( ( result ) => {
60+ console . log ( "replace-after-transform" ) ;
2461 result ?. outputFiles ?. forEach ( file => {
62+ console . log ( file . path ) ;
2563 if ( file . path . endsWith ( ".js" ) ) {
2664 const contents = file . text . replace ( / " u s e s t r i c t " ; / g, "" ) ;
2765 if ( contents . indexOf ( "use strict" ) >= 0 ) {
@@ -37,34 +75,88 @@ function replaceAfterTransform() {
3775 } ;
3876}
3977
40- export function browserTpl ( input , output , format = "esm" , globalName = undefined , external = [ ] ) {
41- return buildWatch ( {
42- entryPoints : [ input ] ,
43- outfile : `${ output } .js` ,
44- platform : "browser" ,
45- target : "es2022" ,
46- format,
47- globalName,
48- bundle : true ,
49- minify : isProduction ,
50- sourcemap : "linked" ,
51- external,
52- write : false ,
53- plugins : [ replaceAfterTransform ( ) ] ,
54- } ) ;
55- }
78+ Promise . all ( [
79+ main ( "./src/extension.ts" , "node" , "cjs" , [
80+ copyStaticFiles ( {
81+ src : "./node_modules/@hpcc-js/ddl-shim/schema/v2.json" ,
82+ dest : path . join ( outputDirectory , "v2.json" ) ,
83+ } )
84+ ] ) ,
85+ main ( "./src/notebook/renderers/wuRenderer.tsx" , "node" , "cjs" ) ,
86+ main ( "./src/notebook/renderers/ojsRenderer.ts" , "node" , "cjs" ) ,
87+ main ( "./src/eclwatch.tsx" , "browser" , "iife" , [ replaceAfterTransform ( ) ] )
88+ ] ) . catch ( ( e ) => {
89+ console . error ( e ) ;
90+ process . exit ( 1 ) ;
91+ } ) ;
92+
93+ // import { writeFileSync } from "fs";
94+ // import { buildWatch, isProduction } from "@hpcc-js/esbuild-plugins";
95+ // import { aliasPath } from "esbuild-plugin-alias-path";
96+
97+ // export function nodeTpl(input, output, format = "esm", external = []) {
98+ // return buildWatch({
99+ // entryPoints: [input],
100+ // outfile: `${output}.js`,
101+ // platform: "node",
102+ // target: "node20",
103+ // format,
104+ // bundle: true,
105+ // minify: isProduction,
106+ // sourcemap: "linked",
107+ // external
108+ // });
109+ // }
56110
57- // config ---
58- nodeTpl ( "src/extension.ts" , "dist/extension" , "cjs" , [ "vscode" , "fs" ] ) ;
59- nodeTpl ( "src/notebook/renderers/wuRenderer.tsx" , "dist/notebook/renderers/wuRenderer" , "cjs" , [ "vscode" , "fs" , "path" , "os" ] ) ;
60- nodeTpl ( "src/notebook/renderers/ojsRenderer.ts" , "dist/notebook/renderers/ojsRenderer" , "cjs" , [ "vscode" , "fs" , "path" , "os" ] ) ;
61- nodeTpl ( "src/debugger.ts" , "dist/debugger" , "cjs" , [ "vscode" ] ) ;
111+ // function replaceAfterTransform() {
112+ // return {
113+ // name: "replace-after-transform",
114+ // setup(build) {
115+ // build.onEnd((result) => {
116+ // result?.outputFiles?.forEach(file => {
117+ // if (file.path.endsWith(".js")) {
118+ // const contents = file.text.replace(/"use strict";/g, "");
119+ // if (contents.indexOf("use strict") >= 0) {
120+ // console.error("use strict: ", file.path);
121+ // }
122+ // writeFileSync(file.path, contents, { encoding: "utf8" });
123+ // } else {
124+ // writeFileSync(file.path, file.contents, { encoding: "binary" });
125+ // }
126+ // });
127+ // });
128+ // }
129+ // };
130+ // }
62131
63- const ctx = await browserTpl ( "src/eclwatch.tsx" , "dist/eclwatch" , "iife" ) ;
64- // if (isServe) {
65- // ctx.serve({ servedir: "." }).then(({ host, port }) => {
66- // console.log(`http://localhost:${port}`);
132+ // export function browserTpl(input, output, format = "esm", globalName = undefined, external = []) {
133+ // return buildWatch({
134+ // entryPoints: [input],
135+ // outfile: `${output}.js`,
136+ // platform: "browser",
137+ // target: "es2022",
138+ // format,
139+ // globalName,
140+ // bundle: true,
141+ // minify: isProduction,
142+ // sourcemap: "linked",
143+ // external,
144+ // write: false,
145+ // plugins: [replaceAfterTransform()],
67146// });
68147// }
69- // browserTpl("src/web-extension.ts", "dist-web/extension", "esm", undefined, ["vscode", "fs", "path", "os"]);
148+
149+ // // config ---
150+ // nodeTpl("src/extension.ts", "dist/extension", "cjs", ["vscode", "fs"]);
151+ // nodeTpl("src/notebook/renderers/wuRenderer.tsx", "dist/notebook/renderers/wuRenderer", "cjs", ["vscode", "fs", "path", "os"]);
152+ // nodeTpl("src/notebook/renderers/ojsRenderer.ts", "dist/notebook/renderers/ojsRenderer", "cjs", ["vscode", "fs", "path", "os"]);
153+ // nodeTpl("src/debugger.ts", "dist/debugger", "cjs", ["vscode"]);
154+
155+ // const ctx = await browserTpl("src/eclwatch.tsx", "dist/eclwatch", "iife");
156+ // // if (isServe) {
157+ // // ctx.serve({ servedir: "." }).then(({ host, port }) => {
158+ // // console.log(`http://localhost:${port}`);
159+ // // });
160+ // // }
161+ // // browserTpl("src/web-extension.ts", "dist-web/extension", "esm", undefined, ["vscode", "fs", "path", "os"]);
70162
0 commit comments