使用grunt和processshtml来更新base href(不保留部分路径)

Using grunt and processhtml to update base href (without preserving parts of the path)

本文关键字:保留部 路径 href base grunt processshtml 更新 使用      更新时间:2023-09-26

我试图使用Grunt和processshtml来更新index.html中的<base href="path">。这就增加了这个问题,这个问题似乎已经死了,没有一个公认的答案。这里的答案也不适合我。

my index.html:

<!-- build:[href] /ndt/dist-local/ -->
<base href="/ndt/web/">
<!-- /build -->

结果是:

<base href="/ndt/dist-local/web">

我想说的是:

<base href="/ndt/dist-local/">

添加web部件(不带斜杠)。我怎样才能让它不发生呢?

要绕过<!-- build:<attr> <target> -->始终包括最后一部分,您可以使用removetemplate的组合:

    <!-- build:remove -->
        <base href="/ndt/web/">
    <!-- /build -->
    <!-- build:template
        <base href="/ndt/dist-local/">
    /build -->

这不是最漂亮的选择,但它有效。如果您想让它更动态一点,您可以将distbase作为data -选项包含:

Gruntfile配置:

{
  options: {
    data: {
      distbase: "/ndt/dist-local/
    }
  },
  files: /**/
}
HTML:

<!-- build:remove -->
    <base href="/ndt/web/">
<!-- /build -->
<!-- build:template
    <base href="<%= distbase %>">
/build -->