我实际上是在复制 Laravel Vapor 文档中的内容,但无济于事。所以为了简短起见,我无法访问 ASSET_URL
Taylor 所说的环境变量是在构建步骤中注入(inject)的。
现在请注意,相同的环境变量被注入(inject)到应用程序中,这就是 assets()
的原因。和 mix()
index.blade.php 上的blade 助手工作得很好。但是,我的问题是访问 ASSET_URL
内webpack.mix.js
.
这是来自我下面的 Github 操作。你可以看到 ASSET_URL
登录时为空。
> @ production /github/workspace/.vapor/build/app
> cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js
ASSET URL IS:
DONE Compiled successfully in 26754ms11:23:07 PM
webpack.mix.js。
// To deal with Laravel Vapor
if (mix.inProduction()) {
const ASSET_URL = process.env.ASSET_URL;
console.log("ASSET URL IS: " + ASSET_URL);
mix.webpackConfig(webpack => {
return {
plugins: [
new webpack.DefinePlugin({
"process.env.ASSET_PATH": JSON.stringify(ASSET_URL)
})
],
output: {
publicPath: ASSET_URL
}
};
});
在 Laracasts 论坛上提出的解决方案
window.__ASSET_URL__ = '{{ env('ASSET_URL') }}';
找到了一种解决方法。但我不喜欢这种方法,除非它是不得已的方法。 MIX_ASSET_URL="${ASSET_URL}"
在我的环境文件中查看 Mix 是否可以读取。它没有。 最佳答案
在我们的案例中,我们认为它与 github not sourcing the .env
file 有关。 . env 文件,它包含正确的内容,但甚至手动采购 .env
文件不起作用,显然是 env
变量仅在下一步中可用。
我们已经回过头来手动解析 .env
中的 env 变量。文件,我们使用了这种 grep 方法:
$(grep -oP "ASSET_URL=\K.*$" .env)
关于laravel - 为什么 Laravel Vapor 在我的构建步骤中没有注入(inject) ASSET_URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590081/