sameer chandekar
sameer chandekar

Reputation: 23

Please explain the meaning given code snippet in react js

Below is the code used in server.js file in a react js application. But I am not able to understand the syntax of this statement. Here after require('webpack-dev-middleware') there is no . used and suddenly another bracket started with some arguments. Can someone please explain how is it working?

app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));

Upvotes: 1

Views: 66

Answers (3)

bennygenel
bennygenel

Reputation: 24660

require('webpack-dev-middleware') is returning a function. This is just a shortened version of this

const webpackMiddleware = require('webpack-dev-middleware');
const webpackCompiler = webpackMiddleware(compiler, {
    noInfo: true,
    publicPath: config.output.publicPath
});
app.use(webpackCompiler);

Upvotes: 1

hairmot
hairmot

Reputation: 2975

require('webpack-dev-middleware') returns a function.

The second set of brackets contain arguments to be passed to this returned function.

Upvotes: 1

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

Simply extract parts of this expression as separate variables

const createWebpackMiddleware = require('webpack-dev-middleware')
const options = {
  noInfo: true,
  publicPath: config.output.publicPath
}
const webpackMiddleware = createWebpackMiddleware(compiler, options)

app.use(webpackMiddleware );

Upvotes: 0

Related Questions