Skip to content

Commit

Permalink
Use jsonc-parser instead of LKG compiler in build
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jakebailey committed Oct 27, 2022
1 parent 10fbfc8 commit b52815c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 2 additions & 37 deletions scripts/build/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit b52815c

Please sign in to comment.