Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@anvilco/grunt-embed

Package Overview
Dependencies
Maintainers
6
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@anvilco/grunt-embed

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

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created
Source

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.

Keywords

FAQs

Package last updated on 22 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc