Skip to content

Odd interaction between sideEffects:false and a Worker #12646

Closed

Description

@RReverser

Bug report

Check out this Glitch example with a simplified repro: https://glitch.com/edit/#!/grass-northern-xenon

Here, I'm using a Worker that shares some code with the main thread, but I'm also using sideEffects: false in package.json.

(In the original code the latter is auto-generated by 3rd-party tooling, but it's not important)

What is the current behavior?

The main thread is bundled correctly, but the import in the Worker resolves to undefined and throws when optimizations are enabled.

This doesn't happen when I change sideEffects to true. Maybe this makes it somehow expected behaviour, but it seems odd that Worker code is still correctly included (after all, the only purpose of Workers is to perform side-effects), it's just the import that doesn't resolve.

What is the expected behavior?

The import in Worker should resolve to actual function foo.

Other relevant information:
webpack version: 5.21.2

Activity

  1. alexander-akait commented on Feb 10, 2021

    @alexander-akait
    Member

    I think it critical, thanks for the issue

  2. sokra commented on Feb 10, 2021

    @sokra
    Member

    hmm yeah I see. The worker entrypoint is side-effect-free, so it thinks nothing is needed at all, but still included it. So it probably should either create a empty worker bundle (as there is no side effect, so everything can be removed), or implicitly marking worker entries as non-side-effect-free, or emitting an error/warning that side-effect-free modules can't be used as worker entries since this doesn't have any effect.

  3. RReverser commented on Feb 10, 2021

    @RReverser
    ContributorAuthor

    or implicitly marking worker entries as non-side-effect-free

    I think this would be the expected behaviour, Workers are more similar to entry points than modules. Or would you exclude application entry point too when sideEffects: false is present?

  4. sokra commented on Feb 10, 2021

    @sokra
    Member

    Or would you exclude application entry point too when sideEffects: false is present?

    Would make sense. But at least there should be a warning, as one is incorrectly making it as side-effect-free.

    Once you wrap it into a new entry with import "./old-entry.js" it would be excluded... That seems a bit inconsistent...


    I tried

    // package.json
    	"sideEffects": [
    		"./worker.js"
    	],

    that fixes the problem.


    btw.

        splitChunks: {
          chunks: 'all',
          minSize: 0,
          usedExports: false
        }

    will put utils.js into it's own chunk

  5. RReverser commented on Feb 10, 2021

    @RReverser
    ContributorAuthor

    Would make sense. But at least there should be a warning, as one is incorrectly making it as side-effect-free.

    Idk, to me personally entry points being excluded from default side-effect-free list felt like a feature not a bug 😅 So I thought Workers could be naturally excluded too.

  6. sokra commented on Feb 10, 2021

    @sokra
    Member

    Idk, to me personally entry points being excluded from default side-effect-free list felt like a feature not a bug

    yeah, just found code that enforces entrypoints to to be considered as having side-effects^^

  7. RReverser commented on Feb 10, 2021

    @RReverser
    ContributorAuthor

    yeah, just found code that enforces entrypoints to to be considered as having side-effects^^

    Ah, so it is intended. Good to know.

    In that case Workers should probably be handled in the same way.

  8. added a commit that references this issue on Feb 10, 2021
    b30596e
  9. sokra commented on Feb 10, 2021

    @sokra
    Member

    Ok there is a fix, but I still want to note that your worker files is incorrectly flagged as side-effect-free and we might add a warning for that in future. Also for entrypoints.

  10. RReverser commented on Feb 10, 2021

    @RReverser
    ContributorAuthor

    That was quick. Thanks!

    In this case I don't have much control over sideEffects: false (it's generated by wasm-pack, and it's been unmtaintained for some time now), so a fix at least for now is surely helpful.

  11. RReverser commented on Feb 10, 2021

    @RReverser
    ContributorAuthor

    will put utils.js into it's own chunk

    Interesting, why is usedExports used by default? It seems like sharing at least a subset of code is always benefitial?

  12. RReverser commented on Feb 10, 2021

    @RReverser
    ContributorAuthor

    will put utils.js into it's own chunk

    I actually think I found another bug :(

  13. RReverser commented on Feb 10, 2021

    @RReverser
    ContributorAuthor

    Reported in #12652.

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-5Concerns the webpack 5 line

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions