grunt-packer
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -66,2 +66,95 @@ var path = require('path'), | ||
/** | ||
* Пакует содерживое указанный файлов в один или несколько блоков (по max batchSize селекторов в каждом) | ||
* Если кол-во селекторов (numSelectors) превышает batchSize, создается numSelectors/batchSize файлов | ||
* В каждом из которых будет numSelectors/numFiles селекторов (для более равномерного распределения по пачкам) | ||
* Возвращает массив с элементами-пачками | ||
* | ||
* @param {Array} files | ||
* @param {String} root | ||
* @param {Number} batchSize=4000 | ||
* @param {Function} callback | ||
* @private | ||
*/ | ||
function _packWithInliningAndSplitting(files, root, batchSize, callback) { | ||
async.map(files, function(cssFile, cb){ | ||
fs.readFile(cssFile, function(err, content){ | ||
if (err) { | ||
cb(err); | ||
} else { | ||
var result = postcss() | ||
.use(inlineImportPlugin) | ||
.use(rebaseUrlsPlugin) | ||
.process(content.toString(), { | ||
from: cssFile, | ||
to: path.join(root, 'fake.css') | ||
}); | ||
cb(null, result.root); | ||
} | ||
}); | ||
}, function(err, results){ | ||
if (err) { | ||
callback(err); | ||
} else { | ||
var | ||
concatRoot = postcss.root(), | ||
chunks = [], | ||
batchSize, | ||
numBatches, | ||
currentChunk = postcss.root(); | ||
results.forEach(concatTo(concatRoot)); | ||
numBatches = Math.ceil(concatRoot.nodes.length / (batchSize || 4000)); | ||
batchSize = Math.ceil(concatRoot.nodes.length / numBatches); | ||
concatRoot.each(function(decl){ | ||
if (currentChunk.nodes.length < batchSize) { | ||
currentChunk.append(decl); | ||
} | ||
if (currentChunk.nodes.length == batchSize) { | ||
chunks.push(currentChunk); | ||
currentChunk = postcss.root(); | ||
} | ||
}); | ||
if (currentChunk.nodes.length > 0) { | ||
chunks.push(currentChunk); | ||
} | ||
callback(null, chunks.map(toCSSString)); | ||
} | ||
}); | ||
}, | ||
/** | ||
* Собирает несколько CSS-файлов в один переписывая урлы. Поднимает импорты ввех без инлайнинга. | ||
* Не создает файлы | ||
* | ||
* @param {Array} files | ||
* @param {String} root | ||
* @param {Function} callback | ||
* @private | ||
*/ | ||
function _fastPackNoInline(files, root, callback) { | ||
async.map(files, function(css, cb){ | ||
fs.readFile(css, function(err, content){ | ||
if (err) { | ||
cb(err); | ||
} else { | ||
cb(null, resolveUrl(root, content.toString(), path.dirname(css))); | ||
} | ||
}); | ||
}, function(err, results){ | ||
if (err) { | ||
callback(err); | ||
} else { | ||
callback(null, bumpImportsUp(results.join('\n'))); | ||
} | ||
}); | ||
} | ||
module.exports = { | ||
@@ -147,101 +240,7 @@ | ||
if (options.inlineImports) { | ||
return this._packWithInliningAndSplitting(files, root, options.batchSize, callback); | ||
return _packWithInliningAndSplitting(files, root, options.batchSize, callback); | ||
} else { | ||
return this._fastPackNoInline(files, root, callback); | ||
return _fastPackNoInline(files, root, callback); | ||
} | ||
}, | ||
/** | ||
* Пакует содерживое указанный файлов в один или несколько блоков (по max batchSize селекторов в каждом) | ||
* Если кол-во селекторов (numSelectors) превышает batchSize, создается numSelectors/batchSize файлов | ||
* В каждом из которых будет numSelectors/numFiles селекторов (для более равномерного распределения по пачкам) | ||
* Возвращает массив с элементами-пачками | ||
* | ||
* @param {Array} files | ||
* @param {String} root | ||
* @param {Number} batchSize=4000 | ||
* @param {Function} callback | ||
* @private | ||
*/ | ||
_packWithInliningAndSplitting: function(files, root, batchSize, callback) { | ||
async.map(files, function(cssFile, cb){ | ||
fs.readFile(cssFile, function(err, content){ | ||
if (err) { | ||
cb(err); | ||
} else { | ||
var result = postcss() | ||
.use(inlineImportPlugin) | ||
.use(rebaseUrlsPlugin) | ||
.process(content.toString(), { | ||
from: cssFile, | ||
to: path.join(root, 'fake.css') | ||
}); | ||
cb(null, result.root); | ||
} | ||
}); | ||
}, function(err, results){ | ||
if (err) { | ||
callback(err); | ||
} else { | ||
var | ||
concatRoot = postcss.root(), | ||
chunks = [], | ||
batchSize, | ||
numBatches, | ||
currentChunk = postcss.root(); | ||
results.forEach(concatTo(concatRoot)); | ||
numBatches = Math.ceil(concatRoot.nodes.length / (batchSize || 4000)); | ||
batchSize = Math.ceil(concatRoot.nodes.length / numBatches); | ||
concatRoot.each(function(decl){ | ||
if (currentChunk.nodes.length < batchSize) { | ||
currentChunk.append(decl); | ||
} | ||
if (currentChunk.nodes.length == batchSize) { | ||
chunks.push(currentChunk); | ||
currentChunk = postcss.root(); | ||
} | ||
}); | ||
if (currentChunk.nodes.length > 0) { | ||
chunks.push(currentChunk); | ||
} | ||
callback(null, chunks.map(toCSSString)); | ||
} | ||
}); | ||
}, | ||
/** | ||
* Собирает несколько CSS-файлов в один переписывая урлы. Поднимает импорты ввех без инлайнинга. | ||
* Не создает файлы | ||
* | ||
* @param {Array} files | ||
* @param {String} root | ||
* @param {Function} callback | ||
* @private | ||
*/ | ||
_fastPackNoInline: function (files, root, callback) { | ||
async.map(files, function(css, cb){ | ||
fs.readFile(css, function(err, content){ | ||
if (err) { | ||
cb(err); | ||
} else { | ||
cb(null, resolveUrl(root, content.toString(), path.dirname(css))); | ||
} | ||
}); | ||
}, function(err, results){ | ||
if (err) { | ||
callback(err); | ||
} else { | ||
callback(null, bumpImportsUp(results.join('\n'))); | ||
} | ||
}); | ||
}, | ||
resolveUrl: resolveUrl, | ||
@@ -248,0 +247,0 @@ bumpImportsUp: bumpImportsUp |
{ | ||
"name": "grunt-packer", | ||
"description": "Grunt plugin to automagically concat JS and CSS files found in HTML", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"author": "Oleg Elifantiev <oleg@elifantiev.ru>", | ||
@@ -6,0 +6,0 @@ "contributors": [], |
40580