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

Commit

Permalink
Merge pull request #1 from olirogers/compress
Browse files Browse the repository at this point in the history
Compress Option
  • Loading branch information
matz3 committed Nov 6, 2014
2 parents 589c32f + 95467c3 commit 011d4af
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 31 deletions.
14 changes: 14 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ module.exports = function(grunt) {
src: 'my/ui/lib/**/*.{js,json,xml}'
}
]
},
'raw_options': {
options: {
libraryName: 'my.ui.lib',
dest: 'tmp/library_preload/raw_options/lib1',
compress: false
},
files: [
{
expand: true,
cwd: 'test/library_preload/fixtures/lib1',
src: 'my/ui/lib/**/*.{js,json,xml}'
}
]
}
},

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ Default value: `.`

Path to the dest folder in which the preload file should be created. All other dest paths (from the files configuration) will be ignored.

#### compress
Type: `boolean`
Default value: `true`

Optional parameter to turn off the compression/minifiers on the files. Javascript is minified using UglifyJS and XML by Pretty-data. JSON is parsed for correctness and to remove extra whitespace.

### Usage Examples

```js
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"connect-openui5": "^0.2.1",
"less-openui5": "^0.1.1",
"object-assign": "^1.0.0",
"grunt-contrib-connect": "^0.8.0"
"grunt-contrib-connect": "^0.8.0",
"pretty-data": "^0.40.0",
"uglifyjs": "^2.3.6"
},
"devDependencies": {
"grunt": "~0.4.5",
Expand Down
93 changes: 68 additions & 25 deletions tasks/library_preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,99 @@
'use strict';

// TODO rethink configuration
// TODO use uglify for files (on the fly)


var path = require('path');
var async = require('async');
var path = require('path'),
async = require('async'),
uglify = require('uglifyjs'),
pd = require('pretty-data').pd;

module.exports = function (grunt) {

function readFile(fileName, cb) {
cb(grunt.file.read(fileName, { encoding: 'utf-8' }));
}

function compressFile(fileName, cb) {
var result = grunt.file.read(fileName, { encoding: 'utf-8' }),
extension = path.extname(fileName);

switch (extension) {
case '.js':
// Javascript files are processed by Uglify
result = uglify.minify(result, {
fromString: true,
warnings: true
}).code;
cb(result);
break;

case '.json':
// JSON is parsed and written to string again to remove unwanted white space
result = JSON.stringify(JSON.parse(result));
cb(result);
break;

case '.xml':
// For XML we use the pretty data
result = pd.xmlmin(result, false);
cb(result);
break;

default:
// We want to return just the file if we can't make it happen
cb(result);
}
}

grunt.registerMultiTask('openui5_library_preload', 'Create OpenUI5 Library Preload File', function () {

var done = this.async();
// To call when complete
var done = this.async(),

// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
libraryName: '',
dest: ''
});
// Merge task-specific and/or target-specific options with these defaults.
options = this.options({
libraryName: '',
dest: '',
version: '2.0',
compress: true
}),

var preloadDest = path.join(options.dest, options.libraryName.replace(/\./g, path.sep), 'library-preload.json');
// Location to write library file
preloadDest = path.join(options.dest, options.libraryName.replace(/\./g, path.sep), 'library-preload.json'),

var preload = {
version: '2.0',
name: options.libraryName ? options.libraryName + '.library-preload' : null,
modules: {}
};
// The beginning of the library file
preload = {
version: options.version,
name: options.libraryName ? options.libraryName + '.library-preload' : null,
modules: {}
},

builder = options.compress ? compressFile : readFile;

// TODO check no file

// Iterate over all specified file groups.
async.eachSeries(this.files, function(fileObj, nextFileObj) {
var files = fileObj.src.filter(function(filepath) {
async.eachSeries(this.files, function (fileObj, nextFileObj) {
var files = fileObj.src.filter(function (filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
return true;
});

// TODO check no file

async.concatSeries(files, function(file, next) {
async.concatSeries(files, function (file, next) {
var moduleName = path.relative(fileObj.orig.cwd, file).split(path.sep).join('/');
preload.modules[moduleName] = grunt.file.read(file, { encoding: 'utf-8' });
next();
builder(file, function (src) {
preload.modules[moduleName] = src;
next();
});
}, nextFileObj);

}, function(err, results) {
}, function (err) {

if (err) {
done(err);
Expand All @@ -75,6 +119,5 @@ module.exports = function (grunt) {

done();
});

});
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
"my/ui/lib/myJS.js": "console.log('myJS');\n",
"my/ui/lib/myJSON.json": "{\n\t\"my\": \"json\"\n}\n",
"my/ui/lib/myXML.xml": "<my>XML</my>\n"
"my/ui/lib/myJS.js": "\"use strict\";function myFunction(n,o){return n+o}console.log(\"myJS\");",
"my/ui/lib/myJSON.json": "{\"my\":\"json\"}",
"my/ui/lib/myXML.xml": "<my>XML</my>\r\n"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
"my/ui/lib/myJS.js": "'use strict';\n\nconsole.log('myJS');\n\n/**\n * This is a little comment\n */\nfunction myFunction(longVariableName, longerVariableName) {\n\treturn longVariableName + longerVariableName;\n}\n\n",
"my/ui/lib/myJSON.json": "{\r\n\t\"my\": \"json\"\r\n}\r\n",
"my/ui/lib/myXML.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->"
}
}
10 changes: 10 additions & 0 deletions test/library_preload/fixtures/lib1/my/ui/lib/myJS.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
'use strict';

console.log('myJS');

/**
* This is a little comment
*/
function myFunction(longVariableName, longerVariableName) {
return longVariableName + longerVariableName;
}

1 change: 1 addition & 0 deletions test/library_preload/fixtures/lib1/my/ui/lib/myXML.xml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<my>XML</my>
<!-- A comment in XML is the same as in HTML -->
9 changes: 8 additions & 1 deletion test/library_preload_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ describe('openui5_library_preload', function() {
sExpectedFileSource : 'test/library_preload/expected/default_options/lib1/my/ui/lib/library-preload.json',
sMessage : 'preload JSON should be correctly created.'
});
});

});
it('raw_options', function() {

fileContent.equal({
sActualFileSource : 'tmp/library_preload/raw_options/lib1/my/ui/lib/library-preload.json',
sExpectedFileSource : 'test/library_preload/expected/raw_options/lib1/my/ui/lib/library-preload.json',
sMessage : 'preload JSON should be correctly created.'
});
});
});

0 comments on commit 011d4af

Please sign in to comment.