The Curious Case of Webpack 4 Production Mode and

Last updated: March 9, 2018

I have migrated over to Webpack 4 for Built With Aurelia and I am using the fantastic Aurelia Store plugin.

During development, everything worked fine, but when I would do a production build the state management aspect would fall apart, complaining about something to do with the function that notifies Redux Dev Tools about the change (what the action name and state value was).

It took a lot of trial and error (a solid day) for me to work out what was going on.

My logic which registers my actions looked like this:, loadProjects);

This takes the function name (a string) and uses it as the name of the action being registered. This was to save time creating separate constants or manual string names, it made perfect sense.

It turns out when you enable mode: 'production' in Webpack 4, it uglifies your code using UglifyJS. Unless told otherwise, Uglify will mangle your class and function names but it will not populate the name property on the prototype itself with the newly mangled name, as someone else encountered in this issue.

Inspecting the minified production source code, I could see the action name was provided to the send method was an empty string. This resulted in not only the app breaking but also red errors in the console complaining about the postMessage API and something else ambiguous.

My first instinct was to disable function name mangling and it would have fixed it. However, I just ended up abandoning the idea of using the value as the name of the action and reverting to using constants as the action name.

const LOAD_PROJECTS = 'loadProjects';, loadProjects);

This is the kind of pattern you see promoted in other state management solutions and it kind of makes sense why you would do it this way.

If you use in your application regardless of whether or not it’s an Aurelia app, you will encounter this issue with Webpack 4 production mode enabled or even in Webpack 3 with UglifyJS and no explicit mangle settings.

Purchase Aurelia for Real World Applications over at Leanpub now




Leave a Reply

Your email address will not be published. Required fields are marked *