From 0ef9ef64618b11468ef7a05bb1b9a998a0f891c1 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Thu, 13 Oct 2022 10:49:00 +0530 Subject: [PATCH] fix: get rid of undefined symbols from the bundled api (#51) * fix: get rid of undefined symbols from the bundled api Fixes: https://github.com/postmanlabs/postject/issues/50 Signed-off-by: Darshan Sen * fixup! fix: apply suggestions from code review Signed-off-by: Darshan Sen --- scripts/build.mjs | 10 ++++++++++ test/cli.mjs | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/scripts/build.mjs b/scripts/build.mjs index 3adbf99..25c72b5 100755 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -38,6 +38,16 @@ await $`esbuild api.js --bundle --platform=node --outfile=../dist/api.js`; await fs.copy("../src/cli.js", "../dist/cli.js"); await fs.copy("../postject-api.h", "../dist/postject-api.h"); +// Repace all occurrences of `__filename` and `__dirname` with "" because +// Node.js core doesn't support it. These uses are functionally dead when +// `SINGLE_FILE` is enabled anyways. +// Refs: https://github.com/postmanlabs/postject/issues/50 +// TODO(RaisinTen): Send a PR to emsdk to get rid of these symbols from the +// affected code paths when `SINGLE_FILE` is enabled. +const contents = await fs.readFile("../dist/api.js", "utf-8"); +const replaced = contents.replace(/\b__filename\b|\b__dirname\b/g, "''"); +await fs.writeFile("../dist/api.js", replaced); + // Build tests if (!(await fs.exists("./test"))) { await $`mkdir test`; diff --git a/test/cli.mjs b/test/cli.mjs index c0a2bd0..37ccd58 100644 --- a/test/cli.mjs +++ b/test/cli.mjs @@ -231,3 +231,19 @@ describe("Inject data into Node.js using API", () => { } }).timeout(70000); }); + +describe("api.js should not contain __filename and __dirname", () => { + let contents; + + before(async () => { + contents = await fs.readFile("./dist/api.js", "utf-8"); + }); + + it("should not contain __filename", () => { + expect(contents).to.not.have.string("__filename"); + }); + + it("should not contain __dirname", () => { + expect(contents).to.not.have.string("__dirname"); + }); +});