Skip to content

Commit 993d234

Browse files
authored
ES Modules (#295)
* Update package-lock.json file * Fix broken tests in blank.spec.js Blank binary file was not getting serialized as { type: "Buffer", data: [] } because read(file) was returning a string instead of Buffer. * Fix webpack error when running browser tests Webpack 4 cannot parse module containing optional chaining operator. See https://stackoverflow.com/questions/59972341/how-to-make-webpack-accept-optional-chaining-without-babel * Fix tests in invalid.spec.js when running in Chrome * Change statusCode to status * Set the --openssl-legacy-provider flag when running browser tests Recent versions of node upgraded to OpenSSL 3.0, which deprecated some older crypto hashing algorithms including md4. Webpack 4 hard codes the use of md4 ins some places, so we have to set this flag until we can upgrade Webpack. * Use isomorphic-fetch instead of node-fetch Browser tests were failing as polyfill.js was loading a node package in a browser context. * Increase minimum version of node to 17 This was when the --openssl-legacy-provider option was introduced. Prior versions of node do not recognize this option. * Add chokidar@3 as an explicit dev dependency Browser tests are failing in the CI environment with: chokidar@3: Error: Cannot find module 'chokidar' Don't know why but this thread might be relevant: facebook/create-react-app#10811 * Skip test assertion due to conflict on Windows See also #286 * Explicitly define browsers and plugins in karma.conf.js * Replace karma-edge-launcher with newer package * Increase default timeout of async tests * Change package type to module * Change require/module.exports to import/export in source files * Delete "use strict" directives from source code * Update tests to ES modules * Re-export $RefParser methods as standalone functions Preserve backwards compatibility * Make karma config file a .cjs file Fixes “require() of ES Module …/config.js not supported” error * Avoid top-level await in path.js The current configuration of karma uses Webpack 4, which can't parse modules with top-level await. * Revert back to __dirname for browser tests Karma configuration needs to upgrade to Webpack 5 in order to use import.meta.url * Fix lint issues * Disable code coverage in CI for now * Revert "Disable code coverage in CI for now" This reverts commit f571f0a. * Replace nyc with c8 to work with ES modules * Implement a dual CommonJS/ES module package.json * Hack to allow $RefParser to be imported from require() without .default property Without this hack, you would have to do this: const $RefParser = require("@apidevtools/json-schema-ref-parser).default; Now you can do this: const $RefParser = require("@apidevtools/json-schema-ref-parser);
1 parent da9e32e commit 993d234

File tree

130 files changed

+2199
-2137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+2199
-2137
lines changed

Diff for: .c8rc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extension": [".js", ".cjs", ".mjs", ".ts"],
3+
"reporter": ["text", "lcov"]
4+
}

Diff for: .eslintrc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ env:
99
browser: true
1010
rules:
1111
"@typescript-eslint/no-explicit-any": ["off"]
12+
parserOptions:
13+
sourceType: module

Diff for: .mocharc.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ spec: test/specs/**/*.spec.js
77
bail: true
88
recursive: true
99
async-only: true
10+
timeout: "5000"
1011
require: ./test/fixtures/polyfill.js

Diff for: .nycrc.yml

-10
This file was deleted.

Diff for: .swcrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "http://json.schemastore.org/swcrc",
3+
"jsc": {
4+
"parser": {
5+
"syntax": "ecmascript",
6+
"exportDefaultFrom": true
7+
}
8+
},
9+
"module": {
10+
"type": "commonjs",
11+
12+
// These are defaults.
13+
"strict": false,
14+
"strictMode": true,
15+
"lazy": false,
16+
"noInterop": false
17+
}
18+
}

Diff for: karma.conf.js renamed to karma.conf.cjs

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ module.exports = (karma) => {
4545
}
4646
});
4747

48+
config.files.push({
49+
pattern: "test/**/*.js",
50+
type: "module"
51+
});
52+
4853
if (config.logLevel !== karma.LOG_DISABLE) {
4954
console.debug("Karma Config:\n", nodeUtil.inspect(config, {
5055
depth: 10,

Diff for: lib/bundle.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
1+
import $Ref from "./ref.js";
2+
import Pointer from "./pointer.js";
3+
import * as url from "./util/url.js";
24

3-
const $Ref = require("./ref");
4-
const Pointer = require("./pointer");
5-
const url = require("./util/url");
6-
7-
module.exports = bundle;
5+
export default bundle;
86

97
/**
108
* Bundles all external JSON references into the main JSON schema, thus resulting in a schema that

Diff for: lib/dereference.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
"use strict";
1+
import $Ref from "./ref.js";
2+
import Pointer from "./pointer.js";
3+
import { ono } from "@jsdevtools/ono";
4+
import * as url from "./util/url.js";
25

3-
const $Ref = require("./ref");
4-
const Pointer = require("./pointer");
5-
const { ono } = require("@jsdevtools/ono");
6-
const url = require("./util/url");
7-
8-
module.exports = dereference;
6+
export default dereference;
97

108
/**
119
* Crawls the JSON schema, finds all JSON references, and dereferences them.

Diff for: lib/index.js

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
/* eslint-disable no-unused-vars */
2-
"use strict";
2+
import $Refs from "./refs.js";
3+
import _parse from "./parse.js";
4+
import normalizeArgs from "./normalize-args.js";
5+
import resolveExternal from "./resolve-external.js";
6+
import _bundle from "./bundle.js";
7+
import _dereference from "./dereference.js";
8+
import * as url from "./util/url.js";
9+
import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } from "./util/errors.js";
10+
import maybe from "call-me-maybe";
11+
import { ono } from "@jsdevtools/ono";
312

4-
const $Refs = require("./refs");
5-
const _parse = require("./parse");
6-
const normalizeArgs = require("./normalize-args");
7-
const resolveExternal = require("./resolve-external");
8-
const _bundle = require("./bundle");
9-
const _dereference = require("./dereference");
10-
const url = require("./util/url");
11-
const { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } = require("./util/errors");
12-
const maybe = require("call-me-maybe");
13-
const { ono } = require("@jsdevtools/ono");
14-
15-
module.exports = $RefParser;
16-
module.exports.default = $RefParser;
17-
module.exports.JSONParserError = JSONParserError;
18-
module.exports.InvalidPointerError = InvalidPointerError;
19-
module.exports.MissingPointerError = MissingPointerError;
20-
module.exports.ResolverError = ResolverError;
21-
module.exports.ParserError = ParserError;
22-
module.exports.UnmatchedParserError = UnmatchedParserError;
23-
module.exports.UnmatchedResolverError = UnmatchedResolverError;
13+
export default $RefParser;
14+
export { JSONParserError };
15+
export { InvalidPointerError };
16+
export { MissingPointerError };
17+
export { ResolverError };
18+
export { ParserError };
19+
export { UnmatchedParserError };
20+
export { UnmatchedResolverError };
2421

2522
/**
2623
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
@@ -281,3 +278,13 @@ function finalize (parser) {
281278
throw new JSONParserErrorGroup(parser);
282279
}
283280
}
281+
282+
export const parse = $RefParser.parse.bind($RefParser);
283+
export const resolve = $RefParser.resolve.bind($RefParser);
284+
export const bundle = $RefParser.bundle.bind($RefParser);
285+
export const dereference = $RefParser.dereference.bind($RefParser);
286+
287+
// CommonJS default export hack
288+
if (typeof module === "object" && typeof module.exports === "object") {
289+
module.exports = Object.assign(module.exports.default, module.exports);
290+
}

Diff for: lib/normalize-args.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
"use strict";
1+
import Options from "./options.js";
22

3-
const Options = require("./options");
4-
5-
module.exports = normalizeArgs;
3+
export default normalizeArgs;
64

75
/**
86
* Normalizes the given arguments, accounting for optional args.

Diff for: lib/options.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
2-
"use strict";
2+
import jsonParser from "./parsers/json.js";
3+
import yamlParser from "./parsers/yaml.js";
4+
import textParser from "./parsers/text.js";
5+
import binaryParser from "./parsers/binary.js";
6+
import fileResolver from "./resolvers/file.js";
7+
import httpResolver from "./resolvers/http.js";
38

4-
const jsonParser = require("./parsers/json");
5-
const yamlParser = require("./parsers/yaml");
6-
const textParser = require("./parsers/text");
7-
const binaryParser = require("./parsers/binary");
8-
const fileResolver = require("./resolvers/file");
9-
const httpResolver = require("./resolvers/http");
10-
11-
module.exports = $RefParserOptions;
9+
export default $RefParserOptions;
1210

1311
/**
1412
* Options that determine how JSON schemas are parsed, resolved, and dereferenced.

Diff for: lib/parse.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
"use strict";
1+
import { ono } from "@jsdevtools/ono";
2+
import * as url from "./util/url.js";
3+
import * as plugins from "./util/plugins.js";
4+
import { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } from "./util/errors.js";
25

3-
const { ono } = require("@jsdevtools/ono");
4-
const url = require("./util/url");
5-
const plugins = require("./util/plugins");
6-
const { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors");
7-
8-
module.exports = parse;
6+
export default parse;
97

108
/**
119
* Reads and parses the specified file path or URL.

Diff for: lib/parsers/binary.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
"use strict";
2-
31
let BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;
42

5-
module.exports = {
3+
export default {
64
/**
75
* The order that this parser will run, in relation to other parsers.
86
*

Diff for: lib/parsers/json.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
"use strict";
1+
import { ParserError } from "../util/errors.js";
22

3-
const { ParserError } = require("../util/errors");
4-
5-
module.exports = {
3+
export default {
64
/**
75
* The order that this parser will run, in relation to other parsers.
86
*

Diff for: lib/parsers/text.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
2-
3-
const { ParserError } = require("../util/errors");
1+
import { ParserError } from "../util/errors.js";
42

53
let TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i;
64

7-
module.exports = {
5+
export default {
86
/**
97
* The order that this parser will run, in relation to other parsers.
108
*

Diff for: lib/parsers/yaml.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
1+
import { ParserError } from "../util/errors.js";
2+
import yaml from "js-yaml";
3+
import { JSON_SCHEMA } from "js-yaml";
24

3-
const { ParserError } = require("../util/errors");
4-
const yaml = require("js-yaml");
5-
const { JSON_SCHEMA } = require("js-yaml");
6-
7-
module.exports = {
5+
export default {
86
/**
97
* The order that this parser will run, in relation to other parsers.
108
*

Diff for: lib/pointer.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
1+
export default Pointer;
22

3-
module.exports = Pointer;
4-
5-
const $Ref = require("./ref");
6-
const url = require("./util/url");
7-
const { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } = require("./util/errors");
3+
import $Ref from "./ref.js";
4+
import * as url from "./util/url.js";
5+
import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js";
86
const slashes = /\//g;
97
const tildes = /~/g;
108
const escapedSlash = /~1/g;

Diff for: lib/ref.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
1+
export default $Ref;
22

3-
module.exports = $Ref;
4-
5-
const Pointer = require("./pointer");
6-
const { InvalidPointerError, isHandledError, normalizeError } = require("./util/errors");
7-
const { safePointerToPath, stripHash, getHash } = require("./util/url");
3+
import Pointer from "./pointer.js";
4+
import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js";
5+
import { safePointerToPath, stripHash, getHash } from "./util/url.js";
86

97
/**
108
* This class represents a single JSON reference and its resolved value.

Diff for: lib/refs.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
"use strict";
2-
3-
const { ono } = require("@jsdevtools/ono");
4-
const $Ref = require("./ref");
5-
const url = require("./util/url");
1+
import { ono } from "@jsdevtools/ono";
2+
import $Ref from "./ref.js";
3+
import * as url from "./util/url.js";
64

75
const isWindows = /^win/.test(globalThis.process ? globalThis.process.platform : undefined);
86
const getPathFromOs = filePath => isWindows ? filePath.replace(/\\/g, "/") : filePath;
97

10-
module.exports = $Refs;
11-
128
/**
139
* This class is a map of JSON references and their resolved values.
1410
*/
15-
function $Refs () {
11+
export default function $Refs () {
1612
/**
1713
* Indicates whether the schema contains any circular references.
1814
*

Diff for: lib/resolve-external.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
"use strict";
1+
import $Ref from "./ref.js";
2+
import Pointer from "./pointer.js";
3+
import parse from "./parse.js";
4+
import * as url from "./util/url.js";
5+
import { isHandledError } from "./util/errors.js";
26

3-
const $Ref = require("./ref");
4-
const Pointer = require("./pointer");
5-
const parse = require("./parse");
6-
const url = require("./util/url");
7-
const { isHandledError } = require("./util/errors");
8-
9-
module.exports = resolveExternal;
7+
export default resolveExternal;
108

119
/**
1210
* Crawls the JSON schema, finds all external JSON references, and resolves their values.

Diff for: lib/resolvers/file.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
"use strict";
2-
const fs = require("fs");
3-
const { ono } = require("@jsdevtools/ono");
4-
const url = require("../util/url");
5-
const { ResolverError } = require("../util/errors");
1+
import fs from "fs";
2+
import { ono } from "@jsdevtools/ono";
3+
import * as url from "../util/url.js";
4+
import { ResolverError } from "../util/errors.js";
65

7-
module.exports = {
6+
export default {
87
/**
98
* The order that this resolver will run, in relation to other resolvers.
109
*

Diff for: lib/resolvers/http.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
"use strict";
1+
import { ono } from "@jsdevtools/ono";
2+
import * as url from "../util/url.js";
3+
import { ResolverError } from "../util/errors.js";
24

3-
const { ono } = require("@jsdevtools/ono");
4-
const url = require("../util/url");
5-
const { ResolverError } = require("../util/errors");
6-
7-
module.exports = {
5+
export default {
86
/**
97
* The order that this resolver will run, in relation to other resolvers.
108
*

0 commit comments

Comments
 (0)