From 737cbac7c6dc1ab7f8bd8ec91c1813e9c8d816ab Mon Sep 17 00:00:00 2001 From: neekey Date: Mon, 14 Oct 2013 13:18:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DJS=E8=A7=A3=E6=9E=90=E9=94=99?= =?UTF-8?q?=E8=AF=AF=EF=BC=8C=E4=BD=86=E6=98=AF=E6=B2=A1=E6=9C=89=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tasks/kmc.js | 117 ++++++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 53 deletions(-) diff --git a/tasks/kmc.js b/tasks/kmc.js index 7db27ed..4cca50c 100644 --- a/tasks/kmc.js +++ b/tasks/kmc.js @@ -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); + } }); - };