Comparing version 1.0.22 to 1.0.23
@@ -0,1 +1,7 @@ | ||
## 2013.12.1, Version 1.0.23(Stable) | ||
### enhancement | ||
- issue #35: 支持KISSY 1.4+的get requires from function | ||
## 2013.11.28, Version 1.0.22(Stable) | ||
@@ -2,0 +8,0 @@ |
@@ -29,3 +29,4 @@ /** | ||
Compiler.prototype._addModuleName = function(moduleContent, modName){ | ||
Compiler.prototype._addModuleName = function(moduleContent, mod){ | ||
var modName = mod.name; | ||
//add module path | ||
@@ -56,3 +57,12 @@ var start = moduleContent.indexOf('KISSY.add('); | ||
//KISSY.add(function(xxx)) | ||
moduleContent = [moduleContent.slice(0, start), '\'' + modName + '\',', moduleContent.slice(end)].join(''); | ||
var addedContentArr = [moduleContent.slice(0, start), '\'' + modName + '\',']; | ||
if(mod.version === '1.4+' && mod.requires){ | ||
var requires = []; | ||
mod.requires.forEach(function(requirePath){ | ||
requires.push("'" + requirePath + "'"); | ||
}); | ||
addedContentArr.push("[" + requires.join(", ") + "], "); | ||
} | ||
addedContentArr.push(moduleContent.slice(end)); | ||
moduleContent = addedContentArr.join(''); | ||
} | ||
@@ -210,4 +220,4 @@ | ||
modContent = self._mapContent(modContent, mod); | ||
return self._addModuleName(modContent, mod.name); | ||
modContent = self._addModuleName(modContent, mod); | ||
return self._mapContent(modContent, mod); | ||
}; | ||
@@ -252,2 +262,3 @@ Compiler.prototype._alias = function(modName){ | ||
modType, | ||
modVersion, | ||
modRealPath; | ||
@@ -301,3 +312,8 @@ if(!inputFilePath) return null; | ||
// get this file's dependencies. | ||
modRequires = dependencies(modRealPath); | ||
modRequires = dependencies.getRequiresFromFn(modRealPath); | ||
if(modRequires.length){ | ||
modVersion = '1.4+'; | ||
}else{ | ||
modRequires = dependencies.requires(modRealPath); | ||
} | ||
// if user named module himself, use his name. map rules will not work then. | ||
@@ -324,3 +340,4 @@ if(_.isPlainObject(modRequires) && modRequires.name){ | ||
charset: modPkg.charset, | ||
type: modType | ||
type: modType, | ||
version: modVersion | ||
}; | ||
@@ -327,0 +344,0 @@ self.modules[modName] = mod; |
@@ -8,59 +8,86 @@ /** | ||
"use strict"; | ||
var fs = require('fs'); | ||
var UglifyJS = require('uglify-js'), | ||
getAst = require('./get-ast'); | ||
module.exports = function(inputFile){ | ||
var ast = getAst(inputFile); | ||
var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg, | ||
requireRegExp = /[^.'"]\s*require\s*\(([^)]+)\)/g; | ||
var deps = [], | ||
moduleName = undefined, | ||
call_expression = null, | ||
obj_expression = null; | ||
function getRequireVal(str) { | ||
var m; | ||
// simple string | ||
if (!(m = str.match(/^\s*["']([^'"\s]+)["']\s*$/))) { | ||
throw new Error('can not find required mod in require call: ' + str); | ||
} | ||
return m[1]; | ||
} | ||
if(ast){ | ||
var walker = new UglifyJS.TreeWalker(function(node, descend) { | ||
if(node instanceof UglifyJS.AST_Call && (node.start.value == 'KISSY' || node.start.value == 'S') && node.expression && node.expression.property == 'add'){ | ||
var tmp = call_expression; | ||
call_expression = node; | ||
descend(); | ||
call_expression = tmp; | ||
return true; | ||
} | ||
module.exports = { | ||
getRequiresFromFn: function(inputFile){ | ||
var content = fs.readFileSync(inputFile).toString(); | ||
var requires = []; | ||
content | ||
.replace(commentRegExp, '') | ||
.replace(requireRegExp, function (match, dep) { | ||
requires.push(getRequireVal(dep)); | ||
}); | ||
return requires; | ||
}, | ||
requires: function(inputFile){ | ||
var ast = getAst(inputFile); | ||
if(node instanceof UglifyJS.AST_String && call_expression && obj_expression === null){ | ||
moduleName = node.getValue(); | ||
var deps = [], | ||
moduleName = undefined, | ||
call_expression = null, | ||
obj_expression = null; | ||
if(ast){ | ||
var walker = new UglifyJS.TreeWalker(function(node, descend) { | ||
if(node instanceof UglifyJS.AST_Call && (node.start.value == 'KISSY' || node.start.value == 'S') && node.expression && node.expression.property == 'add'){ | ||
var tmp = call_expression; | ||
call_expression = node; | ||
descend(); | ||
call_expression = tmp; | ||
return true; | ||
} | ||
if(node instanceof UglifyJS.AST_String && call_expression && obj_expression === null){ | ||
moduleName = node.getValue(); | ||
// console.log('Found Module ID: ' + moduleName); | ||
} | ||
} | ||
if(node instanceof UglifyJS.AST_Lambda && call_expression){ | ||
var tmp = call_expression; | ||
call_expression = null; | ||
descend(); | ||
call_expression = tmp; | ||
if(node instanceof UglifyJS.AST_Lambda && call_expression){ | ||
var tmp = call_expression; | ||
call_expression = null; | ||
descend(); | ||
call_expression = tmp; | ||
// console.log('Found Lambda'); | ||
return true; | ||
} | ||
return true; | ||
} | ||
if(node instanceof UglifyJS.AST_ObjectKeyVal && call_expression && obj_expression === null){ | ||
if(node.key && node.key === 'requires'){ | ||
if(node instanceof UglifyJS.AST_ObjectKeyVal && call_expression && obj_expression === null){ | ||
if(node.key && node.key === 'requires'){ | ||
// console.log('Found requires'); | ||
var tmp = obj_expression; | ||
obj_expression = node; | ||
descend(); | ||
obj_expression = null; | ||
return true; | ||
var tmp = obj_expression; | ||
obj_expression = node; | ||
descend(); | ||
obj_expression = null; | ||
return true; | ||
} | ||
} | ||
} | ||
if(node instanceof UglifyJS.AST_String && call_expression && obj_expression){ | ||
var mod = node.getValue(); | ||
deps.push(mod); | ||
if(node instanceof UglifyJS.AST_String && call_expression && obj_expression){ | ||
var mod = node.getValue(); | ||
deps.push(mod); | ||
// console.log('Found required module: ' + mod); | ||
} | ||
}); | ||
} | ||
}); | ||
ast.walk(walker); | ||
ast.walk(walker); | ||
} | ||
return moduleName ? { name: moduleName, deps: deps } :deps; | ||
} | ||
return moduleName ? { name: moduleName, deps: deps } :deps; | ||
}; |
{ | ||
"name":"kmc", | ||
"version":"1.0.22", | ||
"version":"1.0.23", | ||
"description":"KISSY Module Compiler", | ||
@@ -5,0 +5,0 @@ "author":"daxingplay <daxingplay@gmail.com>", |
/** | ||
* 1.0.0 test file. | ||
* kmc test file. | ||
* @author: 橘子<daxingplay@gmail.com> | ||
@@ -1084,2 +1084,46 @@ * @time: 13-3-13 10:46 | ||
}); | ||
describe('When use require in add function', function(){ | ||
var result; | ||
var inputFile = path.resolve(srcPath, 'package1/in-function-require.js'), | ||
outputFile = path.resolve(distPath, 'package1/in-function-require.js'); | ||
before(function(){ | ||
ModuleCompiler.config({ | ||
packages: { | ||
package1: { | ||
base: srcPath, | ||
charset: 'gbk' | ||
} | ||
}, | ||
silent: true, | ||
charset: 'gbk' | ||
}); | ||
result = ModuleCompiler.build({ | ||
src: inputFile, | ||
dest: outputFile | ||
}); | ||
}); | ||
after(function(){ | ||
ModuleCompiler.clean(); | ||
}); | ||
it('should have proper file generated', function(){ | ||
fs.existsSync(outputFile).should.equal(true); | ||
}); | ||
it('should have proper modules', function(){ | ||
var file = result.files[0]; | ||
file.name.should.equal('package1/in-function-require'); | ||
file.should.have.property('requires').with.lengthOf('2'); | ||
}); | ||
it('should have requires array in outputfile', function(){ | ||
var content = fs.readFileSync(outputFile); | ||
/'package1\/in\-function\-require',\s*\['node', '.\/mods\/mod2'\]/.test(content).should.equal(true); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
141471
90
4465
14