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

@shferreira/posthtml-inline-assets

v3.0.2

Published

Inline external scripts, styles, and images in HTML

Downloads

175

Readme

PostHTML Inline Assets

NPM Version Linux Build Status Windows Build Status Gitter Chat

PostHTML Inline Assets lets you inline external scripts, styles, and images in HTML.

<link href="body.css" rel="stylesheet" class="body-style">

<!-- becomes -->

<style class="body-style">body { background-color: black; color: white; }</style>

Usage

Add PostHTML and PostHTML Inline Assets to your build tool:

npm install posthtml posthtml-inline-assets --save-dev

Node

Use PostHTML and PostHTML Inline Assets to process your CSS:

const postHTML = require('posthtml-inline-assets');
const posthtmlInlineAssets = require('posthtml-inline-assets');

posthtml([
  posthtmlInlineAssets({ /* options */ })
]).process(YOUR_HTML);

Gulp

Add Gulp PostHTML to your build tool:

npm install gulp-posthtml --save-dev

Use PostHTML Inline Assets in your Gulpfile:

const posthtml = require('gulp-posthtml');
const posthtmlInlineAssets = require('posthtml-inline-assets');

gulp.task('css', () => gulp.src('./src/*.css').pipe(
  posthtml([
    posthtmlInlineAssets()
  ])
).pipe(
  gulp.dest('.')
));

Grunt

Add Grunt PostHTML to your build tool:

npm install grunt-posthtml --save-dev

Use PostHTML Inline Assets in your Gruntfile:

const posthtmlInlineAssets = require('posthtml-inline-assets');

grunt.loadNpmTasks('grunt-posthtml');

grunt.initConfig({
  posthtml: {
    options: {
      use: [
       posthtmlInlineAssets()
      ]
    },
    dist: {
      src: '*.css'
    }
  }
});

Options

cwd

The cwd option specifies the working directory used by an HTML file, and it is used to determine the relative location of assets. By default, the current file directory is used, otherwise the current working directory is used.

const posthtmlInlineAssets = require('posthtml-inline-assets');

posthtmlInlineAssets({
  cwd: '/path/to/files'
});
<!-- resolves to /path/to/files/body.css -->
<link href="body.css" rel="stylesheet" class="body-style">

root

The root option specifies the root directory used by an HTML file, and it is used to determine the absolute location of assets. By default, the current file directory is used, otherwise the current working directory is used.

const posthtmlInlineAssets = require('posthtml-inline-assets');

posthtmlInlineAssets({
  root: '/path/to/files'
});
<!-- resolves to /path/to/files/body.css -->
<link href="/body.css" rel="stylesheet" class="body-style">

<!-- resolves to the current working directory + body.css -->
<link href="body.css" rel="stylesheet" class="body-style">

errors

The errors option specifies how transform errors should be handled, whether those errors occur when a resolved asset cannot be read, or when something goes wrong while an asset is being transformed. The default behavior is to ignore these errors, but they may also throw an error, or log a warning.

const posthtmlInlineAssets = require('posthtml-inline-assets');

posthtmlInlineAssets({
  // throw an error whenever a resolved asset fails to inline
  errors: 'throw' // the options are to 'throw', 'warn', or 'ignore' errors
});

transforms

The transforms option specifies the transforms used to inline assets. New transforms can be added by creating a child object with two functions; resolve and transform.

resolve

The resolve function is used to determine the path of an asset. It is passed the current node, and it must return the path of the asset to be inlined. If it does not return a string, the asset will not be transformed.

function resolve(node) {
  // if the node is a <foo> element then always return 'some/path'
  return node.tag === 'foo' && 'some/path'; 
}

transform

The transform function is used to transform the asset being inlined. It is passed the current node as well as an object containing the buffer, the full path, and the mime type (if available) of the asset. It may also return a promise if an asynchronous transform is required.

function transform(node, { buffer, path, mime }) {
  // always inline the contents as a child of the node
  node.content = [ buffer.toString('utf8') ];
}

Examples of changing or creating transforms

The default transforms can be modified to alter their functionality. For instance, script.resolve might be changed so that <script> elements with a type attribute are ignored.

const posthtmlInlineAssets = require('posthtml-inline-assets');

posthtmlInlineAssets({
  transforms: {
    script: {
      resolve(node) {
        // transform <script src="file.js"> but not <script src="file.js" type>
        return node.tag === 'script' && node.attrs && !node.attrs.type && node.attrs.src;
      }
    }
  }
});

The transform could also be removed entirely by passing the transform a non-object.

const posthtmlInlineAssets = require('posthtml-inline-assets');

posthtmlInlineAssets({
  transforms: {
    // any non-object will work
    script: false
  }
});

New transforms are easy to add. For instance, a new pics object might be added to inline <picture> elements with a src attribute.

const posthtmlInlineAssets = require('posthtml-inline-assets');

posthtmlInlineAssets({
  transforms: {
    pics: {
      resolve(node) {
        return node.tag === 'picture' && node.attrs && node.attrs.src;
      },
      transform(node, data) {
        node.tag = 'img';

        node.attrs.src = 'data:' + data.mime + ';base64,' + data.buffer.toString('base64');
      }
    }
  }
});

Be creative with your transforms. For instance, script.transform might be changed so that the contents of the script are also minified.

const posthtmlInlineAssets = require('posthtml-inline-assets');
const uglify = require('uglify-js');

posthtmlInlineAssets({
  transforms: {
    script: {
      transform(node, data) {
        delete node.attrs.src;

        node.content = [
          uglify.minify(data.buffer.toString('utf8')).code
        ];
      }
    }
  }
});