Skip to content

new Worker(import.meta.url) doesn't work as expected #12638

Closed

Description

@RReverser

Bug report

I have code like this:

new Worker(import.meta.url)

where module spawns itself as a Worker when multithreading is enabled.

What is the current behavior?

Webpack transforms import.meta.url into a file: URI, which fails to get spawned in runtime.

What is the expected behavior?

Handling of

new Worker(import.meta.url)

should be equivalent to

new Worker(new URL('explicit-own-filename.js', import.meta.url))

which Webpack already supports.

Other relevant information:
webpack version: 5.20.1

Activity

  1. added
    webpack-5
    on Feb 9, 2021
  2. luvsta commented on Mar 23, 2021

    @luvsta

    script tag must be support ESM.

  3. webpack-bot commented on Jun 22, 2021

    @webpack-bot
    Contributor

    This issue had no activity for at least three months.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  4. added
    status: stale
    on Jun 22, 2021
  5. alexander-akait commented on Jun 22, 2021

    @alexander-akait
    Member

    Still valid

  6. removed
    status: stale
    on Jun 22, 2021
  7. RReverser commented on Jun 23, 2021

    @RReverser
    ContributorAuthor

    Having thought about this a bit more, while I think it would be useful, it's likely not the most uncommon usecase and other bundlers don't seem to support this either. To keep consistency I suppose I'll close it.

  8. alexander-akait commented on Jun 23, 2021

    @alexander-akait
    Member

    @RReverser I think we can keep open, for me it is feature/enhancement than bug

  9. devongovett commented on Jul 18, 2021

    @devongovett

    and other bundlers don't seem to support this either

    FYI I just implemented support for this in Parcel (see link above). Came across this issue while I was researching so thought I would let you know. 😉

  10. RReverser commented on Jul 18, 2021

    @RReverser
    ContributorAuthor

    and other bundlers don't seem to support this either

    FYI I just implemented support for this in Parcel (see link above). Came across this issue while I was researching so thought I would let you know. 😉

    Ah thanks :)

  11. 2 remaining items

  12. webpack-bot commented on Nov 2, 2021

    @webpack-bot
    Contributor

    Issue was closed because of inactivity.

    If you think this is still a valid issue, please file a new issue with additional information.

  13. removed
    status: stale
    on Nov 2, 2021
  14. sbc100 commented on Mar 3, 2025

    @sbc100

    Any idea who hard it would be address this?

    Over in emscripten we currently jump through a bunch of hoops to avoid new Worker(import.meta.url) but that is really what we want to generate here: https://github.com/emscripten-core/emscripten/blob/b8b827affe7090dda4a115fbbc6dea0b6046575e/src/lib/libpthread.js#L434-L438

    This is by far the most common usage of new Worker in emscripten since this is how we spawn new pthreads. With pthreads every worker runs exactly the same code so new Worker(import.meta.url) is the most logical choice.

    See https://github.com/emscripten-core/emscripten/pull/23804/files#r1978395660

  15. RReverser commented on Mar 4, 2025

    @RReverser
    ContributorAuthor

    Any idea who hard it would be address this?

    It's mostly hard because you'd have to implement it in every possible bundler out there. I think keeping that pattern is a much easier, if somewhat unfortunate, choice.

  16. sbc100 commented on Mar 4, 2025

    @sbc100

    It more than a little unfortunate. Have you seen what this PR proposes: emscripten-core/emscripten#23804?

    I believe this is the only bundler that doesn't support new Worker(import.meta.url) now. Do you know of another? I did some basic testing with vite and it seems to support new Worker(import.meta.url)

  17. RReverser commented on Mar 4, 2025

    @RReverser
    ContributorAuthor

    It's a bit old by now, but back in https://web.dev/articles/bundling-non-js-resources we listed WebPack, Rollup, Parcel and Vite as those supporting the new (at the time) idiom of new URL(..., import.meta.url), either directly or via plugins.

    Sounds like Vite supports this shorter syntax now, and WebPack doesn't, what about e.g. Parcel?

  18. alexander-akait commented on Mar 4, 2025

    @alexander-akait
    Member

    I did some basic testing with vite and it seems to support new Worker(import.meta.url)

    I tried it on vite with dynamic chunks (without dynamic chunks all works fine), looks like it is buggy (not sure it was reported),

  19. sbc100 commented on Mar 4, 2025

    @sbc100

    I did some basic testing with vite and it seems to support new Worker(import.meta.url)

    I tried it on vite with dynamic chunks (without dynamic chunks all works fine), looks like it is buggy (not sure it was reported),

    Can you elaborate? I'd like to write a test on the emscripten side the proves (or disproves) such bundling is working correctly. i.e. can you explain what that buggy-ness is?

  20. alexander-akait commented on Mar 4, 2025

    @alexander-akait
    Member

    @sbc100 I implemented support - #19279

    There is a small limitation, when you try to bundle such code, you will get:

    WARNING in [entry] [initial]
    Circular dependency between chunks with runtime (main, test_js)
    This prevents using hashes of each other and should be avoided.
    

    This is just a warning, and bundling itself will compile successfully. It only warning about you can't use chunkFilename: "[hash].js. I plan to solve this in the future too, but it requires a little more work. Currently you can ignore such warning.

  21. sbc100 commented on Mar 11, 2025

    @sbc100

    Awesome, thanks for working this! In our case we absolutely do have a fundamental circular dependency of course. Hopefully that can be ignored by users or fixed.

    Is there a reason this bug is still open?

  22. alexander-akait commented on Mar 11, 2025

    @alexander-akait
    Member

    To fix a warning, we can avoid it, so I will close when it will be fixed 😄

  23. self-assigned this
    on Jan 19, 2026
  24. alexander-akait commented on Jan 26, 2026

    @alexander-akait
    Member

    Fixed by #20345

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

Metadata

Metadata

Assignees

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