From f95182ee20bac6a9f357ddc85e8174a3d5bf9972 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Wed, 3 Jul 2024 01:00:31 +0200 Subject: [PATCH] Rollback --- backend/package.json | 9 ++++++--- backend/scripts/rename-mjs.ts | 27 +++++++++++++++++++++++++++ backend/src/lib/config/env.ts | 2 +- backend/src/server/app.ts | 2 +- backend/tsup.config.js | 33 ++++++++++++++++++++++++++++++++- 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 backend/scripts/rename-mjs.ts diff --git a/backend/package.json b/backend/package.json index 4d67a84d41..47081621c0 100644 --- a/backend/package.json +++ b/backend/package.json @@ -2,7 +2,7 @@ "name": "backend", "version": "1.0.0", "description": "", - "main": "./dist/main.js", + "main": "./dist/main.mjs", "bin": "dist/main.js", "pkg": { "scripts": [ @@ -25,16 +25,19 @@ "outputPath": "binary" }, "scripts": { - "binary:build": "npm run binary:clean && npm run build:frontend && npm run build && npm run binary:babel-frontend", + "binary:build": "npm run binary:clean && npm run build:frontend && npm run build && npm run binary:babel-frontend && npm run binary:babel-backend && npm run binary:rename-imports", "binary:package": "pkg --no-bytecode --public-packages \"*\" --public --target host .", + "binary:babel-backend": " babel ./dist -d ./dist", "binary:babel-frontend": "babel --copy-files ../frontend/.next/server -d ../frontend/.next/server", "binary:clean": "rm -rf ./dist && rm -rf ./binary", + "binary:rename-imports": "ts-node ./scripts/rename-mjs.ts", + "test": "echo \"Error: no test specified\" && exit 1", "dev": "tsx watch --clear-screen=false ./src/main.ts | pino-pretty --colorize --colorizeObjects --singleLine", "dev:docker": "nodemon", "build": "tsup", "build:frontend": "npm run build --prefix ../frontend", - "start": "node dist/main.js", + "start": "node dist/main.mjs", "type:check": "tsc --noEmit", "lint:fix": "eslint --fix --ext js,ts ./src", "lint": "eslint 'src/**/*.ts'", diff --git a/backend/scripts/rename-mjs.ts b/backend/scripts/rename-mjs.ts new file mode 100644 index 0000000000..793cb98917 --- /dev/null +++ b/backend/scripts/rename-mjs.ts @@ -0,0 +1,27 @@ +/* eslint-disable @typescript-eslint/no-shadow */ +import fs from "node:fs"; +import path from "node:path"; + +function replaceMjsOccurrences(directory: string) { + fs.readdir(directory, (err, files) => { + if (err) throw err; + files.forEach((file) => { + const filePath = path.join(directory, file); + if (fs.statSync(filePath).isDirectory()) { + replaceMjsOccurrences(filePath); + } else { + fs.readFile(filePath, "utf8", (err, data) => { + if (err) throw err; + const result = data.replace(/\.mjs/g, ".js"); + fs.writeFile(filePath, result, "utf8", (err) => { + if (err) throw err; + // eslint-disable-next-line no-console + console.log(`Updated: ${filePath}`); + }); + }); + } + }); + }); +} + +replaceMjsOccurrences("dist"); diff --git a/backend/src/lib/config/env.ts b/backend/src/lib/config/env.ts index 4d7d39dbd4..90d6a50b51 100644 --- a/backend/src/lib/config/env.ts +++ b/backend/src/lib/config/env.ts @@ -6,7 +6,7 @@ import { zpStr } from "../zod"; export const GITLAB_URL = "https://gitlab.com"; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any -- If `process.pkg` is set, and it's true, then it means that the app is currently running in a packaged environment (a binary) -export const IS_PACKAGED = (process as any)?.pkg === true; +export const IS_PACKAGED = (process as any)?.pkg !== undefined; const zodStrBool = z .enum(["true", "false"]) diff --git a/backend/src/server/app.ts b/backend/src/server/app.ts index c61e121133..ee8acec0fe 100644 --- a/backend/src/server/app.ts +++ b/backend/src/server/app.ts @@ -80,7 +80,7 @@ export const main = async ({ db, smtp, logger, queue, keyStore }: TMain) => { if (appCfg.isProductionMode) { await server.register(registerExternalNextjs, { - standaloneMode: appCfg.STANDALONE_MODE, + standaloneMode: appCfg.STANDALONE_MODE || IS_PACKAGED, dir: path.join(__dirname, IS_PACKAGED ? "../../../" : "../../"), port: appCfg.PORT }); diff --git a/backend/tsup.config.js b/backend/tsup.config.js index 3ba6ad237f..9e37870ba5 100644 --- a/backend/tsup.config.js +++ b/backend/tsup.config.js @@ -17,7 +17,8 @@ export default defineConfig({ clean: true, minify: false, keepNames: true, - splitting: true, + splitting: false, + format: "esm", // copy the files to output loader: { ".handlebars": "copy", @@ -31,7 +32,37 @@ export default defineConfig({ entry: ["./src"], sourceMap: true, skipNodeModulesBundle: true, + esbuildPlugins: [ + { + // esm directory import are not allowed + // /folder1 should be explicitly imported as /folder1/index.ts + // this plugin will append it automatically on build time to all imports + name: "commonjs-esm-directory-import", + setup(build) { + build.onResolve({ filter: /.*/ }, async (args) => { + if (args.importer) { + if (args.kind === "import-statement") { + const isRelativePath = args.path.startsWith("."); + const absPath = isRelativePath + ? path.join(args.resolveDir, args.path) + : path.join(args.path.replace("@app", "./src")); + const isFile = await fs + .stat(`${absPath}.ts`) + .then((el) => el.isFile) + .catch((err) => err.code === "ENOTDIR"); + + return { + path: isFile ? `${args.path}.mjs` : `${args.path}/index.mjs`, + external: true + }; + } + } + return undefined; + }); + } + } + ], async onSuccess() { // this will replace all tsconfig paths await replaceTscAliasPaths({