Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging in devtools with Webpack

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?

like image 687
spacek33z Avatar asked Jun 04 '15 15:06

spacek33z


People also ask

How do I debug a JavaScript bundle in Chrome?

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.

How do I enable Webpack in Chrome?

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.

How do I enable Sourcemaps in Webpack?

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 .


1 Answers

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()

like image 169
Vitaly Volkov Avatar answered Sep 27 '22 22:09

Vitaly Volkov