Skip to content

Commit

Permalink
Do not process imports with context:// and base:// prefixes when crea…
Browse files Browse the repository at this point in the history
…ting a production build. (#4456)
  • Loading branch information
Kirill Bulatov authored and gilberto-torrezan committed Jul 31, 2018
1 parent f32a7b9 commit 471b67e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public String createShellFile(File targetDirectory) {
protected ThemedURLTranslator getTranslator(File es6SourceDirectory,
ClassPathIntrospector introspector) {
return new ThemedURLTranslator(
url -> new File(es6SourceDirectory, removeFlowPrefixes(url)),
url -> new File(es6SourceDirectory, removeFrontendPrefix(url)),
introspector);
}

Expand Down Expand Up @@ -247,18 +247,22 @@ private Set<File> resolveShellFileImports(File es6SourceDirectory,
.applyTheme(htmlImportUrls));

return annotationValues.values().stream().flatMap(Collection::stream)
.map(this::removeFlowPrefixes)
.filter(this::canBeResolvedInFrontendDirectory)
.map(this::removeFrontendPrefix)
.map(annotationImport -> getFileFromSourceDirectory(
es6SourceDirectory, annotationImport))
.filter(fileInSourceDirectory -> !fragmentFiles
.contains(fileInSourceDirectory))
.collect(Collectors.toSet());
}

private String removeFlowPrefixes(String url) {
return url.replace(ApplicationConstants.CONTEXT_PROTOCOL_PREFIX, "")
.replace(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX, "")
.replace(ApplicationConstants.BASE_PROTOCOL_PREFIX, "");
private boolean canBeResolvedInFrontendDirectory(String url) {
return url.startsWith(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX)
|| !url.contains("://");
}

private String removeFrontendPrefix(String url) {
return url.replace(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX, "");
}

private String createFragmentFile(File targetDirectory, String fragmentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -31,6 +32,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
Expand All @@ -43,6 +45,8 @@
import org.mockito.Mockito;

import com.vaadin.flow.component.dependency.HtmlImport;
import com.vaadin.flow.component.dependency.JavaScript;
import com.vaadin.flow.component.dependency.StyleSheet;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -68,7 +72,7 @@ public class FrontendDataProviderTest {
private File cssFile;
private File htmlFile;

private ThemedURLTranslator translator = Mockito
private final ThemedURLTranslator translator = Mockito
.mock(ThemedURLTranslator.class);

public class TestFrontendDataProvider extends FrontendDataProvider {
Expand Down Expand Up @@ -99,9 +103,8 @@ public void createFrontendSources() throws IOException {
cssFile = createFile(sourceDirectory, "test.css");
htmlFile = createFile(sourceDirectory, "test.html");

Mockito.doAnswer(invocation -> {
return invocation.getArgumentAt(0, Set.class);
}).when(translator).applyTheme(Matchers.any());
Mockito.doAnswer(invocation -> invocation.getArgumentAt(0, Set.class))
.when(translator).applyTheme(Matchers.any());
}

private File createFile(File directory, String fileName)
Expand Down Expand Up @@ -336,6 +339,68 @@ public void userDefinedAndConfigurationFileFragmentsAreMerged()
.extractAnnotationValues(anyMap());
}

@Test
public void importsWithContextOrBaseProtocolAreIgnored()
throws IOException {
List<String> expectedFiles = Arrays.asList("result1.js", "result2.js",
"result1.css", "result2.css", "result1.html", "result2.html");

for (String file : expectedFiles) {
assertTrue("Failed to create a test file",
new File(sourceDirectory, file).createNewFile());
}

AnnotationValuesExtractor annotationValuesExtractorMock = mock(
AnnotationValuesExtractor.class);
when(annotationValuesExtractorMock.extractAnnotationValues(
ImmutableMap.of(StyleSheet.class, ThemedURLTranslator.VALUE,
JavaScript.class, ThemedURLTranslator.VALUE)))
.thenReturn(getCssAndJsImports(expectedFiles));
when(annotationValuesExtractorMock.extractAnnotationValues(Collections
.singletonMap(HtmlImport.class, ThemedURLTranslator.VALUE)))
.thenReturn(getHtmlImports(expectedFiles));

FrontendDataProvider dataProvider = new TestFrontendDataProvider(false,
false, sourceDirectory, annotationValuesExtractorMock, null,
Collections.emptyMap());

String shellFile = dataProvider.createShellFile(targetDirectory);
List<String> shellFileContents = Files.lines(Paths.get(shellFile))
.collect(Collectors.toList());

assertTrue(
"No imports with context:// or base:// prefix should be in the shell file",
shellFileContents.stream()
.noneMatch(line -> line.contains("shouldBeIgnored")));

for (String expectedFile : expectedFiles) {
assertTrue(String.format(
"Regular files or files with frontend:// prefix should be imported in the shell file, but the file '%s' is missing",
expectedFile),
shellFileContents.stream()
.anyMatch(line -> line.contains(expectedFile)));
}
}

private ImmutableMap<Class<? extends Annotation>, Set<String>> getHtmlImports(
List<String> expectedFiles) {
return ImmutableMap.of(HtmlImport.class, ImmutableSet.of(
"context://shouldBeIgnored.html", "base://shouldBeIgnored.html",
"frontend://" + expectedFiles.get(4), expectedFiles.get(5)));
}

private HashMap<Class<? extends Annotation>, Set<String>> getCssAndJsImports(
List<String> expectedFiles) {
return new HashMap<>(ImmutableMap.of(JavaScript.class, ImmutableSet.of(
"context://shouldBeIgnored.js", "base://shouldBeIgnored.js",
"frontend://" + expectedFiles.get(0), expectedFiles.get(1)),
StyleSheet.class,
ImmutableSet.of("context://shouldBeIgnored.css",
"base://shouldBeIgnored.css",
"frontend://" + expectedFiles.get(2),
expectedFiles.get(3))));
}

private void findAndVerifyFragment(Collection<String> outputFragmentNames,
String fragmentNameToFind, Collection<String> expectedImports)
throws IOException {
Expand Down

0 comments on commit 471b67e

Please sign in to comment.