fix: require errors with npm run build && npm run start

This commit is contained in:
Sainan 2024-05-05 23:12:55 +02:00
parent 37ecf29c4d
commit 2816db6b92
2 changed files with 13 additions and 1 deletions

View File

@ -4,7 +4,7 @@
"description": "WF Emulator",
"main": "index.ts",
"scripts": {
"start": "node build/index.js",
"start": "node --import ./build/src/pathman.js build/src/index.js",
"dev": "ts-node-dev --openssl-legacy-provider -r tsconfig-paths/register src/index.ts ",
"build": "tsc",
"lint": "eslint --ext .ts .",

12
src/pathman.ts Normal file
View File

@ -0,0 +1,12 @@
// Hooks node to support require from "@/" paths for `npm run build && npm run start`.
// Based on https://github.com/dividab/tsconfig-paths
const Module = require("module");
const originalResolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request: string, _parent: any): string {
if (request.substring(0, 2) == "@/") {
const modifiedArguments = [process.cwd() + "/build/" + request.substr(2), ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
return originalResolveFilename.apply(this, modifiedArguments);
}
return originalResolveFilename.apply(this, arguments);
};