@craftamap/esbuild-plugin-html

中文文档
0.9.0发布于 2025/03/17MITCommonJS近 30 天下载 32.8万

npm

原始使用文档
文档目录7 节
  1. Requirements
  2. Installation
  3. Usage
  4. Sample Configuration
  5. Configuration
  6. Contributing
  7. Kudos: Other *.html -Plugins

@craftamap/esbuild-plugin-html

npm

Simple banner containing the name of the project in a html self-closing tag

@craftamap/esbuild-plugin-html is a plugin to generate HTML files with esbuild. All specified entry points, and their related files (such as .css-files) are automatically injected into the HTML file. @craftamap/esbuild-plugin-html is inspired by jantimon/html-webpack-plugin.

Is any feature missing? Please create a ticket.

Requirements

This plugin requires at least esbuild v0.12.26. The minimum node version supported is Node.js 18.

Deno is officially not supported - however, it has been reported that the plugin does work with Deno.

Installation

yarn add -D @craftamap/esbuild-plugin-html
# or
npm install --save-dev @craftamap/esbuild-plugin-html

Usage

This plugin works by analyzing the metafile esbuild provides. This metafile contains information about all entryPoints and their output files. This way, this plugin can map input files to their output file (javascript as well as css).

craftamap/esbuild-plugin-html uses the jsdom under the hood to create a model of your HTML from the provided template. In this model, all discovered resources are injected. The plugin also uses lodash templates to insert custom user data into the template.

@craftamap/esbuild-plugin-html requires to have some options set in your esbuild script:

  • outdir must be set. The html files are generated within the outdir.
  • metafile must be set to true (the plugin does this automatically, if it's not set to false on purpose).

⚠️: you can set a specific output name for resources using esbuild's entryNames feature. While this plugin tries to support this as best as it can, it may or may not work reliable. If you encounter any issues with it, please create a ticket.

Sample Configuration

const esbuild = require('esbuild');
const { htmlPlugin } = require('@craftamap/esbuild-plugin-html');

const options = {
    entryPoints: ['src/index.jsx'],
    bundle: true,
    metafile: true, // needs to be set
    outdir: 'dist/', // needs to be set
    plugins: [
        htmlPlugin({
            files: [
                {
                    entryPoints: [
                        'src/index.jsx',
                    ],
                    filename: 'index.html',
                    htmlTemplate: `
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
            </head>
            <body>
                <div id="root">
                </div>
            </body>
            </html>
          `,
                },
                {
                    entryPoints: [
                        'src/auth/auth.jsx',
                    ],
                    filename: 'auth.html',
                    title: 'Login',
                    scriptLoading: 'module',
                    favicon: './public/favicon.ico',
                    hash: true,
                },
                {
                    entryPoints: [
                        'src/installation/installation.jsx',
                    ],
                    filename: 'installation.html',
                    title: 'title',
                    scriptLoading: 'module',
                    define: {
                        "version": "0.3.0",
                    },
                    htmlTemplate: `
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
            </head>
            <body>
                You are using version <%- define.version %>
                <div id="root">
                </div>
            </body>
            </html>
          `,
                },
            ]
        })
    ]
}

esbuild.build(options).catch(() => process.exit(1))

Configuration

interface Configuration {
    files: HtmlFileConfiguration[],
}

interface HtmlFileConfiguration {
    filename: string,           // Output filename, e.g. index.html. This path is relative to the out dir
    entryPoints: string[],      // Entry points to inject into the created html file, e.g. ['src/index.jsx']. 
                                // Multiple entryPoints are possible.
    title?: string,             // title to inject into the head, will not be set if not specified
    htmlTemplate?: string,      // custom html document template string. If you omit a template, 
                                // a default template will be used (see below)
                                // can also be a relative path to an html file
    define?: Record<string, string>,
                                // Define custom values that can be accessed in the lodash template context
    scriptLoading?: 'blocking' | 'defer' | 'module', 
                                // Decide if the script tag will be inserted as blocking script tag, 
                                // with `defer=""` (default) or with `type="module"`
    favicon?: string,           // path to favicon.ico. If not specified, no favicon will be injected
    findRelatedCssFiles?: boolean,
                                // Find related output *.css-files and inject them into the html. 
                                // Defaults to true.
    findRelatedOutputFiles?: boolean,
                                // Find output files following the same name schema of the output file 
                                // like (*.css)-files and inject them into the html. This option is deprecated,
                                // consider using findRelatedCssFiles.
                                // Defaults to false.
    inline?: boolean | {        // Inline all js and css entry points into the html file.
        js?: boolean,           // Inline all js resources into the html file. 
        css?: boolean,          // Inline all css resources into the html file.
    } | ((filepath: string) => boolean), // Inline resources by custom function.
                                // Not set by default - will not inline any resources.
    extraScripts?: (string | {  // accepts an array of src strings or objects with src and attributes
        src: string;            // src to use for the script
        attrs?: { [key: string]: string } // attributes to append to the script, e.g. { type: 'module', async: true }
    })[],
    hash?: boolean | string,    // Append a hash to all included scripts and CSS files for cache-busting. The
                                // hash is based on the given string. If given a boolean, the hash is based on
                                // the current time.
}

In case a publicPath is specified in the esbuild configuration, esbuild-plugin-html will use absolute paths with the provided publicPath.

You can also change the verbosity of the plugin by changing esbuild's verbosity.

Default HTML template

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
  </body>
</html>

Contributing

Contributions are always welcome.

Currently tsc is used to build the project.

Commits should be messaged according to Conventional Commits.

Kudos: Other *.html-Plugins

There exist some other *.html-plugins for esbuild. Those work differently than @craftamap/esbuild-plugin-html, and might be a better fit for you:

版本与发布记录

@craftamap/esbuild-plugin-html 共发布 15 个版本,首次发布于 2021/09/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.9.0latest2025/03/171.7 年前
0.8.02024/08/292 年前
0.7.02024/05/142 年前
0.6.12023/07/053 年前
0.6.02023/07/053 年前
0.5.02022/12/293 年前

关于 @craftamap/esbuild-plugin-html 的常见问题

如何安装 @craftamap/esbuild-plugin-html?

在项目目录运行 npm install @craftamap/esbuild-plugin-html,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:esbuild(>=0.15.10)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@craftamap/esbuild-plugin-html 支持 TypeScript 吗?

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

@craftamap/esbuild-plugin-html 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@craftamap/esbuild-plugin-html 需要什么版本的 Node.js?

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

@craftamap/esbuild-plugin-html 可以商用吗?

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

@craftamap/esbuild-plugin-html 的下载量有多少?

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

@craftamap/esbuild-plugin-html 还在维护吗?

最新版本 0.9.0 发布于 2025/03/17(1.7 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/09/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。