Search in sources :

Example 1 with CssImportData

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;
    }
}
Also used : JsModule(com.vaadin.flow.component.dependency.JsModule) CssImport(com.vaadin.flow.component.dependency.CssImport) VaadinContext(com.vaadin.flow.server.VaadinContext) FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) JavaScript(com.vaadin.flow.component.dependency.JavaScript) CssImportData(com.vaadin.flow.server.frontend.FallbackChunk.CssImportData)

Example 2 with CssImportData

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);
}
Also used : JsonArray(elemental.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(elemental.json.JsonObject) CssImportData(com.vaadin.flow.server.frontend.FallbackChunk.CssImportData)

Example 3 with CssImportData

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());
}
Also used : FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) CssImportData(com.vaadin.flow.server.frontend.FallbackChunk.CssImportData) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Aggregations

CssImportData (com.vaadin.flow.server.frontend.FallbackChunk.CssImportData)3 FallbackChunk (com.vaadin.flow.server.frontend.FallbackChunk)2 CssImport (com.vaadin.flow.component.dependency.CssImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 JsModule (com.vaadin.flow.component.dependency.JsModule)1 VaadinContext (com.vaadin.flow.server.VaadinContext)1 JsonArray (elemental.json.JsonArray)1 JsonObject (elemental.json.JsonObject)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ServletConfig (javax.servlet.ServletConfig)1 ServletContext (javax.servlet.ServletContext)1 Test (org.junit.Test)1