fix: simple depth imports working

This commit is contained in:
Remy Sharp 2015-07-27 14:26:25 +01:00
parent d6d13c83e4
commit eabd7c5441
4 changed files with 47 additions and 49 deletions

16
jquery.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -16,7 +16,7 @@ var debug = require('debug')('inliner');
function load(url) {
var inliner = this;
inliner.jobs.add('link');
return get(url).then(function then(res) {
return inliner.get(url).then(function then(res) {
debug('css loaded: %s', url);
inliner.jobs.done.links();
return res.body;
@ -57,23 +57,24 @@ function getImports(root, css) {
inliner.jobs.add('link', 1);
var match = (css.match(/@import\s*(.*)/) || [null, ''])[1];
var url = match.replace(/url/, '')
.replace(/['}"]/g, '')
.replace(/['}"()]/g, '')
.replace(/;/, '')
.trim()
.split(' '); // clean up
// if url has a length > 1, then we have media types to target
var resolvedURL = inliner.resolve(root, url[0]);
return get(resolvedURL).then(function then(importedCSS) {
inliner.jobs.done('link');
return inliner.get(resolvedURL).then(function then(res) {
var importedCSS = res.body;
inliner.jobs.done.links();
inliner.emit('progress', 'import ' + resolvedURL);
if (url.length > 1) {
url.shift();
importedCSS = '@media ' + url.join(' ') + '{' + importedCSS + '}';
}
css = css.replace(match[0], importedCSS);
return getImports(root, css);
css = css.replace('@import ' + match, importedCSS);
return getImports.call(inliner, root, css);
});
} else {
if (inliner.options.compressCSS) {

View File

@ -4,7 +4,7 @@ var get = require('./get');
var debug = require('debug')('inliner');
function image(url) {
return get(url, { encoding: 'binary' }).then(function then(res) {
return inliner.get(url, { encoding: 'binary' }).then(function then(res) {
debug('image loaded: %s', url);
var buffer = new Buffer(res.body, 'binary').toString('base64');
return 'data:' + res.headers['content-type'] + ';base64,' + buffer;

View File

@ -8,7 +8,7 @@ var fs = require('then-fs');
var assign = require('lodash.assign');
var forEach = require('lodash.foreach');
var Promise = require('es6-promise').Promise; // jshint ignore:line
var get = require('./get');
var request = require('./get');
var findAssets = require('./find-assets');
var getImage = require('./image');
var version = require(path.resolve(__dirname, '..', 'package.json')).version;
@ -62,6 +62,7 @@ Inliner.prototype.version = version;
Inliner.prototype.css = require('./css');
Inliner.prototype.resolve = resolve;
Inliner.prototype.removeComments = removeComments;
Inliner.prototype.get = get;
Inliner.prototype.main = main;
// static properties and methods
@ -81,21 +82,18 @@ function main() {
var url = this.url;
var callback = this.callback;
var promise = fs.exists(url).then(function exists(isFile) {
fs.exists(url)
.then(function exists(isFile) {
if (!isFile) {
throw new Error();
}
debug('inlining file');
inliner.isFile = true;
debug('loading file: %s', url);
return fs.readFile(url, 'utf8').then(function (body) {
return {
body: body,
};
});
}).catch(function isUrl() {
return url;
})
.catch(function isUrl() {
// check for protocol on URL
if (url.indexOf('http') !== 0) {
url = 'http://' + url;
@ -103,12 +101,12 @@ function main() {
inliner.url = url;
debug('loading url: %s', url);
return get(url);
}).then(inliner.jobs.add('html'));
promise.then(function processHTML(res) {
debug('inlining url');
return url;
})
.then(inliner.get.bind(this))
.then(inliner.jobs.add('html'))
.then(function processHTML(res) {
inliner.jobs.done.html();
debug('then');
@ -134,7 +132,7 @@ function main() {
var promises = [];
if (todo.images) {
if (todo.images.length) {
var imagePromises = todo.images.map(function images(i, image) {
var url = inliner.resolve(inliner.url, $(image).attr('src'));
return getImage(url).then(function then(dataURL) {
@ -145,7 +143,7 @@ function main() {
[].push.apply(promises, imagePromises);
}
if (todo.links) {
if (todo.links.length) {
debug('start %s links', todo.links.length);
var linkPromises = todo.links.map(function links(i, link) {
var url = inliner.resolve(inliner.url, $(link).attr('href'));
@ -160,8 +158,8 @@ function main() {
[].push.apply(promises, linkPromises);
}
if (todo.styles) {
debug('start %s links', todo.links.length);
if (todo.styles.length) {
debug('start %s styles', todo.styles.length);
var stylePromises = todo.styles.map(function links(i, style) {
var css = $(style).html();
return inliner.css.getImports(url, css)
@ -209,6 +207,21 @@ function main() {
});
}
function get(url) {
debug('get', this.isFile, url.indexOf('http'), url);
if (this.isFile && url.indexOf('http') !== 0) {
debug('loading file: %s', url);
return fs.readFile(url, 'utf8').then(function (body) {
return {
body: body,
};
});
} else {
debug('loading url: %s', url);
return request(url);
}
}
function removeComments(element, $) {
if (!element || !element.childNodes) {
return;
@ -231,15 +244,15 @@ function resolve(from, to) {
from = this.url;
}
var fn = null;
if (!this.isFile) {
fn = require('url').resolve;
return require('url').resolve(from, to);
} else {
fn = require('path').resolve;
var path = require('path');
var base = path.dirname(from);
debug('resolved from %s to %s: %s', base, to, path.resolve(base, to));
return path.resolve(base, to);
}
return fn(from, to);
}
function updateTodo() {