Skip to content

Commit

Permalink
feat: Add ESM (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored Apr 1, 2022
1 parent 7347f3a commit 2b2fbeb
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*"
],
Expand Down Expand Up @@ -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",
Expand All @@ -62,7 +76,10 @@
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"coverageProvider": "v8"
"coverageProvider": "v8",
"moduleNameMapper": {
"^(.*)\\.js$": "$1"
}
},
"prettier": {
"tabWidth": 4,
Expand Down
4 changes: 2 additions & 2 deletions scripts/trie/encode-trie.spec.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/trie/encode-trie.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
6 changes: 3 additions & 3 deletions scripts/trie/utils.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down
4 changes: 2 additions & 2 deletions scripts/write-decode-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/decode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as entities from "./decode";
import * as entities from "./decode.js";

describe("Decode test", () => {
const testcases = [
Expand Down
8 changes: 4 additions & 4 deletions src/decode.ts
Original file line number Diff line number Diff line change
@@ -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, // "#"
Expand Down
2 changes: 1 addition & 1 deletion src/encode.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { decodeXML, decodeHTML, decodeHTMLStrict } from "./decode";
import { decodeXML, decodeHTML, decodeHTMLStrict } from "./decode.js";
import {
encodeXML,
escapeUTF8,
encodeHTML,
encodeNonAsciiHTML,
escapeAttribute,
escapeText,
} from "./encode";
} from "./encode.js";

/** The level of entities to support. */
export enum EntityLevel {
Expand Down Expand Up @@ -174,7 +174,7 @@ export {
// Legacy aliases (deprecated)
encodeHTML as encodeHTML4,
encodeHTML as encodeHTML5,
} from "./encode";
} from "./encode.js";

export {
decodeXML,
Expand All @@ -186,4 +186,4 @@ export {
decodeHTMLStrict as decodeHTML4Strict,
decodeHTMLStrict as decodeHTML5Strict,
decodeXML as decodeXMLStrict,
} from "./decode";
} from "./decode.js";
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'. */

Expand All @@ -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"],
Expand Down

0 comments on commit 2b2fbeb

Please sign in to comment.