我想为我的基本Laravel应用程序使用webpack-dev-server。从官方Webpack文档中,我了解到以下信息:
页面底部提供了一个示例,以说明如何实现这一点。 我按照示例进行操作,但遇到了两个错误消息。 这是我将Webpack集成到Laravel应用程序中的尝试。 webpack.config.js您可能希望在开发中运行后端服务器或其模拟。 您不应将webpack-dev-server用作后端。 它的唯一目的是提供静态(webpacked)资产。
您可以并行运行两个服务器:webpack-dev-server和后端服务器。
var path = require("path");
module.exports = {
context: path.resolve('resources'),
entry: [
'./assets/js/app.js'
],
output: {
path: path.resolve('public/assets/'),
publicPath: 'http://localhost:8080/assets/',
filename: "bundle.js"
},
devServer: {
contentBase: 'public',
hot: true,
proxy: {
"*": "http://laravel.dev/"
}
}
};
app.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Learning Laravel</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
@yield('content')
</div>
@yield('footer')
<script src="http://localhost:8080/assets/bundle.js"/>
vagrant@learning-laravel:~/learning-laravel$ webpack-dev-server --inline
控制台输出:
http://localhost:8080/
webpack result is served from http://localhost:8080/assets/
content is served from public
Hash: 3346964212f5b22513c6
Version: webpack 1.12.2
Time: 347ms
Asset Size Chunks Chunk Names
bundle.js 228 kB 0 [emitted] main
chunk {0} bundle.js (main) 213 kB [rendered]
[0] multi main 52 bytes {0} [built] [1 error]
[1] /usr/lib/~/webpack-dev-server/client?http://localhost:8080 2.14 kB {0} [built]
[2] (webpack)/~/node-libs-browser/~/url/url.js 22.3 kB {0} [built]
[3] (webpack)/~/node-libs-browser/~/punycode/punycode.js 14.6 kB {0} [built]
[4] (webpack)/buildin/module.js 251 bytes {0} [built]
[5] (webpack)/~/node-libs-browser/~/url/~/querystring/index.js 127
...
[61] ./resources/assets/js/app.js 103 bytes {0} [built]
[62] ./resources/assets/js/login.js 28 bytes {0} [built]
ERROR in multi main
Module not found: Error: Cannot resolve module 'webpack/hot/dev-server' in /home/vagrant/learning-laravel/resources
@ multi main
webpack: bundle is now VALID.
我正在使用Homestead。应用程序是在http://laravel.dev/上提供服务的。那么我的问题是如何同时运行两个服务器(webpack-dev-server和后端服务器)。我缺少什么?
devServer
中完全删除了proxy
,这会有所帮助吗?我有几乎相同的设置,它可以正常工作。当然,你仍然可以使用http://learning-laravel.dev:8888或类似的方式来访问你的网站。 - bernhardw