JavaScriptModuleBuilder implements IModuleBuilder, IExtensionInitializer, ILayerListener, IShutdownListener, IConfigListener { @Override public String layerBeginEndNotifier(EventType type, HttpServletRequest request, List<IModule> modules, Set<String> dependentFeatures) { String result = null; if (type == EventType.BEGIN_LAYER) { StringBuffer sb = new StringBuffer(); if (RequestUtil.isExplodeRequires(request)) { request.setAttribute(MODULE_EXPANDED_DEPS, new ConcurrentListBuilder<String[]>(modules.size())); request.setAttribute(FORMULA_CACHE_REQATTR, new ConcurrentHashMap<Object, Object>()); boolean isReqExpLogging = RequestUtil.isDependencyExpansionLogging(request); IRequestedModuleNames requestedModuleNames = (IRequestedModuleNames)request.getAttribute(IHttpTransport.REQUESTEDMODULENAMES_REQATTRNAME); List<String> moduleIds = new ArrayList<String>(modules.size()); List<String> excludeIds = Collections.emptyList(); try { excludeIds = requestedModuleNames != null ? requestedModuleNames.getExcludes() : Collections.<String>emptyList(); } catch (IOException ignore) { } for (IModule module : modules) { moduleIds.add(module.getModuleId()); } IAggregator aggr = (IAggregator)request.getAttribute(IAggregator.AGGREGATOR_REQATTRNAME); Features features = (Features)request.getAttribute(IHttpTransport.FEATUREMAP_REQATTRNAME); DependencyList excludeList = newDependencyList( DEPSOURCE_REQEXPEXCLUDES, excludeIds, aggr, features, true, isReqExpLogging); DependencyList layerDepList = newDependencyList( DEPSOURCE_LAYER, moduleIds, aggr, features, false, isReqExpLogging); ModuleDeps excludeDeps = new ModuleDeps(); ModuleDeps layerDeps = new ModuleDeps(); try { excludeDeps.addAll(excludeList.getExplicitDeps()); excludeDeps.addAll(excludeList.getExpandedDeps()); layerDeps.addAll(layerDepList.getExplicitDeps()); layerDeps.addAll(layerDepList.getExpandedDeps()); dependentFeatures.addAll(excludeList.getDependentFeatures()); dependentFeatures.addAll(layerDepList.getDependentFeatures()); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } if (isReqExpLogging) { sb.append("console.log(\"%c" + Messages.JavaScriptModuleBuilder_4 + "\", \"color:blue;background-color:yellow\");"); sb.append("console.log(\"%c" + Messages.JavaScriptModuleBuilder_2 + "\", \"color:blue\");") .append("console.log(\"%c"); for (Map.Entry<String, String> entry : excludeDeps.getModuleIdsWithComments().entrySet()) { sb.append("\t" + entry.getKey() + " (" + entry.getValue() + ")\\r\\n"); } sb.append("\", \"font-size:x-small\");"); sb.append("console.log(\"%c" + Messages.JavaScriptModuleBuilder_3 + "\", \"color:blue\");") .append("console.log(\"%c"); for (Map.Entry<String, String> entry : layerDeps.getModuleIdsWithComments().entrySet()) { sb.append("\t" + entry.getKey() + " (" + entry.getValue() + ")\\r\\n"); } sb.append("\", \"font-size:x-small\");"); result = sb.toString(); } layerDeps.addAll(excludeDeps); ModuleDeps resolvedDeps = new ModuleDeps(); layerDeps.simplify((Map<?,?>)request.getAttribute(JavaScriptModuleBuilder.FORMULA_CACHE_REQATTR)); for (Map.Entry<String, ModuleDepInfo> entry : layerDeps.resolveWith(Features.emptyFeatures).entrySet()) { if (entry.getValue().containsTerm(BooleanTerm.TRUE)) { resolvedDeps.add(entry.getKey(), entry.getValue()); } } request.setAttribute(EXPANDED_DEPENDENCIES, resolvedDeps); } result = sb.append(moduleNameIdEncodingBeginLayer(request)).toString(); } else if (type == EventType.BEGIN_AMD) { result = moduleNameIdEncodingBeginAMD(request); } else if (type == EventType.END_LAYER) { result = moduleNameIdEncodingEndLayer(request, modules); Map<?, ?> formulaCache = (Map<?, ?>)request.getAttribute(FORMULA_CACHE_REQATTR); if (formulaCache != null) { formulaCache.clear(); } } 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 testLayerBeginEndNotifier_disableExportModuleNames() throws Exception { List<IModule> modules = new ArrayList<IModule>(); Set<String> dependentFeatures = new HashSet<String>(); JavaScriptModuleBuilder builder = new JavaScriptModuleBuilder(); mockRequest.setAttribute(IHttpTransport.EXPORTMODULENAMES_REQATTRNAME, Boolean.TRUE); String result = builder.layerBeginEndNotifier(EventType.BEGIN_LAYER, mockRequest, modules, dependentFeatures); Assert.assertEquals("", result); Assert.assertTrue(TypeUtil.asBoolean(mockRequest.getAttribute(IHttpTransport.EXPORTMODULENAMES_REQATTRNAME))); }
@Test public void testLayerBeginEndNotifier_moduleIdEncoding() { List<IModule> modules = new ArrayList<IModule>(); Set<String> dependentFeatures = new HashSet<String>(); JavaScriptModuleBuilder builder = new JavaScriptModuleBuilder() { @Override protected String moduleNameIdEncodingBeginLayer(HttpServletRequest request) { return "[moduleNameIdEncodingBeginLayer]"; } @Override protected String moduleNameIdEncodingBeginAMD(HttpServletRequest request) { return "[moduleNameIdEncodingBeginAMD]"; } @Override protected String moduleNameIdEncodingEndLayer(HttpServletRequest request, List<IModule> modules) { return "[moduleNameIdEncodingEndLayer]"; } }; String result = builder.layerBeginEndNotifier(EventType.BEGIN_LAYER, mockRequest, modules, dependentFeatures); Assert.assertEquals("[moduleNameIdEncodingBeginLayer]", result); result = builder.layerBeginEndNotifier(EventType.BEGIN_AMD, mockRequest, modules, dependentFeatures); Assert.assertEquals("[moduleNameIdEncodingBeginAMD]", result); result = builder.layerBeginEndNotifier(EventType.END_LAYER, mockRequest, modules, dependentFeatures); Assert.assertEquals("[moduleNameIdEncodingEndLayer]", result); }