@anvilco/grunt-embed

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

Grunt plugin to convert external scripts and stylesheets into embedded ones.

原始使用文档
文档目录8 节
  1. Getting Started
  2. The "embed" task
  3. Overview
  4. Options
  5. Usage Examples
  6. Contributing
  7. Release History
  8. License

grunt-embed

Build Status Dependency Status

This is a Grunt plugin wrapper around resource-embedder.

Turns short external scripts and stylesheets into embedded ones:

  • <script src="foo.js"></script> becomes <script> ... </script>
  • <link rel="stylesheet" href="bar.css"> becomes <style> ... </style>

The default behaviour is to embed anything under 5KB in size, but this threshold is configurable.

Should you embed your scripts? Depends. Embedding reduces the number of HTTP requests, and can reduce blocking of subsequent requests and page rendering, but it also means the resources can't be cached individually and shared between pages.

You should do your own measurements to work out if this is a good trade-off in your situation. But, as a guide: short, blocking scripts in the head are often a good candidate for embedding.

A small Modernizr build is a good example: if it's embedded (as a script before your main stylesheet), it will have been executed and applied any special CSS classes to the <html> tag before your styles are received. Then as soon as the styles are received, any subsequent background-image downloads can be started immediately, because Modernizr's classes will already have been added to the <html> tag.

Getting Started

This plugin requires Grunt (see Getting Started).

Install:

npm install grunt-embed --save-dev

Load the task in your Gruntfile:

grunt.loadNpmTasks('grunt-embed');

The "embed" task

Overview

In your Gruntfile, add a section named embed to the data object passed into grunt.initConfig().

grunt.initConfig({
  embed: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
})

Options

Any options will be passed through to resource-embedder – see full list of options.

Usage Examples

Default Options

Embed any external scripts and stylesheets under 5KB in size (the default threshold):

grunt.initConfig({
  embed: {
    some_target: {
      files: {
        'dest/output.html': 'src/input.html'
      }
    }
  }
})

Custom Options

Custom threshold – embed anything under 3KB in size:

grunt.initConfig({
  embed: {
    options: {
      threshold: '3KB'
    },
    some_target: {
      files: {
        'dest/output.html': 'src/input.html'
      }
    }
  }
})

Overriding the options for a given script/stylesheet

You can use data-embed attributes to override the options for an individual resource.

<script src="foo.js" data-embed></script> <!-- always embed -->
<script src="foo.js" data-embed="false"></script> <!-- never embed -->
<script src="foo.js" data-embed="10KB"></script> <!-- embed if under 10KB -->

Contributing

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

Release History

(Nothing yet)

License

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

版本与发布记录

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

版本发布日期距今
0.0.1latest2022/06/224 年前

关于 @anvilco/grunt-embed 的常见问题

@anvilco/grunt-embed 是什么?

Grunt plugin to convert external scripts and stylesheets into embedded ones.

如何安装 @anvilco/grunt-embed?

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

@anvilco/grunt-embed 支持 TypeScript 吗?

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

@anvilco/grunt-embed 支持 ESM 还是 CommonJS?

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

@anvilco/grunt-embed 可以商用吗?

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

@anvilco/grunt-embed 的下载量有多少?

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

@anvilco/grunt-embed 还在维护吗?

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