inline-resource v0.1.7
inline-resource
A node utility to inline everything you want to inline, including image, css, html, etc.
Features
Support inline
imagereferred byimghtml element or cssurlusingbase64data-uriSupport inline
svgfiles referred by css or html using svg source file orbase64data-uriSupport inline
fontfiles usingbase64data-uriSupport inline
cssfile referred bylinkelement or@importruleSupport inline
htmlfile referred bylinkelementSupport inline
jsfile referred byscriptelement ordocument.writestatementSupport custom inline method using
__inline(path)in script or htmlThe script and style element content defined in html can also be processed
The inline is processed recursively and support inline all local assets into a single file or specify which asset need to been processed inline
The inline file support compress option
You can custom your inline processor or inline task if existed cannot satisfy
How to use
Install
npm install inline-resource --saveA simple example
var inliner = require('inline-resource');
var result = inliner.inline({
inlineAll: true,
files: ['index.html'],
svg: {
useSource: true
}
});Using with web server
{ location: /\.js($|\?)/, handler: [ file(), function (context) { var req = context.request; var path = req.pathname.replace(/^\/+/, ''); var result = inliner.inline({ files: [{path: path, data: context.content}], inlinePathResolver: function (path) { return {path: path.replace(/\{\$course_host\}\//, ''), dir: '.'}; } }); context.content = result[0].data; } ] }, { location: /\.php/, handler: [ php('php-cgi'), function (context) { var req = context.request; var path = req.pathname.replace(/^\/+/, ''); var result = inliner.inline({ files: [{path: path, data: context.content}], processor: {php: 'html'}, img: false, css: false, html: false, inlinePathResolver: function (path) { var url = require('url').parse(path, true); var newPath = url.pathname.replace(/^\/+/, '') + url.search; return {path: newPath, dir: '.'}; } }); context.content = result[0].data; } ] },
Options
root -
stringoptionalthe root directory to process, by defautl using current working directoryoutput -
stringoptionalthe output directory, by default, none will outputfiles -
Arraythe file to been processed inline, the file pattern using minimatch, the regexp or file object is supported, the file object structure:{data: string, path: string}, thepathis relative torootfileMap -
Objectoptionalthe all read-ahead file collection, the key ispathrelative toroot, the value is file dataprocessor -
Objectoptionalcustom the processor type using, e.g., {mustache: 'html'}inlinePathResolver -
Functionoptionalresolve the inline file pathinlinePathResolver: function (path, file) { var path = path.replace(/{%site_host%}\//, ''); return path; // var dir; // if (/\W+views\//.test(file.path)) { // dir = 'example'; // } // you can specify the directory that the path relative to // return {path: path, dir: dir}; }inlineAll -
booleanoptionalwhether inline all local resources referred by the processed file, by defaultfalse, specify which resource need to be inline manually using url inline query param. Notice if settingtrue, you should manuall specify which resource type you want to inline all using the following resource type option, e.g., settingcss: trueorcss: {/*options*/}, it will inline all css files.inlineParamName -
stringoptionalby default_inline, specify the inline resource like:<img src="a/b.jpg?_inline"> <!-- the value of the inline param can used to specify the relative directory of the inline path --> <img src="a/b.jpg?_inline=example">ignoreCompressFiles -
Array<string|RegExp>optionalthe files to been ignored when enable compress optionimg -
boolean|Objectoptionalwhether enable image inline process using base64 encode, by defaulttrueifinlineAllis nottrueimg: { // the image file size less than or equal 1024 byte will be inlined limit: 1024 }font -
boolean|Objectoptionalwhether enable font inline process using base64 encode, by defaulttrueifinlineAllis nottruefont: { // the font file file size less than or equal 1024 byte will be inlined limit: 1024 }svg -
boolean|Objectoptionalwhether enable svg inline process using base64 encode or svg source, by defaulttrueifinlineAllis nottruesvg: { // by default, using base64 encode useSource: false, // the svg file size less than or equal 1024 byte will be inlined limit: 1024 // whether compress svg source file when inline svg source, by default false // if enabled, please make sure `svgo@^1.0.0` is installed in global or working dir compress: false }css -
boolean|Objectoptionalwhether enable css inline process, by defaulttrueifinlineAllis nottruecss: { // whether rebase the file path referred by inline css file, by default false rebase: false, rebase: { absolute: true, // rebase as absolute path ignore: function (url, relativeFile, targetFile) { // ignore url rebase return false; } }, rebase: function (url, relativeFile, targetFile) { // custom rebase logic var isLocalPath = this.isLocal(url); var absPath = this.resolve(url, relativeFile); var rebasePath = this.rebase(url, relativeFile, targetFile); return url; } // whether compress css source file, by default false // if enabled, please make sure `clean-css@4` is installed in global or working dir compress: false }js -
boolean|Objectoptionalwhether enable js inline process, by defaulttrueifinlineAllis nottruejs: { // whether using the custom inline method, by default true // e.g., var tpl = '__inline("./a.tpl")'; // output: var tpl = '<inline tpl content>' // '__inline("./a.js")' // output: <inline js file content> custom: false, // whether compress js source file, by default false // if enabled, please make sure `uglify-js@3` is installed in global or working dir compress: false }html -
boolean|Objectoptionalwhether enable html inline process, by defaulttrueifinlineAllis nottruehtml: { // whether compress html source file, by default false compress: false }
API
- addInlineTaskFor(type, tasks) - add custom inline task for the specified processor type
var inliner = require('inline-resource');
inliner.addInlineTaskFor('html', function (file) {
var me = this;
var inlineTplRegexp = /<!--\s*inline:\s*([^\s]+)\s*-->/g;
return file.data.replace(inlineTplRegexp, function (match, path) {
var result = me.getInlineResult(path);
if (result) {
return result.data;
}
return match;
});
});- registerInlineProcessor(type, processor) - register inline processor for custom file type
inliner.registerInlineProcessor('etpl', {
taskList: [
function (file) {
var me = this;
return file.data.replace(
/<!--\s*include:\s*([^\s]+)\s*-->/g,
function (match, path) {
var result = me.getInlineResult(path);
if (result) {
return result.data;
}
return match;
}
)
}
],
// if support compress option, implement it
compress: function (file, compressOption) {
return file.data;
}
});