Inline external scripts, styles, and images in HTML
npm install @shferreira/posthtml-inline-assets[![NPM Version][npm-img]][npm-url]
[![Linux Build Status][cli-img]][cli-url]
[![Windows Build Status][win-img]][win-url]
[![Gitter Chat][git-img]][git-url]
[PostHTML Inline Assets] lets you inline external scripts, styles, and images
in HTML.
``html
`
Add [PostHTML] and [PostHTML Inline Assets] to your build tool:
`bash`
npm install posthtml posthtml-inline-assets --save-dev
#### Node
Use [PostHTML] and [PostHTML Inline Assets] to process your CSS:
`js
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:
`bash`
npm install gulp-posthtml --save-dev
Use [PostHTML Inline Assets] in your Gulpfile:
`js
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:
`bash`
npm install grunt-posthtml --save-dev
Use [PostHTML Inline Assets] in your Gruntfile:
`js
const posthtmlInlineAssets = require('posthtml-inline-assets');
grunt.loadNpmTasks('grunt-posthtml');
grunt.initConfig({
posthtml: {
options: {
use: [
posthtmlInlineAssets()
]
},
dist: {
src: '*.css'
}
}
});
`
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.
`js
const posthtmlInlineAssets = require('posthtml-inline-assets');
posthtmlInlineAssets({
cwd: '/path/to/files'
});
`
`html`
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.
`js
const posthtmlInlineAssets = require('posthtml-inline-assets');
posthtmlInlineAssets({
root: '/path/to/files'
});
`
`html
`
The errors option specifies how transform errors should be handled,ignore
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 these errors, but they may also throw an error,warning
or log a .
`js
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
});
`
The transforms option specifies the transforms used to inline assets. Newresolve
transforms can be added by creating a child object with two functions; 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.
`js`
function resolve(node) {
// if the node is a
return node.tag === 'foo' && 'some/path';
}
#### transform
The transform function is used to transform the asset being inlined. It isbuffer
passed the current node as well as an object containing the , the fullpath, and the mime type (if available) of the asset. It may also return a
promise if an asynchronous transform is required.
`js`
function transform(node, { buffer, path, mime }) {
// always inline the contents as a child of the node
node.content = [ buffer.toString('utf8') ];
}
The default transforms can be modified to alter their functionality. For
instance, script.resolve might be changed so that