Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/webpack5/src/configuration/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +273 to +275
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.
config.node.set('__dirname', false);
config.node.set('__filename', false);
Comment on lines +270 to +277
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.
}

config.watchOptions({
Expand Down
Loading