From ef98518377ef2eda47aebab98d9c1642a0fbbd47 Mon Sep 17 00:00:00 2001 From: Marat Dulin Date: Mon, 4 Mar 2024 00:37:00 +0100 Subject: [PATCH] feat: include source maps into the npm package --- package-lock.json | 69 +----------------------------- package.json | 13 +++--- src/index.ts | 106 +++++++++++++++++++++++----------------------- tsconfig.json | 2 +- 4 files changed, 60 insertions(+), 130 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8370722..24b16a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,9 +40,7 @@ "prettier": "^2.1.2", "terser": "^5.6.1", "ts-node": "^8.9.1", - "ttypescript": "^1.5.15", - "typescript": "^3.8.3", - "typescript-transform-macros": "^1.1.1" + "typescript": "^3.8.3" } }, "node_modules/@babel/code-frame": { @@ -3855,37 +3853,6 @@ "typescript": ">=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta" } }, - "node_modules/ttypescript": { - "version": "1.5.15", - "resolved": "https://registry.npmjs.org/ttypescript/-/ttypescript-1.5.15.tgz", - "integrity": "sha512-48ykDNHzFnPMnv4hYX1P8Q84TvCZyL1QlFxeuxsuZ48X2+ameBgPenvmCkHJtoOSxpoWTWi8NcgNrRnVDOmfSg==", - "dev": true, - "dependencies": { - "resolve": ">=1.9.0" - }, - "bin": { - "ttsc": "bin/tsc", - "ttsserver": "bin/tsserver" - }, - "peerDependencies": { - "ts-node": ">=8.0.2", - "typescript": ">=3.2.2" - } - }, - "node_modules/ttypescript/node_modules/resolve": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.20.0.tgz", - "integrity": "sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==", - "dev": true, - "license": "MIT", - "dependencies": { - "is-core-module": "^2.2.0", - "path-parse": "^1.0.6" - }, - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, "node_modules/type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", @@ -3940,13 +3907,6 @@ "dev": true, "license": "MIT" }, - "node_modules/typescript-transform-macros": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/typescript-transform-macros/-/typescript-transform-macros-1.1.1.tgz", - "integrity": "sha512-ljKucGAxTOxxH37qfnMZh7yxSN0OPQe6V5eENahNPpfyq24TVD+pEJtdzxTw1dSobwHWcYOXh4wE4/ko4Or+iw==", - "dev": true, - "license": "MIT" - }, "node_modules/uri-js": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", @@ -6807,27 +6767,6 @@ "tslib": "^1.8.1" } }, - "ttypescript": { - "version": "1.5.15", - "resolved": "https://registry.npmjs.org/ttypescript/-/ttypescript-1.5.15.tgz", - "integrity": "sha512-48ykDNHzFnPMnv4hYX1P8Q84TvCZyL1QlFxeuxsuZ48X2+ameBgPenvmCkHJtoOSxpoWTWi8NcgNrRnVDOmfSg==", - "dev": true, - "requires": { - "resolve": ">=1.9.0" - }, - "dependencies": { - "resolve": { - "version": "1.20.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.20.0.tgz", - "integrity": "sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==", - "dev": true, - "requires": { - "is-core-module": "^2.2.0", - "path-parse": "^1.0.6" - } - } - } - }, "type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", @@ -6861,12 +6800,6 @@ "integrity": "sha1-uk99si2RU0oZKdkACdzhYety/T8=", "dev": true }, - "typescript-transform-macros": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/typescript-transform-macros/-/typescript-transform-macros-1.1.1.tgz", - "integrity": "sha512-ljKucGAxTOxxH37qfnMZh7yxSN0OPQe6V5eENahNPpfyq24TVD+pEJtdzxTw1dSobwHWcYOXh4wE4/ko4Or+iw==", - "dev": true - }, "uri-js": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", diff --git a/package.json b/package.json index 0e6851d..b2d2f0c 100644 --- a/package.json +++ b/package.json @@ -47,9 +47,7 @@ "prettier": "^2.1.2", "terser": "^5.6.1", "ts-node": "^8.9.1", - "ttypescript": "^1.5.15", - "typescript": "^3.8.3", - "typescript-transform-macros": "^1.1.1" + "typescript": "^3.8.3" }, "repository": { "type": "git", @@ -60,14 +58,15 @@ "typings": "./lib/index.d.ts", "types": "./lib/index.d.ts", "scripts": { - "test": "TS_NODE_COMPILER=ttypescript mocha --recursive -r ts-node/register test/**/*.ts", + "test": "mocha --recursive -r ts-node/register test/**/*.ts", + "test:watch": "mocha -w --recursive -r ts-node/register test/**/*.ts", "test:lib": "TEST_LIB=1 npm run test", - "benchmark": "TS_NODE_COMPILER=ttypescript ts-node benchmark/benchmark", + "benchmark": "ts-node benchmark/benchmark", "lint": "eslint src/**.ts", "flow-type-gen": "flowgen --add-flow-header lib/index.d.ts -o lib/index.js.flow", "remove-unused-declarations": "find lib -type f \\( -name '*.d.ts' ! -name index.d.ts \\) | xargs rm", - "minimize-lib-files": "find lib -type f \\( -name '*.js' ! -name index.js \\) | while read fn; do terser $fn -o $fn; done", - "build": "rm -Rf lib/* && ttsc && npm run remove-unused-declarations && npm run flow-type-gen && npm run minimize-lib-files && npm run test:lib", + "minimize-lib-files": "cd lib && find . -type f \\( -name '*.js' \\) | while read fn; do terser --source-map \"content='$fn.map',filename='$fn.map',url='$fn.map'\" $fn -o $fn; done", + "build": "rm -Rf lib/* && tsc && npm run remove-unused-declarations && npm run flow-type-gen && npm run minimize-lib-files && npm run test:lib", "prepublishOnly": "npm run build" }, "files": [ diff --git a/src/index.ts b/src/index.ts index 49fbe21..7817610 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,35 +7,30 @@ const allNamedReferences = { all: namedReferences.html5 }; -// MACRO from https://github.com/LeDDGroup/typescript-transform-macros -declare function MACRO(t: T): T; - -const replaceUsingRegExp = MACRO( - (macroText: string, macroRegExp: RegExp, macroReplacer: (input: string) => string): string => { - macroRegExp.lastIndex = 0; - let replaceMatch = macroRegExp.exec(macroText); - let replaceResult; - if (replaceMatch) { - replaceResult = ''; - let replaceLastIndex = 0; - do { - if (replaceLastIndex !== replaceMatch.index) { - replaceResult += macroText.substring(replaceLastIndex, replaceMatch.index); - } - const replaceInput = replaceMatch[0]; - replaceResult += macroReplacer(replaceInput); - replaceLastIndex = replaceMatch.index + replaceInput.length; - } while ((replaceMatch = macroRegExp.exec(macroText))); - - if (replaceLastIndex !== macroText.length) { - replaceResult += macroText.substring(replaceLastIndex); +function replaceUsingRegExp(macroText: string, macroRegExp: RegExp, macroReplacer: (input: string) => string): string { + macroRegExp.lastIndex = 0; + let replaceMatch = macroRegExp.exec(macroText); + let replaceResult; + if (replaceMatch) { + replaceResult = ''; + let replaceLastIndex = 0; + do { + if (replaceLastIndex !== replaceMatch.index) { + replaceResult += macroText.substring(replaceLastIndex, replaceMatch.index); } - } else { - replaceResult = macroText; + const replaceInput = replaceMatch[0]; + replaceResult += macroReplacer(replaceInput); + replaceLastIndex = replaceMatch.index + replaceInput.length; + } while ((replaceMatch = macroRegExp.exec(macroText))); + + if (replaceLastIndex !== macroText.length) { + replaceResult += macroText.substring(replaceLastIndex); } - return replaceResult; + } else { + replaceResult = macroText; } -); + return replaceResult; +} export type Level = 'xml' | 'html4' | 'html5' | 'all'; @@ -131,36 +126,39 @@ const defaultDecodeEntityOptions: CommonOptions = { level: 'all' }; -const getDecodedEntity = MACRO( - (entity: string, references: Record, isAttribute: boolean, isStrict: boolean): string => { - let decodeResult = entity; - const decodeEntityLastChar = entity[entity.length - 1]; - if (isAttribute && decodeEntityLastChar === '=') { - decodeResult = entity; - } else if (isStrict && decodeEntityLastChar !== ';') { - decodeResult = entity; - } else { - const decodeResultByReference = references[entity]; - if (decodeResultByReference) { - decodeResult = decodeResultByReference; - } else if (entity[0] === '&' && entity[1] === '#') { - const decodeSecondChar = entity[2]; - const decodeCode = - decodeSecondChar == 'x' || decodeSecondChar == 'X' - ? parseInt(entity.substr(3), 16) - : parseInt(entity.substr(2)); - - decodeResult = - decodeCode >= 0x10ffff - ? outOfBoundsChar - : decodeCode > 65535 - ? fromCodePoint(decodeCode) - : fromCharCode(numericUnicodeMap[decodeCode] || decodeCode); - } +function getDecodedEntity( + entity: string, + references: Record, + isAttribute: boolean, + isStrict: boolean +): string { + let decodeResult = entity; + const decodeEntityLastChar = entity[entity.length - 1]; + if (isAttribute && decodeEntityLastChar === '=') { + decodeResult = entity; + } else if (isStrict && decodeEntityLastChar !== ';') { + decodeResult = entity; + } else { + const decodeResultByReference = references[entity]; + if (decodeResultByReference) { + decodeResult = decodeResultByReference; + } else if (entity[0] === '&' && entity[1] === '#') { + const decodeSecondChar = entity[2]; + const decodeCode = + decodeSecondChar == 'x' || decodeSecondChar == 'X' + ? parseInt(entity.substr(3), 16) + : parseInt(entity.substr(2)); + + decodeResult = + decodeCode >= 0x10ffff + ? outOfBoundsChar + : decodeCode > 65535 + ? fromCodePoint(decodeCode) + : fromCharCode(numericUnicodeMap[decodeCode] || decodeCode); } - return decodeResult; } -); + return decodeResult; +} /** Decodes a single entity */ export function decodeEntity( diff --git a/tsconfig.json b/tsconfig.json index 8cf2d71..68d6d41 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ "outDir": "./lib", "strict": true, "resolveJsonModule": true, - "plugins": [{ "transform": "typescript-transform-macros" }] + "sourceMap": true }, "include": ["src"], "exclude": ["node_modules", "test"]