With require.js it was very easy to debug a module in Chrome's DevTools, by simply entering:
require('my-module').callThisFunction()
With Webpack this is not possible anymore, because it compiles the modules via CLI and does not export require
.
window.webpackJsonp
is globally exposed, so I thought I could just find the module ID and call it like this: webpackJsonp([1],[])
, but unfortunately this returns undefined
.
Are there any workarounds to still be able to debug like require.js?
Open developer tools in chrome by pressing F12 /Ctrl + Shift + I/ right-click anywhere inside the web page and select Inspect/Inspect Element which will be mostly the last option. Go to Sources tab in developer tools and open any minified JS which you want to debug as shown in the image.
In the “Preferences” tab, make sure that both “Enable JavaScript source maps” and “Enable CSS source maps” are ticked. To generate source maps with Webpack running in production, all you have to do is add the devtool and sourceMapFilename properties to your webpack. config. js file.
Using Webpack, specifying devtool: "source-map" in your Webpack config will enable source maps, and Webpack will output a sourceMappingURL directive in your final, minified file. You can customize the source map filename itself by specifying sourceMapFilename .
Add code to module in bundle
require.ensure([], function() {
window.require = function(smth) {
return require('./' + smth);
};
});
Now you can use 'require' from chrome console like require('app').doSmth()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With