Skip to content

Commit

Permalink
feat(hash) generate x-{hash}-debug.js when options hash:true debug:true
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Jan 16, 2015
1 parent 9326547 commit 2a139ea
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 76 deletions.
5 changes: 3 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ module.exports = function(grunt) {
'$': '$'
},
idleading: 'family/name/',
hash: true
hash: true,
debug: false
},
files: [{
expand: true,
Expand All @@ -283,7 +284,7 @@ module.exports = function(grunt) {
},
idleading: 'family/name/',
hash: true,
debug: false
debug: true
},
files: [{
expand: true,
Expand Down
65 changes: 41 additions & 24 deletions tasks/lib/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
exports.init = function(grunt, options) {
var extname = require('path').extname;
var format = require('util').format;
var ast = require('cmd-util').ast;
var md5 = require('./util').md5;
Expand All @@ -11,7 +12,7 @@ exports.init = function(grunt, options) {
var exports = {};

exports[type + 'Parser'] = function(fileObj, options) {
var dest, id = unixy(options.idleading + fileObj.name.replace(/\.js$/, ''));
var filepath, id = unixy(options.idleading + fileObj.name.replace(/\.js$/, ''));
var data = fileObj.srcData || grunt.file.read(fileObj.src);
var hash = md5(data);
var deps = depParser ? depParser(data, options) : '';
Expand All @@ -21,35 +22,51 @@ exports.init = function(grunt, options) {
dest: fileObj.dest + '.js'
};

// create .{type}.js
data = ast.modify(file.contents, {
id: id
}).print_to_string(options.uglify);
writeFile(data, file.dest);
if (!options.hash) {

// create debug file xxx-debug.{type}.js
if (options.debug) {
dest = file.dest.replace(retTypeJs, '-debug.' + type + '.js');
// create .{type}.js
data = ast.modify(file.contents, {
id: id.replace(regType, '-debug.' + type),
dependencies: function(id) {
return id + '-debug';
},
require: function(id) {
return id + '-debug';
}
id: id
}).print_to_string(options.uglify);
writeFile(data, dest);
filepath = file.dest;
writeFile(data, filepath);
} else {

// create hash file xxx-{hash}.{type}.js
if (options.hash) {
filepath = file.dest.replace(retTypeJs, '-' + hash + '.' + type + '.js');
data = ast.modify(file.contents, {
id: id.replace(regType, '-' + hash + '.' + type)
}).print_to_string(options.uglify);
writeFile(data, filepath);
}
}

// create hash file xxx-{hash}.{type}.js
if (options.hash) {
dest = file.dest.replace(retTypeJs, '-' + hash + '.' + type + '.js');
data = ast.modify(file.contents, {
id: id.replace(regType, '-' + hash + '.' + type)
}).print_to_string(options.uglify);
writeFile(data, dest);
// create debug file xxx-debug.{type}.js
if (options.debug) {
data = ast.modify(data, addDebug).print_to_string(options.uglify);
filepath = filepath.replace(retTypeJs, '-debug.' + type + '.js');
writeFile(data, filepath);
}

// {
// id: id.replace(regType, '-debug.' + type),
// dependencies: function(id) {
// return id + '-debug';
// },
// require: function(id) {
// return id + '-debug';
// }
// }
function addDebug(v) {
var ext = extname(v);
if (ext && options.parsers[ext]) {
return v.replace(new RegExp('\\' + ext + '$'), '-debug' + ext);
} else {
return v + '-debug';
}
}

};
return exports;

Expand Down
60 changes: 34 additions & 26 deletions tasks/lib/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,43 @@ exports.init = function(grunt) {
function jsParser(fileObj, options) {
grunt.log.verbose.writeln('Transport ' + fileObj.src + ' -> ' + fileObj.dest);

/*
cache every filepath content to generate hash
*/

// cache every filepath content to generate hash
//
// {
// '/path/to/file': {
// id: undefined,
// dependencies: [],
// depMap: {},
// depsSpecified: false,
// contents: contents,
// path: path,
// hash: md5(contents, [])
// }
// }
var fileCache = {};

var file = getFileInfo(path.join(process.cwd(), fileObj.src));

if (!file) return;

// create original file, xxx.js
var data = ast.modify(file.contents, {
id: unixy(options.idleading) + getId(file),
dependencies: getDeps(file),
require: function(v) {
// ignore when deps is specified by developer
return file.depsSpecified ? v : iduri.parseAlias(options, v);
}
}).print_to_string(options.uglify);
writeFile(data, fileObj.dest);
var data, filepath;
if (!options.hash) {

// create file with hash, xxx-2cio56s.js
if (options.hash) {
// create original file, xxx.js
data = ast.modify(file.contents, {
id: unixy(options.idleading) + getId(file),
dependencies: getDeps(file),
require: function(v) {
// ignore when deps is specified by developer
return file.depsSpecified ? v : iduri.parseAlias(options, v);
}
}).print_to_string(options.uglify);
filepath = fileObj.dest;
writeFile(data, filepath);
} else {

// create file with hash, xxx-2cio56s.js
var hash = file.hash;
data = ast.modify(file.contents, {
id: unixy(options.idleading) + getId(file) + '-' + hash,
Expand All @@ -51,20 +66,14 @@ exports.init = function(grunt) {
return iduri.parseAlias(options, v);
}
}).print_to_string(options.uglify);
writeFile(data, fileObj.dest.replace(/\.js$/, '-' + hash + '.js'));
filepath = fileObj.dest.replace(/\.js$/, '-' + hash + '.js');
writeFile(data, filepath);
}

// create file with debug, xxx-debug.js
if (options.debug) {
data = ast.modify(file.contents, {
id: unixy(options.idleading) + addDebug(getId(file)),
dependencies: getDeps(file, addDebug),
require: function(v) {
// ignore when deps is specified by developer
return file.depsSpecified ? v : addDebug(iduri.parseAlias(options, v));
}
}).print_to_string(options.uglify);
writeFile(data, addDebug(fileObj.dest));
data = ast.modify(data, addDebug).print_to_string(options.uglify);
writeFile(data, addDebug(filepath));
}

function getId(file) {
Expand All @@ -81,7 +90,6 @@ exports.init = function(grunt) {
}

function addDebug(v) {
if (v.id) v = v.id;
var ext = extname(v);
if (ext && options.parsers[ext]) {
return v.replace(new RegExp('\\' + ext + '$'), '-debug' + ext);
Expand Down
34 changes: 18 additions & 16 deletions tasks/lib/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,31 @@ exports.init = function(grunt) {

// the real css parser
exports.cssParser = function(fileObj, options) {
var data = fileObj.srcData || grunt.file.read(fileObj.src);
var id, filepath, data = fileObj.srcData || grunt.file.read(fileObj.src);

var dest = fileObj.dest;
var ret = parseCss(data);
var id = unixy(options.idleading + fileObj.name);
var code = format('/*! define %s */\n%s', id, ret);
writeFile(code, dest);
if (!options.hash) {

if (options.debug) {
dest = fileObj.dest.replace(/\.css$/, '-debug.css');
ret = parseCss(data, addDebug);
id = unixy(options.idleading + fileObj.name.replace(/\.css$/, '-debug.css'));
code = format('/*! define %s */\n%s', id, ret);
writeFile(code, dest);
}
var ret = parseCss(data);
id = unixy(options.idleading + fileObj.name);
var code = format('/*! define %s */\n%s', id, ret);
filepath = fileObj.dest;
writeFile(code, filepath);
} else {

if (options.hash) {
var hash = md5(data);
dest = fileObj.dest.replace(/\.css$/, '-' + hash + '.css');
ret = parseCss(data, addHash);
id = unixy(options.idleading + fileObj.name.replace(/\.css$/, '-' + hash + '.css'));
code = format('/*! define %s */\n%s', id, ret);
writeFile(code, dest);
filepath = fileObj.dest.replace(/\.css$/, '-' + hash + '.css');
writeFile(code, filepath);
}

if (options.debug) {
ret = parseCss(data, addDebug);
id = id.replace(/\.css$/, '-debug.css');
code = format('/*! define %s */\n%s', id, ret);
filepath = filepath.replace(/\.css$/, '-debug.css');
writeFile(code, filepath);
}

function addDebug(node) {
Expand Down
4 changes: 0 additions & 4 deletions test/cases/hash/a.js.expect

This file was deleted.

4 changes: 0 additions & 4 deletions test/cases/hash/b.js.expect

This file was deleted.

9 changes: 9 additions & 0 deletions test/cases/project/a-33cd4a1a-debug.js.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define("family/name/a-33cd4a1a-debug", [ "./a-670b8177-debug.handlebars", "./a-d41d8cd9-debug.json", "./a-2cee5097-debug.html", "./a-d41d8cd9-debug.tpl", "./a-e1f4111c-debug.css", "./b-0ec4c8ca-debug", "arale/base/1.1.1/base-debug", "arale/class/1.1.0/class-debug", "arale/events/1.1.0/events-debug", "alice/loading/1.0.0/loading-debug.css", "arale/dialog/1.3.1/confirmbox-debug", "arale/overlay/1.1.4/overlay-debug", "arale/position/1.0.1/position-debug", "arale/iframe-shim/1.0.2/iframe-shim-debug", "arale/widget/1.1.1/widget-debug", "arale/overlay/1.1.4/mask-debug", "arale/templatable/0.9.2/templatable-debug", "gallery/handlebars/1.0.2/handlebars-debug", "gallery/handlebars/1.0.2/runtime-debug" ], function(require) {
require("./a-670b8177-debug.handlebars");
require("./a-d41d8cd9-debug.json");
require("./a-2cee5097-debug.html");
require("./a-d41d8cd9-debug.tpl");
require("./a-e1f4111c-debug.css");
require("./b-0ec4c8ca-debug.js");
require("arale/dialog/1.3.1/confirmbox-debug");
});
1 change: 1 addition & 0 deletions test/cases/project/a-d41d8cd9-debug.json.js.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
define("family/name/a-d41d8cd9-debug.json", [], {});
5 changes: 5 additions & 0 deletions test/cases/project/a-e1f4111c-debug.css.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*! define family/name/a-e1f4111c-debug.css */
/*! import ./b-debug.css */
ul {
margin: 0;
}
5 changes: 5 additions & 0 deletions test/cases/project/b-aa896723-debug.css.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*! define family/name/b-aa896723-debug.css */
/*! import alice/list/1.0.1/list-debug.css */
html {
margin: 0;
}

0 comments on commit 2a139ea

Please sign in to comment.