Skip to content

ES Modules #295

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 28 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c6d0bbd
Update package-lock.json file
jcmosc Jan 2, 2023
f34d4bf
Fix broken tests in blank.spec.js
jcmosc Jan 2, 2023
6b623d2
Fix webpack error when running browser tests
jcmosc Jan 2, 2023
d009a0e
Fix tests in invalid.spec.js when running in Chrome
jcmosc Jan 2, 2023
1f674e1
Change statusCode to status
jcmosc Jan 2, 2023
0b187c5
Set the --openssl-legacy-provider flag when running browser tests
jcmosc Jan 3, 2023
038b0f3
Use isomorphic-fetch instead of node-fetch
jcmosc Jan 3, 2023
5f5a5f1
Increase minimum version of node to 17
jcmosc Jan 3, 2023
66d06ab
Add chokidar@3 as an explicit dev dependency
jcmosc Jan 3, 2023
0f5d861
Skip test assertion due to conflict on Windows
jcmosc Jan 4, 2023
48dd76c
Explicitly define browsers and plugins in karma.conf.js
jcmosc Jan 11, 2023
8c1cebc
Replace karma-edge-launcher with newer package
jcmosc Jan 11, 2023
2b43588
Increase default timeout of async tests
jcmosc Jan 5, 2023
ef6974f
Change package type to module
jcmosc Jan 5, 2023
7744d3d
Change require/module.exports to import/export in source files
jcmosc Jan 5, 2023
6c35cd5
Delete "use strict" directives from source code
jcmosc Jan 5, 2023
c4fec02
Update tests to ES modules
jcmosc Jan 5, 2023
29e21af
Re-export $RefParser methods as standalone functions
jcmosc Jan 5, 2023
f0cb47a
Make karma config file a .cjs file
jcmosc Jan 8, 2023
e55ce71
Avoid top-level await in path.js
jcmosc Jan 8, 2023
fe72cca
Revert back to __dirname for browser tests
jcmosc Jan 8, 2023
56f7263
Fix lint issues
jcmosc Jan 9, 2023
f571f0a
Disable code coverage in CI for now
jcmosc Jan 9, 2023
24fc8f8
Revert "Disable code coverage in CI for now"
jcmosc Jan 11, 2023
a9df761
Replace nyc with c8 to work with ES modules
jcmosc Jan 11, 2023
0ea7c63
Implement a dual CommonJS/ES module package.json
jcmosc Jan 11, 2023
90f37ec
Hack to allow $RefParser to be imported from require() without .defau…
jcmosc Jan 11, 2023
9dd8d0c
Merge branch 'main' into esmodules
jcmosc Jan 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extension": [".js", ".cjs", ".mjs", ".ts"],
"reporter": ["text", "lcov"]
}
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ env:
browser: true
rules:
"@typescript-eslint/no-explicit-any": ["off"]
parserOptions:
sourceType: module
1 change: 1 addition & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ spec: test/specs/**/*.spec.js
bail: true
recursive: true
async-only: true
timeout: "5000"
require: ./test/fixtures/polyfill.js
10 changes: 0 additions & 10 deletions .nycrc.yml

This file was deleted.

18 changes: 18 additions & 0 deletions .swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "ecmascript",
"exportDefaultFrom": true
}
},
"module": {
"type": "commonjs",

// These are defaults.
"strict": false,
"strictMode": true,
"lazy": false,
"noInterop": false
}
}
5 changes: 5 additions & 0 deletions karma.conf.js → karma.conf.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ module.exports = (karma) => {
}
});

config.files.push({
pattern: "test/**/*.js",
type: "module"
});

if (config.logLevel !== karma.LOG_DISABLE) {
console.debug("Karma Config:\n", nodeUtil.inspect(config, {
depth: 10,
Expand Down
10 changes: 4 additions & 6 deletions lib/bundle.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";
import $Ref from "./ref.js";
import Pointer from "./pointer.js";
import * as url from "./util/url.js";

const $Ref = require("./ref");
const Pointer = require("./pointer");
const url = require("./util/url");

module.exports = bundle;
export default bundle;

/**
* Bundles all external JSON references into the main JSON schema, thus resulting in a schema that
Expand Down
12 changes: 5 additions & 7 deletions lib/dereference.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict";
import $Ref from "./ref.js";
import Pointer from "./pointer.js";
import { ono } from "@jsdevtools/ono";
import * as url from "./util/url.js";

const $Ref = require("./ref");
const Pointer = require("./pointer");
const { ono } = require("@jsdevtools/ono");
const url = require("./util/url");

module.exports = dereference;
export default dereference;

/**
* Crawls the JSON schema, finds all JSON references, and dereferences them.
Expand Down
49 changes: 28 additions & 21 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
/* eslint-disable no-unused-vars */
"use strict";
import $Refs from "./refs.js";
import _parse from "./parse.js";
import normalizeArgs from "./normalize-args.js";
import resolveExternal from "./resolve-external.js";
import _bundle from "./bundle.js";
import _dereference from "./dereference.js";
import * as url from "./util/url.js";
import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } from "./util/errors.js";
import maybe from "call-me-maybe";
import { ono } from "@jsdevtools/ono";

const $Refs = require("./refs");
const _parse = require("./parse");
const normalizeArgs = require("./normalize-args");
const resolveExternal = require("./resolve-external");
const _bundle = require("./bundle");
const _dereference = require("./dereference");
const url = require("./util/url");
const { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } = require("./util/errors");
const maybe = require("call-me-maybe");
const { ono } = require("@jsdevtools/ono");

module.exports = $RefParser;
module.exports.default = $RefParser;
module.exports.JSONParserError = JSONParserError;
module.exports.InvalidPointerError = InvalidPointerError;
module.exports.MissingPointerError = MissingPointerError;
module.exports.ResolverError = ResolverError;
module.exports.ParserError = ParserError;
module.exports.UnmatchedParserError = UnmatchedParserError;
module.exports.UnmatchedResolverError = UnmatchedResolverError;
export default $RefParser;
export { JSONParserError };
export { InvalidPointerError };
export { MissingPointerError };
export { ResolverError };
export { ParserError };
export { UnmatchedParserError };
export { UnmatchedResolverError };

/**
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
Expand Down Expand Up @@ -281,3 +278,13 @@ function finalize (parser) {
throw new JSONParserErrorGroup(parser);
}
}

export const parse = $RefParser.parse.bind($RefParser);
export const resolve = $RefParser.resolve.bind($RefParser);
export const bundle = $RefParser.bundle.bind($RefParser);
export const dereference = $RefParser.dereference.bind($RefParser);

// CommonJS default export hack
if (typeof module === "object" && typeof module.exports === "object") {
module.exports = Object.assign(module.exports.default, module.exports);
}
6 changes: 2 additions & 4 deletions lib/normalize-args.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
import Options from "./options.js";

const Options = require("./options");

module.exports = normalizeArgs;
export default normalizeArgs;

/**
* Normalizes the given arguments, accounting for optional args.
Expand Down
16 changes: 7 additions & 9 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
"use strict";
import jsonParser from "./parsers/json.js";
import yamlParser from "./parsers/yaml.js";
import textParser from "./parsers/text.js";
import binaryParser from "./parsers/binary.js";
import fileResolver from "./resolvers/file.js";
import httpResolver from "./resolvers/http.js";

const jsonParser = require("./parsers/json");
const yamlParser = require("./parsers/yaml");
const textParser = require("./parsers/text");
const binaryParser = require("./parsers/binary");
const fileResolver = require("./resolvers/file");
const httpResolver = require("./resolvers/http");

module.exports = $RefParserOptions;
export default $RefParserOptions;

/**
* Options that determine how JSON schemas are parsed, resolved, and dereferenced.
Expand Down
12 changes: 5 additions & 7 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"use strict";
import { ono } from "@jsdevtools/ono";
import * as url from "./util/url.js";
import * as plugins from "./util/plugins.js";
import { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } from "./util/errors.js";

const { ono } = require("@jsdevtools/ono");
const url = require("./util/url");
const plugins = require("./util/plugins");
const { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } = require("./util/errors");

module.exports = parse;
export default parse;

/**
* Reads and parses the specified file path or URL.
Expand Down
4 changes: 1 addition & 3 deletions lib/parsers/binary.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";

let BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;

module.exports = {
export default {
/**
* The order that this parser will run, in relation to other parsers.
*
Expand Down
6 changes: 2 additions & 4 deletions lib/parsers/json.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";
import { ParserError } from "../util/errors.js";

const { ParserError } = require("../util/errors");

module.exports = {
export default {
/**
* The order that this parser will run, in relation to other parsers.
*
Expand Down
6 changes: 2 additions & 4 deletions lib/parsers/text.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";

const { ParserError } = require("../util/errors");
import { ParserError } from "../util/errors.js";

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

module.exports = {
export default {
/**
* The order that this parser will run, in relation to other parsers.
*
Expand Down
10 changes: 4 additions & 6 deletions lib/parsers/yaml.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";
import { ParserError } from "../util/errors.js";
import yaml from "js-yaml";
import { JSON_SCHEMA } from "js-yaml";

const { ParserError } = require("../util/errors");
const yaml = require("js-yaml");
const { JSON_SCHEMA } = require("js-yaml");

module.exports = {
export default {
/**
* The order that this parser will run, in relation to other parsers.
*
Expand Down
10 changes: 4 additions & 6 deletions lib/pointer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";
export default Pointer;

module.exports = Pointer;

const $Ref = require("./ref");
const url = require("./util/url");
const { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } = require("./util/errors");
import $Ref from "./ref.js";
import * as url from "./util/url.js";
import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js";
const slashes = /\//g;
const tildes = /~/g;
const escapedSlash = /~1/g;
Expand Down
10 changes: 4 additions & 6 deletions lib/ref.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";
export default $Ref;

module.exports = $Ref;

const Pointer = require("./pointer");
const { InvalidPointerError, isHandledError, normalizeError } = require("./util/errors");
const { safePointerToPath, stripHash, getHash } = require("./util/url");
import Pointer from "./pointer.js";
import { InvalidPointerError, isHandledError, normalizeError } from "./util/errors.js";
import { safePointerToPath, stripHash, getHash } from "./util/url.js";

/**
* This class represents a single JSON reference and its resolved value.
Expand Down
12 changes: 4 additions & 8 deletions lib/refs.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
"use strict";

const { ono } = require("@jsdevtools/ono");
const $Ref = require("./ref");
const url = require("./util/url");
import { ono } from "@jsdevtools/ono";
import $Ref from "./ref.js";
import * as url from "./util/url.js";

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

module.exports = $Refs;

/**
* This class is a map of JSON references and their resolved values.
*/
function $Refs () {
export default function $Refs () {
/**
* Indicates whether the schema contains any circular references.
*
Expand Down
14 changes: 6 additions & 8 deletions lib/resolve-external.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"use strict";
import $Ref from "./ref.js";
import Pointer from "./pointer.js";
import parse from "./parse.js";
import * as url from "./util/url.js";
import { isHandledError } from "./util/errors.js";

const $Ref = require("./ref");
const Pointer = require("./pointer");
const parse = require("./parse");
const url = require("./util/url");
const { isHandledError } = require("./util/errors");

module.exports = resolveExternal;
export default resolveExternal;

/**
* Crawls the JSON schema, finds all external JSON references, and resolves their values.
Expand Down
11 changes: 5 additions & 6 deletions lib/resolvers/file.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"use strict";
const fs = require("fs");
const { ono } = require("@jsdevtools/ono");
const url = require("../util/url");
const { ResolverError } = require("../util/errors");
import fs from "fs";
import { ono } from "@jsdevtools/ono";
import * as url from "../util/url.js";
import { ResolverError } from "../util/errors.js";

module.exports = {
export default {
/**
* The order that this resolver will run, in relation to other resolvers.
*
Expand Down
10 changes: 4 additions & 6 deletions lib/resolvers/http.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";
import { ono } from "@jsdevtools/ono";
import * as url from "../util/url.js";
import { ResolverError } from "../util/errors.js";

const { ono } = require("@jsdevtools/ono");
const url = require("../util/url");
const { ResolverError } = require("../util/errors");

module.exports = {
export default {
/**
* The order that this resolver will run, in relation to other resolvers.
*
Expand Down
Loading