mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-10-29 17:40:29 +00:00
Fix bug where bundler stops watching files when an error occurs.
This commit is contained in:
64
lib/index.js
64
lib/index.js
@@ -1,13 +1,21 @@
|
||||
var co = require('co');
|
||||
var cheerio = require('cheerio');
|
||||
var fs = require('mz/fs');
|
||||
var inlineStyle = require('./inline-style');
|
||||
var inlineImg = require('./inline-img');
|
||||
var inlineLinkLess = require('./inline-link-less');
|
||||
var inlineLess = require('./inline-less');
|
||||
var R = require('ramda');
|
||||
var Ru = require('@panosoft/ramda-utils');
|
||||
|
||||
var inline = co.wrap(function * (html, options) {
|
||||
/**
|
||||
* Embed referenced local assets within and HTML file.
|
||||
*
|
||||
* @param {String} html
|
||||
* Filename or html string.
|
||||
* @param {Object} options
|
||||
*
|
||||
* @return {Promise}
|
||||
*/
|
||||
var inlineHtml = co.wrap(function * (html, options) {
|
||||
options = Ru.defaults({
|
||||
filename: null,
|
||||
less: {},
|
||||
@@ -19,39 +27,37 @@ var inline = co.wrap(function * (html, options) {
|
||||
html = yield fs.readFile(filename, 'utf8');
|
||||
}
|
||||
catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
filename = options.filename;
|
||||
}
|
||||
else {
|
||||
throw error;
|
||||
}
|
||||
if (error.code === 'ENOENT') filename = options.filename;
|
||||
else throw error;
|
||||
}
|
||||
|
||||
// Embed assets
|
||||
var files = [filename];
|
||||
try {
|
||||
var lessResult = yield inlineLess(html, filename, options.less);
|
||||
html = lessResult.html;
|
||||
files = R.concat(files, lessResult.files);
|
||||
|
||||
// Inline links
|
||||
var lessResult = yield inlineLinkLess(html, filename, options.less);
|
||||
html = lessResult.html;
|
||||
files.push(lessResult.files);
|
||||
var styleResult = inlineStyle(html, filename);
|
||||
html = styleResult.html;
|
||||
files = R.concat(files, styleResult.files);
|
||||
|
||||
// TODO inline links: css
|
||||
|
||||
// TODO inline scripts
|
||||
// browserify js? => scriptify
|
||||
|
||||
// Inline paths -> datauris
|
||||
var styleResult = inlineStyle(html, filename); // Inline styles
|
||||
html = styleResult.html;
|
||||
files.push(styleResult.files);
|
||||
|
||||
var imgResult = inlineImg(html, filename); // Inline images
|
||||
html = imgResult.html;
|
||||
files.push(imgResult.files);
|
||||
var imgResult = inlineImg(html, filename);
|
||||
html = imgResult.html;
|
||||
files = R.concat(files, imgResult.files);
|
||||
}
|
||||
catch (error) {
|
||||
if (!error.filename) error.filename = filename;
|
||||
error.files = R.uniq(R.concat(files, error.files || []));
|
||||
throw error;
|
||||
}
|
||||
|
||||
files = R.uniq(files);
|
||||
var result = {
|
||||
html: html,
|
||||
files: R.uniq(R.flatten(files, true))
|
||||
html,
|
||||
files
|
||||
};
|
||||
return (options.verbose ? result : result.html);
|
||||
});
|
||||
|
||||
module.exports = inline;
|
||||
module.exports = inlineHtml;
|
||||
|
||||
Reference in New Issue
Block a user