Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
openui5_preload task: improve error logging / log compression
Browse files Browse the repository at this point in the history
Added a few error logs in case no files were found or no / wrong options
were provided.

If compression is active, the savings for each file (only with
--verbose) will be logged as well as the overall compression for each
preload file (also in non-verbose mode).
  • Loading branch information
matz3 committed Mar 16, 2015
1 parent cfd0026 commit aa16e59
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"connect-openui5": "^0.4.0",
"cors": "^2.0.0",
"less-openui5": "^0.1.1",
"maxmin": "^1.0.1",
"multiline": "^1.0.2",
"pretty-data": "^0.40.0",
"slash": "^1.0.0",
Expand Down
74 changes: 68 additions & 6 deletions tasks/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var path = require('path');
var slash = require('slash');
var uglify = require('uglify-js');
var pd = require('pretty-data').pd;
var maxmin = require('maxmin');

var defaultResourcePatterns = [
'**/*.js',
Expand Down Expand Up @@ -51,6 +52,13 @@ module.exports = function (grunt) {
options.resources = [ options.resources ];
}

if (options.resources.length === 0) {
grunt.fail.warn('"resources" option is not specified!');
return;
}

grunt.verbose.subhead('Collecting resources');

// process resources array
options.resources.forEach(function(resource) {
// transform string shorthand to object
Expand All @@ -64,27 +72,41 @@ module.exports = function (grunt) {
resource.prefix = '';
}

var resourcePatterns = resource.src ? resource.src : defaultResourcePatterns;
resource.src = resource.src || defaultResourcePatterns;

grunt.verbose.writeflags(resource, 'resource');

grunt.file.expand({
cwd: resource.cwd,
dot: true,
filter: 'isFile'
}, resourcePatterns).forEach(function(file) {
}, resource.src).forEach(function(file) {
var localFile = file;
if (resource.prefix) {
localFile = slash(path.join(resource.prefix, file));
}
var fullPath = path.join(resource.cwd, file);
grunt.verbose.write('Collecting ' + localFile + ' (' + fullPath + ')...').ok();
resourceMap[localFile] = {
fullPath: path.join(resource.cwd, file),
fullPath: fullPath,
prefix: resource.prefix
};
});

});

var resourceFiles = Object.keys(resourceMap);

if (resourceFiles.length === 0) {
grunt.fail.warn('No files found. Check your "resources" option!');
return;
}

var preloadData = this.data;
if (!preloadData['components'] && !preloadData['libraries']) {
grunt.fail.warn('No preload type specified. Please provide "components" and/or "libraries" in task target object!');
return;
}

['components', 'libraries'].forEach(function(preloadType) {

Expand Down Expand Up @@ -119,19 +141,29 @@ module.exports = function (grunt) {
preloadOptions[pattern] = {};
}

Object.keys(preloadOptions).forEach(function(preloadPattern) {
var preloadOptionKeys = Object.keys(preloadOptions);

if (preloadOptionKeys.length === 0) {
grunt.log.writeflags(preloadOptions, 'preloadOptions');
grunt.fail.warn('No valid options provided for "' + preloadType + '" preload!');
return;
}

preloadOptionKeys.forEach(function(preloadPattern) {
var preloadOption = preloadOptions[preloadPattern];
var preloadFiles = grunt.file.match(preloadPattern + '/' + preloadInfo.indicatorFile, resourceFiles);

if (preloadFiles.length < 1) {
grunt.log.error('No "' + preloadInfo.indicatorFile + '" found for pattern "' + preloadPattern + '"');
grunt.fail.warn('No "' + preloadInfo.indicatorFile + '" found for pattern "' + preloadPattern + '"!');
return;
}

preloadFiles.forEach(function(preloadFile) {
var preloadDir = path.dirname(preloadFile);
var preloadModuleName = preloadDir + '/' + preloadInfo.moduleName;

grunt.verbose.subhead('Creating preload module for ' + preloadFile);

var preloadObject = {
version: '2.0',
name: preloadModuleName,
Expand All @@ -146,13 +178,27 @@ module.exports = function (grunt) {

var preloadPatterns = preloadOption.src ? preloadOption.src : (preloadDir + '/**');
var preloadFiles = grunt.file.match(preloadPatterns, resourceFiles);
if (preloadFiles.length === 0) {
var patternsString = (typeof preloadPatterns === 'string') ? preloadPatterns : preloadPatterns.join('", "');
grunt.fail.warn('No files found for pattern(s): "' + patternsString + '"!');
return;
}

var iPreloadOriginalSize = 0, iPreloadCompressedSize = 0;

preloadFiles.forEach(function(preloadFile) {

var fileName = resourceMap[preloadFile].fullPath;
var fileContent = grunt.file.read(fileName);
var fileExtension = path.extname(fileName);

var iOriginalSize, iCompressedSize;

if (options.compress) {

iOriginalSize = fileContent.length;
iPreloadOriginalSize += iOriginalSize;

switch (fileExtension) {
case '.js':
// Javascript files are processed by Uglify
Expand All @@ -178,6 +224,18 @@ module.exports = function (grunt) {

break;
}

iCompressedSize = fileContent.length;
iPreloadCompressedSize += iCompressedSize;

}

if (grunt.option('verbose')) {
var log = 'Adding ' + preloadFile;
if (iOriginalSize && iCompressedSize && iOriginalSize !== iCompressedSize) {
log += ' (' + maxmin({ length: iOriginalSize }, { length: iCompressedSize }) + ')';
}
grunt.verbose.writeln(log);
}

preloadObject.modules[preloadFile] = fileContent;
Expand All @@ -198,7 +256,11 @@ module.exports = function (grunt) {
destPath += preloadInfo.ext;

grunt.file.write(destPath, content);
grunt.log.writeln('File ' + destPath + ' created with ' + Object.keys(preloadObject.modules).length + ' files.');
var log = 'File ' + destPath + ' created with ' + Object.keys(preloadObject.modules).length + ' files';
if (iPreloadOriginalSize && iPreloadCompressedSize && iPreloadOriginalSize !== iPreloadCompressedSize) {
log += ' (' + maxmin({ length: iPreloadOriginalSize }, { length: iPreloadCompressedSize }) + ')';
}
grunt.log.writeln(log);

});

Expand Down

0 comments on commit aa16e59

Please sign in to comment.