JavaScriptBuildRenderer implements Serializable, IModuleBuildRenderer { public String renderBuild(HttpServletRequest request, Set<String> dependentFeatures) { final String methodName = "renderBuild"; final boolean isDebugLogging = log.isLoggable(Level.FINER); final boolean isTraceLogging = log.isLoggable(Level.FINEST); if (isDebugLogging) { log.entering(JavaScriptBuildRenderer.class.getName(), methodName, new Object[]{request, dependentFeatures}); } if (isDebugLogging) { log.finer("Rendering expanded dependencies for " + mid); } String result = null; Features features = (Features)request.getAttribute(IHttpTransport.FEATUREMAP_REQATTRNAME); ModuleDeps enclosingDeps = (ModuleDeps)request.getAttribute(JavaScriptModuleBuilder.EXPANDED_DEPENDENCIES); if (enclosingDeps != null && features != null) { enclosingDeps.resolveWith(features); } if (contentFragments.size() < 2) { result = contentFragments.get(0); } else { if (contentFragments.size() != expDepIndicies.size() + 1) { throw new IllegalStateException(mid + "(" + contentFragments.size() + "," + expandedDeps.size() + ")"); } @SuppressWarnings("unchecked") ConcurrentListBuilder<String[]> expDeps = (ConcurrentListBuilder<String[]>)request.getAttribute(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS); Map<?,?> formulaCache = (Map<?,?>)request.getAttribute(JavaScriptModuleBuilder.FORMULA_CACHE_REQATTR); Map<Integer, Integer> indexMap = new HashMap<Integer, Integer>(); Map<Integer, Set<String>> midMap = new HashMap<Integer, Set<String>>(); StringBuffer sb = new StringBuffer(); int i; for (Map.Entry<Integer, ModuleDeps> entry : expandedDeps.entrySet()) { ModuleDeps expanded = new ModuleDeps(entry.getValue()); if (features != null) { expanded.resolveWith(features); } if (enclosingDeps != null) { expanded.subtractAll(enclosingDeps); } Set<String> moduleIds = expanded.getModuleIds(formulaCache); indexMap.put(entry.getKey(), expDeps.add(moduleIds.toArray(new String[moduleIds.size()]))); midMap.put(entry.getKey(), moduleIds); } for (i = 0; i < contentFragments.size()-1; i++) { if (isTraceLogging) { log.finest("contentFragment = " + contentFragments.get(i)); log.finest("moduleIds = " + midMap.get(i)); } sb.append(contentFragments.get(i)); boolean isLog = isLogOutput != null && isLogOutput.get(i); if (!isLog) { sb.append(String.format(REQUIRE_EXPANSION_REPLACE_FMT, indexMap.get(expDepIndicies.get(i)))); } else { int j = 0; for (String s : midMap.get(expDepIndicies.get(i))) { sb.append(j++ > 0 ? ", " : "").append(s); } } } sb.append(contentFragments.get(i)); if (isTraceLogging) { log.finest("contentFragment = " + contentFragments.get(i)); } result = sb.toString(); } if (isDebugLogging) { log.exiting(JavaScriptBuildRenderer.class.getName(), methodName, result); } return result; } JavaScriptBuildRenderer(String mid, String content, List<ModuleDeps> depsList, boolean isReqExpLogging); String renderBuild(HttpServletRequest request, Set<String> dependentFeatures);  }
@Test public void testRenderBuild() throws Exception { ModuleDeps deps1 = new ModuleDeps(); ModuleDeps deps2 = new ModuleDeps(); deps1.add("foodep1", new ModuleDepInfo()); deps1.add("foodep2", new ModuleDepInfo()); deps2.add("bardep", new ModuleDepInfo()); List<ModuleDeps> depsList = Arrays.asList(new ModuleDeps[]{deps1, deps2}); JavaScriptBuildRenderer compiled = new JavaScriptBuildRenderer("test", content, depsList, false); ConcurrentListBuilder<String[]> expDeps = new ConcurrentListBuilder<String[]>(); Map<String, Object> requestAttributes = new HashMap<String, Object>(); HttpServletRequest mockRequest = TestUtils.createMockRequest(TestUtils.createMockAggregator(), requestAttributes); EasyMock.replay(mockRequest); requestAttributes.put(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); String result = compiled.renderBuild(mockRequest, Collections.<String>emptySet()); System.out.println(result); Assert.assertEquals("define([],function() {require([\"foo\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][000]);require([\"bar\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][001])});", result); System.out.println(expDeps); Assert.assertEquals(expDeps.size(), 2); Assert.assertEquals(Arrays.asList(new String[]{"foodep1","foodep2"}), Arrays.asList(expDeps.toList().get(0))); Assert.assertEquals(Arrays.asList(new String[]{"bardep"}), Arrays.asList(expDeps.toList().get(1))); requestAttributes.remove(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS); ModuleDeps enclosingDeps = new ModuleDeps(); enclosingDeps.add("foodep2", new ModuleDepInfo()); enclosingDeps.add("bardep", new ModuleDepInfo()); mockRequest.setAttribute(JavaScriptModuleBuilder.EXPANDED_DEPENDENCIES, enclosingDeps); expDeps = new ConcurrentListBuilder<String[]>(); requestAttributes.put(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); result = compiled.renderBuild(mockRequest, Collections.<String>emptySet()); System.out.println(result); Assert.assertEquals("define([],function() {require([\"foo\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][000]);require([\"bar\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][001])});", result); System.out.println(expDeps); Assert.assertEquals(expDeps.size(), 2); Assert.assertEquals(Arrays.asList(new String[]{"foodep1"}), Arrays.asList(expDeps.toList().get(0))); Assert.assertEquals(new ArrayList<String>(), Arrays.asList(expDeps.toList().get(1))); }
@Test public void testRenderBuild_withDetails() throws Exception { String contentWithComments = content + "console.log(\"deps1=" + String.format(JavaScriptBuildRenderer.REQUIRE_EXPANSION_LOG_PLACEHOLDER_FMT, 0) + "\");console.log(\"deps2=" + String.format(JavaScriptBuildRenderer.REQUIRE_EXPANSION_LOG_PLACEHOLDER_FMT, 1) + "\");"; ModuleDeps deps1 = new ModuleDeps(); ModuleDeps deps2 = new ModuleDeps(); deps1.add("foodep1", new ModuleDepInfo()); deps1.add("foodep2", new ModuleDepInfo()); deps2.add("bardep", new ModuleDepInfo()); List<ModuleDeps> depsList = Arrays.asList(new ModuleDeps[]{deps1, deps2, deps1, deps2}); JavaScriptBuildRenderer compiled = new JavaScriptBuildRenderer("test", contentWithComments, depsList, true); ConcurrentListBuilder<String[]> expDeps = new ConcurrentListBuilder<String[]>(); Map<String, Object> requestAttributes = new HashMap<String, Object>(); requestAttributes.put(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); HttpServletRequest mockRequest = TestUtils.createMockRequest(TestUtils.createMockAggregator(), requestAttributes); EasyMock.replay(mockRequest); expDeps = new ConcurrentListBuilder<String[]>(); requestAttributes.put(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); String result = compiled.renderBuild(mockRequest, Collections.<String>emptySet()); System.out.println(result); Assert.assertEquals("define([],function() {require([\"foo\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][000]);require([\"bar\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][001])});console.log(\"deps1=foodep1, foodep2\");console.log(\"deps2=bardep\");", result); Assert.assertEquals(expDeps.size(), 2); Assert.assertEquals(Arrays.asList(new String[]{"foodep1","foodep2"}), Arrays.asList(expDeps.toList().get(0))); Assert.assertEquals(Arrays.asList(new String[]{"bardep"}), Arrays.asList(expDeps.toList().get(1))); requestAttributes.remove(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS); ModuleDeps enclosingDeps = new ModuleDeps(); enclosingDeps.add("foodep2", new ModuleDepInfo()); enclosingDeps.add("bardep", new ModuleDepInfo()); mockRequest.setAttribute(JavaScriptModuleBuilder.EXPANDED_DEPENDENCIES, enclosingDeps); expDeps = new ConcurrentListBuilder<String[]>(); expDeps.add(new String[]{"pad1"}); expDeps.add(new String[]{"pad2"}); requestAttributes.put(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); result = compiled.renderBuild(mockRequest, Collections.<String>emptySet()); System.out.println(result); Assert.assertEquals("define([],function() {require([\"foo\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][002]);require([\"bar\"].concat(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + "[0][003])});console.log(\"deps1=foodep1\");console.log(\"deps2=\");", result); Assert.assertEquals(expDeps.size(), 4); Assert.assertEquals(Arrays.asList(new String[]{"foodep1"}), Arrays.asList(expDeps.toList().get(2))); Assert.assertEquals(new ArrayList<String>(), Arrays.asList(expDeps.toList().get(3))); }