popeindustries / inline-source

Tool for inlining flagged js, css, and img sources in html
MIT License
194 stars 44 forks source link

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

inlineSource(htmlpath: string, [options: Options]): Promise<string>: 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:

$ 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="https://github.com/popeindustries/inline-source/blob/master/css/inlineStyle.css" />
    <!-- inline project/src/js/inlineScript.js as <script> -->
    <script inline src="https://github.com/popeindustries/inline-source/raw/master/./src/js/inlineScript.js"></script>
    <!-- inline remote file as <script> -->
    <script inline src="http://js/inlineScript.js"></script>
    <!-- inline project/www/images/inlineImage.png as base64 <img> -->
    <img inline src="https://github.com/popeindustries/inline-source/raw/master/images/inlineImage.png" />
    <!-- inline project/www/images/inlineImage.svg as <svg> -->
    <img inline src="https://github.com/popeindustries/inline-source/raw/master/images/inlineImage.svg" />
  </head>
</html>
import { inlineSource } from 'inline-source';
import fs from 'node:fs';
import path from 'node:path';

const htmlpath = path.resolve('project/src/html/index.html');

inlineSource(htmlpath, {
  compress: true,
  rootpath: path.resolve('www'),
  // Skip all css types and png formats
  ignore: ['css', 'png'],
})
  .then((html) => {
    // Do something with html
  })
  .catch((err) => {
    // Handle error
  });

...or preferably using async/await:

import { inlineSource } from 'inline-source';
import fs from 'node:fs';
import path from 'node:path';

const htmlpath = path.resolve('project/src/html/index.html');

try {
  const html = await inlineSource(htmlpath, {
    compress: true,
    rootpath: path.resolve('www'),
    // Skip all css types and png formats
    ignore: ['css', 'png'],
  });
  // Do something with html
} catch (err) {
  // Handle error
}

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: (source: Source, context: Context) => Promise<void> | void

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

export function handler(source, context) {
  if (source.fileContent && !source.content && source.type == 'js') {
    source.content = "Hey! I'm overriding the file's content!";
  }
}

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.

Custom Pre Handlers

Custom pre handlers are the same as custom handlers only they run before loading the file. All handlers have the following signature: (source: Source, context: Context) => Promise<void> | void

With custom Pre handlers you can make changes to the file name

export function prehandler(source, context) {
  const { version } = getVersionFromSomewhere();
  source.filepath = source.filepath.replace('.js', `_${version}.js`);
}

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="false"
  src="https://github.com/popeindustries/inline-source/raw/master/./src/js/inlineScript.js"
></script>
export function handler(source, context) {
  if (source.fileContent && !source.content && source.type == 'js') {
    // The `inline-compress` attribute automatically overrides the global flag
    if (!source.compress) {
      // do something
    }
    if (source.props.foo == 'foo') {
      // foo content
    }
  }
}