配置Webpack以在Laravel应用程序中工作

时间:2015-09-19 16:16:39

标签: laravel webpack webpack-dev-server

我想将 webpack-dev-server 用于我的基本Laravel应用程序。从official Webpack docs我学到了以下内容:

  

您可能希望在开发中运行后端服务器或模拟它。   您不应该使用webpack-dev-server作为后端。这只是   目的是提供静态(webpacked)资产。

     

你可以并排运行两个服务器:webpack-dev-server和你的   后端服务器。

在页面底部,他们举例说明如何实现这一目标。我跟进并遇到了两条错误消息。这是我尝试将Webpack集成到Laravel应用程序中。

webpack.config.js



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"/>
</body>

</html>
&#13;
&#13;
&#13;

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和后端服务器)。我错过了什么?

1 个答案:

答案 0 :(得分:0)

您缺少的很简单,您必须在Laravel后端服务器(例如路由/资产)下代理webpack-dev-server。

为此,我让你检查http-proxy,Express中间件,甚至是socat / netcat CLI工具。