Skip to content
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

Make import.meta.url dynamic #14445

Closed
Congelli501 opened this issue Oct 9, 2021 · 11 comments · Fixed by #15246
Closed

Make import.meta.url dynamic #14445

Congelli501 opened this issue Oct 9, 2021 · 11 comments · Fixed by #15246

Comments

@Congelli501
Copy link

Bug report

What is the current behavior?

import.meta.url is replaced by the current file location at the build time.
https://github.com/webpack/webpack/blob/main/lib/dependencies/ImportMetaPlugin.js#L99

If the current behavior is a bug, please provide the steps to reproduce.

If you build

console.log(import.meta.url)

it will ouput a constant string (le location of the file on the current system).

This is problematic to build a project using yargs (https://github.com/yargs/yargs), as it uses meta.import.url (https://github.com/yargs/yargs/blob/main/lib/platform-shims/esm.mjs#L18).

When building under an Unix file system, meta.import.url is replaced by something like file:///path/to/your/project/src/index.js. When trying to run the built code under a Windows filesystem, a TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute is thrown, as the filename doesn't contain file://C:/....

Making the meta.import.url variable dynamic (based on __dirname) should solve the problem.

What is the expected behavior?

import.meta.url should be based on __dirname for cjs build, to keep the original intended behavior of the application developer.

Other relevant information:
webpack version: 5.58.1
Node.js version: 16.10.0
Operating System: Ubuntu Linux 20.04
Additional tools: yargs

@webpack-bot
Copy link
Contributor

webpack-bot commented Oct 9, 2021

For maintainers only:

  • webpack-4
  • webpack-5
  • bug
  • critical-bug
  • enhancement
  • documentation
  • performance
  • dependencies
  • question

@alexander-akait
Copy link
Member

Duplicate #14072

@Congelli501
Copy link
Author

The linked issue is about supporting __dirname & __filename in a commonjs module required via an ESM module.

This issue is about supporting dynamic import.meta.url in a full ESM build, I don't think those issues are duplicates.

@alexander-akait
Copy link
Member

alexander-akait commented Oct 11, 2021

But import.meta.url is file:///something/file.js, what is the problem?

@Congelli501
Copy link
Author

It is the build time location of the file and not the run time dynamic location of the JS file

@alexander-akait
Copy link
Member

We are doing the same for __filename in commonjs

@mika-fischer
Copy link

We are doing the same for __filename in commonjs

Yes, but that behavior can be adjusted via https://webpack.js.org/configuration/node/#node__filename and https://webpack.js.org/configuration/node/#node__dirname

We have those both set to false which means webpack will not touch __filename and __dirname at all. And we need a way to do the same for import.meta.url.

I agree that #14072 is a completely orthogonal issue and that this should be reopened.

@alexander-akait
Copy link
Member

Make sense to allow keeping it

@Silvia813
Copy link

Any updates?

@pavelsavara
Copy link
Contributor

Hi all, I tried to contribute new configuration option to fix this, but I struggle to make all tests work. I would appreciate some help.

Also I wonder what is the use-case for the current behavior, with the file name from build time. Could somebody advise ? @vankop

@dandeancook
Copy link

dandeancook commented Feb 4, 2023

Also I wonder what is the use-case for the current behavior

I have a lib, which is loaded by app through http:

import libfoo from "https://bar.exampleserver.com";

and inside libfoo, it has to know whether it is loaded from in order to load other specific files for that server.

webpack-cli: 5.0.1
webpack: 5.75.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants