New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

grunt-packer

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grunt-packer - npm Package Compare versions

Comparing version 0.1.14 to 0.2.0

test/fixture/1/css/imports/imported1.css

130

lib/pack-css-task.js
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){

71

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc