@@ -245,45 +245,46 @@ class NodeStuffPlugin {
245245 parser . hooks . expression
246246 . for ( expressionName )
247247 . tap ( PLUGIN_NAME , ( expr ) => {
248+ // We use `CachedConstDependency` because of `eval` devtool, there is no `import.meta` inside `eval()`
248249 const { importMetaName, environment, module } =
249250 compilation . outputOptions ;
250251
251- if (
252- module &&
253- importMetaName === "import.meta" &&
254- ( expressionName === "import.meta.filename" ||
255- expressionName === "import.meta.dirname" ) &&
256- environment . importMetaDirnameAndFilename
257- ) {
258- return true ;
259- }
260-
261252 // Generate `import.meta.dirname` and `import.meta.filename` when:
262253 // - they are supported by the environment
263254 // - it is a universal target, because we can't use `import mod from "node:url"; ` at the top file
264- const dep =
255+ if (
265256 environment . importMetaDirnameAndFilename ||
266257 ( compiler . platform . web === null &&
267258 compiler . platform . node === null &&
268259 module )
269- ? new ConstDependency (
270- `${ importMetaName } .${ property } ` ,
271- /** @type {Range } */
272- ( expr . range )
273- )
274- : new ExternalModuleDependency (
275- "url" ,
276- [
277- {
278- name : "fileURLToPath" ,
279- value : URL_MODULE_CONSTANT_FUNCTION_NAME
280- }
281- ] ,
282- undefined ,
283- `${ URL_MODULE_CONSTANT_FUNCTION_NAME } (${ value ( ) } )` ,
284- /** @type {Range } */ ( expr . range ) ,
285- `__webpack_${ property } __`
286- ) ;
260+ ) {
261+ const dep = new CachedConstDependency (
262+ `${ importMetaName } .${ property } ` ,
263+ /** @type {Range } */
264+ ( expr . range ) ,
265+ `__webpack_${ property } __` ,
266+ CachedConstDependency . PLACE_CHUNK
267+ ) ;
268+
269+ dep . loc = /** @type {DependencyLocation } */ ( expr . loc ) ;
270+ parser . state . module . addPresentationalDependency ( dep ) ;
271+ return ;
272+ }
273+
274+ const dep = new ExternalModuleDependency (
275+ "url" ,
276+ [
277+ {
278+ name : "fileURLToPath" ,
279+ value : URL_MODULE_CONSTANT_FUNCTION_NAME
280+ }
281+ ] ,
282+ undefined ,
283+ `${ URL_MODULE_CONSTANT_FUNCTION_NAME } (${ value ( ) } )` ,
284+ /** @type {Range } */ ( expr . range ) ,
285+ `__webpack_${ property } __` ,
286+ ExternalModuleDependency . PLACE_CHUNK
287+ ) ;
287288 dep . loc = /** @type {DependencyLocation } */ ( expr . loc ) ;
288289 parser . state . module . addPresentationalDependency ( dep ) ;
289290
@@ -300,34 +301,43 @@ class NodeStuffPlugin {
300301 compilation . outputOptions ;
301302
302303 if (
303- module &&
304- importMetaName === "import.meta" &&
305- ( expressionName === "import.meta.filename" ||
306- expressionName === "import.meta.dirname" ) &&
307- environment . importMetaDirnameAndFilename
304+ environment . importMetaDirnameAndFilename ||
305+ ( compiler . platform . web === null &&
306+ compiler . platform . node === null &&
307+ module )
308308 ) {
309- return `${ property } : ${ importMetaName } .${ property } ,` ;
310- }
311-
312- if ( environment . importMetaDirnameAndFilename ) {
313- return `${ property } : ${ importMetaName } .${ property } ,` ;
309+ const dep = new CachedConstDependency (
310+ `${ importMetaName } .${ property } ` ,
311+ null ,
312+ `__webpack_${ property } __` ,
313+ CachedConstDependency . PLACE_CHUNK
314+ ) ;
315+ dep . loc = /** @type {DependencyLocation } */ (
316+ usingProperty . loc
317+ ) ;
318+ parser . state . module . addPresentationalDependency ( dep ) ;
319+ return `${ property } : __webpack_${ property } __,` ;
314320 }
315321
316- const dep = new ExternalModuleInitFragmentDependency (
322+ const dep = new ExternalModuleDependency (
317323 "url" ,
318324 [
319325 {
320326 name : "fileURLToPath" ,
321327 value : URL_MODULE_CONSTANT_FUNCTION_NAME
322328 }
323329 ] ,
324- undefined
330+ undefined ,
331+ `${ URL_MODULE_CONSTANT_FUNCTION_NAME } (${ value ( ) } )` ,
332+ null ,
333+ `__webpack_${ property } __` ,
334+ ExternalModuleDependency . PLACE_CHUNK
325335 ) ;
326336
327337 dep . loc = /** @type {DependencyLocation } */ ( usingProperty . loc ) ;
328338 parser . state . module . addPresentationalDependency ( dep ) ;
329339
330- return `${ property } : ${ URL_MODULE_CONSTANT_FUNCTION_NAME } ( ${ value ( ) } ) ,` ;
340+ return `${ property } : __webpack_ ${ property } __ ,` ;
331341 }
332342 } ) ;
333343 }
0 commit comments