npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@anvilco/resource-embedder

v0.0.4

Published

Embeds the contents of external scripts and stylesheets into HTML.

Downloads

11,005

Readme

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