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.
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.
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.
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.
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.
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