commonjs output clashes with chunk splitting

Bug report

What is the current behavior?

Attempting to use optimization.splitChunks.cacheGroups in combination with commonjs results in broken output packages, importing of which fails with:

TypeError: Cannot read property 'call' of undefined
    at __webpack_require__ (/Users/garthk/src/webpack-4-bug/build/index.js:20:30)

If there’s a documented way to get the entry bundle build/index.js to inhale the split bundle build/vendors.js so its imports don’t fail, please let me know where it is and I’ll file a PR to make it easier to find.

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

git clone https://github.com/garthk/webpack-4-chunk-splitting-bug.git
cd webpack-4-chunk-splitting-bug
cat README.md
npm install
node_modules/.bin/webpack
node -p 'Object.keys(require("./build"))'

Note error:

/Users/garthk/src/webpack-4-bug/build/index.js:20
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
                              ^

TypeError: Cannot read property 'call' of undefined
    at __webpack_require__ (/Users/garthk/src/webpack-4-bug/build/index.js:20:30)

(modules has keys ./src/index.ts, crypto, tty, and util, and we’re trying to find a moduleId of ./node_modules/uuid/index.js in there. No wonder it’s failing. Anyhow; comment out the splitChunks block in webpack.config.js, try again, and note no error.

What is the expected behavior?

Being able to split the output into multiple files and require the entry point without failure.

Other relevant information:

webpack version: 4.8.3
Node.js version: 8.11.2
Operating System: macOS, Linux
Additional tools: none

Author: Fantashit

1 thought on “commonjs output clashes with chunk splitting

Comments are closed.