Skip to content

Commit 5a55352

Browse files
authored
Combine esbuild scripts (microsoft#184531)
* Combine esbuild scripts This combines the various build scripts used for building webview/notebook content. This should make it easier to update settings for them As part of this, I also fixed the script so that on watch it restarts automatically on syntax errors instead of exiting * Migrate other build script * Fixing math build script
1 parent 12533c5 commit 5a55352

File tree

7 files changed

+147
-224
lines changed

7 files changed

+147
-224
lines changed

extensions/esbuild-webview-common.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
// @ts-check
6+
7+
/**
8+
* @fileoverview Common build script for extension scripts used in in webviews.
9+
*/
10+
11+
const path = require('path');
12+
const esbuild = require('esbuild');
13+
14+
/**
15+
* @typedef {Partial<import('esbuild').BuildOptions> & {
16+
* entryPoints: string[] | Record<string, string> | { in: string, out: string }[];
17+
* outdir: string;
18+
* }} BuildOptions
19+
*/
20+
21+
/**
22+
* Build the source code once using esbuild.
23+
*
24+
* @param {BuildOptions} options
25+
* @param {(outDir: string) => unknown} [didBuild]
26+
*/
27+
async function build(options, didBuild) {
28+
await esbuild.build({
29+
bundle: true,
30+
minify: true,
31+
sourcemap: false,
32+
format: 'esm',
33+
platform: 'browser',
34+
target: ['es2020'],
35+
...options,
36+
});
37+
38+
await didBuild?.(options.outdir);
39+
}
40+
41+
/**
42+
* Build the source code once using esbuild, logging errors instead of throwing.
43+
*
44+
* @param {BuildOptions} options
45+
* @param {(outDir: string) => unknown} [didBuild]
46+
*/
47+
async function tryBuild(options, didBuild) {
48+
try {
49+
await build(options, didBuild);
50+
} catch (err) {
51+
console.error(err);
52+
}
53+
}
54+
55+
/**
56+
* @param {{
57+
* srcDir: string;
58+
* outdir: string;
59+
* entryPoints: string[] | Record<string, string> | { in: string, out: string }[];
60+
* additionalOptions?: Partial<import('esbuild').BuildOptions>
61+
* }} config
62+
* @param {string[]} args
63+
* @param {(outDir: string) => unknown} [didBuild]
64+
*/
65+
module.exports.run = function (config, args, didBuild) {
66+
let outdir = config.outdir;
67+
68+
const outputRootIndex = args.indexOf('--outputRoot');
69+
if (outputRootIndex >= 0) {
70+
const outputRoot = args[outputRootIndex + 1];
71+
const outputDirName = path.basename(outdir);
72+
outdir = path.join(outputRoot, outputDirName);
73+
}
74+
75+
/** @type {BuildOptions} */
76+
const resolvedOptions = {
77+
entryPoints: config.entryPoints,
78+
outdir,
79+
...(config.additionalOptions || {}),
80+
};
81+
82+
const isWatch = args.indexOf('--watch') >= 0;
83+
if (isWatch) {
84+
tryBuild(resolvedOptions);
85+
86+
const watcher = require('@parcel/watcher');
87+
watcher.subscribe(config.srcDir, () => tryBuild(resolvedOptions, didBuild));
88+
} else {
89+
return build(resolvedOptions, didBuild).catch(() => process.exit(1));
90+
}
91+
};

extensions/ipynb/esbuild.js

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,14 @@
55
//@ts-check
66

77
const path = require('path');
8-
const fse = require('fs-extra');
9-
const esbuild = require('esbuild');
10-
11-
const args = process.argv.slice(2);
12-
13-
const isWatch = args.indexOf('--watch') >= 0;
14-
15-
let outputRoot = __dirname;
16-
const outputRootIndex = args.indexOf('--outputRoot');
17-
if (outputRootIndex >= 0) {
18-
outputRoot = args[outputRootIndex + 1];
19-
}
208

219
const srcDir = path.join(__dirname, 'notebook-src');
22-
const outDir = path.join(outputRoot, 'notebook-out');
23-
24-
async function build() {
25-
await esbuild.build({
26-
entryPoints: [
27-
path.join(srcDir, 'cellAttachmentRenderer.ts'),
28-
],
29-
bundle: true,
30-
minify: false,
31-
sourcemap: false,
32-
format: 'esm',
33-
outdir: outDir,
34-
platform: 'browser',
35-
target: ['es2020'],
36-
});
37-
}
38-
39-
40-
build().catch(() => process.exit(1));
41-
42-
if (isWatch) {
43-
const watcher = require('@parcel/watcher');
44-
watcher.subscribe(srcDir, async () => {
45-
try {
46-
await build();
47-
} catch (e) {
48-
console.error(e);
49-
}
50-
});
51-
}
10+
const outDir = path.join(__dirname, 'notebook-out');
11+
12+
require('../esbuild-webview-common').run({
13+
entryPoints: [
14+
path.join(srcDir, 'cellAttachmentRenderer.ts'),
15+
],
16+
srcDir,
17+
outdir: outDir,
18+
}, process.argv);

extensions/markdown-language-features/esbuild-notebook.js

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,14 @@
44
*--------------------------------------------------------------------------------------------*/
55
// @ts-check
66
const path = require('path');
7-
const esbuild = require('esbuild');
8-
9-
const args = process.argv.slice(2);
10-
11-
const isWatch = args.indexOf('--watch') >= 0;
12-
13-
let outputRoot = __dirname;
14-
const outputRootIndex = args.indexOf('--outputRoot');
15-
if (outputRootIndex >= 0) {
16-
outputRoot = args[outputRootIndex + 1];
17-
}
187

198
const srcDir = path.join(__dirname, 'notebook');
20-
const outDir = path.join(outputRoot, 'notebook-out');
21-
22-
function build() {
23-
return esbuild.build({
24-
entryPoints: [
25-
path.join(__dirname, 'notebook', 'index.ts'),
26-
],
27-
bundle: true,
28-
minify: true,
29-
sourcemap: false,
30-
format: 'esm',
31-
outdir: outDir,
32-
platform: 'browser',
33-
target: ['es2020'],
34-
});
35-
}
36-
37-
38-
build().catch(() => process.exit(1));
39-
40-
if (isWatch) {
41-
const watcher = require('@parcel/watcher');
42-
watcher.subscribe(srcDir, () => {
43-
return build();
44-
});
45-
}
9+
const outDir = path.join(__dirname, 'notebook-out');
10+
11+
require('../esbuild-webview-common').run({
12+
entryPoints: [
13+
path.join(srcDir, 'index.ts'),
14+
],
15+
srcDir,
16+
outdir: outDir,
17+
}, process.argv);

extensions/markdown-language-features/esbuild-preview.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,15 @@
44
*--------------------------------------------------------------------------------------------*/
55
// @ts-check
66
const path = require('path');
7-
const esbuild = require('esbuild');
8-
9-
const args = process.argv.slice(2);
10-
11-
const isWatch = args.indexOf('--watch') >= 0;
12-
13-
let outputRoot = __dirname;
14-
const outputRootIndex = args.indexOf('--outputRoot');
15-
if (outputRootIndex >= 0) {
16-
outputRoot = args[outputRootIndex + 1];
17-
}
187

198
const srcDir = path.join(__dirname, 'preview-src');
20-
const outDir = path.join(outputRoot, 'media');
21-
22-
function build() {
23-
return esbuild.build({
24-
entryPoints: [
25-
path.join(srcDir, 'index.ts'),
26-
path.join(srcDir, 'pre'),
27-
],
28-
bundle: true,
29-
minify: true,
30-
sourcemap: false,
31-
format: 'iife',
32-
outdir: outDir,
33-
platform: 'browser',
34-
target: ['es2020'],
35-
});
36-
}
37-
38-
build().catch(() => process.exit(1));
9+
const outDir = path.join(__dirname, 'media');
3910

40-
if (isWatch) {
41-
const watcher = require('@parcel/watcher');
42-
watcher.subscribe(srcDir, () => {
43-
return build();
44-
});
45-
}
11+
require('../esbuild-webview-common').run({
12+
entryPoints: [
13+
path.join(srcDir, 'index.ts'),
14+
path.join(srcDir, 'pre'),
15+
],
16+
srcDir,
17+
outdir: outDir,
18+
}, process.argv);

extensions/markdown-math/esbuild.js

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,13 @@
66

77
const path = require('path');
88
const fse = require('fs-extra');
9-
const esbuild = require('esbuild');
109

1110
const args = process.argv.slice(2);
1211

13-
const isWatch = args.indexOf('--watch') >= 0;
14-
15-
let outputRoot = __dirname;
16-
const outputRootIndex = args.indexOf('--outputRoot');
17-
if (outputRootIndex >= 0) {
18-
outputRoot = args[outputRootIndex + 1];
19-
}
20-
2112
const srcDir = path.join(__dirname, 'notebook');
22-
const outDir = path.join(outputRoot, 'notebook-out');
23-
24-
async function build() {
25-
await esbuild.build({
26-
entryPoints: [
27-
path.join(srcDir, 'katex.ts'),
28-
],
29-
bundle: true,
30-
minify: true,
31-
sourcemap: false,
32-
format: 'esm',
33-
outdir: outDir,
34-
platform: 'browser',
35-
target: ['es2020'],
36-
});
13+
const outDir = path.join(__dirname, 'notebook-out');
3714

15+
function postBuild(outDir) {
3816
fse.copySync(
3917
path.join(__dirname, 'node_modules', 'katex', 'dist', 'katex.min.css'),
4018
path.join(outDir, 'katex.min.css'));
@@ -51,16 +29,10 @@ async function build() {
5129
}
5230
}
5331

54-
55-
build().catch(() => process.exit(1));
56-
57-
if (isWatch) {
58-
const watcher = require('@parcel/watcher');
59-
watcher.subscribe(srcDir, async () => {
60-
try {
61-
await build();
62-
} catch (e) {
63-
console.error(e);
64-
}
65-
});
66-
}
32+
require('../esbuild-webview-common').run({
33+
entryPoints: [
34+
path.join(srcDir, 'katex.ts'),
35+
],
36+
srcDir,
37+
outdir: outDir,
38+
}, process.argv, postBuild);

extensions/notebook-renderers/esbuild.js

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,14 @@
44
*--------------------------------------------------------------------------------------------*/
55
// @ts-check
66
const path = require('path');
7-
const esbuild = require('esbuild');
8-
9-
const args = process.argv.slice(2);
10-
11-
const isWatch = args.indexOf('--watch') >= 0;
12-
13-
let outputRoot = __dirname;
14-
const outputRootIndex = args.indexOf('--outputRoot');
15-
if (outputRootIndex >= 0) {
16-
outputRoot = args[outputRootIndex + 1];
17-
}
187

198
const srcDir = path.join(__dirname, 'src');
20-
const outDir = path.join(outputRoot, 'renderer-out');
21-
22-
function build() {
23-
return esbuild.build({
24-
entryPoints: [
25-
path.join(srcDir, 'index.ts'),
26-
],
27-
bundle: true,
28-
minify: false,
29-
sourcemap: false,
30-
format: 'esm',
31-
outdir: outDir,
32-
platform: 'browser',
33-
target: ['es2020'],
34-
});
35-
}
36-
37-
build().catch(() => process.exit(1));
9+
const outDir = path.join(__dirname, 'renderer-out');
3810

39-
if (isWatch) {
40-
const watcher = require('@parcel/watcher');
41-
watcher.subscribe(srcDir, () => {
42-
return build();
43-
});
44-
}
11+
require('../esbuild-webview-common').run({
12+
entryPoints: [
13+
path.join(srcDir, 'index.ts'),
14+
],
15+
srcDir,
16+
outdir: outDir,
17+
}, process.argv);

0 commit comments

Comments
 (0)