Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a Webpack module from the browser dev tools?

I have an ES6 module that is compiled with webpack. To debug it I have to manually attach certain objects to the window so that I can reference them through the debugging tools in Chrome/Safari:

export class Dialog {
  ...
}

window.debugdialog = Dialog;

This is very cumbersome and surely not the best way to do it. Is there a way to reference the modules without having to modify the source?

Yes I know about breakpoints, and I use them. But sometimes I want to load the all the code and tweak the UI by controlling it with inline JavaScript.

like image 575
Elliot Chance Avatar asked Feb 18 '16 05:02

Elliot Chance


People also ask

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 debug a Webpack in Chrome?

Click the "inspect" link under each script to open a dedicated debugger or the Open dedicated DevTools for Node link for a session that will connect automatically. You can also check out the NiM extension, a handy Chrome plugin that will automatically open a DevTools tab every time you --inspect a script.


1 Answers

Source Maps

If you're running the server in a development environment or on your own machine, you can leverage sourcemaps so that you can open the original JavaScript files in the devtools, rather than your bundle.js (or equivalent).

There's a number of awesome resources out there for getting started with Sourcemaps and setting them up, both with Chrome and Webpack.

Setting up Source Maps in Webpack can be done by adding the following config:

devtool: 'source-map'

(See also)

Additionally, if you're using the webpack cli command, you can utilize webpack -d to compile in development mode to enable your sourcemaps.

For Chrome, there's a great guide here for using source maps.

Debugging Modules

After you get sourcemaps enabled, you can simply open up the JavaScript file for your module and set a breakpoint anywhere necessary. When the compiled version of the code executes, the sourcemap should kick in and break within the source version of the module, allowing you to step through using your original source file.

You can use Ctrl+P to open your particular Source File.

Additionally, while focussed in the Sources panel, you can use Ctrl+Shift+O to jump to a particular class or member declaration.

like image 152
Swivel Avatar answered Sep 27 '22 00:09

Swivel