use of com.vaadin.flow.server.frontend.FallbackChunk.CssImportData in project flow by vaadin.
the class UIInternals method addFallbackDependencies.
private void addFallbackDependencies(DependencyInfo dependency) {
if (isFallbackChunkLoaded) {
return;
}
VaadinContext context = ui.getSession().getService().getContext();
FallbackChunk chunk = context.getAttribute(FallbackChunk.class);
if (chunk == null) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Fallback chunk is not available, skipping fallback dependencies load");
}
return;
}
Set<String> modules = chunk.getModules();
Set<CssImportData> cssImportsData = chunk.getCssImports();
if (modules.isEmpty() && cssImportsData.isEmpty()) {
getLogger().debug("Fallback chunk is empty, skipping fallback dependencies load");
return;
}
List<CssImport> cssImports = dependency.getCssImports();
List<JavaScript> javaScripts = dependency.getJavaScripts();
List<JsModule> jsModules = dependency.getJsModules();
if (jsModules.stream().map(JsModule::value).anyMatch(modules::contains)) {
loadFallbackChunk();
return;
}
if (javaScripts.stream().map(JavaScript::value).anyMatch(modules::contains)) {
loadFallbackChunk();
return;
}
if (cssImports.stream().map(this::buildData).anyMatch(cssImportsData::contains)) {
loadFallbackChunk();
return;
}
}
use of com.vaadin.flow.server.frontend.FallbackChunk.CssImportData in project flow by vaadin.
the class FrontendUtils method readFallbackChunk.
/**
* Read fallback chunk data from a json object.
*
* @param object
* json object to read fallback chunk data
* @return a fallback chunk data
*/
public static FallbackChunk readFallbackChunk(JsonObject object) {
if (!object.hasKey(CHUNKS)) {
return null;
}
JsonObject obj = object.getObject(CHUNKS);
if (!obj.hasKey(FALLBACK)) {
return null;
}
obj = obj.getObject(FALLBACK);
List<String> fallbackModles = new ArrayList<>();
JsonArray modules = obj.getArray(JS_MODULES);
for (int i = 0; i < modules.length(); i++) {
fallbackModles.add(modules.getString(i));
}
List<CssImportData> fallbackCss = new ArrayList<>();
JsonArray css = obj.getArray(CSS_IMPORTS);
for (int i = 0; i < css.length(); i++) {
fallbackCss.add(createCssData(css.getObject(i)));
}
return new FallbackChunk(fallbackModles, fallbackCss);
}
use of com.vaadin.flow.server.frontend.FallbackChunk.CssImportData in project flow by vaadin.
the class DeploymentConfigurationFactoryTest method createInitParameters_servletConfigDefinesTokenFile_fallbackChunkObjectIsInInitParams.
@Test
public void createInitParameters_servletConfigDefinesTokenFile_fallbackChunkObjectIsInInitParams() throws IOException {
ServletContext context = Mockito.mock(ServletContext.class);
ServletConfig config = Mockito.mock(ServletConfig.class);
Mockito.when(config.getServletContext()).thenReturn(context);
Mockito.when(config.getInitParameterNames()).thenReturn(Collections.enumeration(Collections.singleton(FrontendUtils.PARAM_TOKEN_FILE)));
File tokenFile = temporaryFolder.newFile();
Mockito.when(config.getInitParameter(FrontendUtils.PARAM_TOKEN_FILE)).thenReturn(tokenFile.getPath());
Files.write(tokenFile.toPath(), Collections.singletonList("{ 'chunks': { " + "'fallback': {" + " 'jsModules': ['foo', 'bar']," + " 'cssImports': [ { 'value' :'foo-value' , 'id': 'bar-id'}]" + "}}" + "}"));
Mockito.when(context.getInitParameter(FrontendUtils.PARAM_TOKEN_FILE)).thenReturn(tokenFile.getPath());
Properties properties = new DeploymentConfigurationFactory().createInitParameters(Object.class, new VaadinServletConfig(config));
Object object = properties.get(DeploymentConfigurationFactory.FALLBACK_CHUNK);
Assert.assertTrue(object instanceof FallbackChunk);
FallbackChunk chunk = (FallbackChunk) object;
Set<String> modules = chunk.getModules();
Assert.assertEquals(2, modules.size());
Assert.assertTrue(modules.contains("foo"));
Assert.assertTrue(modules.contains("bar"));
Set<CssImportData> cssImports = chunk.getCssImports();
Assert.assertEquals(1, cssImports.size());
CssImportData data = cssImports.iterator().next();
Assert.assertEquals("foo-value", data.getValue());
Assert.assertEquals("bar-id", data.getId());
}
Aggregations