Configure webpack to allow browser debugging
Asked Answered
O

6

152

I am new to webpack and I am converting an existing web application to use it.

I am using webpack to bundle and minify my JS which is great when deployed, however this makes it very challenging to debug while in developement.

Typically I use chrome's built in debugger to debug JS issues. (Or Firebug on firefox). However with webpack everything is stuffed in one file and it becomes challenging to debug using that mechanism.

Is there a way to quickly turn on and off bundeling? or turn on and off minifying?

I have looked to see if there is some script loader configuration or other setting but it does not appear ovious.

I have not yet had the time to convert everything to act like a module and use requires. So I simply use require("script!./file.js") pattern for my loading.

Oneidaoneil answered 23/12, 2014 at 19:27 Comment(3)
Did you end up finding a solution to this problem? I also prefer to use the JS console to look at available variables. My main issue is that webpack hides all of these variables inside the module, so they become inaccessiblePunt
Never really found a solution so we unfortunately aborted using webpack.Oneidaoneil
What do you use now? At the time of writing webpack still seems like the most popular build tool I can find.Cortege
W
115

You can use source maps to preserve the mapping between your source code and the bundled/minified one.

Webpack provides the devtool option to enhance debugging in the developer tool just creating a source map of the bundled file for you. This option can be used from the command line or used in your webpack.config.js configuration file.

Below you can find a contrived example using the command line to generate the bundled file (bundle.js) along with the generated source map file (bundle.js.map).

$ webpack --devtool source-map ./entry.js bundle.js
Hash: b13b8d9e3292806f8563
Version: webpack 1.12.2
Time: 90ms
        Asset     Size  Chunks             Chunk Names
    bundle.js  1.74 kB       0  [emitted]  main
bundle.js.map  1.89 kB       0  [emitted]  main
   [0] ./entry.js 85 bytes {0} [built]
   [1] ./hello.js 59 bytes {0} [built]

index.html

<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <script src="bundle.js"></script>
  </body>
</html>

entry.js

var hello = require('./hello.js');
document.body.innerHTML += 'It works ' + hello();

hello.js

module.exports = function () {
  return 'Hello world!';
};

If you open index.html in your browser (I use Chrome but I think it is also supported in other browsers), you will see in the tab Sources that you have the bundled file under the file:// scheme and the source files under the special webpack:// scheme.

debug with source maps

And yes, you can start debugging as if you had the original source code! Try to put a breakpoint in one line and refresh the page.

breakpoint with source maps

Westcott answered 29/10, 2015 at 19:11 Comment(1)
bundle.map points to the js file but what if the actual js file also has a map to lets say tsx or ts?Woodie
S
7

I think its better to setup your project using production and development mode https://webpack.js.org/guides/production/ Its also include how to map your code to debug

devtool: 'inline-source-map'

Synectics answered 22/4, 2018 at 0:30 Comment(0)
C
4

Source maps are very useful as already pointed out.
But sometimes selecting which source map to use could be a pain.

This comment on one of the Webpack source map issue might be helpful for selecting which source map to use based on requirements.

Canny answered 17/5, 2017 at 8:34 Comment(0)
C
3

Chrome also has a format option in the debugger. It doesn't have all the information a normal source file would but it's a great start, also you can set breakpoints. The button you click is on the bottom left of the first screen shot, looks like {}.

Before formatting: enter image description here

After formatting.

enter image description here

Commensal answered 17/12, 2019 at 17:10 Comment(0)
R
1

Have a look Here

its a beautifier that deminifies javascript. at the bottom, it has a list of various plugins and extensions for browsers, check them out.

you might be interested in FireFox Deminifier , its supposed to deminify and style your javascript when its retrieved from the server.

enter image description here
(source: mozilla.net)

Ribose answered 23/12, 2014 at 19:35 Comment(1)
deminifying is not the same as disabling minifying as comments are still stripped, line numbers no longer match, and variable names are not the same. That being said it is better then nothing.Oneidaoneil
C
1
  • Source Mapping can be used when your local development build isn't changed, but if you want to test your local bundle changes on production sites, this is difficult to set up.
  • If you aren't using component or bundle-level code-splitting, then you can use a Chrome extension like Requestly to swap the production version with the local version. Here is one such example you can see: maplocal feature
Cemetery answered 26/7, 2023 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.