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

grunt-assets-inline

v1.2.4

Published

A grunt task which takes a html file, finds all the css, js links and images, and outputs a version with all the css, js and images (Base64) written inline.

Downloads

6,312

Readme

grunt-assets-inline

Greenkeeper badge Build Status Dependency Status devDependency Status npm Version npm Downloads

A grunt task which takes a html file, finds all the css, js links and images, and outputs a version with all the css, js and images (Base64) written inline.

This is a fork of original grunt-html-smoosher by Ben Breedlove with fixes and new features.

Getting Started

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-assets-inline --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-assets-inline');

The "assets_inline" task

Overview

In your project's Gruntfile, add a section named assets_inline to the data object passed into grunt.initConfig().

grunt.initConfig({
  assets_inline: {
    options: {
      jsTags: { // optional
        start: '<script type="text/javascript">', // default: <script>
        end: '</script>'                          // default: </script>
      },
    },
    all: {
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

Options

minify

Defaults to false.

Minify scripts with UglifyJS.


grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        minify: true
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

Path config

Defaults to "".

When you have absolute paths for your external assets, it helps to add the local address of your asset files; relative to uncompiled HTML file.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        jsDir: "../",
        cssDir: "/project/styles/",
        assetsDir: "/project/"
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

Example

If the local cwd for your uncompiled file is /project/html then the above settings would resolve:

<script src="/assets/js/script.js"> will use a local file at /project/js/script.js

<link href="/assets/css/styles.css"> will use a local file at /project/styles/assets/css/styles.css

<img src="/assets/svg/header.svg"> will use a local file at /project/assets/svg/header.svg

inlineImg

Defaults to false.

If you want to smoosh img sources to Base64 in HTML, you can set inlineImg to true.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        inlineImg: true
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

inlineImgFileLimit

Defaults to undefined.

If you want to smoosh img only smaller than certain size, use inlineImgFileLimit.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        inlineImg: true,
        inlineImgFileLimit: 20 // 20 KB
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

inlineSvg

Defaults to true.

If you want to smoosh SVGs in img tags in HTML, you can set inlineSvg to true.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        inlineSvg: true
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

<img src="src/to/header.svg"> will turn into <img src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='2175....

inlineSvgFileLimit

Defaults to undefined.

If you want to smoosh svg only smaller than certain size, use inlineSvgFileLimit.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        inlineSvg: true,
        inlineSvgFileLimit: 40 // 40 KB
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

inlineSvgBase64

Inline SVGs with Base64. Defaults to false.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        inlineSvg: true,
        inlineSvgBase64: true
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

SVG images are inlined directly into HTML by default. To have more compatibility with older browsers, you can also Base64 SVGs:

<img src="src/to/header.svg"> will turn into <img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0....

Please note that Base64 generated files are always slightly bigger than the original files.

inlineLinkTags

Inline link Tags. Defaults to false.

If you want to smoosh images inside your link tags, for example favicons, you can set inlineLinkTags to true.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        inlineLinkTags: true
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

assetsUrlPrefix

Additional Assets URL Prefix. If you have assets in CSS or JS (for example images, fonts, SVGs), smooshing CSS or JS into HTMLs may break relative URLs, so you may have to replace the original URLs to absolute URLs.

This option only searchs for URLs begin with ../.

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        assetsUrlPrefix: '<%= config.base %>/assets/'
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

includeTag

Defaults to "".

grunt.initConfig({
  assets_inline: {
    all: {
      options: {
        includeTag: "?assets-inline"
      },
      files: {
        'dest-index.html': 'source-index.html',
      },
    },
  },
});

By default all CSS and JS files are smooshed in HTML, If you only need specific files to be smooshed, you should define includeTag.

For example the above configuration only smoosh filenames with ?assets-inline queries:

<link href="/assets/css/styles.css?assets-inline">

cssTags

Defaults to

{
  start: "<style>",
  end: "</style>"
}

jsTags

Defaults to

{
  start: "<script>",
  end: "</script>"
}

deleteOriginals

Defaults to

{
  deleteOriginals: false
}

You can delete smooshed files after the main task. Please note that once a file is smooshed into the HTML, it will marked as pending delete, no matter if it is included elsewhere without smooshing.

verbose

Defaults to

{
  verbose: false
}

Get detailed output log.

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

  • See CHANGELOG.md for further release history
  • 2015-08-12   v0.1.7   Add assetsDir, inlineSvg, and inlineSvgBase64 support
  • 2015-07-31   v0.1.6   Fix test and new default for assetsUrlPrefix
  • 2015-07-31   v0.1.5   Add assetsUrlPrefix support for fixing relative assets URLs in smooshed CSS
  • 2015-07-28   v0.1.4   Add ignoreImg support
  • 2015-07-20   v0.1.3   Add includeTag support, only selected files will be smooshed if this option is defined