Skip to content

Commit 6927c50

Browse files
fix: resolve webpack warning for optional date-fns-tz dependency
Use __non_webpack_require__ to load the optional date-fns-tz module. This tells webpack to use the native Node.js require at runtime instead of trying to bundle/analyze the module, eliminating the "Critical dependency: the request of a dependency is an expression" warning. Fixes #6181 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 644f2f5 commit 6927c50

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/date_utils.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export function __setDateFnsTzNull(): void {
9999
dateFnsTzLoadAttempted = true;
100100
}
101101

102+
// Declare webpack's special require function that bypasses bundling
103+
// This is used to avoid webpack warnings for optional dependencies
104+
declare const __non_webpack_require__: typeof require | undefined;
105+
102106
/**
103107
* Attempts to load date-fns-tz module.
104108
* Returns null if the module is not installed.
@@ -111,13 +115,15 @@ function getDateFnsTz(): DateFnsTz | null {
111115
dateFnsTzLoadAttempted = true;
112116

113117
try {
114-
// Dynamic require for date-fns-tz
115-
// Use a variable to prevent webpack from statically analyzing the require
116-
// and showing warnings when the optional dependency is not installed
117-
// See: https://github.com/Hacker0x01/react-datepicker/issues/6154
118-
const dateFnsTzModuleName = "date-fns-tz";
118+
// Use __non_webpack_require__ to tell webpack to use native require
119+
// and avoid bundling warnings for this optional dependency
120+
// See: https://github.com/Hacker0x01/react-datepicker/issues/6181
119121
// eslint-disable-next-line @typescript-eslint/no-require-imports
120-
dateFnsTz = require(dateFnsTzModuleName) as DateFnsTz;
122+
const requireFn =
123+
typeof __non_webpack_require__ !== "undefined"
124+
? __non_webpack_require__
125+
: require;
126+
dateFnsTz = requireFn("date-fns-tz") as DateFnsTz;
121127
} catch {
122128
/* istanbul ignore next - only executes when date-fns-tz is not installed */
123129
dateFnsTz = null;

0 commit comments

Comments
 (0)