A
A
Artyom2019-07-03 13:11:49
webpack
Artyom, 2019-07-03 13:11:49

How to set path to index.html in webpack?

How to set path to index.html in webpack? If you transfer it from the root, then npm run dev opens a list of directories in the browser. How to make it open index.html even if it's not in the root?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-07-03
@rockon404

It all depends on what directories you want to work with.
You can set the devServer.contentBase parameter to the directory you need:

devServer: {
  contentBase: path.join(__dirname, 'build', 'myFolder'),
}

But then static paths will be resolved relative to this folder. If you are going to work with the root directory, then you should not do this.
webpack-dev-server uses express and you can use the devServer.before hook to access the app and add an endpoint for the root path:
devServer: {
  before: (app, server) => {
    app.get('/', (req, res) => {
      res.sendFile(path.resolve(__dirname, './build/myFolder/index.html'));
    });
  },
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question