Skip to content

Commit

Permalink
feat: include source maps into the npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Mar 3, 2024
1 parent 8425dee commit ef98518
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 130 deletions.
69 changes: 1 addition & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": [
Expand Down
106 changes: 52 additions & 54 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,30 @@ const allNamedReferences = {
all: namedReferences.html5
};

// MACRO from https://github.com/LeDDGroup/typescript-transform-macros
declare function MACRO<T>(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';

Expand Down Expand Up @@ -131,36 +126,39 @@ const defaultDecodeEntityOptions: CommonOptions = {
level: 'all'
};

const getDecodedEntity = MACRO(
(entity: string, references: Record<string, string>, 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<string, string>,
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(
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outDir": "./lib",
"strict": true,
"resolveJsonModule": true,
"plugins": [{ "transform": "typescript-transform-macros" }]
"sourceMap": true
},
"include": ["src"],
"exclude": ["node_modules", "test"]
Expand Down

0 comments on commit ef98518

Please sign in to comment.