如何从 manifest.json 链接一些东西 [英] How to link something from manifest.json

查看:22
本文介绍了如何从 manifest.json 链接一些东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Webpack 与 CleanWebpackPlugin 一起使用,但我使用的是 index.php.所以 HtmlWebpackPlugin 不是一个选项.我发现了一个名为 WebpackManifestPlugin 的不同插件.这将创建一个包含文件及其哈希值的文件,名为 manifest.json.它看起来像这样:

I'm using Webpack with the CleanWebpackPlugin, but I use an index.php. So HtmlWebpackPlugin isn't an option. I found a different plugin called WebpackManifestPlugin. This creates a file with the files and their hash called manifest.json. It looks like this:

<代码>{"main.js": "css.d915ef.js","main.css": "main.d915ef.css"}

但我如何在脑海中使用它?

But how do I use this in my head?

因为这行不通.

推荐答案

在 php 脚本中使用该函数

Use that function in php script

function asset($asset_name)
{
    $manifest = file_get_contents("./manifest.json");
    $manifest = json_decode($manifest, true); //decode json string to php associative array
    if (!isset($manifest[$asset_name])) return $asset_name; //if manifest.json doesn't contain $asset_name then return $asset_name itself
    return "/dist/" . $manifest[$asset_name];
}

它将读取并解析 manifest.json 并将 "main.js" 替换为 "css.d915ef.js"您可以在生成 html 时使用 asset() 函数,如下所示:

It will read and parse manifest.json and replace "main.js" with "css.d915ef.js" You can use asset() function while generating yout html, like this:

<link rel="stylesheet" type="text/css" href="<?php echo asset("main.js"); ?>">

请注意,file_get_contents("./manifest.json") 仅在您的 php 脚本和 manifest.json 位于同一文件夹中时才能正常工作.如果没有 - 您需要在此处提供 manifest.json 的正确路径.

Please, be aware, that file_get_contents("./manifest.json") will only work correctly if your php script and manifest.json is in the same folder. If not - you need to provide correct path to manifest.json here.

这篇关于如何从 manifest.json 链接一些东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆