From 7cbdd1f759a4f30fee4e73d13240a364cf95e83c Mon Sep 17 00:00:00 2001 From: CatLover <152669316+catloversg@users.noreply.github.com> Date: Fri, 29 Nov 2024 22:14:10 +0700 Subject: [PATCH] MISC: Add source map to transformed scripts --- package-lock.json | 11 ++++++++++- package.json | 2 ++ src/NetscriptJSEvaluator.ts | 18 ++++++++++++++++-- src/utils/ScriptTransformer.ts | 18 +++++++++++------- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index bdd1fd31c9..f977456dd8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "bcryptjs": "^2.4.3", "better-react-mathjax": "^2.0.3", "clsx": "^1.2.1", + "convert-source-map": "^2.0.0", "date-fns": "^2.30.0", "escodegen": "^2.1.0", "jszip": "^3.10.1", @@ -60,6 +61,7 @@ "@swc/core": "^1.4.14", "@types/babel__standalone": "^7.1.7", "@types/bcryptjs": "^2.4.4", + "@types/convert-source-map": "^2.0.3", "@types/escodegen": "^0.0.7", "@types/file-saver": "^2.0.5", "@types/jest": "^29.5.5", @@ -4762,6 +4764,13 @@ "@types/node": "*" } }, + "node_modules/@types/convert-source-map": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@types/convert-source-map/-/convert-source-map-2.0.3.tgz", + "integrity": "sha512-ag0BfJLZf6CQz8VIuRIEYQ5Ggwk/82uvTQf27RcpyDNbY0Vw49LIPqAxk5tqYfrCs9xDaIMvl4aj7ZopnYL8bA==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/debug": { "version": "4.1.9", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.9.tgz", @@ -7095,7 +7104,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", "integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==", - "dev": true + "license": "MIT" }, "node_modules/cookie": { "version": "0.6.0", diff --git a/package.json b/package.json index 0a8fdbc467..176e9d714f 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "bcryptjs": "^2.4.3", "better-react-mathjax": "^2.0.3", "clsx": "^1.2.1", + "convert-source-map": "^2.0.0", "date-fns": "^2.30.0", "escodegen": "^2.1.0", "jszip": "^3.10.1", @@ -61,6 +62,7 @@ "@swc/core": "^1.4.14", "@types/babel__standalone": "^7.1.7", "@types/bcryptjs": "^2.4.4", + "@types/convert-source-map": "^2.0.3", "@types/escodegen": "^0.0.7", "@types/file-saver": "^2.0.5", "@types/jest": "^29.5.5", diff --git a/src/NetscriptJSEvaluator.ts b/src/NetscriptJSEvaluator.ts index 702682d8b6..811fdbd1aa 100644 --- a/src/NetscriptJSEvaluator.ts +++ b/src/NetscriptJSEvaluator.ts @@ -5,6 +5,7 @@ import * as walk from "acorn-walk"; import { parse } from "acorn"; import type * as acorn from "acorn"; +import { fromObject } from "convert-source-map"; import { LoadedModule, type ScriptURL, type ScriptModule } from "./Script/LoadedModule"; import type { Script } from "./Script/Script"; @@ -82,6 +83,7 @@ function generateLoadedModule(script: Script, scripts: Map v !== fileType)) { throw new Error(`Invalid file type: ${fileType}`); } @@ -149,10 +148,15 @@ export function transformScript(code: string, fileType: FileType): string | null parserConfig.jsx = true; } } - return transformSync(code, { + const result = transformSync(code, { jsc: { parser: parserConfig, target: "es2020", }, - }).code; + sourceMaps: true, + }); + return { + scriptCode: result.code, + sourceMap: result.map, + }; }