20

I am using Laravel 5.4 in my project and trying to set up the frontend build system using Mix. Everything is working fine except I could not be able to get browser auto reload option. There is nothing about it on documentation.

Someone please help, how can I achieve that?

4

5 回答 5

23

重要更新!

忘记我之前说过的话,现在 laravel mix 已经更新,并且在功能和文档方面做了一些改进。

现在你可以简单:

mix.browserSync('my-domain.dev');

// Or:

mix.browserSync({
    proxy: 'my-domain.dev'
})

然后npm run watch你就可以走了!

如果要更新 webpack 版本,请将 package.json 上的版本更改为 *:

"laravel-mix": "*",

并运行npm update laravel-mix

请查看github 上的 mix 更新文档

重要更新结束

根据您只需要npm run hot在项目和脚本或样式表参考中运行的文档,请使用:

<link rel="stylesheet" href="{{ mix('css/app.css') }}">

因此该mix()函数会将您的资产网址指向http://localhost:8080/.

buuuuuuuut ...这就是您在此处看到的文档所说的内容。我无法让它在运行 arch linux 的 laravel 全新安装上运行,一切都按应有的方式编译,并且mix()函数指向8080但没有注入任何东西,我又回到了Ctrl+R年龄。

希望你有更多的运气!

于 2017-02-07T16:12:32.333 回答
13

你仍然可以使用browser-sync. Laravel 5.4 随 webpack 一起提供,并且有一个插件:browser-sync-webpack-plugin.

  1. 安装browser-sync的东西:

    npm install --save-dev browser-sync browser-sync-webpack-plugin

  2. 添加到webpack.mix.js

const BrowserSyncPlugin = require('browser-sync-webpack-plugin');

mix.webpackConfig({
   plugins: [
       new BrowserSyncPlugin({
           files: [
               'app/**/*',
               'public/**/*',
               'resources/views/**/*',
               'routes/**/*'
           ]
       })
   ]
});
  1. 在页面底部之前添加此代码段</body>

    @if (getenv('APP_ENV') === 'local')
    <script id="__bs_script__">//<![CDATA[
        document.write("<script async src='http://HOST:3000/browser-sync/browser-sync-client.js?v=2.18.6'><\/script>".replace("HOST", location.hostname));
        //]]>
    </script>
    @endif
    
  2. 同时启动 Laravel 和 webpack-dev-server:

    php artisan serve & npm run watch

于 2017-02-12T11:07:09.987 回答
6

对于我接下来要做的项目:

1)安装chrome浏览器扩展Livereload https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei

2)全局安装livereload(通过CLI):

npm install -g livereload

或本地在您的项目文件夹中(通过 CLI):

npm install livereload

3) 将此代码段放入您的layout.blade.php或其他模板中:

<script>
   document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] +
        ':35729/livereload.js?snipver=1"></' + 'script>')
</script>

4)在你的项目文件夹中运行 livereload(通过 CLI):

livereload

5) 在 Chrome 浏览器中按 livereload 按钮 实时加载按钮

而已!

于 2017-07-01T13:23:06.253 回答
6

是的,我一直有同样的问题,我会说坚持使用长生不老药,因为如果你看一下 github,它就会一团糟。从引导程序加载字体存在问题,组合方法流存在问题,甚至有太多问题无法详细介绍。它太新了,他们没有解决所有问题。

我的 2 美分是,如果你要升级到新的东西,至少要确保开箱即用的长生不老药有效。

于 2017-02-07T17:10:15.610 回答
2

如果有人需要另一种方法来使其工作,例如,如果在后端没有使用 mix ,这就是我解决它的方法:

从项目根目录编辑server.php并替换return false;

if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

如下:

if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {

    // set log file for debugging purposes if needed
    #ini_set('error_log', __DIR__ . '/storage/logs/laravel.log');

    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://localhost:8080" . $uri);

    exit();
}

当然你需要同时开始artisan servenpm run hot

于 2017-06-04T15:18:45.200 回答