webpack - Webpack 开发服务器 4.0.0 选项“打开”不起作用
问题描述
这是我的webpack.config.js
。使用此设置可以liveReload
正常工作(因为默认情况下它是true
),open: true
但没有。当我运行./node_modules/.bin/webpack serve
时,浏览器不会自动为我打开。
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
mode: "development",
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.join(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules)/,
use: {
loader: "babel-loader",
},
}
]
},
devServer: {
static: {
directory: path.join(__dirname, 'dist'),
},
open: {
app: {
name: "edge",
}
},
port: 3003,
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
inject: 'head',
scriptLoading: 'defer'
})
]
}
这是控制台消息:
> official-docs@1.0.0 server
> ./node_modules/.bin/webpack serve
(node:97874) [DEP_WEBPACK_DEV_SERVER_CONSTRUCTOR] DeprecationWarning: Using 'compiler' as the first argument is deprecated. Please use 'options' as the first argument and 'compiler' as the second argument.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:97874) [DEP_WEBPACK_DEV_SERVER_LISTEN] DeprecationWarning: 'listen' is deprecated. Please use async 'start' or 'startCallback' methods.
<i> [webpack-dev-server] Project is running at:
<i> [webpack-dev-server] Loopback: http://localhost:3000/
<i> [webpack-dev-server] On Your Network (IPv4): http://MY_IP:3000/
<i> [webpack-dev-server] On Your Network (IPv6): http://[MY_IP2]:3000/
<i> [webpack-dev-server] Content not from webpack is served from 'MY_PATH/babel-practice/official-docs/dist' directory
asset bundle.js 1 MiB [emitted] (name: main)
asset index.html 266 bytes [emitted]
runtime modules 27 KiB 13 modules
modules by path ./node_modules/core-js/ 252 KiB
modules by path ./node_modules/core-js/internals/*.js 122 KiB 145 modules
modules by path ./node_modules/core-js/modules/*.js 129 KiB 64 modules
modules by path ./node_modules/webpack-dev-server/client/ 50.7 KiB 12 modules
modules by path ./node_modules/webpack/hot/*.js 4.3 KiB 4 modules
modules by path ./node_modules/html-entities/lib/*.js 81.3 KiB 4 modules
modules by path ./node_modules/url/ 37.4 KiB 3 modules
modules by path ./node_modules/querystring/*.js 4.51 KiB 3 modules
./src/index.js 3.1 KiB [built] [code generated]
./node_modules/jquery/dist/jquery.js 282 KiB [built] [code generated]
./node_modules/ansi-html/index.js 4.16 KiB [built] [code generated]
./node_modules/events/events.js 14.5 KiB [built] [code generated]
webpack 5.48.0 compiled successfully in 974 ms
解决方案
你必须传递一些额外的选项,arguments
和(可选)target
。从迁移指南:
v3:
module.exports = {
devServer: {
// openPage: '/my-page',
openPage: true,
},
};
v4:
module.exports = {
devServer: {
// open: ['/my-page'],
open: true,
},
};
module.exports = {
devServer: {
open: {
target: ["first.html", `http://localhost:8080/second.html`],
app: {
name: "google-chrome",
arguments: ["--incognito", "--new-window"],
},
},
},
};
当我将浏览器指定为firefox
(而不是edge,
在您的示例中)并添加arguments: ["--new-window"]
时,我能够使功能正常工作
推荐阅读
- javascript - NODE.JS Twitch 机器人函数 fetch 完成工作但返回未定义
- javascript - Snowpack 内部模块导入路径问题
- elasticsearch - Update By Query API 是在 elasticsearch 中更新单个文档的好选择吗?
- ios - 按下任何按钮后,游戏似乎冻结了
- sql - VoltDB 在视图上计数不同
- r - 如何解决 R 上的重复行名错误
- azure - Azure 资源标记更改历史记录/还原已删除的标记
- docker - Statefulset 在本地 minikube 中不起作用。它给出,来自服务器的错误(BadRequest):pod mysql-0 没有分配主机
- android - 在没有安装 PDF 直接软件的情况下在 Android 中以编程方式打印 ZPL 文件
- java - 为什么 Spring "ForwardedHeaderFilter" 和 "RelativeRedirectFilter" 使用代码 303 而不是 302 重定向?