Skip to content

Commit

Permalink
perf(compiler@json): ⚡ replace jsonc-parser with @std/jsonc
Browse files Browse the repository at this point in the history
Strips off ~200KB on this change. But this renders the disability to check JSON errors useless.
  • Loading branch information
TBroz15 committed Oct 23, 2024
1 parent ee3cda7 commit d426477
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@jsr:registry=https://npm.jsr.io
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
"type": "module",
"dependencies": {
"@clack/prompts": "^0.7.0",
"@std/jsonc": "npm:@jsr/std__jsonc@^1.0.1",
"adm-zip": "^0.5.15",
"fdir": "^6.3.0",
"fdir-size": "^1.0.3",
"jsonc-parser": "^3.3.1",
"mri": "^1.2.0",
"nanospinner": "^1.1.0",
"picocolors": "^1.0.1",
Expand Down
37 changes: 29 additions & 8 deletions pnpm-lock.yaml

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

10 changes: 3 additions & 7 deletions src/compiler/workers/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sharp from "sharp";
import { join } from "path";
import JSONC from "jsonc-parser";
import { parse as parseJSONC } from "@std/jsonc";
import { createHash } from "crypto";
import { copyFile, writeFile } from "fs/promises";
import { existsSync, readFileSync, writeFileSync } from "fs";
Expand Down Expand Up @@ -33,7 +33,7 @@ const errorList: unknown[] = [];

const checkParseJSON = (data: string, filePath: string): unknown => {
try {
return JSON.parse(JSONC.stripComments(data));
return parseJSONC(data);
} catch (error) {
errorList.push({
error: (error as Error).toString(),
Expand All @@ -45,8 +45,6 @@ const checkParseJSON = (data: string, filePath: string): unknown => {

function minifyJSON() {
if (files.JSON.length === 0) return;
const { errorChecking } = compiler.JSON ?? { errorChecking: false };

const promises = files.JSON.map((path) => {
const tempPath = join(tempPack, path);
const filePath = join(inPath, path);
Expand All @@ -58,9 +56,7 @@ function minifyJSON() {
const cacheFileExists = existsSync(cacheFile);

if (!cacheFileExists) {
const parsedJSON: unknown = errorChecking
? checkParseJSON(data, filePath)
: JSONC.parse(data);
const parsedJSON: unknown = checkParseJSON(data, filePath);

if (!parsedJSON) return;

Expand Down

0 comments on commit d426477

Please sign in to comment.