Skip to content

Commit 8c6461a

Browse files
committed
Fix ESM exports
This uses the `package.json` `exports` field to support native ESM. The `module` field is redundant and can even lead to compatibility issues, so it was removed. TypeScript project references are used for building the project with two different settings. An additional `package.json` is written to mark the ESM output as actual ESM. Since ESM requires file extensions, those were added to all imports. Mocha has been reconfigured to run tests for both CJS and ESM. Also ESLint has been configured to lint all files in the repo, not just a shell specific glob pattern. As a result, this change contains some small ESLint fixes.
1 parent 18b2209 commit 8c6461a

17 files changed

+101
-76
lines changed

.eslintrc.json

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11

22
{
33
"root": true,
4-
"parser": "@typescript-eslint/parser",
54
"parserOptions": {
6-
"ecmaVersion": 6,
5+
"ecmaVersion": "latest",
76
"sourceType": "module"
87
},
9-
"plugins": [
10-
"@typescript-eslint"
11-
],
128
"rules": {
13-
"@typescript-eslint/naming-convention": [
14-
"warn",
15-
{
16-
"selector": "typeLike",
17-
"format": [
18-
"PascalCase"
19-
]
20-
}
21-
],
229
"@typescript-eslint/semi": "warn",
2310
"curly": "warn",
2411
"eqeqeq": "warn",
@@ -27,5 +14,25 @@
2714
"no-unused-expressions": "warn",
2815
"no-duplicate-imports": "warn",
2916
"new-parens": "warn"
30-
}
31-
}
17+
},
18+
"overrides": [
19+
{
20+
"files": ["*.ts"],
21+
"plugins": [
22+
"@typescript-eslint"
23+
],
24+
"parser": "@typescript-eslint/parser",
25+
"rules": {
26+
"@typescript-eslint/naming-convention": [
27+
"warn",
28+
{
29+
"selector": "typeLike",
30+
"format": [
31+
"PascalCase"
32+
]
33+
}
34+
]
35+
}
36+
}
37+
]
38+
}

.mocharc.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
22
"ui": "tdd",
3-
"color": true
4-
}
3+
"color": true,
4+
"spec": [
5+
"./lib/cjs/test/*.test.js",
6+
"./lib/esm/test/*.test.js"
7+
]
8+
}

build/fix-esm.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env node
2+
/*---------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
7+
import fs from 'fs/promises';
8+
9+
await fs.writeFile(
10+
new URL('../lib/esm/package.json', import.meta.url),
11+
JSON.stringify({ type: 'module' }, undefined, 2) + '\n'
12+
);

build/remove-sourcemap-refs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ function deleteRefs(dir) {
1515
deleteRefs(filePath);
1616
} else if (path.extname(file) === '.js') {
1717
const content = fs.readFileSync(filePath, 'utf8');
18-
const newContent = content.replace(/\/\/\# sourceMappingURL=[^]+.js.map/, '')
18+
const newContent = content.replace(/\/\/\# sourceMappingURL=[^]+.js.map/, '');
1919
if (content.length !== newContent.length) {
2020
console.log('remove sourceMappingURL in ' + filePath);
2121
fs.writeFileSync(filePath, newContent);
2222
}
2323
} else if (path.extname(file) === '.map') {
24-
fs.unlinkSync(filePath)
24+
fs.unlinkSync(filePath);
2525
console.log('remove ' + filePath);
2626
}
2727
}

package.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
"name": "jsonc-parser",
33
"version": "3.2.0",
44
"description": "Scanner and parser for JSON with comments.",
5-
"main": "./lib/umd/main.js",
6-
"typings": "./lib/umd/main.d.ts",
7-
"module": "./lib/esm/main.js",
5+
"main": "./lib/cjs/main.js",
6+
"exports": {
7+
".": {
8+
"import": "./lib/esm/main.js",
9+
"default": "./lib/cjs/main.js"
10+
}
11+
},
812
"author": "Microsoft Corporation",
913
"repository": {
1014
"type": "git",
@@ -25,13 +29,12 @@
2529
"rimraf": "^3.0.2"
2630
},
2731
"scripts": {
28-
"prepack": "npm run clean && npm run compile-esm && npm run test && npm run remove-sourcemap-refs",
29-
"compile": "tsc -p ./src && npm run lint",
30-
"compile-esm": "tsc -p ./src/tsconfig.esm.json",
32+
"prepack": "npm run clean && npm run test && npm run remove-sourcemap-refs",
33+
"compile": "tsc -b && node ./build/fix-esm.mjs && npm run lint",
3134
"remove-sourcemap-refs": "node ./build/remove-sourcemap-refs.js",
3235
"clean": "rimraf lib",
33-
"watch": "tsc -w -p ./src",
34-
"test": "npm run compile && mocha ./lib/umd/test",
35-
"lint": "eslint src/**/*.ts"
36+
"watch": "tsc -w -b",
37+
"test": "npm run compile && mocha",
38+
"lint": "eslint ."
3639
}
3740
}

src/impl/edit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { Edit, ParseError, Node, JSONPath, Segment, ModificationOptions } from '../main';
8-
import { format, isEOL } from './format';
9-
import { parseTree, findNodeAtLocation } from './parser';
7+
import { Edit, ParseError, Node, JSONPath, Segment, ModificationOptions } from '../main.js';
8+
import { format, isEOL } from './format.js';
9+
import { parseTree, findNodeAtLocation } from './parser.js';
1010

1111
export function removeProperty(text: string, path: JSONPath, options: ModificationOptions): Edit[] {
1212
return setProperty(text, path, void 0, options);

src/impl/format.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { Range, FormattingOptions, Edit, SyntaxKind, ScanError } from '../main';
8-
import { createScanner } from './scanner';
7+
import { Range, FormattingOptions, Edit, SyntaxKind, ScanError } from '../main.js';
8+
import { createScanner } from './scanner.js';
99

1010
export function format(documentText: string, range: Range | undefined, options: FormattingOptions): Edit[] {
1111
let initialIndentLevel: number;

src/impl/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { createScanner } from './scanner';
7+
import { createScanner } from './scanner.js';
88
import {
99
JSONPath,
1010
JSONVisitor,
@@ -17,7 +17,7 @@ import {
1717
ScanError,
1818
Segment,
1919
SyntaxKind
20-
} from '../main';
20+
} from '../main.js';
2121

2222
namespace ParseOptions {
2323
export const DEFAULT = {

src/impl/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import { ScanError, SyntaxKind, JSONScanner } from '../main';
7+
import { ScanError, SyntaxKind, JSONScanner } from '../main.js';
88

99
/**
1010
* Creates a JSON scanner on the given text.

src/main.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
import * as formatter from './impl/format';
8-
import * as edit from './impl/edit';
9-
import * as scanner from './impl/scanner';
10-
import * as parser from './impl/parser';
7+
import * as formatter from './impl/format.js';
8+
import * as edit from './impl/edit.js';
9+
import * as scanner from './impl/scanner.js';
10+
import * as parser from './impl/parser.js';
1111

1212
/**
1313
* Creates a JSON scanner on the given text.
@@ -183,7 +183,6 @@ export function printParseErrorCode(code: ParseErrorCode) {
183183
case ParseErrorCode.InvalidEscapeCharacter: return 'InvalidEscapeCharacter';
184184
case ParseErrorCode.InvalidCharacter: return 'InvalidCharacter';
185185
}
186-
return '<unknown ParseErrorCode>';
187186
}
188187

189188
export type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null';

0 commit comments

Comments
 (0)