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.
To reproduce the problem:
npm installnpm test
That will:
- compile the TypeScript files to JavaScript files in dist/
- use webpack to build a bundle from those JavaScript files
- 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.
src/index.tssimply imports a value frommoduleAand outputs it to theconsole. This module is marked as havingsideEffectssrc/moduleA.tsprovides the value imported bysrc/index.tsbut also re-exports everything thatmoduleBexports. This is a very common pattern with TypeScript code.src/moduleB.tsis the problematic module. It doesn't export any concrete classes orconsts or other values; it only exports interfaces.
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.
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 };