| 363 | * @returns {void} |
| 364 | */ |
| 365 | const dirnameAndFilenameHandler = ( |
| 366 | parser, |
| 367 | nodeOptions, |
| 368 | { dirname, filename } |
| 369 | ) => { |
| 370 | // Keep `import.meta.filename` in code |
| 371 | if ( |
| 372 | nodeOptions.__filename === false && |
| 373 | filename === IMPORT_META_FILENAME |
| 374 | ) { |
| 375 | setModuleConstant(parser, filename, () => filename, "filename"); |
| 376 | } |
| 377 | |
| 378 | if (nodeOptions.__filename) { |
| 379 | switch (nodeOptions.__filename) { |
| 380 | case "mock": |
| 381 | setConstant(parser, filename, "/index.js", "filename"); |
| 382 | break; |
| 383 | case "warn-mock": |
| 384 | setConstant( |
| 385 | parser, |
| 386 | filename, |
| 387 | "/index.js", |
| 388 | "filename", |
| 389 | "__filename is a Node.js feature and isn't available in browsers." |
| 390 | ); |
| 391 | break; |
| 392 | case "node-module": { |
| 393 | const importMetaName = compilation.outputOptions.importMetaName; |
| 394 | |
| 395 | setUrlModuleConstant( |
| 396 | parser, |
| 397 | filename, |
| 398 | "filename", |
| 399 | () => `${importMetaName}.url` |
| 400 | ); |
| 401 | break; |
| 402 | } |
| 403 | case "eval-only": |
| 404 | // Keep `import.meta.filename` in the source code for the ES module output, or create a fallback using `import.meta.url` if possible |
| 405 | if (compilation.outputOptions.module) { |
| 406 | const { importMetaName } = compilation.outputOptions; |
| 407 | |
| 408 | setUrlModuleConstant( |
| 409 | parser, |
| 410 | filename, |
| 411 | "filename", |
| 412 | () => `${importMetaName}.url` |
| 413 | ); |
| 414 | } |
| 415 | // Replace `import.meta.filename` with `__filename` for the non-ES module output |
| 416 | else if (filename === IMPORT_META_FILENAME) { |
| 417 | setModuleConstant( |
| 418 | parser, |
| 419 | filename, |
| 420 | () => "__filename", |
| 421 | "filename" |
| 422 | ); |