4

I've lost too many hours trying to debug an SSR react application (the server side). We're building an app from scratch, and it's a very large project so debug the code is really important.

The webpack config for the server is the following:

const path = require('path');
const merge = require('webpack-merge');
const webpackNodeExternals = require('webpack-node-externals');
const webpack = require('webpack');

const baseConfig = require('./app.webpack.base');

const server = {
  name: 'server',
  entry: ['./app/server/index.js'],
  target: 'node',
  mode: 'development',
  devtool: 'source-map',
  output: {
    path: path.resolve(__dirname, 'public/server'),
    filename: 'server.js',
    libraryTarget: 'commonjs2'
  },
  module: {
    rules: [
      {
        test: [/\.svg$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
        loader: 'file-loader',
        exclude: /node_modules/,
        options: {
          name: 'public/media/[name].[ext]',
          publicPath: (url) => url.replace(/public/, ''),
          emit: false
        }
      },
      {
        test: /\.scss$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'css-loader/locals'
          },
          {
            loader: 'sass-loader'
          }
        ]
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      'SERVER_SIDE': true,
      'ENVIRONMENT': JSON.stringify(process.env.NODE_ENV),
    }),
    new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1
    })
  ],
  externals: [webpackNodeExternals()]
};

module.exports = merge(baseConfig, server);

The baseConfig only adds a loader for js and jsx files:

  module: {
    rules: [
      {
        test: [/js$/, /\.jsx?$/],
        exclude: /node_modules/,
        loader: 'babel-loader'
      }
    ]
  },

I can't debug it neither in VSCode nor Chrome.

In VSCode, I get the famous:

Unverified breakpoint, Breakpoints set but not yet bound

I'm running the generated server.js file (after bundled with webpack) with node --inspect flag:

I've tried many launch.json configs, example:

{
    "name": "Attach to Process",
    "type": "node",
    "protocol": "inspector",
    "request": "attach",
    "port": 9229,
    "sourceMaps": true
}

And in Chrome, if I open the DevTools for Chrome, I am able to see the source maps and set breakpoints but they never get hit.

I will be very grateful with you guys if you can give me a hand with this. Any insight or idea is appreciated too.

Thanks.

MartinGian
  • 283
  • 4
  • 13

1 Answers1

0

You can check out my solution here. By the way, I had some experiments with SSR here