Skip to content

webpack can generate a reference to a module it has optimised out #11536

Closed

Description

@RoystonS

Bug report

What is the current behavior?

If webpack sees an ES module that consists merely of

export {};

and tree-shaking is turned on (via sideEffects flag and the presence of a sideEffects property in package.json), then the resulting bundle attempts to initialize that module BUT the module has been optimised out. This produces a bundle that won't load with the error:

/******/                modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
                                         ^
TypeError: Cannot read property 'call' of undefined
    at __webpack_require__ (C:\Dev\issue\dist\main.js:20:30)

(Why does TS4.0 generate modules like this? It's because of this commit: microsoft/TypeScript#38696. Without that commit, the resulting module is empty, which seems to make webpack think it's a CJS module, which causes tree-shaking to go awry and bundle sizes to explode. We have 35 such modules in our codebase, as they occur when you have TypeScript files that contain only type information and no concrete code.)

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

Here's a tiny repro repository:
https://github.com/RoystonS/webpack-sideeffect-issue

That sample:

  • contains a tiny amount of TypeScript code that gets compiled to plain ES modules
  • bundles up those ES modules using a tiny webpack config with no loaders
  • runs the bundle, which fails

What is the expected behavior?

Webpack has removed the module but kept the reference to it. It could keep the module so the reference works, or, preferably, remove the reference if it has correctly established that the module is redundant.

Other relevant information:
webpack version: 4.44.2
Node.js version: 10.15.1 / 12.18.3
Operating System: Windows 10 / Linux
Additional tools:

Activity

  1. alexander-akait commented on Sep 28, 2020

    @alexander-akait
    Member

    Solved in webpack@5, can you try?

  2. RoystonS commented on Sep 28, 2020

    @RoystonS
    Author

    Yup, will try to get some time. I've lost a lot of time over the last few weeks upgrading to Emotion 11, TS3.9, TS4.0 and latest eslint, all of which have had significant breaking changes, but I'll give it a go.

  3. RoystonS commented on Sep 28, 2020

    @RoystonS
    Author

    I've not been able to try it on our main codebase as we have a few loaders that are specifying "webpack@^3 || ^4" so they're bound to cause trouble.

    But I have tried it on the above repro repository, and it does indeed work with webpack 5rc1 there.
    https://github.com/RoystonS/webpack-sideeffect-issue/tree/webpack5

  4. alexander-akait commented on Sep 28, 2020

    @alexander-akait
    Member

    Let's close, because fixed, feel free to update loader to support webpack@5 and update your code base, feel free to feedback

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

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions