Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging webpack dev server in vs code?

Is it possible to configure launch.json for debugging webpack dev server? In my case, I'm working on a universal (server-rendered via express) React app and it would be really nice to be able to debug the server side directly in VS Code.

like image 275
Brendon Avatar asked Dec 01 '15 19:12

Brendon


People also ask

How do I debug a webpack code?

Enable source maps in your webpack app to debug easier Now rebuild your project and open up in the browser. You will be able to see the original code straight in your browser. From here you can set break points and debug !

How do I start debugging in Visual Studio Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do I debug a webpack bundle file?

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.

How do I enable debugging in Visual Studio?

In the Visual Studio toolbar, make sure the configuration is set to Debug. To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5.

Is there a better way to debug Webpack in VS Code?

There is a better way, especially if you already use VS Code. With its built-in debugger, seeing what’s going on with webpack or any Node script is just a couple of clicks away. There are two ways to set up your launch configurations, globally and on a per-project basis. This will open up your User Settings ( Preferences -> Settings)

How to set Webpack in development mode?

There are two ways to set webpack in development mode. Either add this to your webpack config: Now rebuild your project and open up in the browser. You will be able to see the original code straight in your browser.

How do I debug a browser in Visual Studio Code?

Now, you can press F5 or the Start button in the Debug view to attach to the running browser. You can even add a host property to debug a browser running on a different machine. Debugging configurations are stored in a launch.json file located in your workspace's .vscode folder.

What is source maps in Webpack?

Source maps is a text file with instructions that tell the browser how to translate the obfuscated code into how the code looked like before the bundeling. You can enable webpack to create that file during the build process. That way, webpack spits out not only your bundle, but also the source maps as part of the build process.


1 Answers

I've been working on a VueJS application using Webpack and I was able to successfully debug it using the VSCode chrome debugger after digging around for a few hours. I know your application is a React one, but I'll try and explain the steps I went through to get debugging working. Hopefully that helps you configure your launch.json to debug a react/webpack app in VSCode. Here was my final launch.json:

    {
        // Using the chrome debugger to debug a Vue application
        "type": "chrome",
        "request": "launch",
        "name": "Chrome launch",
        // Set the URL to match the root URL of your application
        "url":"http://localhost:8000/#/",
        "webRoot": "${workspaceRoot}",
        /** Configure source map paths using the instructions below */
        "sourceMapPathOverrides": {
            "webpack:///./src/*.js": "${workspaceRoot}/src/*.js",
            "webpack:///src/*.vue": "${workspaceRoot}/src/*.vue",
            "webpack:///./node_modules/*": "${workspaceRoot}/node_modules/*"
        }
    }

The key for me was correctly configuring the soureMapPathOverrides option in launch.json. First, make sure that the "devtool" option in the webpack config has been set to either "source-map" or "cheap-eval-source-map" (I used "source-map", other settings may also work, but I have not tested them).

--Setting the sourceMapPathOverrides--

What you want to do is map the source map files (which seem to be incorrectly mapped by default, at least for me) to their corresponding location on your local machine. To do this, run the program normally using webpack-dev-server or webpack. Then, open it up in Chrome and open up the devtools "sources" tab. In the "sources" tab, within the navigator (on the left by default), open the network tab and look at the folder structure.

You should see a folder structure that is something like:

top
    localhost:8000
        assets
        src
        ...
    (no domain)
        ...
    webpack://
        (webpack)
            ...
        (webpack)-dev-server
            ...
        .
            ...
        src
            ...

Now, you should be able to find both the transpiled files and your original source files somewhere here (If not, double check your "devtool" was set to "source-map" or "cheap-eval-source-map". Now, you need to map each source file to their location on your hard drive. Preferably, you want to map them by file extension, rather than individually. In the case of Vue, I had to map .js and .vue files to their corresponding locations, with the node-modules folder mapped separately (as you can see in my launch.json). It's probably going to be a different mapping for react.

Set the launch.json url to match the URL of your webpack application and your launch.json should be set!

Now, you should be able to run the file using webpack-dev-server (or webpack) and then start the debugger. You should be able to set breakpoints in VSCode and debug as normal. Hopefully this helps someone.

like image 154
keithlee96 Avatar answered Sep 20 '22 15:09

keithlee96