From b52815c52730e8c404e521725b7261f61b0da100 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Sat, 8 Oct 2022 17:07:26 -0700 Subject: [PATCH] Use jsonc-parser instead of LKG compiler in build Profiling the build roughly half of the time spent loading the build is spent importing typescript.js, for this one function. Since this stack is already adding required devDependencies, switch readJson to use jsonc-parser (published by the VS Code team), rather than importing the entire LKG typescript.js library. --- package-lock.json | 13 +++++++++++++ package.json | 1 + scripts/build/utils.mjs | 39 ++------------------------------------- 3 files changed, 16 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4fa587b378159..549fd66133ad9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -44,6 +44,7 @@ "fs-extra": "^9.1.0", "glob": "latest", "hereby": "^1.6.1", + "jsonc-parser": "^3.2.0", "minimist": "latest", "mkdirp": "latest", "mocha": "latest", @@ -3024,6 +3025,12 @@ "json5": "lib/cli.js" } }, + "node_modules/jsonc-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz", + "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==", + "dev": true + }, "node_modules/jsonfile": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", @@ -6617,6 +6624,12 @@ "minimist": "^1.2.0" } }, + "jsonc-parser": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.2.0.tgz", + "integrity": "sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==", + "dev": true + }, "jsonfile": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", diff --git a/package.json b/package.json index 266e976f88fcf..fbdae0a7913b7 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "fs-extra": "^9.1.0", "glob": "latest", "hereby": "^1.6.1", + "jsonc-parser": "^3.2.0", "minimist": "latest", "mkdirp": "latest", "mocha": "latest", diff --git a/scripts/build/utils.mjs b/scripts/build/utils.mjs index 9cca5c278aee1..3bf99758d8d13 100644 --- a/scripts/build/utils.mjs +++ b/scripts/build/utils.mjs @@ -2,11 +2,11 @@ import fs from "fs"; import path from "path"; -import ts from "../../lib/typescript.js"; import chalk from "chalk"; import which from "which"; import { spawn } from "child_process"; import assert from "assert"; +import JSONC from "jsonc-parser"; /** * Executes the provided command once with the supplied arguments. @@ -46,48 +46,13 @@ export async function exec(cmd, args, options = {}) { })); } -/** - * @param {ts.Diagnostic[]} diagnostics - * @param {{ cwd?: string, pretty?: boolean }} [options] - */ -function formatDiagnostics(diagnostics, options) { - return options && options.pretty - ? ts.formatDiagnosticsWithColorAndContext(diagnostics, getFormatDiagnosticsHost(options && options.cwd)) - : ts.formatDiagnostics(diagnostics, getFormatDiagnosticsHost(options && options.cwd)); -} - -/** - * @param {ts.Diagnostic[]} diagnostics - * @param {{ cwd?: string }} [options] - */ -function reportDiagnostics(diagnostics, options) { - console.log(formatDiagnostics(diagnostics, { cwd: options && options.cwd, pretty: process.stdout.isTTY })); -} - -/** - * @param {string | undefined} cwd - * @returns {ts.FormatDiagnosticsHost} - */ -function getFormatDiagnosticsHost(cwd) { - return { - getCanonicalFileName: fileName => fileName, - getCurrentDirectory: () => cwd ?? process.cwd(), - getNewLine: () => ts.sys.newLine, - }; -} - /** * Reads JSON data with optional comments using the LKG TypeScript compiler * @param {string} jsonPath */ export function readJson(jsonPath) { const jsonText = fs.readFileSync(jsonPath, "utf8"); - const result = ts.parseConfigFileTextToJson(jsonPath, jsonText); - if (result.error) { - reportDiagnostics([result.error]); - throw new Error("An error occurred during parse."); - } - return result.config; + return JSONC.parse(jsonText); } /**