From 074593a4107679f590b74750d370302e557ca677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= <188768+fb55@users.noreply.github.com> Date: Fri, 22 Apr 2022 12:25:29 +0100 Subject: [PATCH 1/2] feat: Add ESM, stricter TS --- package.json | 9 ++++++++- src/FeedHandler.ts | 4 ++-- src/Parser.ts | 4 ++-- src/Tokenizer.ts | 2 +- src/WritableStream.ts | 10 +++++++--- src/index.ts | 14 +++++++------- tsconfig.json | 35 ++++++++++++++++++++--------------- 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 0e301436..6fc87639 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,11 @@ }, "main": "lib/index.js", "types": "lib/index.d.ts", + "module": "lib/esm/index.js", + "exports": { + "require": "./lib/index.js", + "import": "./lib/esm/index.js" + }, "files": [ "lib/**/*" ], @@ -44,7 +49,9 @@ "format:es": "npm run lint:es -- --fix", "format:prettier": "npm run format:prettier:raw -- --write", "format:prettier:raw": "prettier '**/*.{ts,md,json,yml}'", - "build": "tsc", + "build": "npm run build:cjs && npm run build:esm", + "build:cjs": "tsc --sourceRoot https://raw.githubusercontent.com/fb55/htmlparser2/$(git rev-parse HEAD)/src/", + "build:esm": "npm run build:cjs -- --module esnext --target es2019 --outDir lib/esm && echo '{\"type\":\"module\"}' > lib/esm/package.json", "prepare": "npm run build" }, "dependencies": { diff --git a/src/FeedHandler.ts b/src/FeedHandler.ts index c5f15550..1e36ed5a 100644 --- a/src/FeedHandler.ts +++ b/src/FeedHandler.ts @@ -1,6 +1,6 @@ import DomHandler, { DomHandlerOptions } from "domhandler"; import { getFeed, Feed } from "domutils"; -import { Parser, ParserOptions } from "./Parser"; +import { Parser, ParserOptions } from "./Parser.js"; export { getFeed }; @@ -24,7 +24,7 @@ export class FeedHandler extends DomHandler { super(callback, options); } - onend(): void { + override onend(): void { const feed = getFeed(this.dom); if (feed) { diff --git a/src/Parser.ts b/src/Parser.ts index 6a04ec3a..f987d8ec 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -1,5 +1,5 @@ -import Tokenizer, { Callbacks, QuoteType } from "./Tokenizer"; -import decodeCodePoint from "entities/lib/decode_codepoint"; +import Tokenizer, { Callbacks, QuoteType } from "./Tokenizer.js"; +import { decodeCodePoint } from "entities/lib/decode.js"; const formTags = new Set([ "input", diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index 807d6669..70bbde71 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -3,7 +3,7 @@ import { xmlDecodeTree, BinTrieFlags, determineBranch, -} from "entities/lib/decode"; +} from "entities/lib/decode.js"; const enum CharCodes { Tab = 0x9, // "\t" diff --git a/src/WritableStream.ts b/src/WritableStream.ts index 932654c9..2f3a5af3 100644 --- a/src/WritableStream.ts +++ b/src/WritableStream.ts @@ -1,4 +1,4 @@ -import { Parser, Handler, ParserOptions } from "./Parser"; +import { Parser, Handler, ParserOptions } from "./Parser.js"; /* * NOTE: If either of these two imports produces a type error, * please update your @types/node dependency! @@ -25,14 +25,18 @@ export class WritableStream extends Writable { this._parser = new Parser(cbs, options); } - _write(chunk: string | Buffer, encoding: string, cb: () => void): void { + override _write( + chunk: string | Buffer, + encoding: string, + cb: () => void + ): void { this._parser.write( isBuffer(chunk, encoding) ? this._decoder.write(chunk) : chunk ); cb(); } - _final(cb: () => void): void { + override _final(cb: () => void): void { this._parser.end(this._decoder.end()); cb(); } diff --git a/src/index.ts b/src/index.ts index aab2af7e..2b144ce3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import { Parser, ParserOptions } from "./Parser"; -export { Parser, ParserOptions }; +import { Parser, ParserOptions } from "./Parser.js"; +export { Parser, type ParserOptions }; import { DomHandler, @@ -9,7 +9,7 @@ import { Document, } from "domhandler"; -export { DomHandler, DomHandlerOptions }; +export { DomHandler, type DomHandlerOptions }; type Options = ParserOptions & DomHandlerOptions; @@ -57,8 +57,8 @@ export function createDomStream( export { default as Tokenizer, - Callbacks as TokenizerCallbacks, -} from "./Tokenizer"; + type Callbacks as TokenizerCallbacks, +} from "./Tokenizer.js"; import * as ElementType from "domelementtype"; export { ElementType }; @@ -67,9 +67,9 @@ export { ElementType }; * They should probably be removed eventually. */ -export * from "./FeedHandler"; +export * from "./FeedHandler.js"; export * as DomUtils from "domutils"; // Old names for Dom- & FeedHandler export { DomHandler as DefaultHandler }; -export { FeedHandler as RssHandler } from "./FeedHandler"; +export { FeedHandler as RssHandler } from "./FeedHandler.js"; diff --git a/tsconfig.json b/tsconfig.json index 5d16dc75..045b82d5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,27 +1,32 @@ { "compilerOptions": { /* Basic Options */ - "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */, - "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, - // "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. */ - "outDir": "lib" /* Redirect output structure to the directory. */, - // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + "target": "es5", + "module": "commonjs", + "lib": ["ES2015.Core"], + "declaration": true, + "declarationMap": true, + "sourceMap": true, + "outDir": "lib", /* Strict Type-Checking Options */ - "strict": true /* Enable all strict type-checking options. */, + "strict": true, /* Additional Checks */ - "noUnusedLocals": true /* Report errors on unused locals. */, - "noUnusedParameters": true /* Report errors on unused parameters. */, - "noImplicitReturns": true /* Report error when not all code paths in function return a value. */, - "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */, + "exactOptionalPropertyTypes": true, + "forceConsistentCasingInFileNames": true, + "importsNotUsedAsValues": "error", + "isolatedModules": true, + "noFallthroughCasesInSwitch": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "noPropertyAccessFromIndexSignature": true, + "noUnusedLocals": true, + "noUnusedParameters": true, /* 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'. */, + "esModuleInterop": true, + "moduleResolution": "node", "resolveJsonModule": true }, "include": ["src"], From c1465d5fa0bfda57f386f31464d31b990a13b2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= <188768+fb55@users.noreply.github.com> Date: Fri, 22 Apr 2022 12:29:47 +0100 Subject: [PATCH 2/2] Add moduleNameMapper for jest --- package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 6fc87639..9b744dc7 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,11 @@ }, "jest": { "preset": "ts-jest", - "testEnvironment": "node" + "testEnvironment": "node", + "coverageProvider": "v8", + "moduleNameMapper": { + "^(.*)\\.js$": "$1" + } }, "prettier": { "tabWidth": 4