Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复JS解析错误,但是没有报错的问题 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 64 additions & 53 deletions tasks/kmc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,74 @@ module.exports = function (grunt) {

kmc.config(options);

// Iterate over all specified file groups.
this.files.forEach(function (f) {
f.src.forEach(function(src){
var depFile = '',
inputSrc = path.resolve(src),
outputSrc = path.resolve(String(f.dest));
if(depExt || depFilePath){
depExt = depExt || '.dep';
var outputIsDir = grunt.file.isDir(outputSrc) || !/\.j$/.test(outputSrc);
if(depFilePath){
depFile = grunt.file.isDir(depFilePath) ? path.resolve(depFilePath, path.basename(outputIsDir ? path.basename(inputSrc, '.js') :outputSrc) + depExt + '.js') : depFilePath;
/**
* Catch error threw by UglifyJS.parse or something else. An easy way to cause this problem is write some code like below:
* `KISSY.('hello');`
* And without try & catch, no warning will be present and grunt just end with `Done, without error'.
* But in fact, the kmc task is stopped right after this error has been threw and all files following the file causing this error will not be processed.
*/
try {
// Iterate over all specified file groups.
this.files.forEach(function (f) {
f.src.forEach(function(src){
var depFile = '',
inputSrc = path.resolve(src),
outputSrc = path.resolve(String(f.dest));
if(depExt || depFilePath){
depExt = depExt || '.dep';
var outputIsDir = grunt.file.isDir(outputSrc) || !/\.j$/.test(outputSrc);
if(depFilePath){
depFile = grunt.file.isDir(depFilePath) ? path.resolve(depFilePath, path.basename(outputIsDir ? path.basename(inputSrc, '.js') :outputSrc) + depExt + '.js') : depFilePath;
}else{
var dir = outputIsDir ? outputSrc : path.dirname(outputSrc);
depFile = path.resolve(dir, path.basename(inputSrc, '.js') + depExt + '.js');
}
}
var result = '';
if(comboOnly === true){
kmc.combo(inputSrc, depFile, depFileCharset);
grunt.log.writeln('Dep File "' + depFile + '" created.');
}else{
var dir = outputIsDir ? outputSrc : path.dirname(outputSrc);
depFile = path.resolve(dir, path.basename(inputSrc, '.js') + depExt + '.js');
result = kmc.build(inputSrc, outputSrc, null, depFile, traverse);
grunt.log.writeln('File "' + result.files[0].outputFile + '" created.');
}
}
var result = '';
if(comboOnly === true){
kmc.combo(inputSrc, depFile, depFileCharset);
grunt.log.writeln('Dep File "' + depFile + '" created.');
}else{
result = kmc.build(inputSrc, outputSrc, null, depFile, traverse);
grunt.log.writeln('File "' + result.files[0].outputFile + '" created.');
}

});
});


// added by jayli
var fullMods = {};
if(options.comboMap === true){
var ap = path.normalize(options.packages[0].path);
if(typeof options.packages[0].name == 'undefined'){
grunt.log.writeln('packages is not defined!');
return;
}
if(typeof options.depFilePath == 'undefined'){
grunt.log.writeln('depFilePath is not defined!');
return;
}
var pkg = options.packages[0].name;
var depFile = options.depFilePath;
f.src.forEach(function(src){
kmc.clean();
var a = kmc.analyze(path.resolve(src));
var p = pkg + a.path.replace(ap,'').replace(/\.js$/,'');
fullMods[p] = a.requires || [];
});
var mods = JSON.stringify(fullMods);
var res = '';
res += '/*generated by KMC*/\n';
res += 'KISSY.config("modules",';
res += mods;
res += ');';
fs.writeFileSync(path.resolve(depFile),res,{'encoding':'utf8'});
}
});
// added by jayli
var fullMods = {};
if(options.comboMap === true){
var ap = path.normalize(options.packages[0].path);
if(typeof options.packages[0].name == 'undefined'){
grunt.log.writeln('packages is not defined!');
return;
}
if(typeof options.depFilePath == 'undefined'){
grunt.log.writeln('depFilePath is not defined!');
return;
}
var pkg = options.packages[0].name;
var depFile = options.depFilePath;
f.src.forEach(function(src){
kmc.clean();
var a = kmc.analyze(path.resolve(src));
var p = pkg + a.path.replace(ap,'').replace(/\.js$/,'');
fullMods[p] = a.requires || [];
});
var mods = JSON.stringify(fullMods);
var res = '';
res += '/*generated by KMC*/\n';
res += 'KISSY.config("modules",';
res += mods;
res += ');';
fs.writeFileSync(path.resolve(depFile),res,{'encoding':'utf8'});
}
});
}
catch( e ){
// Warning and terminal grunt.
grunt.fail.warn(e);
}
});

};