mirror of
https://github.com/tauri-apps/tauri-inliner.git
synced 2026-02-04 02:31:19 +01:00
96 lines
2.6 KiB
JavaScript
96 lines
2.6 KiB
JavaScript
var match = /url\((?:['"]*)(?!['"]*data:)(.*?)(?:['"]*)\)/g;
|
|
|
|
module.exports = {
|
|
getImages: getImages,
|
|
getImports: getImports,
|
|
};
|
|
|
|
var Promise = require('es6-promise').Promise; // jshint ignore:line
|
|
var debug = require('debug')('inliner');
|
|
var parse = require('path').parse;
|
|
|
|
function getImages(root, css) {
|
|
var inliner = this;
|
|
var singleURLMatch = /url\(\s*(['"]*)(?!['"]*data:)(.*?)(['"]*)\s*\)/;
|
|
var matches = css.match(match) || [];
|
|
var images = matches.map(function eachURL(url) {
|
|
var match = url.match(singleURLMatch);
|
|
var source = match[2];
|
|
return {
|
|
source: match.input,
|
|
resolved: inliner.resolve(root, source),
|
|
};
|
|
});
|
|
|
|
debug('adding %s CSS assets', images.length);
|
|
inliner.jobs.add('images', images.length);
|
|
|
|
return Promise.all(images.map(function map(url) {
|
|
return inliner.image(url.resolved).then(function then(dataURL) {
|
|
inliner.jobs.done.images();
|
|
css = replace(css, url.source, 'url("' + dataURL + '")');
|
|
return css;
|
|
});
|
|
})).then(function then() {
|
|
return css;
|
|
});
|
|
}
|
|
|
|
function replace(body, source, target) {
|
|
return body.split(source).join(target);
|
|
}
|
|
|
|
function getImports(root, css) {
|
|
// change to a string in case the CSS is a buffer, which is the case
|
|
// when we're reading off the local file system
|
|
if (typeof css !== 'string') {
|
|
css = css.toString();
|
|
}
|
|
var position = css.indexOf('@import');
|
|
var inliner = this;
|
|
|
|
if (position !== -1) {
|
|
inliner.jobs.add('link', 1);
|
|
var match = (css.match(/@import\s*(.*?);/) || [null, ''])[1];
|
|
var url = match.replace(/url/, '')
|
|
.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 inliner.get(resolvedURL).then(function then(res) {
|
|
var importedCSS = res.body;
|
|
inliner.jobs.done.links();
|
|
inliner.emit('progress', 'import ' + parse(resolvedURL).base);
|
|
if (url.length > 1) {
|
|
url.shift();
|
|
importedCSS = '@media ' + url.join(' ') + '{' + importedCSS + '}';
|
|
}
|
|
|
|
css = css.replace('@import ' + match, importedCSS);
|
|
return getImports.call(inliner, root, css);
|
|
});
|
|
}
|
|
|
|
if (inliner.options.compressCSS) {
|
|
inliner.emit('progress', 'compress css');
|
|
css = compress(css);
|
|
}
|
|
|
|
return Promise.resolve(css);
|
|
}
|
|
|
|
function compress(css) {
|
|
return css
|
|
.replace(/\s+/g, ' ')
|
|
.replace(/:\s+/g, ':')
|
|
.replace(/\/\*.*?\*\//g, '')
|
|
.replace(/\} /g, '}')
|
|
.replace(/ \{/g, '{')
|
|
// .replace(/\{ /g, '{')
|
|
.replace(/; /g, ';')
|
|
.replace(/\n+/g, '');
|
|
}
|