How to deal with an empty catch {} block with Webpack/Babel?

Viewed 28

I'm dealing with an issue I'm having when trying to run my local dev environment. I'm using Webpack 2 and Babel 6.

I can't seem to figure out how to get a package that contains empty catches to parse.

Is there a plugin for Babel to do so? Do I need to do something else?

[0] Module parse failed: C:\Users\PROJECT\node_modules\ssh2\lib\server.js Unexpected token (490:16)
[0] You may need an appropriate loader to handle this file type.
[0] |         try {
[0] |           socket.end();
[0] |         } catch {} << it's complaining about this section.
[0] |       },

.babelrc file:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": ["add-module-exports"],
  "env": {
    "production": {
        "presets": ["react-optimize"],
        "plugins": [
          "babel-plugin-transform-remove-console",
          "babel-plugin-transform-remove-debugger",
          "babel-plugin-dev-expression",
          "add-module-exports"
        ]
    },
    "development": {
      "plugins": ["transform-exponentiation-operator"]
    },
    "test": {
      "plugins": [
        [
          "webpack-loaders",
          {
            "config": "webpack.config.node.js",
            "verbose": false
          }
        ],
        [
          "babel-plugin-module-alias",
          {
            "src": "test/electron/mockElectron",
            "expose": "electron"
          }
        ]
      ]
    }
  }
}

Webpack dev config:

/* eslint max-len: 0 */
import webpack from 'webpack';
import baseConfig from './webpack.config.base';

const config = {
  ...baseConfig,

  devtool: 'cheap-module-eval-source-map',

  entry: [
    'webpack-hot-middleware/client?path=http://localhost:3000/__webpack_hmr',
    '@babel/polyfill',
    './app/index'
  ],

  output: {
    ...baseConfig.output,
    publicPath: 'http://localhost:3000/dist/'
  },

  module: {
    ...baseConfig.module,
    loaders: [
      ...baseConfig.module.loaders,

      {
        test: /\.global\.css$/,
        loaders: [
          'style-loader',
          'css-loader?sourceMap'
        ]
      },

      {
        test: /^((?!\.global).)*\.css$/,
        loaders: [
          'style-loader',
          'css-loader?modules&sourceMap&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]'
        ]
      }
    ]
  },

  plugins: [
    ...baseConfig.plugins,
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('development')
    })
  ],

  target: 'electron-renderer'
};

export default config;

Webpack base config:

import path from 'path';
import WebpackNotifierPlugin  from 'webpack-notifier';

export default {
  module: {
    loaders: [{
      test: /\.jsx?$/,
      loaders: ['babel-loader'],
      exclude: /node_modules/
    }, {
      test: /\.json$/,
      loader: 'json-loader'
    }, {
        test: /\.cjs?$/,
        loaders: ['babel-loader']
      }
    ]
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    libraryTarget: 'commonjs2'
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.cjs'],
    mainFields: ['webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main']
  },
  plugins: [
    new WebpackNotifierPlugin({excludeWarnings: true})
  ],
  externals: [
  ]
};

Relevant packages:

"@babel/core": "^7.9.0",
"@babel/plugin-transform-exponentiation-operator": "7.18.6",
"@babel/plugin-proposal-optional-catch-binding": "^7.18.6",
"@babel/polyfill": "^7.12.1",
"@babel/preset-env": "^7.19.0",
"@babel/preset-react": "^7.18.6",
"@babel/preset-stage-0": "^7.8.3",
"@babel/register": "^7.18.9",
"@babel/parser": "^7.19.0",
"@babel/eslint-parser": "^7.18.9",
"@babel/eslint-plugin": "7.18.10",
"babel-loader": "8.0.0",
"babel-plugin-add-module-exports": "1.0.4",
"babel-plugin-dev-expression": "0.2.1",
"babel-plugin-module-alias": "1.6.0",
"babel-plugin-transform-react-inline-elements": "7.0.0-beta.3",
"babel-plugin-transform-remove-console": "^6.9.4",
"babel-plugin-transform-remove-debugger": "^6.9.4",
"babel-plugin-webpack-loaders": "0.9.0",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-react-optimize": "1.0.*",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "~10.1.0",
"webpack": "2.7.0",
"webpack-dev-middleware": "2.0.6",
"webpack-hot-middleware": "2.25.2"

The package that causes the issue is ssh2 post 0.8.9. Any version from 1.0.0 onwards. From 1.0.0 the files seem to have a number of catch {} occurrences.

0 Answers
Related