Disable __dirname & __filename webpack polyfills#11146
Disable __dirname & __filename webpack polyfills#11146dangrima90 wants to merge 1 commit intoNativeScript:mainfrom
Conversation
|
View your CI Pipeline Execution ↗ for commit 025eb51
☁️ Nx Cloud last updated this comment at |
There was a problem hiding this comment.
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
__dirnameand__filenamehandling in the ESM output branch viaconfig.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.
| // 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. |
There was a problem hiding this comment.
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.
| // 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. |
| // 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); |
There was a problem hiding this comment.
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.
| // 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); | |
| } |
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:
To help explain the issue via claude code it described the issue with the following:
What is the new behavior?
The fix applied with this PR disables webpack's polyfills for
__dirnameand__filenameas NativeScript already handles these.