Skip to content

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

Closed

Description

@wmurphyrd

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

Activity

  1. added
    webpack-5
    on Nov 23, 2021
  2. alan-agius4 commented on Dec 16, 2021

    @alan-agius4

    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.

  3. alexander-akait commented on Dec 16, 2021

    @alexander-akait
    Member

    Yep, we should improve this

  4. alexander-akait commented on Jan 29, 2022

    @alexander-akait
    Member

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

  5. alexander-akait commented on Jan 29, 2022

    @alexander-akait
    Member

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

  6. wmurphyrd commented on Jan 29, 2022

    @wmurphyrd
    Author

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

  7. vankop commented on Jan 29, 2022

    @vankop
    Contributor

    yep, this can be closed

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    webpack-5

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions