-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Disable __dirname & __filename webpack polyfills #11146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -267,6 +267,14 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| if (env === null || env === void 0 ? void 0 : env.uniqueBundle) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| config.output.filename(`[name].${env.uniqueBundle}.mjs`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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); | |
| // 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); | |
| } |
There was a problem hiding this comment.
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.tsthe code explicitly notes that__dirnameis not defined in ESM builds and instead populatesglobal.__dirnamefromimport.meta.dirname. To avoid misleading future maintainers, update this comment to refer toglobal.__dirname(and/orimport.meta.dirname) rather than implying the__dirnameidentifier exists in ESM.