Skip to content

Commit

Permalink
Merge pull request #65 from 5jiji/main
Browse files Browse the repository at this point in the history
Fixed source maps
  • Loading branch information
Naviary2 authored Jul 11, 2024
2 parents 4fa0b9c + 41c0c33 commit 339054e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
43 changes: 29 additions & 14 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { readdir, cp as copy, rm as remove, readFile, writeFile } from "node:fs/promises";
import { minify } from "terser";

/** Whether to generate source maps */
const generateMaps = true;
import { DEV_BUILD } from "./src/server/config/config.js";
import { exit } from "node:process";

/**
*
Expand All @@ -28,6 +27,15 @@ async function getExtFiles(path, ext) {
return files;
}

/**
* @param {string} path
* @returns {string}
*/
function getFilenamePath(path) {
const places = path.split("/");
return places[places.length-1];
}

await remove("./dist", {
recursive: true,
force: true,
Expand All @@ -38,31 +46,38 @@ await copy("./src/client", "./dist", {
force: true,
});

if (DEV_BUILD) exit();

const clientScript = await getExtFiles("./src/client/scripts", ".js");
const clientStyle = []; // await getExtFiles("./src/client/css", ".css");

const clientFiles = [];
clientFiles.push(...clientScript.map(v => `./src/client/scripts/${v}`), ...clientStyle.map(v => `./src/client/css/${v}`));
clientFiles.push(
...clientScript.map(v => `scripts/${v}`),
...clientStyle.map(v => `css/${v}`)
);

const filesToWrite = [];

for (const file of clientFiles) {
const filePath = `./dist/${file.replace('./src/client/', '')}`;
const code = await readFile(filePath, 'utf8');
const minified = await minify(code, {
const code = await readFile(`./src/client/${file}`, 'utf8');

const minifyInput = {};
minifyInput[`/src/client/${file}`] = code;

const minified = await minify(minifyInput, {
mangle: true, // Disable variable name mangling
compress: true, // Enable compression
sourceMap: generateMaps
sourceMap: {
includeSources: true,
url: `${getFilenamePath(file)}.map`,
}
});

filesToWrite.push(
writeFile(filePath, minified.code, 'utf8')
writeFile(`./dist/${file}`, minified.code, 'utf8'),
writeFile(`./dist/${file}.map`, minified.map, 'utf8')
);
if (generateMaps) {
filesToWrite.push(
writeFile(filePath + ".map", minified.map, "utf8")
);
}
}

await Promise.all(filesToWrite);
16 changes: 16 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"ws": "^8.16.0"
},
"devDependencies": {
"@types/node": "^20.14.10",
"terser": "^5.31.2"
},
"scripts": {
Expand Down

0 comments on commit 339054e

Please sign in to comment.