如何使用webpack-serve启动并运行? [英] How to get up and running with webpack-serve?
问题描述
我正在尝试使用 webpack-serv (不是dev-server)和我对如何真正运行它感到困惑.
I am trying to use webpack-serve(not dev-server) and I am a bit confused on how to actually run it.
我安装了它,我有npm,webpack 4,并在命令行中使用VS Code.
I installed it, I have npm, webpack 4 and using VS Code with command line.
在我看来,我应该只放入"webpack-serve",它应该可以运行,但是我得到了
It seems to me I should just put "webpack-serve" and it should run but I just get
'webpack-serve' is not recognized as an internal or external command,
operable program or batch file.
我是否需要沿着这些路线设置路径或其他内容?
Do I need to set some pathing or something along those lines?
编辑
我通过全局安装将其构建为构建".
I got it to "Build" by installing it globally.
我现在正在尝试使webpack-serv的功能与使用webpack-serve时的功能完全相同,但现在还不能(它无法打开浏览器,不确定如何在开发模式和生产模式之间进行切换并且不确定如何使路由正常工作
I am now trying to get webpack-serve to function exactly like what I had when I was using webpack-serve but right now it is not(it does not open the browser, not sure how switch between dev mode and production mode and not sure how to get routing to work)
我在这里举了一个例子: https://github.com/chobo2/webpack-服务示例
I have made an example here: https://github.com/chobo2/webpack-serve-example
推荐答案
它是不建议全局安装webpack
或任何与webpack
相关的工具,因此建议您首先全局卸载所有工具(NPM和Yarn).
It is NOT recommended to install webpack
or any webpack
related tools globally so I'd recommend you uninstall all of them globally (NPM and Yarn) first..
我开始了一个新项目,并创建了3个webpack
配置文件:
I started a new project and created 3 webpack
config files:
-
webpack.common.js
-
webpack.dev.js
-
webpack.prod.js
webpack.common.js
webpack.dev.js
webpack.prod.js
在我们的package.json
中:
"scripts": {
"serve": "webpack-serve --config webpack.dev.js",
"build": "webpack --config webpack.prod.js"
}
我从webpack-serve
中学到的东西是,我们必须在webpack
配置中定义一个serve
对象:
The thing I learned about webpack-serve
is we have to define a serve
object in our webpack
config:
const path = require('path');
module.exports = {
// other configs ....
serve: {
// a minimal example
content: path.resolve(__dirname, "dist")
}
}
我们将仅通过调用package.json
脚本
例如yarn run serve
或yarn run build
完整示例在 Github
这篇关于如何使用webpack-serve启动并运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!