From e7fffbf1c9169087f1098aedfe54c59c079fa3ac Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 23 Mar 2022 14:48:54 -0700 Subject: [PATCH] Use parcel for watching esbuild build files Seeing small but consistent cpu usage when using esbuild's watcher. Switch to parcel to avoid this --- .../esbuild-notebook.js | 40 ++++++++----- .../esbuild-preview.js | 41 ++++++++----- extensions/markdown-math/esbuild.js | 57 +++++++++++-------- extensions/notebook-renderers/esbuild.js | 39 ++++++++----- extensions/simple-browser/esbuild-preview.js | 51 ++++++++++------- 5 files changed, 142 insertions(+), 86 deletions(-) diff --git a/extensions/markdown-language-features/esbuild-notebook.js b/extensions/markdown-language-features/esbuild-notebook.js index bd5a325438d..82b2451e0e3 100644 --- a/extensions/markdown-language-features/esbuild-notebook.js +++ b/extensions/markdown-language-features/esbuild-notebook.js @@ -2,8 +2,10 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +// @ts-check const path = require('path'); const esbuild = require('esbuild'); +const watcher = require('@parcel/watcher'); const args = process.argv.slice(2); @@ -15,19 +17,29 @@ if (outputRootIndex >= 0) { outputRoot = args[outputRootIndex + 1]; } +const srcDir = path.join(__dirname, 'notebook'); const outDir = path.join(outputRoot, 'notebook-out'); -esbuild.build({ - entryPoints: [ - path.join(__dirname, 'notebook', 'index.ts'), - ], - bundle: true, - minify: true, - sourcemap: false, - format: 'esm', - outdir: outDir, - platform: 'browser', - target: ['es2020'], - watch: isWatch, - incremental: isWatch, -}).catch(() => process.exit(1)); +function build() { + return esbuild.build({ + entryPoints: [ + path.join(__dirname, 'notebook', 'index.ts'), + ], + bundle: true, + minify: true, + sourcemap: false, + format: 'esm', + outdir: outDir, + platform: 'browser', + target: ['es2020'], + }); +} + + +build().catch(() => process.exit(1)); + +if (isWatch) { + watcher.subscribe(srcDir, () => { + return build(); + }); +} diff --git a/extensions/markdown-language-features/esbuild-preview.js b/extensions/markdown-language-features/esbuild-preview.js index 727353c6e44..d570e5a97a3 100644 --- a/extensions/markdown-language-features/esbuild-preview.js +++ b/extensions/markdown-language-features/esbuild-preview.js @@ -2,8 +2,10 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +// @ts-check const path = require('path'); const esbuild = require('esbuild'); +const watcher = require('@parcel/watcher'); const args = process.argv.slice(2); @@ -15,20 +17,29 @@ if (outputRootIndex >= 0) { outputRoot = args[outputRootIndex + 1]; } +const srcDir = path.join(__dirname, 'preview-src'); const outDir = path.join(outputRoot, 'media'); -esbuild.build({ - entryPoints: [ - path.join(__dirname, 'preview-src', 'index.ts'), - path.join(__dirname, 'preview-src', 'pre'), - ], - bundle: true, - minify: true, - sourcemap: false, - format: 'esm', - outdir: outDir, - platform: 'browser', - target: ['es2020'], - watch: isWatch, - incremental: isWatch, -}).catch(() => process.exit(1)); +function build() { + return esbuild.build({ + entryPoints: [ + path.join(srcDir, 'index.ts'), + path.join(srcDir, 'pre'), + ], + bundle: true, + minify: true, + sourcemap: false, + format: 'esm', + outdir: outDir, + platform: 'browser', + target: ['es2020'], + }); +} + +build().catch(() => process.exit(1)); + +if (isWatch) { + watcher.subscribe(srcDir, () => { + return build(); + }); +} diff --git a/extensions/markdown-math/esbuild.js b/extensions/markdown-math/esbuild.js index dd94d2e1eed..1dcd8b8c61a 100644 --- a/extensions/markdown-math/esbuild.js +++ b/extensions/markdown-math/esbuild.js @@ -7,6 +7,7 @@ const path = require('path'); const fse = require('fs-extra'); const esbuild = require('esbuild'); +const watcher = require('@parcel/watcher'); const args = process.argv.slice(2); @@ -18,34 +19,44 @@ if (outputRootIndex >= 0) { outputRoot = args[outputRootIndex + 1]; } +const srcDir = path.join(__dirname, 'notebook'); const outDir = path.join(outputRoot, 'notebook-out'); -esbuild.build({ - entryPoints: [ - path.join(__dirname, 'notebook', 'katex.ts'), - ], - bundle: true, - minify: true, - sourcemap: false, - format: 'esm', - outdir: outDir, - platform: 'browser', - target: ['es2020'], - watch: isWatch, - incremental: isWatch, -}).catch(() => process.exit(1)); -fse.copySync( - path.join(__dirname, 'node_modules', 'katex', 'dist', 'katex.min.css'), - path.join(outDir, 'katex.min.css')); +async function build() { + await esbuild.build({ + entryPoints: [ + path.join(srcDir, 'katex.ts'), + ], + bundle: true, + minify: true, + sourcemap: false, + format: 'esm', + outdir: outDir, + platform: 'browser', + target: ['es2020'], + }); + fse.copySync( + path.join(__dirname, 'node_modules', 'katex', 'dist', 'katex.min.css'), + path.join(outDir, 'katex.min.css')); -const fontsDir = path.join(__dirname, 'node_modules', 'katex', 'dist', 'fonts'); -const fontsOutDir = path.join(outDir, 'fonts/'); + const fontsDir = path.join(__dirname, 'node_modules', 'katex', 'dist', 'fonts'); + const fontsOutDir = path.join(outDir, 'fonts/'); -fse.mkdirSync(fontsOutDir, { recursive: true }); + fse.mkdirSync(fontsOutDir, { recursive: true }); -for (const file of fse.readdirSync(fontsDir)) { - if (file.endsWith('.woff2')) { - fse.copyFileSync(path.join(fontsDir, file), path.join(fontsOutDir, file)); + for (const file of fse.readdirSync(fontsDir)) { + if (file.endsWith('.woff2')) { + fse.copyFileSync(path.join(fontsDir, file), path.join(fontsOutDir, file)); + } } } + + +build().catch(() => process.exit(1)); + +if (isWatch) { + watcher.subscribe(srcDir, () => { + return build(); + }); +} diff --git a/extensions/notebook-renderers/esbuild.js b/extensions/notebook-renderers/esbuild.js index 11a04a0a982..7200fd2c24e 100644 --- a/extensions/notebook-renderers/esbuild.js +++ b/extensions/notebook-renderers/esbuild.js @@ -2,8 +2,10 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +// @ts-check const path = require('path'); const esbuild = require('esbuild'); +const watcher = require('@parcel/watcher'); const args = process.argv.slice(2); @@ -15,19 +17,28 @@ if (outputRootIndex >= 0) { outputRoot = args[outputRootIndex + 1]; } +const srcDir = path.join(__dirname, 'src'); const outDir = path.join(outputRoot, 'renderer-out'); -esbuild.build({ - entryPoints: [ - path.join(__dirname, 'src', 'index.ts'), - ], - bundle: true, - minify: false, - sourcemap: false, - format: 'esm', - outdir: outDir, - platform: 'browser', - target: ['es2020'], - watch: isWatch, - incremental: isWatch, -}).catch(() => process.exit(1)); +function build() { + return esbuild.build({ + entryPoints: [ + path.join(srcDir, 'index.ts'), + ], + bundle: true, + minify: false, + sourcemap: false, + format: 'esm', + outdir: outDir, + platform: 'browser', + target: ['es2020'], + }); +} + +build().catch(() => process.exit(1)); + +if (isWatch) { + watcher.subscribe(srcDir, () => { + return build(); + }); +} diff --git a/extensions/simple-browser/esbuild-preview.js b/extensions/simple-browser/esbuild-preview.js index d6447532501..b6fc9d0699a 100644 --- a/extensions/simple-browser/esbuild-preview.js +++ b/extensions/simple-browser/esbuild-preview.js @@ -2,9 +2,11 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ +// @ts-check const path = require('path'); const fs = require('fs'); const esbuild = require('esbuild'); +const watcher = require('@parcel/watcher'); const args = process.argv.slice(2); @@ -16,27 +18,36 @@ if (outputRootIndex >= 0) { outputRoot = args[outputRootIndex + 1]; } +const srcDir = path.join(__dirname, 'preview-src'); const outDir = path.join(outputRoot, 'media'); -fs.copyFileSync( - path.join(__dirname, 'node_modules', 'vscode-codicons', 'dist', 'codicon.css'), - path.join(outDir, 'codicon.css')); +async function build() { + fs.copyFileSync( + path.join(__dirname, 'node_modules', 'vscode-codicons', 'dist', 'codicon.css'), + path.join(outDir, 'codicon.css')); -fs.copyFileSync( - path.join(__dirname, 'node_modules', 'vscode-codicons', 'dist', 'codicon.ttf'), - path.join(outDir, 'codicon.ttf')); + fs.copyFileSync( + path.join(__dirname, 'node_modules', 'vscode-codicons', 'dist', 'codicon.ttf'), + path.join(outDir, 'codicon.ttf')); -esbuild.build({ - entryPoints: [ - path.join(__dirname, 'preview-src', 'index.ts') - ], - bundle: true, - minify: true, - sourcemap: false, - format: 'esm', - outdir: outDir, - platform: 'browser', - target: ['es2020'], - watch: isWatch, - incremental: isWatch, -}).catch(() => process.exit(1)); + await esbuild.build({ + entryPoints: [ + path.join(srcDir, 'index.ts') + ], + bundle: true, + minify: true, + sourcemap: false, + format: 'esm', + outdir: outDir, + platform: 'browser', + target: ['es2020'], + }); +} + +build().catch(() => process.exit(1)); + +if (isWatch) { + watcher.subscribe(srcDir, () => { + return build(); + }); +}