Skip to content

Commit

Permalink
Add shim support
Browse files Browse the repository at this point in the history
  • Loading branch information
rbackhouse committed Nov 16, 2012
1 parent 4e0f899 commit 6522c1e
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Rhinoast
Bundle-SymbolicName: org.dojotoolkit.optimizer.amd.rhinoast
Bundle-Version: 0.4.4
Bundle-Version: 0.4.8
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.dojotoolkit.optimizer
Import-Package: org.dojotoolkit.json,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ protected JSAnalysisDataImpl _getAnalysisData(String[] modules, JSAnalysisData[]
Map<String, List<Map<String, String>>> pluginRefs = new HashMap<String, List<Map<String, String>>>();
List<Map<String, Object>> missingNamesList = new ArrayList<Map<String, Object>>();
Map<String, Module> moduleMap = new HashMap<String, Module>();
Map<String, String> shims = new HashMap<String, String>();

for (String moduleId : modules) {
logger.logp(Level.INFO, getClass().getName(), "_getAnalysisData", "AST parsing ["+moduleId+"] using the Rhino AST API");
AstVisitor visitor = new AstVisitor(moduleId, moduleMap, pluginRefs, missingNamesList, cfg, new Stack<String>(), excludeList, pageConfigString, scanCJSRequires);
AstVisitor visitor = new AstVisitor(moduleId, moduleMap, pluginRefs, missingNamesList, cfg, new Stack<String>(), excludeList, pageConfigString, scanCJSRequires, shims);
if (visitor.getError() != null) {
logger.logp(Level.INFO, getClass().getName(), "_getAnalysisData", "AST parsing error for ["+moduleId+"] error : "+visitor.getError());
throw new IOException("AST parsing error for ["+moduleId+"] error : "+visitor.getError());
Expand All @@ -164,7 +165,7 @@ protected JSAnalysisDataImpl _getAnalysisData(String[] modules, JSAnalysisData[]
scanForCircularDependencies(m, new Stack<String>(), moduleMap);
}

jsAnalysisData = new JSAnalysisDataImpl(modules, dependencies, null, null, missingNamesList, pluginRefs, resourceLoader, JSAnalysisDataImpl.getExludes(exclude), pageConfig);
jsAnalysisData = new JSAnalysisDataImpl(modules, dependencies, null, null, missingNamesList, pluginRefs, resourceLoader, JSAnalysisDataImpl.getExludes(exclude), pageConfig, shims);
return jsAnalysisData;
}

Expand Down Expand Up @@ -361,7 +362,9 @@ private class AstVisitor implements NodeVisitor {
private String pageConfigString = null;
private String error = null;
private boolean scanCJSRequires = false;

private boolean defineFound = false;
private Map<String, String> shims = null;

public AstVisitor(String moduleId,
Map<String, Module> moduleMap,
Map<String, List<Map<String, String>>> pluginRefList,
Expand All @@ -370,7 +373,8 @@ public AstVisitor(String moduleId,
Stack<String> pathStack,
List<String> excludeList,
String pageConfigString,
boolean scanCJSRequires) {
boolean scanCJSRequires,
Map<String, String> shims) {

if (moduleId.equals("require") || moduleId.equals("exports") || moduleId.equals("module")) {
moduleMap.put(moduleId, new Module(moduleId, moduleId));
Expand All @@ -386,6 +390,7 @@ public AstVisitor(String moduleId,
this.excludeList = excludeList;
this.pageConfigString = pageConfigString;
this.scanCJSRequires = scanCJSRequires;
this.shims = shims;

this.baseUrl = (String)config.get("baseUrl");

Expand Down Expand Up @@ -415,7 +420,7 @@ public AstVisitor(String moduleId,
if (addDep) {
Stack<String> s = new Stack<String>();
s.push(this.moduleId);
AstVisitor visitor = new AstVisitor(pluginDep, moduleMap, pluginRefList, missingNamesList, config, s, excludeList, pageConfigString, scanCJSRequires);
AstVisitor visitor = new AstVisitor(pluginDep, moduleMap, pluginRefList, missingNamesList, config, s, excludeList, pageConfigString, scanCJSRequires, shims);
if (visitor.getError() != null) {
error = visitor.getError();
return;
Expand Down Expand Up @@ -445,6 +450,9 @@ public AstVisitor(String moduleId,
try {
ast = parser.parse(source, this.moduleId, 1);
ast.visit(this);
if (!defineFound) {
findShim();
}
} catch (EvaluatorException e) {
error = "Failed to parse ["+url+"] [line:"+e.lineNumber()+" column:"+e.columnNumber()+"] reason ["+e.details()+"]";
}
Expand All @@ -468,12 +476,15 @@ public boolean visit(AstNode astNode) {
String callName = getCallName(target);
if (callName.equals("define") || callName.equals("require")) {
List<AstNode> args = functionCall.getArguments();
if (callName.equals("define") && args.get(0) instanceof StringLiteral == false) {
Map<String, Object> missingName = new HashMap<String, Object>();
missingName.put("nameIndex", new Long(functionCall.getAbsolutePosition()+functionCall.getLp()+1));
missingName.put("uri", url);
missingName.put("id", this.moduleId);
missingNamesList.add(missingName);
if (callName.equals("define")) {
defineFound = true;
if (args.get(0) instanceof StringLiteral == false) {
Map<String, Object> missingName = new HashMap<String, Object>();
missingName.put("nameIndex", new Long(functionCall.getAbsolutePosition()+functionCall.getLp()+1));
missingName.put("uri", url);
missingName.put("id", this.moduleId);
missingNamesList.add(missingName);
}
}
List<String> dependencies = new ArrayList<String>();
if (callName.equals("require") && args.get(0) instanceof StringLiteral) {
Expand Down Expand Up @@ -526,7 +537,7 @@ public boolean visit(AstNode astNode) {
}
if (addDep) {
module.dependencies.add(pluginDep);
AstVisitor visitor = new AstVisitor(pluginDep, moduleMap, pluginRefList, missingNamesList, config, pathStack, excludeList, pageConfigString, scanCJSRequires);
AstVisitor visitor = new AstVisitor(pluginDep, moduleMap, pluginRefList, missingNamesList, config, pathStack, excludeList, pageConfigString, scanCJSRequires, shims);
if (visitor.getError() != null) {
error = visitor.getError();
return false;
Expand Down Expand Up @@ -559,7 +570,7 @@ public boolean visit(AstNode astNode) {
}
if (addDep) {
module.dependencies.add(dependencyId);
AstVisitor visitor = new AstVisitor(dependencyId, moduleMap, pluginRefList, missingNamesList, config, pathStack, excludeList, pageConfigString, scanCJSRequires);
AstVisitor visitor = new AstVisitor(dependencyId, moduleMap, pluginRefList, missingNamesList, config, pathStack, excludeList, pageConfigString, scanCJSRequires, shims);
if (visitor.getError() != null) {
error = visitor.getError();
return false;
Expand Down Expand Up @@ -683,6 +694,47 @@ private Map<String, Object> callProxy(String proxy, String pluginName, String re
}
return proxyReturn;
}

private void findShim() {
Map<String, Object> shimConfig = (Map<String, Object>)config.get("shim");
if (shimConfig != null) {
Map<String, Object> shim = (Map<String, Object>)shimConfig.get(moduleId);
if (shim != null) {
StringBuffer shimContent = new StringBuffer();
shimContent.append("\n(function(root, cfg) {\n");
shimContent.append("define('");
shimContent.append(moduleId);
shimContent.append("', ");
List<String> deps = (List<String>)shim.get("deps");
if (deps != null) {
shimContent.append("[");
for (String dep : deps) {
module.dependencies.add(dep);
shimContent.append("'");
shimContent.append(dep);
shimContent.append("',");
}
shimContent.deleteCharAt(shimContent.length()-1);
shimContent.append("], ");
}
shimContent.append("function() {\n");
String exports = (String)shim.get("exports");
if (shim.containsKey("init")) {
shimContent.append("\tvar initFunc = cfg.shim['"+moduleId+"'].init;\n");
shimContent.append("\tvar initRet = initFunc.apply(root, arguments);\n");
if (exports != null) {
shimContent.append("\tif (initRet) { return initRet; } else { return root." + exports + "; }\n");
} else {
shimContent.append("\tif (initRet) { return initRet; } else { return {}; }\n");
}
} else if (exports != null) {
shimContent.append("return root." + exports + ";\n");
}
shimContent.append("});\n}(this, zazl._getConfig()));\n");
shims.put(this.url, shimContent.toString());
}
}
}
}

private class Module {
Expand Down
2 changes: 1 addition & 1 deletion org.dojotoolkit.optimizer.amd/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: AMD Optimizer
Bundle-SymbolicName: org.dojotoolkit.optimizer.amd;singleton:=true
Bundle-Version: 0.4.6
Bundle-Version: 0.4.8
29 changes: 28 additions & 1 deletion org.dojotoolkit.optimizer.amd/loader/amd/zazl.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,32 @@ var define;
if (window.dojoConfig && window.dojoConfig.locale) {
locale = dojoConfig.locale;
}
var configString = JSON.stringify(cfg);
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
if (obj instanceof Array) {
var copy = [];
var len = obj.length;
for (var i = 0; i < len; ++i) {
copy[i] = clone(obj[i]);
}
return copy;
}
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) {
if (isFunction(obj[attr])) {
copy[attr] = "function";
} else {
copy[attr] = clone(obj[attr]);
}
}
}
return copy;
}
throw new Error("Unable to clone");
}
var configString = JSON.stringify(clone(cfg));
var url = cfg.injectUrl+"?modules=";
for (var i = 0; i < notLoaded.length; i++) {
url += notLoaded[i];
Expand Down Expand Up @@ -610,6 +635,8 @@ var define;
zazl.addToCache = function(id, value) {
precache[id] = value;
};

zazl._getConfig = function() { return cfg};

function processCache() {
for (var id in precache) {
Expand Down
5 changes: 3 additions & 2 deletions org.dojotoolkit.optimizer.amd/optimizer/amd/AMDAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ AMDAnalyzer.prototype = {
_analyze: function(modules, exclude) {
this.pluginRefList = {};
this.missingNamesList = [];
this.shims = {};
this.moduleMap = map.createMap();
for (var i = 0; i < modules.length; i++) {
astwalker.walker(modules[i], exclude, this.moduleMap, this.pluginRefList, this.missingNamesList, this.config, []);
astwalker.walker(modules[i], exclude, this.moduleMap, this.pluginRefList, this.missingNamesList, this.config, this.shims, []);
}
},

Expand All @@ -119,7 +120,7 @@ AMDAnalyzer.prototype = {

getAnalysisData: function(modules, exclude) {
var dependencyList = this.getDependencyList(modules, exclude);
return ({dependencyList: dependencyList, pluginRefs: this.pluginRefList, missingNamesList: this.missingNamesList});
return ({dependencyList: dependencyList, pluginRefs: this.pluginRefList, missingNamesList: this.missingNamesList, shims: this.shims});
}
};

Expand Down
Loading

0 comments on commit 6522c1e

Please sign in to comment.