4

我实际上是在复制 Laravel Vapor 文档所述的内容,但无济于事。因此,为了简短起见,我无法访问ASSET_URLTaylor 所说的在构建步骤中注入的环境变量。

现在请注意,相同的环境变量被注入到应用程序中,这就是为什么index.blade.php 上的刀片中的assets()和帮助程序工作得很好。mix()但是,我的问题是访问ASSET_URLinside 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 论坛上提出的解决方案

  1. Paul Marshall 建议他通过使用找到了一种解决方法,window.__ASSET_URL__ = '{{ env('ASSET_URL') }}';但我不喜欢这种方法,除非它是最后的手段。
  2. @fylzero 建议我MIX_ASSET_URL="${ASSET_URL}"在我的环境文件中查看 Mix 是否可以读取。它没有。
4

1 回答 1

0

在我们的案例中,我们认为这与github 没有采购.env文件有关。env 文件,它包含正确的内容,但即使手动获取.env文件也不起作用,因为显然env变量仅在下一步中可用。

我们已经依靠手动解析.env文件中的 env 变量,我们使用了这种 grep 方法:

$(grep -oP "ASSET_URL=\K.*$" .env)
于 2021-09-06T09:32:13.990 回答