简体   繁体   English

选择 Webpack 打开哪个浏览器?

[英]Choose which browser Webpack opens?

I installed Vue.js using the CLI as found here :我使用 CLI 安装了 Vue.js,如下所示

# install vue-cli
$ npm install --global vue-cli
# create a new project using the "webpack" template
$ vue init webpack my-project
# install dependencies and go!
$ cd my-project
$ npm install
$ npm run dev

When I run it opens Safari, my default browser.当我运行它时,它会打开我的默认浏览器 Safari。 I would like to specify Chrome (used only for development) without changing the OS default browser.我想在不更改操作系统默认浏览器的情况下指定 Chrome(仅用于开发)。

The webpack.dev.conf.js is as follows: webpack.dev.conf.js 如下:

var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')

// add hot-reload related code to entry chunks
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
  baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
})

module.exports = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
  },
  // cheap-module-eval-source-map is faster for development
  devtool: '#source-map',
  plugins: [
    new webpack.DefinePlugin({
      'process.env': config.dev.env
    }),
    // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      inject: true
    }),
    new FriendlyErrorsPlugin()
  ]
})

Anybody know how to specify Chrome in this configuration?有人知道如何在此配置中指定 Chrome 吗?

There is already a issue assigned for it on github but it's still under development.在 github 上已经为它分配了一个问题,但它仍在开发中。

Issue Link问题链接

Update更新

The issue finally got merged.问题终于合并了。 Now you can specify the browser using CLI or webpack.dev.conf.现在您可以使用 CLI 或 webpack.dev.conf 指定浏览器。

  1. Using CLI "start": "webpack-dev-server --config webpack.dev.js --open chrome"使用 CLI "start": "webpack-dev-server --config webpack.dev.js --open chrome"

  2. Using webpack.config.js:使用 webpack.config.js:

     module.exports = { //... devServer: { open: 'Google Chrome' } };

    Documentation Link 文档链接

For DevServer v4.0.0+适用于开发服务器v4.0.0+

webpack.config.json webpack.config.json

{
  // ...
  devServer:
  {
    open:
    {
      app:
      {
        name: 'firefox'
      }
    }
  },
  // ...
}

CLI命令行界面

npx webpack serve --open-app-name 'firefox'

For vue-cli 5.04, the only version that seems to work currently is in vue.config.js对于 vue-cli 5.04,目前唯一可行的版本是 vue.config.js

module.exports = defineConfig({
    devServer: {
        open: {
            app: {
                name: 'opera',
            }
        }
    },
});

however然而

vue-cli-service serve --open-app-name 'opera' 

does not work.不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM