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

With library.type: module, import.meta.url shouldn't be replaced with static string #14809

Closed
wmurphyrd opened this issue Nov 23, 2021 · 6 comments

Comments

@wmurphyrd
Copy link

wmurphyrd commented Nov 23, 2021

Bug report

What is the current behavior?

When bundling a library with type: 'module', source references to import.meta.url are replaced with static file:///... strings.

As a result, operations on query parameters like the example from the MDN page on import.meta do not work.

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

Observable at https://immers-space.github.io/immers-client/

<script type="module" src="https://unpkg.com/immers-client/dist/destination.bundle.js?position=top-right&title=immers-client%20Documentation"></script>
// destination.bundle.js source
scriptArgs = Object.fromEntries(new URL(import.meta.url).searchParams)
// destination.bundle.js output
P = Object.fromEntries(new URL("file:///home/username/git/immers-client/oneLiner.js").searchParams)

scriptArgs is always empty because the import.meta.url is replaced with the build-time filepath

Webpack config (45 lines): https://github.com/immers-space/immers-client/blob/main/webpack.common.js

What is the expected behavior?

import.meta.url appears unmodified in the output and I am able to retrieve query parameters from it at runtime

Other relevant information:
webpack version: 5.64.2
Node.js version: v16.13.0
Operating System: Ubuntu 21.10
Additional tools: none

@alan-agius4
Copy link

alan-agius4 commented Dec 16, 2021

I did came across this issue, as I was trying to use import.meta.url natively in the browser at runtime in an application, but it turned out that Webpack will always replace this with a static string.

@alexander-akait
Copy link
Member

Yep, we should improve this

@alexander-akait
Copy link
Member

Now we allow to disable import.meta.url behavior #15246

@alexander-akait
Copy link
Member

So original issue resolved, just disable it for required files and it will work

@wmurphyrd
Copy link
Author

Thanks @alexander-akait this looks like it will work. I'll try it out as soon as it is released

@vankop
Copy link
Member

vankop commented Jan 29, 2022

yep, this can be closed

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

Successfully merging a pull request may close this issue.

4 participants