Skip to content

Commit

Permalink
MISC: Add source map to transformed scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
catloversg committed Nov 29, 2024
1 parent 9c7223f commit 7cbdd1f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
11 changes: 10 additions & 1 deletion package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 16 additions & 2 deletions src/NetscriptJSEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -82,6 +83,7 @@ function generateLoadedModule(script: Script, scripts: Map<ScriptFilePath, Scrip
}

let scriptCode;
let sourceMap;
const fileType = getFileType(script.filename);
switch (fileType) {
case FileType.JS:
Expand All @@ -90,7 +92,7 @@ function generateLoadedModule(script: Script, scripts: Map<ScriptFilePath, Scrip
case FileType.JSX:
case FileType.TS:
case FileType.TSX:
scriptCode = transformScript(script.code, fileType);
({ scriptCode, sourceMap } = transformScript(script.code, fileType));
break;
default:
throw new Error(`Invalid file type: ${fileType}. Filename: ${script.filename}, server: ${script.server}.`);
Expand Down Expand Up @@ -176,7 +178,19 @@ function generateLoadedModule(script: Script, scripts: Map<ScriptFilePath, Scrip
// servers; it will be listed under the first server it was compiled for.
// We don't include this in the cache key, so that other instances of the
// script dedupe properly.
const adjustedCode = newCode + `\n//# sourceURL=${script.server}/${script.filename}`;
const sourceURL = `${script.server}/${script.filename}`;
let adjustedCode = newCode + `\n//# sourceURL=${sourceURL}`;
if (sourceMap) {
let inlineSourceMap;
try {
inlineSourceMap = fromObject({ ...JSON.parse(sourceMap), sources: [sourceURL], sourceRoot: "/" }).toComment();
} catch (error) {
console.warn(`Cannot generate inline source map for ${script.filename} in ${script.server}`, error);
}
if (inlineSourceMap) {
adjustedCode += `\n${inlineSourceMap}`;
}
}
// At this point we have the full code and can construct a new blob / assign the URL.

const url = URL.createObjectURL(makeScriptBlob(adjustedCode)) as ScriptURL;
Expand Down
18 changes: 11 additions & 7 deletions src/utils/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ export function getModuleScript(
/**
* This function must be synchronous to avoid race conditions. Check https://github.com/bitburner-official/bitburner-src/pull/1173#issuecomment-2026940461
* for more information.
*
* @param code
* @param fileType
* @returns
*/
export function transformScript(code: string, fileType: FileType): string | null | undefined {
export function transformScript(
code: string,
fileType: FileType,
): { scriptCode: string; sourceMap: string | undefined } {
if (supportedFileTypes.every((v) => v !== fileType)) {
throw new Error(`Invalid file type: ${fileType}`);
}
Expand All @@ -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,
};
}

0 comments on commit 7cbdd1f

Please sign in to comment.