无法在 Laravel 中为生产环境进行 vite 构建

new*_*aux 2 laravel laravel-livewire vite

我有一个 Laravel + Livewire 应用程序,我正在尝试为生产构建它。

\n

我可以成功运行./vendor/bin/sail npm run build

\n
./vendor/bin/sail npm run build\n\n> build\n> vite build\n\nvite v3.2.7 building for production...\ntransforming (19) node_modules/axios/lib/helpers/buildURL.js\n daisyUI components 2.52.0  https://daisyui.com\n  \xe2\x9c\x94\xef\xb8\x8e Including:  base, components, 2 themes, utilities\n  \xe2\x9d\xa4\xef\xb8\x8e Support daisyUI:  https://opencollective.com/daisyui \n  \n\xe2\x9c\x93 60 modules transformed.\npublic/build/manifest.json             0.25 KiB\npublic/build/assets/app.bf5ec64f.css   80.27 KiB / gzip: 12.73 KiB\npublic/build/assets/app.8c40d1a4.js    143.69 KiB / gzip: 51.52 KiB\n
Run Code Online (Sandbox Code Playgroud)\n

使用 @vite 指令将资源加载到刀片布局中:

\n
...\n{{-- Scripts --}}\n@vite([\'resources/css/app.css\', \'resources/js/app.js\'])\n...\n
Run Code Online (Sandbox Code Playgroud)\n

然而,当我查看页面源代码时,我可以看到它仍然引用端口 5173(这将是 vite 开发服务器):

\n
<script type="module" src="http://localhost:5173/@vite/client"></script><link rel="stylesheet" href="http://localhost:5173/resources/css/app.css" /><script type="module" src="http://localhost:5173/resources/js/app.js"></script>\n
Run Code Online (Sandbox Code Playgroud)\n

我期待看到参考public/build/assets/...

\n

我的 vite.config.js 相当简单:

\n
import { defineConfig } from \'vite\';\nimport laravel from \'laravel-vite-plugin\';\n\nexport default defineConfig({\n    plugins: [\n        laravel({\n            input: [\n                \'resources/css/app.css\',\n                \'resources/js/app.js\',\n            ],\n            refresh: true,\n        }),\n    ],\n    server: {\n        hmr: {\n            host: \'localhost\',\n        },\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n

文件(https://laravel.com/docs/10.x/vite#loading-your-scripts-and-styles)说:

\n

“在构建模式下,该指令将加载已编译和版本控制的资源,包括任何导入的 CSS。”

\n

...但它似乎并没有这样做。

\n

如何阻止 @vite 指令 / vite 尝试使用 vite 开发服务器并使用编译后的资源?(显然,vite 服务器不会在产品中运行)。

\n

我已经在我的 .env 中尝试过了APP_DEBUG=falseAPP_ENV=prod./vendor/bin/sail artisan view:clear没有什么区别。

\n

Deps 是最新的:“laravel/framework”:“^10.0”和“vite”:“^3.2.7”(当前为 4.4.2)。

\n

new*_*aux 8

事实证明,如果“hotfile”存在,./public/hot那么 vite Blade 指令(请参阅:)vendor/laravel/framework/src/Illuminate/Foundation/Vite.php将尝试查找 vite 服务器并执行热重载。

只需删除./public/hot即可一切正常!