Skip to content

Inject __dirname for commonjs compatibility instead of banner#94

Open
adcreare wants to merge 6 commits into
mainfrom
inject-commonjs-compatability
Open

Inject __dirname for commonjs compatibility instead of banner#94
adcreare wants to merge 6 commits into
mainfrom
inject-commonjs-compatability

Conversation

@adcreare

Copy link
Copy Markdown

Closes #93

@adcreare adcreare self-assigned this Jul 22, 2026
@adcreare adcreare added the MINOR label Jul 22, 2026
@adcreare
adcreare marked this pull request as ready for review July 22, 2026 02:26
Copilot AI review requested due to automatic review settings July 22, 2026 02:26
@adcreare
adcreare requested a review from carlansley July 22, 2026 02:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the esbuild bundling prelude logic to provide CommonJS-compatibility __dirname via esbuild’s inject mechanism (rather than defining __dirname directly in a banner), avoiding collisions when downstream dependencies also introduce top-level __dirname (per issue #93).

Changes:

  • Removes __dirname computation from the esbuild banner and instead injects an __dirname provider module when bundling (outFile set).
  • Refactors the TypeScript config root path calculation into a packageRoot constant.
  • Updates compile bundling tests to expect the injected __dirname prelude and adds coverage for a project-level __dirname declaration coexisting with injected usage.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/compile.ts Switches __dirname from a banner-defined constant to an esbuild injected symbol to avoid naming collisions in bundled output.
src/compile.spec.ts Updates expected bundle prelude output and adds a test ensuring local __dirname declarations don’t break injected __dirname usage.
package.json Bumps package version to 10.2.0.
package-lock.json Updates lockfile version fields to match 10.2.0.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/compile.ts
Comment thread src/compile.ts Outdated
Comment on lines +20 to +22
const dirNameJsCompatabilityInjection = `import path from 'node:path';
export const __dirname = path.dirname(__filename);`; // relies on __filename from commonJsCompatabilityBanner
const dirNameJsCompatabilityInjectionURL = `data:text/javascript,${encodeURIComponent(dirNameJsCompatabilityInjection)}`;
@github-actions

Copy link
Copy Markdown

❌ PR review status - has 1 reviewer outstanding

@github-actions

Copy link
Copy Markdown

Beta Published - Install Command: npm install @checkdigit/typescript-config@10.2.0-PR.94-ce04

@carlansley carlansley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

behaviorally lgtm, just a couple of minor things

Comment thread src/compile.spec.ts
);

const output = await import(path.join(outDir, 'index.mjs'));
assert.deepEqual(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be simpler:

assert.equal(output.injectedDirname, await fs.realpath(outDir));
assert.equal(output.localDirname, 'declared in project');

Comment thread src/compile.ts
typescript.sys,
// @checkdigit/typescript-config package root:
path.dirname(path.dirname(fileURLToPath(import.meta.url))),
packageRoot,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to have this run at the module level? not sure I understand this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle __dirname when other dependencies are adding it as banner

3 participants