Skip to content

Disable __dirname & __filename webpack polyfills#11146

Open
dangrima90 wants to merge 1 commit intoNativeScript:mainfrom
dangrima90:fix/ignore-dirname-filename-webpack-polyfills
Open

Disable __dirname & __filename webpack polyfills#11146
dangrima90 wants to merge 1 commit intoNativeScript:mainfrom
dangrima90:fix/ignore-dirname-filename-webpack-polyfills

Conversation

@dangrima90
Copy link
Copy Markdown

@dangrima90 dangrima90 commented Mar 27, 2026

PR Checklist

What is the current behavior?

As discussed here: https://discord.com/channels/603595811204366337/1481617562310676620/1487134181602361444, when importing ESM libraries in workers is causing an error on iOS.

For example:

Error instantiating module: [redacted_path]/src_workers_csv-generation-worker_js.mjs
  Native stack trace:
    [redacted_stack]
  JavaScript error:
  SyntaxError: The requested module 'node:url' does not provide an export named 'fileURLToPath'
  Module instantiation failed: [redacted_path]/src_workers_csv-generation-worker_js.mjs
  Debug mode - ES module returned empty namespace, but telling iOS it succeeded

To help explain the issue via claude code it described the issue with the following:

The failure chain is:

  1. Your code: Worker imports getNumberLocale from @my-scope/helpers (now ESM)
  2. helpers ESM causes webpack to output the worker as an ESM bundle (.mjs) instead of CJS
  3. NativeScript itself — specifically @nativescript/core/globals/index.js:15:

global.__dirname = typeof __dirname !== 'undefined' ? __dirname : import.meta.dirname;

This references __dirname, which webpack detects in ESM output mode and auto-generates:

import {fileURLToPath} from "node:url"; // ← webpack's own generated code

  1. iOS doesn't expose fileURLToPath from node:url → crash

What is the new behavior?

The fix applied with this PR disables webpack's polyfills for __dirname and __filename as NativeScript already handles these.

@dangrima90 dangrima90 changed the title Ignore __dirname & __filename webpack polyfills Disable __dirname & __filename webpack polyfills Mar 27, 2026
@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Mar 27, 2026

View your CI Pipeline Execution ↗ for commit 025eb51

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 22s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded 3s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-27 18:34:04 UTC

Copy link
Copy Markdown

Copilot AI left a comment

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 NativeScript’s webpack5 base configuration to avoid webpack’s automatic __dirname/__filename polyfills in ESM output, preventing an iOS worker crash caused by webpack generating import { fileURLToPath } from "node:url".

Changes:

  • Disable webpack’s __dirname and __filename handling in the ESM output branch via config.node.set('__dirname', false) / config.node.set('__filename', false).
  • Add explanatory inline comments describing the iOS worker failure mode and why the webpack behavior is undesirable in this environment.

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

Comment on lines +273 to +275
// NativeScript's runtime already provides __dirname as a global, and
// @nativescript/core/globals/index.js handles the ESM case via import.meta.dirname,
// so webpack's polyfill is both redundant and broken on iOS.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

The new inline comment says "NativeScript's runtime already provides __dirname as a global", but in @nativescript/core/globals/index.ts the code explicitly notes that __dirname is not defined in ESM builds and instead populates global.__dirname from import.meta.dirname. To avoid misleading future maintainers, update this comment to refer to global.__dirname (and/or import.meta.dirname) rather than implying the __dirname identifier exists in ESM.

Suggested change
// NativeScript's runtime already provides __dirname as a global, and
// @nativescript/core/globals/index.js handles the ESM case via import.meta.dirname,
// so webpack's polyfill is both redundant and broken on iOS.
// NativeScript's runtime already provides a __dirname value via global.__dirname, and
// @nativescript/core/globals/index.js handles the ESM case by deriving global.__dirname
// from import.meta.dirname, so webpack's polyfill is both redundant and broken on iOS.

Copilot uses AI. Check for mistakes.
Comment on lines +270 to +277
// Prevent webpack from polyfilling __dirname via `import {fileURLToPath} from "node:url"`.
// In ESM output mode, webpack auto-generates this import whenever bundled code references
// __dirname — but iOS's node:url does not export fileURLToPath, crashing workers.
// NativeScript's runtime already provides __dirname as a global, and
// @nativescript/core/globals/index.js handles the ESM case via import.meta.dirname,
// so webpack's polyfill is both redundant and broken on iOS.
config.node.set('__dirname', false);
config.node.set('__filename', false);
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

This change affects the ESM output path only, but the existing packages/webpack5/__tests__/configuration/base.spec.ts snapshot coverage appears to exercise only the CommonJS configuration (no experiments.outputModule / output.module assertions). Add a unit test that forces the ESM branch (e.g., by mocking the runtime dependency version check to >=9) and asserts node.__dirname/node.__filename are disabled, so regressions in worker ESM bundling are caught.

Suggested change
// Prevent webpack from polyfilling __dirname via `import {fileURLToPath} from "node:url"`.
// In ESM output mode, webpack auto-generates this import whenever bundled code references
// __dirname — but iOS's node:url does not export fileURLToPath, crashing workers.
// NativeScript's runtime already provides __dirname as a global, and
// @nativescript/core/globals/index.js handles the ESM case via import.meta.dirname,
// so webpack's polyfill is both redundant and broken on iOS.
config.node.set('__dirname', false);
config.node.set('__filename', false);
// Determine NativeScript runtime version (iOS/Android) so we only disable
// webpack's __dirname/__filename polyfills on runtimes that support the
// ESM helpers used by @nativescript/core (e.g., import.meta.dirname).
const nsRuntimeVersionForCheck =
getResolvedDependencyVersionForCheck('@nativescript/ios') ??
getResolvedDependencyVersionForCheck('@nativescript/android');
if (nsRuntimeVersionForCheck && isVersionGteConsideringPrerelease(nsRuntimeVersionForCheck, '9.0.0')) {
// Prevent webpack from polyfilling __dirname via `import {fileURLToPath} from "node:url"`.
// In ESM output mode, webpack auto-generates this import whenever bundled code references
// __dirname — but iOS's node:url does not export fileURLToPath, crashing workers.
// NativeScript's runtime already provides __dirname as a global, and
// @nativescript/core/globals/index.js handles the ESM case via import.meta.dirname,
// so webpack's polyfill is both redundant and broken on iOS for supported runtimes.
config.node.set('__dirname', false);
config.node.set('__filename', false);
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants