5

Can I custom the url that be automatically opened in browser? I found there is no api for that? Since that there is not a index under the project root, but the default url is localhost:8080. Or in the condition that I want to debug the page being developing.

ruby huang
  • 51
  • 1
  • 3
  • If you need to change the host, set the `host` property. More here - https://webpack.js.org/configuration/dev-server/#devserverhost – Lucas Jan 22 '21 at 15:55

6 Answers6

5

September 2021 (webpack 5)

From the documentation: https://webpack.js.org/configuration/dev-server/#devserveropen

In the dev server config

{
  ...,
  host: '0.0.0.0',
  open: ['http://locahost:3001']
  ...
}

(and remove the --open flag from the webpack serve command

Zach Smith
  • 8,458
  • 13
  • 59
  • 133
4

You can configure you webpack.config.js like this:

 entry: __dirname + '/src/index.js',
 output: {
    path: path.join(__dirname, 'static'),,
    filename: "bundle.js",
    publicPath: "/static/dist/"
  },
  devServer: {
    publicPath: '/static/dist/',
    open: true, 
    openPage: 'static/dist/somefile.html' 
  },...

The important thing here is devServer.open and devServer.openPage. You have to enable devServer.open and set your custom url in devServer.openPage to open first in your devServer. My configuration in package.json:

"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production",
"dev": "webpack --mode development"
},...

Finally run it by:

npm run start

Hope it helps. Regards

elopezp
  • 617
  • 7
  • 7
2

You can try with this plugin: Open Browser Webpack Plugin

Follow this steps...

  1. First of all install the plugin:

    npm install open-browser-webpack-plugin --save-dev

  2. Remove the --open option from package.json or the open: true option from webpack.config.js, devServer configuration or the open: true in your webpack.config.js

  3. Now you need to require and configure the plugin into webpack.config.js

    var OpenBrowserPlugin = require('open-browser-webpack-plugin');`
    
    module.exports = {
      ...
      ...
      ...
      plugins: [
        new OpenBrowserPlugin({ url: 'http://localhost:3000/mycustompath' })
      ]
    };
    
Luca Ritossa
  • 1,118
  • 11
  • 22
1

devServer: { open: true, openPage: 'oa' /* when browser open this */ }

0

my solution without any plugins

modify your package.json file, like below code shows

  1. macOS / Linux
{
  "scripts": {
    "dev": "open http://localhost:8080 && webpack --config webpack.config.js",
  }
}
  1. Windows
{
  "scripts": {
    "dev": "start http://localhost:8080 && webpack --config webpack.config.js",
  }
}
  1. webpack-dev-server
{
  "scripts": {
    "dev": "webpack-dev-server --open --config webpack.config.js",
  }
}

refs

https://webpack.js.org/configuration/dev-server/

https://webpack.js.org/guides/development/#using-webpack-dev-server

https://www.cnblogs.com/xgqfrms/p/12858667.html

Webpack launch browser automatically

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
0

To open a specified page in a browser:

webpack.config.js

module.exports = {
  //...
  devServer: {
    open: ['/my-page'],
  },
};

Usage via the CLI:

npx webpack serve --open /my-page

You can find this section https://webpack.js.org/configuration/dev-server/

fyalavuz
  • 99
  • 2
  • 3