JavaScriptModuleBuilder implements IModuleBuilder, IExtensionInitializer, ILayerListener, IShutdownListener, IConfigListener { protected String moduleNameIdEncodingEndLayer(HttpServletRequest request, List<IModule> modules) { IAggregator aggr = (IAggregator)request.getAttribute(IAggregator.AGGREGATOR_REQATTRNAME); String result = ""; @SuppressWarnings("unchecked") ConcurrentListBuilder<String[]> expDepsBuilder = (ConcurrentListBuilder<String[]>)request.getAttribute(MODULE_EXPANDED_DEPS); IRequestedModuleNames requestedModules = (IRequestedModuleNames)request.getAttribute(IHttpTransport.REQUESTEDMODULENAMES_REQATTRNAME); if (expDepsBuilder == null) { expDepsBuilder = new ConcurrentListBuilder<String[]>(); } boolean noAMD = false; if (requestedModules != null) { try { if (!requestedModules.getDeps().isEmpty()) { expDepsBuilder.add(requestedModules.getDeps().toArray(new String[requestedModules.getDeps().size()])); } if (!requestedModules.getPreloads().isEmpty()) { expDepsBuilder.add(requestedModules.getPreloads().toArray(new String[requestedModules.getPreloads().size()])); } if (RequestUtil.isServerExpandedLayers(request) && !requestedModules.getModules().isEmpty()) { expDepsBuilder.add(requestedModules.getModules().toArray(new String[requestedModules.getModules().size()])); } noAMD = requestedModules.getDeps().isEmpty() && requestedModules.getPreloads().isEmpty() && requestedModules.getModules().isEmpty(); } catch (IOException ignore) { } } List<String[]> expDeps = expDepsBuilder.toList(); StringBuffer sb = new StringBuffer(); Map<String, Integer> idMap = null; if (!aggr.getOptions().isDisableModuleNameIdEncoding()) { idMap = aggr.getTransport().getModuleIdMap(); } if (RequestUtil.isExplodeRequires(request)) { if (expDeps.size() > 0) { sb.append(EXPDEPS_VARNAME).append("=") .append(generateModuleIdReg(expDeps, idMap)) .append(";"); if (idMap != null) { sb.append(aggr.getTransport().getModuleIdRegFunctionName()) .append("(") .append(EXPDEPS_VARNAME) .append(");"); } } } else if (expDeps.size() > 0 && idMap != null) { sb.append(aggr.getTransport().getModuleIdRegFunctionName()).append("(") .append(generateModuleIdReg(expDeps, idMap)) .append(");"); } if (!noAMD) { sb.append("})();"); } result = sb.toString(); return result; }  static CompilationLevel getCompilationLevel(HttpServletRequest request); @Override void initialize(IAggregator aggregator,
			IAggregatorExtension extension, IExtensionRegistrar registrar); @Override String layerBeginEndNotifier(EventType type, HttpServletRequest request, List<IModule> modules, Set<String> dependentFeatures); @Override void shutdown(IAggregator aggregator); @Override void configLoaded(IConfig config, long sequence); @Override ModuleBuild build(
			String mid,
			IResource resource,
			HttpServletRequest request,
			List<ICacheKeyGenerator> keyGens
			); @Override List<ICacheKeyGenerator> getCacheKeyGenerators(IAggregator aggregator); @Override boolean handles(String mid, IResource resource); @Override boolean isScript(HttpServletRequest request); static final String MODULE_EXPANDED_DEPS; }
@Test public void testModuleNameEncodingIdEndLayer() throws Exception { List<IModule> modules = new ArrayList<IModule>(); moduleIdMap[0] = new HashMap<String, Integer>(); TestJavaScriptModuleBuilder builder = new TestJavaScriptModuleBuilder() { @Override protected String generateModuleIdReg(List<String[]> expDeps, Map<String, Integer> idMap) { StringBuffer sb = new StringBuffer("["); for (String[] names : expDeps) { sb.append(Arrays.asList(names).toString()); } sb.append("]"); return sb.toString(); } }; Assert.assertEquals("})();", builder.moduleNameIdEncodingEndLayer(mockRequest, modules)); requestAttributes.put(IHttpTransport.EXPANDREQUIRELISTS_REQATTRNAME, true); ConcurrentListBuilder<String[]> expDeps = new ConcurrentListBuilder<String[]>(); mockRequest.setAttribute(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); MockRequestedModuleNames reqNames = new MockRequestedModuleNames(); String result = builder.moduleNameIdEncodingEndLayer(mockRequest, modules); Assert.assertEquals("})();", result); System.out.println(result); expDeps.add(new String[]{"dep1"}); result = builder.moduleNameIdEncodingEndLayer(mockRequest, modules); System.out.println(result); Assert.assertEquals(JavaScriptModuleBuilder.EXPDEPS_VARNAME + "=[[dep1]];require.combo.reg(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + ");})();", result); expDeps = new ConcurrentListBuilder<String[]>(); mockRequest.setAttribute(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); expDeps.add(new String[]{"dep1", "dep2"}); expDeps.add(new String[]{"foodep"}); result = builder.moduleNameIdEncodingEndLayer(mockRequest, modules); System.out.println(result); Assert.assertEquals(JavaScriptModuleBuilder.EXPDEPS_VARNAME + "=[[dep1, dep2][foodep]];require.combo.reg(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + ");})();", result); mockRequest.setAttribute(IHttpTransport.REQUESTEDMODULENAMES_REQATTRNAME, reqNames); reqNames.setDeps(Arrays.asList("bootLayerDep")); reqNames.setPreloads(Arrays.asList("bootLayerPreload")); reqNames.setModules(Arrays.asList("module1", "module2")); result = builder.moduleNameIdEncodingEndLayer(mockRequest, modules); System.out.println(result); Assert.assertEquals(JavaScriptModuleBuilder.EXPDEPS_VARNAME + "=[[dep1, dep2][foodep][bootLayerDep][bootLayerPreload]];require.combo.reg(" + JavaScriptModuleBuilder.EXPDEPS_VARNAME + ");})();", result); expDeps = new ConcurrentListBuilder<String[]>(); mockRequest.setAttribute(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS, expDeps); mockRequest.setAttribute(IHttpTransport.SERVEREXPANDLAYERS_REQATTRNAME, true); result = builder.moduleNameIdEncodingEndLayer(mockRequest, modules); System.out.println(result); Assert.assertEquals("require.combo.reg([[bootLayerDep][bootLayerPreload][module1, module2]]);})();", result); mockRequest.removeAttribute(JavaScriptModuleBuilder.MODULE_EXPANDED_DEPS); reqNames = new MockRequestedModuleNames(); reqNames.setScripts(Arrays.asList("script1")); mockRequest.setAttribute(IHttpTransport.REQUESTEDMODULENAMES_REQATTRNAME, reqNames); result = builder.moduleNameIdEncodingEndLayer(mockRequest, modules); Assert.assertEquals("", result); }