Skip to content

Commit

Permalink
feat: Add ESM, stricter TS (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Apr 22, 2022
1 parent 3778341 commit 5aa9b84
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 32 deletions.
15 changes: 13 additions & 2 deletions package.json
Expand Up @@ -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/**/*"
],
Expand All @@ -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": {
Expand All @@ -67,7 +74,11 @@
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
"testEnvironment": "node",
"coverageProvider": "v8",
"moduleNameMapper": {
"^(.*)\\.js$": "$1"
}
},
"prettier": {
"tabWidth": 4
Expand Down
4 changes: 2 additions & 2 deletions 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 };

Expand All @@ -24,7 +24,7 @@ export class FeedHandler extends DomHandler {
super(callback, options);
}

onend(): void {
override onend(): void {
const feed = getFeed(this.dom);

if (feed) {
Expand Down
4 changes: 2 additions & 2 deletions 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",
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer.ts
Expand Up @@ -3,7 +3,7 @@ import {
xmlDecodeTree,
BinTrieFlags,
determineBranch,
} from "entities/lib/decode";
} from "entities/lib/decode.js";

const enum CharCodes {
Tab = 0x9, // "\t"
Expand Down
10 changes: 7 additions & 3 deletions 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!
Expand All @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions 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,
Expand All @@ -9,7 +9,7 @@ import {
Document,
} from "domhandler";

export { DomHandler, DomHandlerOptions };
export { DomHandler, type DomHandlerOptions };

type Options = ParserOptions & DomHandlerOptions;

Expand Down Expand Up @@ -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 };

Expand All @@ -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";
35 changes: 20 additions & 15 deletions 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"],
Expand Down

0 comments on commit 5aa9b84

Please sign in to comment.