Skip to content

Commit

Permalink
add support for json5
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyDigital committed Oct 7, 2024
1 parent 8c64a16 commit d433a76
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
3 changes: 3 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@
"lint": [
"**/*.{ts,tsx}"
]
},
"dependencies": {
"json5": "^2.2.3"
}
}
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 d433a76

Please sign in to comment.