Skip to content

Commit

Permalink
feat: add support for json5 (#94)
Browse files Browse the repository at this point in the history
Co-authored-by: Zyie <[email protected]>
  • Loading branch information
GoodBoyDigital and Zyie authored Oct 14, 2024
1 parent 586f6e6 commit 2d77554
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions package-lock.json

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

9 changes: 5 additions & 4 deletions packages/assetpack/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@assetpack/core",
"version": "1.1.1",
"license": "MIT",
"type": "module",
"keywords": [],
"homepage": "https://pixijs.io/assetpack/",
"bugs": "https://github.com/pixijs/assetpack/issues",
"repository": {
"type": "git",
"url": "https://github.com/pixijs/assetpack.git"
},
"keywords": [],
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/core/index.js",
"./cache-buster": "./dist/cache-buster/index.js",
Expand All @@ -35,8 +35,8 @@
],
"scripts": {
"build": "tsc",
"release": "xs bump,git-push",
"publish-ci": "xs publish",
"release": "xs bump,git-push",
"watch": "tsc -w"
},
"husky": {
Expand Down Expand Up @@ -70,6 +70,7 @@
"fs-extra": "^11.2.0",
"glob": "^10.4.1",
"gpu-tex-enc": "^1.2.5",
"json5": "^2.2.3",
"maxrects-packer": "^2.7.3",
"merge": "^2.1.1",
"minimatch": "9.0.4",
Expand Down
11 changes: 8 additions & 3 deletions packages/assetpack/src/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json5 from 'json5';
import { checkExt, createNewAssetAt, Logger } from '../core/index.js';

import type { Asset, AssetPipe } from '../core/index.js';
Expand All @@ -13,14 +14,18 @@ export function json(): AssetPipe<any, 'nc'>
},
test(asset: Asset)
{
return !asset.metaData[this.tags!.nc] && checkExt(asset.path, '.json');
return !asset.metaData[this.tags!.nc] && checkExt(asset.path, '.json', '.json5');
},
async transform(asset: Asset)
{
try
{
const json = JSON.parse(asset.buffer.toString());
const compressedJsonAsset = createNewAssetAt(asset, asset.filename);
const json = json5.parse(asset.buffer.toString());

// replace the json5 with json
const filename = asset.filename.replace('.json5', '.json');

const compressedJsonAsset = createNewAssetAt(asset, filename);

compressedJsonAsset.buffer = Buffer.from(JSON.stringify(json));

Expand Down
43 changes: 43 additions & 0 deletions packages/assetpack/test/json/Json.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readJSONSync } from 'fs-extra';
import { existsSync, readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import { AssetPack } from '../../src/core/index.js';
Expand Down Expand Up @@ -124,4 +125,46 @@ describe('Json', () =>

expect(data.replace(/\\/g, '').trim()).toEqual(`{"hello":"world","Im":"not broken"}`);
});

it('should support json5 format', async () =>
{
const testName = 'json5';
const inputDir = getInputDir(pkg, testName);
const outputDir = getOutputDir(pkg, testName);

createFolder(
pkg,
{
name: testName,
files: [
{
name: 'json5.json',
content: assetPath('json/json5.json'),
},
{
name: 'other-json-5.json5',
content: assetPath('json/json5.json'),
},
],
folders: [],
}
);

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: false,
pipes: [
json()
]
});

await assetpack.run();

const json5Data = readJSONSync(`${outputDir}/json5.json`, 'utf8');

expect(json5Data).toEqual({ hello: 'world', Im: 'not broken' });

expect(existsSync(`${outputDir}/other-json-5.json`)).toBe(true);
});
});
5 changes: 5 additions & 0 deletions packages/assetpack/test/resources/json/json5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
// so cool support for comments
hello: "world",
Im: "not broken"
}

0 comments on commit 2d77554

Please sign in to comment.