1

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
}));
4
  • I would recommend adding a "react" tag as well. Commented Sep 5, 2017 at 11:55
  • 1
    @Taurus, the post had one, but I removed it. OP's app might be React, but the question is per se irrelevant to React. This is a general JS question. Commented Sep 5, 2017 at 11:57
  • @Taurus I wouldn't, this is not about ReactJS Commented Sep 5, 2017 at 11:57
  • @Chris Ahh excuse me, never worked with React and i just assumed that it had something to do with React. Commented Sep 5, 2017 at 11:58

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

0

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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.