@anvilco/resource-embedder

中文文档
0.0.4发布于 2022/06/22MITCommonJS近 30 天下载 7599

Embeds the contents of external scripts and stylesheets into HTML.

原始使用文档
文档目录8 节
  1. Usage
  2. Choosing which files to embed
  3. Options
  4. Contributing
  5. Tasks
  6. Release History
  7. License
  8. Wishlist

resource-embedder

Build Status Dependency Status

Also available as a Grunt plugin: grunt-embed

Automatically embeds the contents of external scripts and stylesheets into HTML markup.

Takes an HTML file path and generates a string of modified markup. Any small external scripts and stylesheets are replaced with inline <script>...</script> or <style>...</style> elements.

This reduces the number of HTTP requests in return for inflating your HTML. It's up to you to decide whether this is a good trade-off in your situation.

Usage

npm install resource-embedder
var ResourceEmbedder = require('@anvilco/resource-embedder');

var embedder = new ResourceEmbedder('./app/page.html');

embedder.get(function (markup) {
  fs.writeFileSync('./dist/page.html', markup);
});

Choosing which files to embed

By default, any scripts or stylesheets below 5KB in size will be embedded. You can change this setting in the options.

You can also manually decide the threshold for each resource using a data-embed attribute.

To always embed a particular resource, regardless of filesize, just include the attribute:

<script src="foo.js" data-embed></script>
<link rel="stylesheet" href="foo.css" data-embed>

To prevent a particular script from ever being embedded, set it to false (or 0):

<script src="foo.js" data-embed="false"></script>
<link rel="stylesheet" href="foo.css" data-embed="false">

To embed a particular resource only if it is below a certain size, specify the maximum number of bytes, or something like 5KB:

<script src="foo.js" data-embed="2000"></script>
<link rel="stylesheet" href="foo.css" data-embed="5KB">

Options

To specify options:

new ResourceEmbedder('./file.html', options);

...or just: new ResourceEmbedder(options);

  • htmlFile — only required if you don't pass a file path as the first argument to the constructor.
  • assetRoot — (optional) – use this if you need to specify the directory that the relative resource URLs will be considered relative to. (By default, it's the same directory as the HTML file.)
  • threshold (default: "5KB") — all resources below this filesize will be embedded. NB: you can set this to 0 if you want, in which case nothing will be embedded except those resources you mark with a data-embed attribute (see above).
  • stylesheets (default: true) — whether to embed stylesheets.
  • scripts (default: true) — whether to embed scripts.
  • deleteEmbeddedFiles (default: false) – whether the external files should be deleted after being embedded.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Test your code using Grunt.

Tasks

When you're working on a specific module in src, e.g. parse-file-size.coffee, first run grunt watch:parse-file-size in a terminal and keep the window open. Then open src/parse-file-size.coffee and test/parse-file-size_test.coffee and work on them side-by-side. New test results will appear in the terminal whenever you save either file.

You can also just do grunt watch to watch all modules at once and run all test files whenever anything changes.

Release History

(Nothing yet)

License

Copyright (c) 2013 Callum Locke. Licensed under the MIT license.

Wishlist

  • Connect/Express middleware
  • ability to specify root for relative paths, in case different from the HTML file
  • remove data-embed attributes from elements that don't get embedded, so they don't litter the output markup unnecessarily

版本与发布记录

@anvilco/resource-embedder 共发布 3 个版本,首次发布于 2022/06/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.4latest2022/06/224 年前
0.0.22022/06/224 年前
0.0.12022/06/224 年前

关于 @anvilco/resource-embedder 的常见问题

@anvilco/resource-embedder 是什么?

Embeds the contents of external scripts and stylesheets into HTML.

如何安装 @anvilco/resource-embedder?

在项目目录运行 npm install @anvilco/resource-embedder,或使用 pnpm add / yarn add / bun add。

@anvilco/resource-embedder 支持 TypeScript 吗?

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

@anvilco/resource-embedder 支持 ESM 还是 CommonJS?

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

@anvilco/resource-embedder 可以商用吗?

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

@anvilco/resource-embedder 的下载量有多少?

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

@anvilco/resource-embedder 还在维护吗?

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