grunt-packer
Advanced tools
Comparing version 0.1.14 to 0.2.0
var path = require('path'), | ||
helpers = require('./helpers.js'), | ||
fs = require('fs'), | ||
async = require('async'); | ||
async = require('async'), | ||
postcss = require('postcss'), | ||
postcssUrl = require('postcss-url'), | ||
postcssImport = require('postcss-import'); | ||
var rebaseUrlsPlugin = postcssUrl({ | ||
url: 'rebase' | ||
}); | ||
var inlineImportPlugin = postcssImport(); | ||
function toCSSString(root) { | ||
return root.toResult().css; | ||
} | ||
function concatTo(concatRoot) { | ||
return function(singleRoot) { | ||
concatRoot.append(singleRoot); | ||
} | ||
} | ||
function resolveUrl(root, code, cssDir) { | ||
@@ -101,3 +121,109 @@ var matchResult = code.match(/url\((?!("|')?data:)[^#\)]+?\)/ig); | ||
nativePackCSS: function (files, root, callback) { | ||
/** | ||
* Собирает несколько css-файлов в один, переписывая URLы на правильные (с учетом нового местоположения итогового файла). | ||
* Возвращает результат паковки. Не создает файлов! | ||
* | ||
* Опции: | ||
* inlineImports: false - выполнять ли обработку найденных директив @import | ||
* batchSize: 4000 - имеет смысл только при inlineImports = true. Разбивает итоговый файл на пачки по batchSize селекторов | ||
* | ||
* @param {Array} files пути до файлов, которые нужно собрать | ||
* @param {String} root папка, в которуй будет размещен итоговый файл | ||
* @param {Object} [options={inlineImports: false}] Опции | ||
* @param {Function} callback | ||
* @returns {*} | ||
*/ | ||
nativePackCSS: function(files, root, options, callback) { | ||
if (typeof options == 'function') { | ||
callback = options; | ||
options = { | ||
inlineImports: false | ||
}; | ||
} | ||
options = options || {}; | ||
if (options.inlineImports) { | ||
return this._packWithInliningAndSplitting(files, root, options.batchSize, callback); | ||
} else { | ||
return this._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){ | ||
@@ -104,0 +230,0 @@ fs.readFile(css, function(err, content){ |
{ | ||
"name": "grunt-packer", | ||
"description": "Grunt plugin to automagically concat JS and CSS files found in HTML", | ||
"version": "0.1.14", | ||
"author": "Oleg Elifantiev <oleg@elifantiev.ru>", | ||
"contributors": [], | ||
"main": "index.js", | ||
"keywords": [ | ||
], | ||
"repository": { | ||
"url": "https://github.com/Olegas/grunt-packer.git", | ||
"type": "git" | ||
}, | ||
"dependencies": { | ||
"tensor-xmldom": "0.1.18", | ||
"grunt": "0.4.x", | ||
"async": "0.2.9" | ||
}, | ||
"devDependencies": { | ||
"mocha": "", | ||
"coveralls": "", | ||
"istanbul": "", | ||
"mocha-istanbul": "", | ||
"assert": "", | ||
"mockfs": "" | ||
}, | ||
"scripts": { | ||
"instrument": "node ./node_modules/.bin/istanbul instrument --output lib-cov --no-compact --variable global.__coverage__ lib", | ||
"test-cov": "npm run-script instrument && COVER=gruntpacker ISTANBUL_REPORTERS=lcovonly node ./node_modules/.bin/mocha -R mocha-istanbul", | ||
"test": "node ./node_modules/mocha/bin/mocha -R spec" | ||
}, | ||
"engines": { | ||
"node": ">=0.8" | ||
} | ||
} | ||
"name": "grunt-packer", | ||
"description": "Grunt plugin to automagically concat JS and CSS files found in HTML", | ||
"version": "0.2.0", | ||
"author": "Oleg Elifantiev <oleg@elifantiev.ru>", | ||
"contributors": [], | ||
"main": "index.js", | ||
"keywords": [], | ||
"repository": { | ||
"url": "https://github.com/Olegas/grunt-packer.git", | ||
"type": "git" | ||
}, | ||
"dependencies": { | ||
"async": "0.2.9", | ||
"grunt": "0.4.x", | ||
"postcss": "^4.1.16", | ||
"postcss-import": "^6.2.0", | ||
"postcss-url": "^4.0.0", | ||
"tensor-xmldom": "0.1.18" | ||
}, | ||
"devDependencies": { | ||
"mocha": "", | ||
"coveralls": "", | ||
"istanbul": "", | ||
"mocha-istanbul": "", | ||
"assert": "", | ||
"mockfs": "" | ||
}, | ||
"scripts": { | ||
"instrument": "node ./node_modules/.bin/istanbul instrument --output lib-cov --no-compact --variable global.__coverage__ lib", | ||
"test-cov": "npm run-script instrument && COVER=gruntpacker ISTANBUL_REPORTERS=lcovonly node ./node_modules/.bin/mocha -R mocha-istanbul", | ||
"test": "node ./node_modules/mocha/bin/mocha -R spec" | ||
}, | ||
"engines": { | ||
"node": ">=0.8" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
40845
58
874
6
1
+ Addedpostcss@^4.1.16
+ Addedpostcss-import@^6.2.0
+ Addedpostcss-url@^4.0.0
+ Addedamdefine@1.0.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedclone@0.1.19(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddirectory-encoder@0.6.1(transitive)
+ Addedes6-promise@2.3.0(transitive)
+ Addedfs-extra@0.8.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedglob@5.0.15(transitive)
+ Addedhandlebars@1.1.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedimg-stats@0.4.2(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedis-core-module@2.16.1(transitive)
+ Addedjs-base64@2.1.9(transitive)
+ Addedjsonfile@1.1.1(transitive)
+ Addedlodash@2.4.0(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.3.50.5.6(transitive)
+ Addedncp@0.4.2(transitive)
+ Addedobject-assign@3.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedoptimist@0.3.7(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpostcss@4.1.16(transitive)
+ Addedpostcss-import@6.2.0(transitive)
+ Addedpostcss-message-helpers@2.0.0(transitive)
+ Addedpostcss-url@4.0.1(transitive)
+ Addedreduce-function-call@1.0.3(transitive)
+ Addedresolve@1.22.10(transitive)
+ Addedsource-map@0.1.430.4.4(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addeduglify-js@2.3.6(transitive)
+ Addedwordwrap@0.0.3(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedxmldom@0.1.16(transitive)