diff --git a/.eslintrc.json b/.eslintrc.json index b6d7249e..c6c74370 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -59,6 +59,8 @@ "@typescript-eslint/prefer-includes": 2, "@typescript-eslint/switch-exhaustiveness-check": 2, + "dot-notation": 0, + "jsdoc/require-param-type": 0, "jsdoc/require-returns-type": 0 } diff --git a/package.json b/package.json index b4edcead..460523d8 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,11 @@ "sideEffects": false, "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/**/*" ], @@ -23,8 +28,9 @@ "format:es": "npm run lint:es -- --fix", "format:prettier": "npm run prettier -- --write", "prettier": "prettier \"**/*.{ts,md,json,yml}\" --ignore-path .gitignore", - "build": "tsc", - "build:docs": "typedoc --hideGenerator --exclude \"**/*+(index|.spec).ts\" --categorizeByGroup false --sort enum-value-ascending --sort alphabetical src", + "build": "npm run build:cjs && npm run build:esm", + "build:cjs": "tsc --sourceRoot https://raw.githubusercontent.com/fb55/domutils/$(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" }, "repository": { @@ -57,7 +63,11 @@ }, "jest": { "preset": "ts-jest", - "testEnvironment": "node" + "testEnvironment": "node", + "coverageProvider": "v8", + "moduleNameMapper": { + "^(.*)\\.js$": "$1" + } }, "prettier": { "tabWidth": 4 diff --git a/src/feeds.ts b/src/feeds.ts index 2f25f2e5..8e942bde 100644 --- a/src/feeds.ts +++ b/src/feeds.ts @@ -1,6 +1,6 @@ import type { AnyNode, Element } from "domhandler"; -import { textContent } from "./stringify"; -import { getElementsByTagName } from "./legacy"; +import { textContent } from "./stringify.js"; +import { getElementsByTagName } from "./legacy.js"; /** * The type of a media item. @@ -108,7 +108,7 @@ function getAtomFeed(feedRoot: Element) { addConditionally(entry, "id", "id", children); addConditionally(entry, "title", "title", children); - const href = getOneElement("link", children)?.attribs.href; + const href = getOneElement("link", children)?.attribs["href"]; if (href) { entry.link = href; } @@ -130,7 +130,7 @@ function getAtomFeed(feedRoot: Element) { addConditionally(feed, "id", "id", childs); addConditionally(feed, "title", "title", childs); - const href = getOneElement("link", childs)?.attribs.href; + const href = getOneElement("link", childs)?.attribs["href"]; if (href) { feed.link = href; } @@ -211,10 +211,10 @@ function getMediaElements(where: AnyNode[]): FeedItemMedia[] { const { attribs } = elem; const media: FeedItemMedia = { - medium: attribs.medium as unknown as + medium: attribs["medium"] as unknown as | FeedItemMediaMedium | undefined, - isDefault: !!attribs.isDefault, + isDefault: !!attribs["isDefault"], }; for (const attrib of MEDIA_KEYS_STRING) { @@ -229,9 +229,10 @@ function getMediaElements(where: AnyNode[]): FeedItemMedia[] { } } - if (attribs.expression) { - media.expression = - attribs.expression as unknown as FeedItemMediaExpression; + if (attribs["expression"]) { + media.expression = attribs[ + "expression" + ] as unknown as FeedItemMediaExpression; } return media; diff --git a/src/index.ts b/src/index.ts index 9a236944..6ecab3b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ -export * from "./stringify"; -export * from "./traversal"; -export * from "./manipulation"; -export * from "./querying"; -export * from "./legacy"; -export * from "./helpers"; -export * from "./feeds"; +export * from "./stringify.js"; +export * from "./traversal.js"; +export * from "./manipulation.js"; +export * from "./querying.js"; +export * from "./legacy.js"; +export * from "./helpers.js"; +export * from "./feeds.js"; /** @deprecated Use these methods from `domhandler` directly. */ export { isTag, diff --git a/src/legacy.ts b/src/legacy.ts index 9aac19ff..5449d889 100644 --- a/src/legacy.ts +++ b/src/legacy.ts @@ -1,6 +1,6 @@ import { isTag, isText, AnyNode, Element } from "domhandler"; -import { ElementType } from "domelementtype"; -import { filter, findOne } from "./querying"; +import type { ElementType } from "domelementtype"; +import { filter, findOne } from "./querying.js"; type TestType = (elem: AnyNode) => boolean; @@ -148,7 +148,12 @@ export function getElementsByTagName( recurse = true, limit = Infinity ): Element[] { - return filter(Checks.tag_name(tagName), nodes, recurse, limit) as Element[]; + return filter( + Checks["tag_name"](tagName), + nodes, + recurse, + limit + ) as Element[]; } /** @@ -165,5 +170,5 @@ export function getElementsByTagType( recurse = true, limit = Infinity ): AnyNode[] { - return filter(Checks.tag_type(type as string), nodes, recurse, limit); + return filter(Checks["tag_type"](type as string), nodes, recurse, limit); } 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"],