Skip to content

Commit 0ef9ef6

Browse files
authored
fix: get rid of undefined symbols from the bundled api (#51)
* fix: get rid of undefined symbols from the bundled api Fixes: #50 Signed-off-by: Darshan Sen <[email protected]> * fixup! fix: apply suggestions from code review Signed-off-by: Darshan Sen <[email protected]>
1 parent 52502e6 commit 0ef9ef6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

scripts/build.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ await $`esbuild api.js --bundle --platform=node --outfile=../dist/api.js`;
3838
await fs.copy("../src/cli.js", "../dist/cli.js");
3939
await fs.copy("../postject-api.h", "../dist/postject-api.h");
4040

41+
// Repace all occurrences of `__filename` and `__dirname` with "" because
42+
// Node.js core doesn't support it. These uses are functionally dead when
43+
// `SINGLE_FILE` is enabled anyways.
44+
// Refs: https://github.com/postmanlabs/postject/issues/50
45+
// TODO(RaisinTen): Send a PR to emsdk to get rid of these symbols from the
46+
// affected code paths when `SINGLE_FILE` is enabled.
47+
const contents = await fs.readFile("../dist/api.js", "utf-8");
48+
const replaced = contents.replace(/\b__filename\b|\b__dirname\b/g, "''");
49+
await fs.writeFile("../dist/api.js", replaced);
50+
4151
// Build tests
4252
if (!(await fs.exists("./test"))) {
4353
await $`mkdir test`;

test/cli.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,19 @@ describe("Inject data into Node.js using API", () => {
231231
}
232232
}).timeout(70000);
233233
});
234+
235+
describe("api.js should not contain __filename and __dirname", () => {
236+
let contents;
237+
238+
before(async () => {
239+
contents = await fs.readFile("./dist/api.js", "utf-8");
240+
});
241+
242+
it("should not contain __filename", () => {
243+
expect(contents).to.not.have.string("__filename");
244+
});
245+
246+
it("should not contain __dirname", () => {
247+
expect(contents).to.not.have.string("__dirname");
248+
});
249+
});

0 commit comments

Comments
 (0)