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

kyh-inline-source

v5.1.5

Published

Inline all flagged js, css, image source files

Downloads

2

Readme

NPM Version Build Status Downloads

inline-source

Inline and compress tags that contain the inline attribute. Supports <script>, <link>, and <img> (including *.svg sources) tags by default, and is easily extensible to handle others.

You can use inline-source-cli to run inline-source from the command line or NPM Scripts.

Usage

inline(htmlpath, [options], callback(err, html)): asynchronously parse htmlpath content for tags containing an inline attribute, and replace with (optionally compressed) file contents.

htmlpath can be either a filepath or a string of html content.

Available options include:

  • attribute: attribute used to parse sources (default inline)
  • compress: enable/disable compression of inlined content (default true)
  • fs: specify fs implementation (default is Node core fs)
  • handlers: specify custom handlers (default []) [see custom handlers]
  • ignore: disable inlining based on tag, type, and/or format (default [])
  • pretty: maintain leading whitespace when options.compress is false (default false)
  • rootpath: directory path used for resolving inlineable paths (default process.cwd())
  • swallowErrors: enable/disable suppression of errors (default false)
  • svgAsImage: convert <img inline src="*.svg" /> to <img> and not <svg> (default false)
$ npm install inline-source
<!-- located at project/src/html/index.html -->
<!DOCTYPE html>
<html>
<head>
  <!-- inline project/www/css/inlineStyle.css as <style> -->
  <link inline href="css/inlineStyle.css">
  <!-- inline project/src/js/inlineScript.js as <script> -->
  <script inline src="../js/inlineScript.js"></script>
  <!-- inline project/www/images/inlineImage.png as base64 <img> -->
  <img inline src="images/inlineImage.png" />
  <!-- inline project/www/images/inlineImage.svg as <svg> -->
  <img inline src="images/inlineImage.svg" />
</head>
</html>
var inline = require('inline-source')
  , fs = require('fs')
  , path = require('path')
  , htmlpath = path.resolve('project/src/html/index.html');

inline(htmlpath, {
  compress: true,
  rootpath: path.resolve('www'),
  // Skip all css types and png formats
  ignore: ['css', 'png']
}, function (err, html) {
  // Do something with html
});

inline.sync(htmlpath, [options]): same as inline, but synchronously returns string of html content.

var inline = require('inline-source').sync
  , fs = require('fs')
  , path = require('path')
  , htmlpath = path.resolve('project/src/html/index.html');

var html = inline(htmlpath, {
  compress: true,
  rootpath: path.resolve('www'),
  // Skip all script tags
  ignore: 'script'
});

Custom Handlers

Custom handlers are simple middleware-type functions that enable you to provide new, or override existing, inlining behaviour. All handlers have the following signature: function handler (source, context, next) {}

  • source: the current source object to act upon

    • attributes: the parsed tag attributes object
    • compress: the compress flag (may be overriden at the tag level via props)
    • content: the processed fileContent string
    • extension: the file extension
    • fileContent: the loaded file content string
    • filepath: the fully qualified path string
    • format: the format string (jpg, gif, svg+xml, etc)
    • match: the matched html tag string, including closing tag if appropriate
    • props: the parsed namespaced attributes object (see props)
    • replace: the tag wrapped content string to replace match
    • tag: the tag string (script, link, etc)
    • type: the content type based on type mime-type attribute, or tag (js for application/javascript, css for text/css, etc)
  • context: the global context object storing all configuration options (attribute, compress, ignore, pretty, rootpath, swallowErrors, svgAsImage), in addtion to:

    • html: the html file's content string
    • htmlpath: the html file's path string
    • sources: the array of source objects
  • next(err): a function to be called to advance to the next middleware function. Accepts an optional error object with behaviour determined by swallowErrors flag (stops all processing if false, skips current source if true)

Custom handlers are inserted before the defaults, enabling overriding of default behaviour:

module.exports = function customjs (source, context, next) {
  if (source.fileContent
    && !source.content
    && (source.type == 'js')) {
      source.content = "Hey! I'm overriding the file's content!";
      next();
  } else {
    next();
  }
};

In general, default file content processing will be skipped if source.content is already set, and default wrapping of processed content will be skipped if source.replace is already set.

Props

Source props are a subset of attributes that are namespaced with the current global attribute ('inline' by default), and allow declaratively passing data or settings to handlers:

<script inline inline-foo="foo" inline-compress src="../js/inlineScript.js"></script>
module.exports = function customjs (source, context, next) {
  if (source.fileContent
    && !source.content
    && (source.type == 'js')) {
      // The `inline-compress` attribute automatically overrides the global flag
      if (source.compress) {
        // compress content
      }
      if (source.props.foo == 'foo') {
        // foo content
      }
      next();
  } else {
    next();
  }
};