diff --git a/.eslintrc.json b/.eslintrc.json index 5ba83e79..3293f472 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,6 +23,7 @@ "curly": [2, "multi-line"], "no-else-return": 2, + "node/no-missing-import": 0, "node/no-unsupported-features/es-syntax": 0 }, "overrides": [ diff --git a/package.json b/package.json index c3889950..d2bec4a0 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,17 @@ }, "main": "lib/index.js", "types": "lib/index.d.ts", + "module": "lib/esm/index.js", + "exports": { + ".": { + "require": "./lib/index.js", + "import": "./lib/esm/index.js" + }, + "./lib/decode.js": { + "require": "./lib/decode.js", + "import": "./lib/esm/decode.js" + } + }, "files": [ "lib/**/*" ], @@ -48,7 +59,10 @@ "format:es": "npm run lint:es -- --fix", "format:prettier": "npm run prettier -- --write", "prettier": "prettier '**/*.{ts,md,json,yml}'", - "build": "tsc && cp -r src/maps lib", + "build": "npm run build:cjs && npm run build:esm", + "build:cjs": "tsc && cp -r src/maps lib", + "build:esm": "tsc --module esnext --target es2019 --outDir lib/esm && npm rum build:esm:fixup && echo '{\"type\":\"module\"}' > lib/esm/package.json", + "build:esm:fixup": "sed -i.b '1s|\".*json\"|\"../maps/entities-encode.json\" assert {type:\"json\"}|' lib/esm/encode-trie.js && rm lib/esm/encode-trie.js.b", "build:docs": "typedoc --hideGenerator src/index.ts", "build:trie": "ts-node scripts/write-decode-map.ts", "build:encode-map": "jq -c 'to_entries | reverse | map( {(.value) : .key } ) | sort | add' maps/entities.json > src/maps/entities-encode.json", @@ -62,7 +76,10 @@ "jest": { "preset": "ts-jest", "testEnvironment": "node", - "coverageProvider": "v8" + "coverageProvider": "v8", + "moduleNameMapper": { + "^(.*)\\.js$": "$1" + } }, "prettier": { "tabWidth": 4, diff --git a/scripts/trie/encode-trie.spec.ts b/scripts/trie/encode-trie.spec.ts index 0a8250f1..c3185167 100644 --- a/scripts/trie/encode-trie.spec.ts +++ b/scripts/trie/encode-trie.spec.ts @@ -1,5 +1,5 @@ -import { encodeTrie } from "./encode-trie"; -import type { TrieNode } from "./trie"; +import { encodeTrie } from "./encode-trie.js"; +import type { TrieNode } from "./trie.js"; describe("encode_trie", () => { it("should encode an empty node", () => { diff --git a/scripts/trie/encode-trie.ts b/scripts/trie/encode-trie.ts index 7b690b4f..2ea88325 100644 --- a/scripts/trie/encode-trie.ts +++ b/scripts/trie/encode-trie.ts @@ -1,7 +1,7 @@ /* eslint-disable node/no-unsupported-features/es-builtins */ import * as assert from "assert"; -import { TrieNode } from "./trie"; +import { TrieNode } from "./trie.js"; function binaryLength(num: number) { return Math.ceil(Math.log2(num)); diff --git a/scripts/trie/utils.ts b/scripts/trie/utils.ts index 37a6f4ca..d679e8af 100644 --- a/scripts/trie/utils.ts +++ b/scripts/trie/utils.ts @@ -1,6 +1,6 @@ -import { getTrie, TrieNode } from "./trie"; -import { encodeTrie } from "./encode-trie"; -import { BinTrieFlags } from "../../src/decode"; +import { getTrie, TrieNode } from "./trie.js"; +import { encodeTrie } from "./encode-trie.js"; +import { BinTrieFlags } from "../../src/decode.js"; import xmlMap from "../../maps/xml.json"; /** diff --git a/scripts/write-decode-map.ts b/scripts/write-decode-map.ts index 08dbc408..75cdaa0a 100644 --- a/scripts/write-decode-map.ts +++ b/scripts/write-decode-map.ts @@ -3,8 +3,8 @@ import entityMap from "../maps/entities.json"; import legacyMap from "../maps/legacy.json"; import xmlMap from "../maps/xml.json"; -import { getTrie } from "./trie/trie"; -import { encodeTrie } from "./trie/encode-trie"; +import { getTrie } from "./trie/trie.js"; +import { encodeTrie } from "./trie/encode-trie.js"; function convertMapToBinaryTrie( name: string, diff --git a/src/decode.spec.ts b/src/decode.spec.ts index 04d99fe9..e0eaa729 100644 --- a/src/decode.spec.ts +++ b/src/decode.spec.ts @@ -1,4 +1,4 @@ -import * as entities from "./decode"; +import * as entities from "./decode.js"; describe("Decode test", () => { const testcases = [ diff --git a/src/decode.ts b/src/decode.ts index 3b6d3bbd..36e409d1 100644 --- a/src/decode.ts +++ b/src/decode.ts @@ -1,9 +1,9 @@ -import htmlDecodeTree from "./generated/decode-data-html"; -import xmlDecodeTree from "./generated/decode-data-xml"; -import decodeCodePoint from "./decode_codepoint"; +import htmlDecodeTree from "./generated/decode-data-html.js"; +import xmlDecodeTree from "./generated/decode-data-xml.js"; +import decodeCodePoint from "./decode_codepoint.js"; // Re-export for use by eg. htmlparser2 -export { htmlDecodeTree, xmlDecodeTree }; +export { htmlDecodeTree, xmlDecodeTree, decodeCodePoint }; const enum CharCodes { NUM = 35, // "#" diff --git a/src/encode.ts b/src/encode.ts index 66defd8d..c872a712 100644 --- a/src/encode.ts +++ b/src/encode.ts @@ -1,4 +1,4 @@ -import { encodeHTMLTrieRe, getCodePoint } from "./encode-trie"; +import { encodeHTMLTrieRe, getCodePoint } from "./encode-trie.js"; const htmlReplacer = /[\t\n!-,./:-@[-`\f{-}$\x80-\uFFFF]/g; const xmlReplacer = /["&'<>$\x80-\uFFFF]/g; diff --git a/src/index.ts b/src/index.ts index 1978c15e..16912ada 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { decodeXML, decodeHTML, decodeHTMLStrict } from "./decode"; +import { decodeXML, decodeHTML, decodeHTMLStrict } from "./decode.js"; import { encodeXML, escapeUTF8, @@ -6,7 +6,7 @@ import { encodeNonAsciiHTML, escapeAttribute, escapeText, -} from "./encode"; +} from "./encode.js"; /** The level of entities to support. */ export enum EntityLevel { @@ -174,7 +174,7 @@ export { // Legacy aliases (deprecated) encodeHTML as encodeHTML4, encodeHTML as encodeHTML5, -} from "./encode"; +} from "./encode.js"; export { decodeXML, @@ -186,4 +186,4 @@ export { decodeHTMLStrict as decodeHTML4Strict, decodeHTMLStrict as decodeHTML5Strict, decodeXML as decodeXMLStrict, -} from "./decode"; +} from "./decode.js"; diff --git a/tsconfig.json b/tsconfig.json index 55c02961..62861a08 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ // "lib": [], /* Specify library files to be included in the compilation. */ "declaration": true /* Generates corresponding '.d.ts' file. */, "declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */, - // "sourceMap": true, /* Generates corresponding '.map' file. */ + "sourceMap": true /* Generates corresponding '.map' file. */, "outDir": "lib" /* Redirect output structure to the directory. */, // "importHelpers": true, /* Import emit helpers from 'tslib'. */ @@ -22,6 +22,7 @@ /* Module Resolution Options */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "moduleResolution": "node", "resolveJsonModule": true }, "include": ["src"],