@commercetools-frontend/mc-html-template

中文文档
27.17.1发布于 2026/11/11MITESM + CJS构建来源已验证近 30 天下载 2.6万

Everything related to render the index.html for a MC application

原始使用文档
文档目录7 节
  1. Install
  2. API
  3. generateTemplate
  4. replaceHtmlPlaceholders
  5. compileHtml
  6. processHeaders
  7. Bundler entry points

@commercetools-frontend/mc-html-template

Latest release (latest dist-tag) Latest release (next dist-tag) Minified + GZipped size GitHub license

This package contains utils and scripts related to the index.html for a Merchant Center customization.

Install

$ npm install --save @commercetools-frontend/mc-html-template

API

generateTemplate

This method will return the template HTML document with the provided CSS/JS scripts injected.

NOTE that the HTML document will still have the placeholders (see replaceHtmlPlaceholders)

type TGenerateTemplateOptions = {
  cssImports?: string[];
  scriptImports?: string[];
};

function generateTemplate({
  cssImports = [],
  scriptImports = [],
}: TGenerateTemplateOptions);

replaceHtmlPlaceholders

This method will replace the placeholders defined in the HTML document based on the application config.

This method should be used as the final step to get the fully compiled index.html.

At the moment we define the following placeholders:

  • __CDN_URL__: the cdnUrl value defined in the application config
  • __MC_API_URL__: the mcApiUrl value defined in the application config
  • __LOADING_SCREEN_CSS__: (defined internally) the CSS for the loading animation in case the page takes longer to load
  • __LOADING_SCREEN_JS__: (defined internally) the JS for the loading animation in case the page takes longer to load
  • __APP_ENVIRONMENT__: the sanitized application config environment, which will be available at the global variable window.app
  • __CSP__: the generated Content-Security-Policy directives, defined as an HTML meta tag
type TReplaceHtmlPlaceholdersOptions = {
  env: ApplicationRuntimeConfig['env'];
  headers: Record<string, string | undefined>;
};

function replaceHtmlPlaceholders(
  indexHtmlContent: string,
  options: TReplaceHtmlPlaceholdersOptions
): string;

compileHtml

This method will compile the template HTML document.

type TCompileHtmlResult = {
  env: ApplicationRuntimeConfig['env'];
  headers: Record<string, string | undefined>;
  indexHtmlContent: string;
};

async function compileHtml(
  indexHtmlTemplatePath: string
): Promise<TCompileHtmlResult>;

processHeaders

This method will return the security headers to be used on the server response, serving the index.html.

The applicationConfig.env is the processed application environment that would be injected as the window.app. The applicationConfig.headers is the processed application headers provided by the user.

The return value of the processHeaders function contains the following ready-to-use HTTP headers:

{
  "Strict-Transport-Security": "max-age=31536000",
  "X-XSS-Protection": "1; mode=block",
  "X-Content-Type-Options": "nosniff",
  "X-Frame-Options": "SAMEORIGIN",
  "Content-Security-Policy": "...",
  "Feature-Policies": "..."
}
function processHeaders(
  applicationConfig: ApplicationRuntimeConfig
): Record<string, string | undefined>;

Bundler entry points

The package exposes some special entry points used by specific bundlers to use the HTML template.

Webpack

If you use Webpack with the HtmlWebpackPlugin, you can pass the webpack entry point that will map the Webpack template params to our generic generateTemplate method.

new HtmlWebpackPlugin({
  template: require.resolve(
    '@commercetools-frontend/mc-html-template/webpack'
  ),
  // ...
}),

版本与发布记录

@commercetools-frontend/mc-html-template 共发布 1,738 个版本,首次发布于 2018/08/15;过去 12 个月发布了 203 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
27.17.1latest2026/11/11今天
0.0.0-canary-202611101453512026/11/10今天
0.0.0-canary-202611061943342026/11/073 天前
0.0.0-canary-202611060848452026/11/064 天前
0.0.0-canary-202611060847432026/11/064 天前
27.17.02026/11/046 天前

其他发布通道:rc → 21.0.0-rc.7,preview → 22.36.0-preview.0,FEC-157-preview-releases → 0.0.0-FEC-157-preview-releases-20241126231507,testing-workflow → 0.0.0-testing-workflow-20241126234621,testing-comment-workflow → 0.0.0-testing-comment-workflow-20241204141931,rc-preview-demo-cm → 0.0.0-rc-preview-demo-cm-20250130161121。

关于 @commercetools-frontend/mc-html-template 的常见问题

@commercetools-frontend/mc-html-template 是什么?

Everything related to render the index.html for a MC application

如何安装 @commercetools-frontend/mc-html-template?

在项目目录运行 npm install @commercetools-frontend/mc-html-template,或使用 pnpm add / yarn add / bun add。

@commercetools-frontend/mc-html-template 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/commercetools-frontend__mc-html-template 是否存在。

@commercetools-frontend/mc-html-template 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@commercetools-frontend/mc-html-template 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.23.4 || ^24.22.1 || >=26.12.1。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@commercetools-frontend/mc-html-template 可以商用吗?

@commercetools-frontend/mc-html-template 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@commercetools-frontend/mc-html-template 的下载量有多少?

根据 npm 官方下载统计,@commercetools-frontend/mc-html-template 在 2026/10/11 – 2026/11/09 这 30 天内被下载 26,168 次,日均约 872 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@commercetools-frontend/mc-html-template 还在维护吗?

最新版本 27.17.1 发布于 2026/11/11(今天);至今共发布 1738 个版本,其中过去 12 个月发布了 203 个;首次发布于 2018/08/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。