Skip to content

Commit 505d179

Browse files
joshballvegarringdal
authored andcommitted
Adding JSON5 support (#57)
* Adding JSON5 support Typescript allows comments and trailing commas in tsconfig and tslint allows comments. The current library uses the default require for loading JSON, which fails with either. Added the JSON5 library, and overwrote the .json extension to use it.
1 parent 0bfc862 commit 505d179

File tree

13 files changed

+173
-52
lines changed

13 files changed

+173
-52
lines changed

build.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
//
2+
// Sorry, this is a hack because this library uses an older version of itself
3+
// to build. So since I added comments to the config files, they will break
4+
// this build, so shimming it with this register.
5+
//
6+
const fs = require('fs');
7+
const JSON5 = require('json5');
8+
// JSON5 has a register for the .json5 extension
9+
// however, it does not overide .json.
10+
// This will just override our require for *.json files and use
11+
// JSON5 to parse.
12+
require.extensions['.json'] = function (module, filename) {
13+
const content = fs.readFileSync(filename, 'utf8');
14+
try {
15+
module.exports = JSON5.parse(content);
16+
} catch (err) {
17+
err.message = filename + ': ' + err.message;
18+
throw err;
19+
}
20+
};
21+
22+
23+
124
const transpiler = require('fuse-box-typechecker').TypeHelper;
225

326
const transpileTo = function (outDir, moduleType) {

dist/commonjs/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ITypeCheckerOptions, IResults } from './interfaces';
2+
import './register.json5';
23
export declare class TypeHelperClass {
34
private options;
45
private worker;

dist/commonjs/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/register.json5.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

dist/commonjs/register.json5.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/register.json5.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 99 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"url": "https://github.com/fuse-box/fuse-box-typechecker"
2222
},
2323
"devDependencies": {
24+
"@types/json5": "0.0.30",
2425
"fuse-box-typechecker": "2.11.0",
2526
"tslint": "^5.11.0",
2627
"typescript": "^3.2.2"
@@ -30,6 +31,7 @@
3031
"@types/watch": "^1.0.0",
3132
"chalk": "2.4.1",
3233
"gulp-changed": "^3.2.0",
34+
"json5": "^2.1.0",
3335
"object.entries": "^1.0.4",
3436
"watch": "^1.0.2"
3537
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { Checker } from './checker';
77
import * as watch from 'watch';
88
import * as ts from 'typescript';
99
import chalk from 'chalk';
10-
10+
// Allows us to read valid (with comments) tsconfig and tslint files.
11+
import './register.json5';
1112

1213
export class TypeHelperClass {
1314
private options: ITypeCheckerOptions;

0 commit comments

Comments
 (0)