Skip to content

Commit

Permalink
Support generating sourceMaps for source debugging (smogon#7797)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviettran authored Dec 14, 2020
1 parent b7c3e6c commit c7c5f42
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 50 deletions.
6 changes: 6 additions & 0 deletions .eslintrc-no-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@
"mocha": true
},
"files": ["test/**/*.js"]
},
{
"files": ["build"],
"rules": {
"no-var": "off"
}
}
]
}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ databases/*.db*
.*-dist/
tools/set-import/importer.js
tools/modlog/converter.js
**/sourceMaps

# visual studio live share
.vs
Expand Down
61 changes: 12 additions & 49 deletions build
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
"use strict";

try {
// technically this was introduced in Node 11, but we'll ask for the most recent LTS with it to be safe
Expand All @@ -12,6 +13,7 @@ var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
var force = process.argv[2] === '--force';
var sucrase = require('./lib/build-utils').sucrase(force);

process.chdir(__dirname);

Expand All @@ -26,17 +28,17 @@ function replace(file, replacements) {
if (stats.isFile()) {
if (!file.endsWith('.js')) return;
fs.readFile(file, "utf-8", function (err, text) {
if (err) throw err;
var anyMatch = false;
for (var i = 0; i < replacements.length; i++) {
anyMatch = anyMatch || text.match(replacements[i].regex);
if (anyMatch) text = text.replace(replacements[i].regex, replacements[i].replace);
}
if (!anyMatch) return;
fs.writeFile(file, text, function (err) {
if (err) throw err;
var anyMatch = false;
for (var i = 0; i < replacements.length; i++) {
anyMatch = anyMatch || text.match(replacements[i].regex);
if (anyMatch) text = text.replace(replacements[i].regex, replacements[i].replace);
}
if (!anyMatch) return;
fs.writeFile(file, text, function (err) {
if (err) throw err;
});
});
});
} else if (stats.isDirectory()) {
fs.readdir(file, function (err, files) {
if (err) throw err;
Expand Down Expand Up @@ -72,45 +74,6 @@ try {
shell('npm install --production');
}

function needsSucrase(source, dest, path = '') {
if (path.endsWith('.ts')) {
if (path.endsWith('.d.ts')) return false;
const sourceStat = fs.lstatSync(source + path);
try {
const destStat = fs.lstatSync(dest + path.slice(0, -2) + 'js');
return (sourceStat.ctimeMs > destStat.ctimeMs);
} catch (e) {
// dest doesn't exist
return true;
}
}
if (!path.includes('.')) {
// probably dir
try {
const files = fs.readdirSync(source + path);
for (const file of files) {
if (needsSucrase(source, dest, path + '/' + file)) {
return true;
}
}
} catch (e) {
// not dir
}
return false;
}
return false;
}

function sucrase(src, out, opts) {
try {
if (!force && src !== './config' && !needsSucrase(src, out)) {
return false;
}
} catch (e) {}
shell(`npx sucrase ${opts || ''} -q ${src} -d ${out} --transforms typescript,imports --enable-legacy-typescript-module-interop`);
return true;
}

if (sucrase('./config', './.config-dist')) {
replace('.config-dist', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
Expand Down Expand Up @@ -139,7 +102,7 @@ if (sucrase('./server', './.server-dist')) {

sucrase('./translations', './.translations-dist');

if (sucrase('./tools/set-import', './tools/set-import', '--exclude-dirs=sets')) {
if (sucrase('./tools/set-import', './tools/set-import', null, ['sets'])) {
replace('./tools/set-import/importer.js', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
]);
Expand Down
129 changes: 129 additions & 0 deletions lib/build-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"use strict";

const {transform} = require("sucrase");
const fs = require("fs");
const path = require("path");

function needsSucrase(source, dest, path = "") {
if (path.endsWith(".ts")) {
if (path.endsWith(".d.ts")) return false;
const sourceStat = fs.lstatSync(source + path);
try {
const destStat = fs.lstatSync(dest + path.slice(0, -2) + "js");
return sourceStat.ctimeMs > destStat.ctimeMs;
} catch (e) {
// dest doesn't exist
return true;
}
}
if (!path.includes(".")) {
// probably dir
try {
const files = fs.readdirSync(source + path);
for (const file of files) {
if (needsSucrase(source, dest, path + "/" + file)) {
return true;
}
}
} catch (e) {
// not dir
}
return false;
}
return false;
}

// https://github.com/alangpierce/sucrase/blob/master/src/cli.ts
function findFiles(options) {
const outDirPath = options.outDirPath;
const srcDirPath = options.srcDirPath;
const pathToSource = options.pathToSource || "..";

const extensions = options.sucraseOptions.transforms.includes("typescript") ?
[".ts", ".tsx"] :
[".js", ".jsx"];

if (!fs.existsSync(outDirPath)) {
fs.mkdirSync(outDirPath);
}

const outArr = [];
for (const child of fs.readdirSync(srcDirPath)) {
if (
["node_modules", ".git"].includes(child) ||
options.excludeDirs.includes(child)
) {
continue;
}
const srcChildPath = path.join(srcDirPath, child);
const outChildPath = path.join(outDirPath, child);
const pathToSourceChild = path.join(pathToSource, "..");
if (fs.statSync(srcChildPath).isDirectory()) {
const innerOptions = {...options};
innerOptions.srcDirPath = srcChildPath;
innerOptions.outDirPath = outChildPath;
innerOptions.pathToSource = pathToSourceChild;
const innerFiles = findFiles(innerOptions);
outArr.push(...innerFiles);
} else if (extensions.some((ext) => srcChildPath.endsWith(ext))) {
const outPath = outChildPath.replace(
/\.\w+$/,
`.${options.outExtension}`
);
outArr.push({
srcPath: srcChildPath,
outPath,
pathToSource: pathToSource,
});
}
}

if (!fs.existsSync(path.join(outDirPath, "sourceMaps"))) {
fs.mkdirSync(path.join(outDirPath, "sourceMaps"));
}

return outArr;
}

exports.sucrase = (force) => (src, out, opts, excludeDirs = []) => {
try {
if (!force && src !== "./config" && !needsSucrase(src, out)) {
return false;
}
} catch (e) {}
const sucraseOptions = {
transforms: ["typescript", "imports"],
enableLegacyTypeScriptModuleInterop: true,

...opts,
};
const files = findFiles({
outDirPath: out,
srcDirPath: src,
sucraseOptions: sucraseOptions,
excludeDirs: ["sourceMaps", ...excludeDirs],
outExtension: "js",
});
for (const file of files) {
const sucraseOptionsFile = {
...sucraseOptions,
sourceMapOptions: {compiledFilename: file.outPath},
filePath: path.join(file.pathToSource, file.srcPath),
};
const code = fs.readFileSync(file.srcPath, "utf-8");
const transformed = transform(code, sucraseOptionsFile);
transformed.code += `\n //# sourceMappingURL=sourceMaps/${path.basename(
file.outPath
)}.map`;
fs.writeFileSync(
path.join(
path.dirname(file.outPath),
"sourceMaps",
path.basename(`${file.outPath}.map`)
),
JSON.stringify(transformed.sourceMap)
);
fs.writeFileSync(file.outPath, transformed.code);
}
return true;
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"./data/text/*",
"./data/mods/*/*",
"./dev-tools/*.ts",
"./lib/*",
"./lib/*.ts",
"./server/**/*.ts",
"./sim/**/*",
"./tools/set-import/*.ts",
Expand Down

0 comments on commit c7c5f42

Please sign in to comment.