Skip to content

Commit 395d7fe

Browse files
committed
feat(hash) generate x-{hash}-debug.js when options hash:true debug:true
1 parent 9326547 commit 395d7fe

11 files changed

+117
-77
lines changed

Gruntfile.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ module.exports = function(grunt) {
262262
'$': '$'
263263
},
264264
idleading: 'family/name/',
265-
hash: true
265+
hash: true,
266+
debug: false
266267
},
267268
files: [{
268269
expand: true,
@@ -283,7 +284,7 @@ module.exports = function(grunt) {
283284
},
284285
idleading: 'family/name/',
285286
hash: true,
286-
debug: false
287+
debug: true
287288
},
288289
files: [{
289290
expand: true,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "grunt-cmd-transport",
33
"description": "Transport javascript into cmd.",
4-
"version": "0.5.0-rc.2",
4+
"version": "0.5.0-rc.3",
55
"homepage": "https://github.com/spmjs/grunt-cmd-transport",
66
"author": {
77
"name": "Hsiaoming Yang",

tasks/lib/common.js

+41-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
exports.init = function(grunt, options) {
2+
var extname = require('path').extname;
23
var format = require('util').format;
34
var ast = require('cmd-util').ast;
45
var md5 = require('./util').md5;
@@ -11,7 +12,7 @@ exports.init = function(grunt, options) {
1112
var exports = {};
1213

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

24-
// create .{type}.js
25-
data = ast.modify(file.contents, {
26-
id: id
27-
}).print_to_string(options.uglify);
28-
writeFile(data, file.dest);
25+
if (!options.hash) {
2926

30-
// create debug file xxx-debug.{type}.js
31-
if (options.debug) {
32-
dest = file.dest.replace(retTypeJs, '-debug.' + type + '.js');
27+
// create .{type}.js
3328
data = ast.modify(file.contents, {
34-
id: id.replace(regType, '-debug.' + type),
35-
dependencies: function(id) {
36-
return id + '-debug';
37-
},
38-
require: function(id) {
39-
return id + '-debug';
40-
}
29+
id: id
4130
}).print_to_string(options.uglify);
42-
writeFile(data, dest);
31+
filepath = file.dest;
32+
writeFile(data, filepath);
33+
} else {
34+
35+
// create hash file xxx-{hash}.{type}.js
36+
if (options.hash) {
37+
filepath = file.dest.replace(retTypeJs, '-' + hash + '.' + type + '.js');
38+
data = ast.modify(file.contents, {
39+
id: id.replace(regType, '-' + hash + '.' + type)
40+
}).print_to_string(options.uglify);
41+
writeFile(data, filepath);
42+
}
4343
}
4444

45-
// create hash file xxx-{hash}.{type}.js
46-
if (options.hash) {
47-
dest = file.dest.replace(retTypeJs, '-' + hash + '.' + type + '.js');
48-
data = ast.modify(file.contents, {
49-
id: id.replace(regType, '-' + hash + '.' + type)
50-
}).print_to_string(options.uglify);
51-
writeFile(data, dest);
45+
// create debug file xxx-debug.{type}.js
46+
if (options.debug) {
47+
data = ast.modify(data, addDebug).print_to_string(options.uglify);
48+
filepath = filepath.replace(retTypeJs, '-debug.' + type + '.js');
49+
writeFile(data, filepath);
5250
}
51+
52+
// {
53+
// id: id.replace(regType, '-debug.' + type),
54+
// dependencies: function(id) {
55+
// return id + '-debug';
56+
// },
57+
// require: function(id) {
58+
// return id + '-debug';
59+
// }
60+
// }
61+
function addDebug(v) {
62+
var ext = extname(v);
63+
if (ext && options.parsers[ext]) {
64+
return v.replace(new RegExp('\\' + ext + '$'), '-debug' + ext);
65+
} else {
66+
return v + '-debug';
67+
}
68+
}
69+
5370
};
5471
return exports;
5572

tasks/lib/script.js

+34-26
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,43 @@ exports.init = function(grunt) {
1515
function jsParser(fileObj, options) {
1616
grunt.log.verbose.writeln('Transport ' + fileObj.src + ' -> ' + fileObj.dest);
1717

18-
/*
19-
cache every filepath content to generate hash
20-
*/
18+
19+
// cache every filepath content to generate hash
20+
//
21+
// {
22+
// '/path/to/file': {
23+
// id: undefined,
24+
// dependencies: [],
25+
// depMap: {},
26+
// depsSpecified: false,
27+
// contents: contents,
28+
// path: path,
29+
// hash: md5(contents, [])
30+
// }
31+
// }
2132
var fileCache = {};
2233

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

2536
if (!file) return;
2637

27-
// create original file, xxx.js
28-
var data = ast.modify(file.contents, {
29-
id: unixy(options.idleading) + getId(file),
30-
dependencies: getDeps(file),
31-
require: function(v) {
32-
// ignore when deps is specified by developer
33-
return file.depsSpecified ? v : iduri.parseAlias(options, v);
34-
}
35-
}).print_to_string(options.uglify);
36-
writeFile(data, fileObj.dest);
38+
var data, filepath;
39+
if (!options.hash) {
3740

38-
// create file with hash, xxx-2cio56s.js
39-
if (options.hash) {
41+
// create original file, xxx.js
42+
data = ast.modify(file.contents, {
43+
id: unixy(options.idleading) + getId(file),
44+
dependencies: getDeps(file),
45+
require: function(v) {
46+
// ignore when deps is specified by developer
47+
return file.depsSpecified ? v : iduri.parseAlias(options, v);
48+
}
49+
}).print_to_string(options.uglify);
50+
filepath = fileObj.dest;
51+
writeFile(data, filepath);
52+
} else {
53+
54+
// create file with hash, xxx-2cio56s.js
4055
var hash = file.hash;
4156
data = ast.modify(file.contents, {
4257
id: unixy(options.idleading) + getId(file) + '-' + hash,
@@ -51,20 +66,14 @@ exports.init = function(grunt) {
5166
return iduri.parseAlias(options, v);
5267
}
5368
}).print_to_string(options.uglify);
54-
writeFile(data, fileObj.dest.replace(/\.js$/, '-' + hash + '.js'));
69+
filepath = fileObj.dest.replace(/\.js$/, '-' + hash + '.js');
70+
writeFile(data, filepath);
5571
}
5672

5773
// create file with debug, xxx-debug.js
5874
if (options.debug) {
59-
data = ast.modify(file.contents, {
60-
id: unixy(options.idleading) + addDebug(getId(file)),
61-
dependencies: getDeps(file, addDebug),
62-
require: function(v) {
63-
// ignore when deps is specified by developer
64-
return file.depsSpecified ? v : addDebug(iduri.parseAlias(options, v));
65-
}
66-
}).print_to_string(options.uglify);
67-
writeFile(data, addDebug(fileObj.dest));
75+
data = ast.modify(data, addDebug).print_to_string(options.uglify);
76+
writeFile(data, addDebug(filepath));
6877
}
6978

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

8392
function addDebug(v) {
84-
if (v.id) v = v.id;
8593
var ext = extname(v);
8694
if (ext && options.parsers[ext]) {
8795
return v.replace(new RegExp('\\' + ext + '$'), '-debug' + ext);

tasks/lib/style.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,31 @@ exports.init = function(grunt) {
1111

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

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

22-
if (options.debug) {
23-
dest = fileObj.dest.replace(/\.css$/, '-debug.css');
24-
ret = parseCss(data, addDebug);
25-
id = unixy(options.idleading + fileObj.name.replace(/\.css$/, '-debug.css'));
26-
code = format('/*! define %s */\n%s', id, ret);
27-
writeFile(code, dest);
28-
}
18+
var ret = parseCss(data);
19+
id = unixy(options.idleading + fileObj.name);
20+
var code = format('/*! define %s */\n%s', id, ret);
21+
filepath = fileObj.dest;
22+
writeFile(code, filepath);
23+
} else {
2924

30-
if (options.hash) {
3125
var hash = md5(data);
32-
dest = fileObj.dest.replace(/\.css$/, '-' + hash + '.css');
3326
ret = parseCss(data, addHash);
3427
id = unixy(options.idleading + fileObj.name.replace(/\.css$/, '-' + hash + '.css'));
3528
code = format('/*! define %s */\n%s', id, ret);
36-
writeFile(code, dest);
29+
filepath = fileObj.dest.replace(/\.css$/, '-' + hash + '.css');
30+
writeFile(code, filepath);
31+
}
32+
33+
if (options.debug) {
34+
ret = parseCss(data, addDebug);
35+
id = id.replace(/\.css$/, '-debug.css');
36+
code = format('/*! define %s */\n%s', id, ret);
37+
filepath = filepath.replace(/\.css$/, '-debug.css');
38+
writeFile(code, filepath);
3739
}
3840

3941
function addDebug(node) {

test/cases/hash/a.js.expect

-4
This file was deleted.

test/cases/hash/b.js.expect

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
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) {
2+
require("./a-670b8177-debug.handlebars");
3+
require("./a-d41d8cd9-debug.json");
4+
require("./a-2cee5097-debug.html");
5+
require("./a-d41d8cd9-debug.tpl");
6+
require("./a-e1f4111c-debug.css");
7+
require("./b-0ec4c8ca-debug.js");
8+
require("arale/dialog/1.3.1/confirmbox-debug");
9+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
define("family/name/a-d41d8cd9-debug.json", [], {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*! define family/name/a-e1f4111c-debug.css */
2+
/*! import ./b-debug.css */
3+
ul {
4+
margin: 0;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*! define family/name/b-aa896723-debug.css */
2+
/*! import alice/list/1.0.1/list-debug.css */
3+
html {
4+
margin: 0;
5+
}

0 commit comments

Comments
 (0)