From 4df1d37ddc54242c339765f22fb90ba2e9e3a99a Mon Sep 17 00:00:00 2001 From: dave caruso Date: Thu, 1 Jun 2023 21:16:47 -0400 Subject: Bundle and minify `.exports.js` files. (#3036) * move all exports.js into src/js * finalize the sort of this * and it works * add test.ts to gitignore * okay * convert some to ts just to show * finish up * fixup makefile * minify syntax in dev * finish rebase * dont minify all modules * merge * finish rebase merge * flaky test that hangs --- src/js/builtins/codegen/builtin-parser.ts | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/js/builtins/codegen/builtin-parser.ts (limited to 'src/js/builtins/codegen/builtin-parser.ts') diff --git a/src/js/builtins/codegen/builtin-parser.ts b/src/js/builtins/codegen/builtin-parser.ts new file mode 100644 index 000000000..e96d79c63 --- /dev/null +++ b/src/js/builtins/codegen/builtin-parser.ts @@ -0,0 +1,89 @@ +import { applyReplacements } from "./replacements"; + +/** + * Slices a string until it hits a }, but keeping in mind JS comments, + * regex, template literals, comments, and matching { + * + * Used to extract function bodies without parsing the code. + * + * If you pass replace=true, it will run replacements on the code + */ +export function sliceSourceCode( + contents: string, + replace: boolean, +): { result: string; rest: string; usesThis: boolean } { + let bracketCount = 0; + let i = 0; + let result = ""; + let usesThis = false; + while (contents.length) { + // TODO: template literal, regexp + // these are important because our replacement logic would replace intrinsics + // within these, when it should remain as the literal dollar. + // but this isn't used in the codebase + i = contents.match(/\/\*|\/\/|'|"|{|}|`/)?.index ?? contents.length; + const chunk = replace ? applyReplacements(contents.slice(0, i)) : contents.slice(0, i); + if (chunk.includes("this")) usesThis = true; + result += chunk; + contents = contents.slice(i); + if (!contents.length) break; + if (contents.startsWith("/*")) { + i = contents.slice(2).indexOf("*/") + 2; + } else if (contents.startsWith("//")) { + i = contents.slice(2).indexOf("\n") + 2; + } else if (contents.startsWith("'")) { + i = contents.slice(1).match(/(?