首页 > 其他分享 >webpack的html模板中插入变量写法

webpack的html模板中插入变量写法

时间:2023-11-22 09:24:02浏览次数:34  
标签:used plugin compilation webpack html htmlWebpackPlugin 模板

vue-cli文档中的描述如下

Index 文件#

public/index.html 文件是一个会被 html-webpack-plugin 处理的模板。在构建过程中,资源链接会被自动注入。另外,Vue CLI 也会自动注入 resource hint (preload/prefetch、manifest 和图标链接 (当用到 PWA 插件时) 以及构建过程中处理的 JavaScript 和 CSS 文件的资源链接。

插值#

因为 index 文件被用作模板,所以你可以使用 lodash template 语法插入内容:

  • <%= VALUE %> 用来做不转义插值;
  • <%- VALUE %> 用来做 HTML 转义插值;
  • <% expression %> 用来描述 JavaScript 流程控制。

除了被 html-webpack-plugin 暴露的默认值之外,所有客户端环境变量也可以直接使用。例如,BASE_URL 的用法:

<link rel="icon" href="<%= BASE_URL %>favicon.ico">

更多内容可以查阅:

1. html可以插入那些变量

1.1 htmlWebpackPlugin本身的变量

htmlWebpackPlugin.options

htmlWebpackPlugin.tags

htmlWebpackPlugin.files

1.2 webpack的对象

webpackConfig对象

compilation对象

The following variables are available in the template by default (you can extend them using the templateParameters option):

  • htmlWebpackPlugin: data specific to this plugin

    • htmlWebpackPlugin.options: the options hash that was passed to the plugin. In addition to the options actually used by this plugin, you can use this hash to pass arbitrary data through to your template.

    • htmlWebpackPlugin.tags: the prepared headTags and bodyTags Array to render the <base><meta><script> and <link> tags. Can be used directly in templates and literals. For example:

      <html>
        <head>
          <%= htmlWebpackPlugin.tags.headTags %>
        </head>
        <body>
          <%= htmlWebpackPlugin.tags.bodyTags %>
        </body>
      </html>
    • htmlWebpackPlugin.files: direct access to the files used during the compilation.

      publicPath: string;
      js: string[];
      css: string[];
      manifest?: string;
      favicon?: string;
  • webpackConfig: the webpack configuration that was used for this compilation. This can be used, for example, to get the publicPath (webpackConfig.output.publicPath).

  • compilation: the webpack compilation object. This can be used, for example, to get the contents of processed assets and inline them directly in the page, through compilation.assets[...].source() (see the inline template example).

详细可参考 html-webpack-plugin模板文档 和 模板变量examples 

2. 区分开发环境和生产环境的变量写法

在 html 标签里面这样使用

<% if (process.env.NODE_ENV === 'development') { %>
  <script src="<%= BASE_URL %>libs/vue/vue.js"></script>
  <% } else { %>
  <script src="<%= BASE_URL %>libs/vue/vue.min.js"></script>
  <% }
%>

在 script 脚本里面这样使用

<script>
    if ('<%= process.env.NODE_ENV %>' === 'development') {
      window.SITE_CONFIG['apiURL'] = '/api';  
    }
    else {
      window.SITE_CONFIG['apiURL'] = '/prod_api';         
    }
</script>

 

参考:webpack 如何在 html 页面里区分环境变量

标签:used,plugin,compilation,webpack,html,htmlWebpackPlugin,模板
From: https://www.cnblogs.com/mengff/p/17848117.html

相关文章

  • 【模板】链式前向星
    /*https://blog.csdn.net/sugarbliss/article/details/86495945?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169977002316800215045285%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=169977002316800215045285&biz_id......
  • 【模板】并查集 (洛谷P3367)
    1#include<bits/stdc++.h>2usingnamespacestd;3template<classT>4inlinevoidread(T&s)5{6s=0;7intw=1;8charch=getchar();9while(ch<'0'||ch>'9')10{11......
  • 【模板】线段树1(洛谷P3372)
    1#include<iostream>2#include<cstdio>34usingnamespacestd;56template<classT>7inlinevoidread(T&s)8{9s=0;10intw=1;11charch=getchar();12while(ch<'0�......
  • 组合数模板(省赛)
    组合数+快速幂#include<bits/stdc++.h>//#pragmaGCCoptimize("Ofast")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cmath>//#definedoublelongdou......
  • KMP模板
    #include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e3+10,inf=0x3f3f3f3f;intnex[N];//nex[j]的意思是当子串的第j个字符和主串的第i个字符不匹配时,我们应该从子串的nex[j]字符开始重新匹配stringa,b;/*kmp指针回退j=nex[j-1]......
  • 行为型模式-模板方法模式
    1什么是模板方法模式模板方法模式是一种行为设计模式,它定义了一个算法的骨架,将一些步骤的具体实现延迟到子类中。这样可以在不改变算法结构的情况下,允许子类根据自身的需求来实现特定的步骤。模板方法模式通常由一个抽象基类提供一个模板方法,该方法定义了算法的骨架,并调用一系......
  • 【AD域控】组策略模板的导入与使用
    接到了leader的需求,希望能够设置浏览器的主页,由于我们是运维岗,负责AD域控,脑海中第一时间就跳出了舍近求远的域控设置。当然最后也是没有成功,但总结出了在Windows设备上配置MicrosoftEdge策略设置,血泪总结!【AD域控】组策略模板的导入与使用 1.下载MicrosoftEdgeforBusiness......
  • pp_orange的多项式模板
    /*Codebypp_orange*/#include<bits/stdc++.h>#definem_p(a,b)make_pair(a,b)#definepbpush_back#definelllonglong#defineullunsignedlonglong#definelldlongdouble#defineinf0x7FFFFFFF#defineinff9223372036854775807#definerep(i,l,......
  • html字间距怎么设置?
    在HTML中,可以通过CSS来设置字间距。字间距指的是字符之间的空白区域,在网页设计中,修改字间距可以改变文字的外观和排版效果。下面详细介绍如何使用CSS来设置字间距。使用letter-spacing属性在CSS中,可以使用letter-spacing属性来设置字间距。该属性控制字符之间的额外水平空间,可......
  • MD转带目录的html
    使用i5ting_toc。1.安装npm2.安装i5ting_tocnpminstall-gi5ting_toc3.转htmli5ting_toc-fxx.md-o需要进入md的目录4.效果 备注:可以修改上图目录里的”tableofcontent“,打开preview\toc\js\ztree_toc.js,修改 ......