Skip to content

About

Demonstrates an issue with webpack's handling of empty modules

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

Repository files navigation

Issue

This repo demonstrates an issue with development-mode sideEffect handling in webpack when provided with a module that simply consists of

export {};

That's precisely what's emitted by TypeScript 4.0 for modules that don't contain 'concrete' exports.

The bundle generated by webpack attempts to require the 'empty' module, but simultaneously optimises out the generation of the module, causing a bundle-load-time error.

Reproduction

To reproduce the problem:

  • npm install
  • npm test

That will:

  1. compile the TypeScript files to JavaScript files in dist/
  2. use webpack to build a bundle from those JavaScript files
  3. attempt to run the bundle

This is the output you'll see:

$> npm test

> [email protected] pretest C:\Dev\issue
> npm run build


> [email protected] prebuild C:\Dev\issue
> tsc -p src --outDir dist


> [email protected] build C:\Dev\issue
> webpack

Hash: 4b668065102aa0e7bef9
Version: webpack 4.44.2
Time: 60ms
Built at: 09/26/2020 00:15:31
  Asset      Size  Chunks             Chunk Names
main.js  5.17 KiB    main  [emitted]  main
Entrypoint main = main.js
[0] ./dist/index.js 333 bytes {main} [built]
[1] ./dist/moduleB.js 133 bytes [built]
[2] ./dist/moduleA.js 315 bytes {main} [built]

> [email protected] test C:\Dev\issue
> node dist/main.js

C:\Dev\issue\dist\main.js:20
/******/                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)
    at eval (webpack:///./dist/moduleA.js?:3:66)
    at Module.<anonymous> (C:\Dev\issue\dist\main.js:108:1)
    at __webpack_require__ (C:\Dev\issue\dist\main.js:20:30)
    at eval (webpack:///./dist/index.js?:2:66)
    at Module.<anonymous> (C:\Dev\issue\dist\main.js:96:1)
    at __webpack_require__ (C:\Dev\issue\dist\main.js:20:30)
    at C:\Dev\issue\dist\main.js:84:18
    at Object.<anonymous> (C:\Dev\issue\dist\main.js:87:10)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
npm ERR! Test failed.  See above for more details.

Code Details

  • src/index.ts simply imports a value from moduleA and outputs it to the console. This module is marked as having sideEffects
  • src/moduleA.ts provides the value imported by src/index.ts but also re-exports everything that moduleB exports. This is a very common pattern with TypeScript code.
  • src/moduleB.ts is the problematic module. It doesn't export any concrete classes or consts or other values; it only exports interfaces.

Other Notes

Note that this problem only occurs in a mode=development build. With a mode=production build, more optimisation takes place and the entire import of moduleB disappears. It's only a problem with sideEffect configuration in a mode=development build.

Workaround

This caused significant problems for our team when we upgraded to TypeScript 4.0 as all our development bundles suddenly failed to load. We've put a workaround in place which consists of a custom webpack loader which detects empty export {} modules and inserts a 'concrete' dummy export:

const dummyRandomVariable = 42;
export { dummyRandomVariable };

About

Demonstrates an issue with webpack's handling of empty modules

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages