Skip to content

Commit 7a5d192

Browse files
authored
fix: Not throw for not yet copied CSS when checking for re-bundling (CP: 24.0) (#16142)
* fix: Not throw for not yet copied CSS when checking for re-bundling * Add tests for checking no exception and styles are applied * fix formatting * fix expected hash
1 parent c49006e commit 7a5d192

12 files changed

Lines changed: 153 additions & 23 deletions

File tree

flow-server/src/main/java/com/vaadin/flow/server/frontend/AbstractUpdateImports.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,19 @@ private boolean isFileOrDirectory(File base, String... path) {
500500
return file.isFile() || file.isDirectory();
501501
}
502502

503-
private boolean addCssLines(Collection<String> lines, CssData cssData,
503+
/**
504+
* Adds CSS imports to the generated flow imports file based on the given
505+
* CssImport data.
506+
*
507+
* @param lines
508+
* collection of generated file lines to add imports to
509+
* @param cssData
510+
* CssImport data
511+
* @param i
512+
* imported CSS counter
513+
* @return true if the imported CSS files does exist, false otherwise
514+
*/
515+
protected boolean addCssLines(Collection<String> lines, CssData cssData,
504516
int i) {
505517
String cssFile = resolveResource(cssData.getValue());
506518
boolean found = importedFileExists(cssFile);

flow-server/src/main/java/com/vaadin/flow/server/frontend/GenerateMainImports.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public List<String> getLines() {
7272
return lines;
7373
}
7474

75+
@Override
76+
protected boolean addCssLines(Collection<String> lines, CssData cssData,
77+
int i) {
78+
super.addCssLines(lines, cssData, i);
79+
// CSS files in 'generated/jar-resources' are not generated at this
80+
// moment, so not let the application interrupt and continue with
81+
// checking the dev-bundle
82+
return true;
83+
}
84+
7585
@Override
7686
protected void writeImportLines(List<String> lines) {
7787
// NO-OP. Only store the lines to write

flow-server/src/main/java/com/vaadin/flow/server/frontend/scanner/CssAnnotationVisitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public void visit(String name, Object obj) {
5757
newData();
5858
}
5959
if (FrontendClassVisitor.VALUE.equals(name)) {
60-
cssData.value = value;
60+
cssData.setValue(value);
6161
} else if (FrontendClassVisitor.ID.equals(name)) {
62-
cssData.id = value;
62+
cssData.setId(value);
6363
} else if (FrontendClassVisitor.INCLUDE.equals(name)) {
64-
cssData.include = value;
64+
cssData.setInclude(value);
6565
} else if (FrontendClassVisitor.THEME_FOR.equals(name)) {
66-
cssData.themefor = value;
66+
cssData.setThemefor(value);
6767
}
6868
}
6969

flow-server/src/main/java/com/vaadin/flow/server/frontend/scanner/CssData.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@
2828
* @since 2.0
2929
*/
3030
public final class CssData implements Serializable {
31-
String value;
32-
String id;
33-
String include;
34-
String themefor;
31+
private String value;
32+
private String id;
33+
private String include;
34+
private String themefor;
35+
36+
public CssData() {
37+
}
38+
39+
public CssData(String value, String id, String include, String themefor) {
40+
this.value = value;
41+
this.id = id;
42+
this.include = include;
43+
this.themefor = themefor;
44+
}
3545

3646
/**
3747
* The value getter.
@@ -69,6 +79,22 @@ public String getThemefor() {
6979
return themefor;
7080
}
7181

82+
void setValue(String value) {
83+
this.value = value;
84+
}
85+
86+
void setId(String id) {
87+
this.id = id;
88+
}
89+
90+
void setInclude(String include) {
91+
this.include = include;
92+
}
93+
94+
void setThemefor(String themefor) {
95+
this.themefor = themefor;
96+
}
97+
7298
@Override
7399
public boolean equals(Object other) {
74100
if (other == null || !(other instanceof CssData)) {

flow-server/src/main/java/com/vaadin/flow/server/frontend/scanner/FullDependenciesScanner.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,11 @@ public Set<String> getClasses() {
207207
}
208208

209209
private CssData createCssData(Annotation cssImport) {
210-
CssData data = new CssData();
211-
data.id = adaptCssValue(cssImport, "id");
212-
data.include = adaptCssValue(cssImport, "include");
213-
data.themefor = adaptCssValue(cssImport, "themeFor");
214-
data.value = adaptCssValue(cssImport, VALUE);
215-
return data;
210+
String id = adaptCssValue(cssImport, "id");
211+
String include = adaptCssValue(cssImport, "include");
212+
String themeFor = adaptCssValue(cssImport, "themeFor");
213+
String value = adaptCssValue(cssImport, VALUE);
214+
return new CssData(value, id, include, themeFor);
216215
}
217216

218217
private String adaptCssValue(Annotation cssImport, String method) {

flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskRunDevBundleBuildTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.vaadin.flow.di.Lookup;
2020
import com.vaadin.flow.server.Constants;
2121
import com.vaadin.flow.server.frontend.scanner.ClassFinder;
22+
import com.vaadin.flow.server.frontend.scanner.CssData;
2223
import com.vaadin.flow.server.frontend.scanner.FrontendDependenciesScanner;
2324
import com.vaadin.flow.testutil.TestUtils;
2425
import com.vaadin.flow.theme.ThemeDefinition;
@@ -1701,6 +1702,40 @@ public void standardVaadinComponent_notAddedToProjectAsJar_noRebuildRequired()
17011702
}
17021703
}
17031704

1705+
@Test
1706+
public void cssImport_cssInMetaInfResources_notThrow_bundleRequired()
1707+
throws IOException {
1708+
createPackageJsonStub(BLANK_PACKAGE_JSON_WITH_HASH);
1709+
1710+
final FrontendDependenciesScanner depScanner = Mockito
1711+
.mock(FrontendDependenciesScanner.class);
1712+
1713+
CssData cssData = new CssData("./addons-styles/my-styles.css", null,
1714+
null, null);
1715+
1716+
Mockito.when(depScanner.getCss())
1717+
.thenReturn(Collections.singleton(cssData));
1718+
1719+
JsonObject stats = getBasicStats();
1720+
1721+
try (MockedStatic<FrontendUtils> utils = Mockito
1722+
.mockStatic(FrontendUtils.class)) {
1723+
utils.when(() -> FrontendUtils.getDevBundleFolder(Mockito.any()))
1724+
.thenReturn(temporaryFolder.getRoot());
1725+
utils.when(() -> FrontendUtils
1726+
.findBundleStatsJson(temporaryFolder.getRoot()))
1727+
.thenReturn(stats.toJson());
1728+
1729+
// Should not throw an IllegalStateException:
1730+
// "Failed to find the following css files in the...."
1731+
boolean needsBuild = TaskRunDevBundleBuild
1732+
.needsBuildInternal(options, depScanner, finder);
1733+
Assert.assertTrue(
1734+
"Should re-bundle if CSS is imported from META-INF/resources",
1735+
needsBuild);
1736+
}
1737+
}
1738+
17041739
@Test
17051740
public void flowFrontendPackageInPackageJson_noBundleRebuild()
17061741
throws IOException {

flow-server/src/test/java/com/vaadin/flow/server/frontend/scanner/FullDependenciesScannerTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,7 @@ public void getModules_explcitTheme_returnAllModulesExcludingNotUsedTheme_getCla
342342

343343
private CssData createCssData(String value, String id, String include,
344344
String themefor) {
345-
CssData data = new CssData();
346-
data.value = value;
347-
data.id = id;
348-
data.include = include;
349-
data.themefor = themefor;
350-
return data;
345+
return new CssData(value, id, include, themefor);
351346
}
352347

353348
private List<? extends Annotation> findAnnotations(Class<?> type,

flow-server/src/test/java/com/vaadin/flow/server/frontend/scanner/ScannerDependenciesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void visitRouteEntryPoint_ExpectToAlsoVisitImplementedInterface() {
5353
assertEquals("There should be 1 css import", 1, deps.getCss().size());
5454

5555
assertEquals("Invalid css import", "frontend://styles/interface.css",
56-
deps.getCss().iterator().next().value);
56+
deps.getCss().iterator().next().getValue());
5757
}
5858

5959
@Test

flow-tests/test-express-build/test-dev-bundle-frontend-add-on/src/main/java/com/vaadin/flow/frontend/DevBundleCssImportView.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
@CssImport("./styles/my-styles.css")
2525
public class DevBundleCssImportView extends Div {
2626

27+
static final String MY_COMPONENT_ID = "test-css-import-meta-inf-resources-span";
28+
2729
static final String SPAN_ID = "test-css-import-frontend-span";
2830

2931
public DevBundleCssImportView() {
3032
Span span = new Span("Test CssImport with dev bundle");
3133
span.setId(SPAN_ID);
3234
add(span);
35+
36+
MyComponent myComponent = new MyComponent();
37+
myComponent.setId(MY_COMPONENT_ID);
38+
add(myComponent);
3339
}
3440
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vaadin.flow.frontend;
2+
3+
import com.vaadin.flow.component.dependency.CssImport;
4+
import com.vaadin.flow.component.html.Div;
5+
6+
@CssImport("./addons-styles/add-on-styles.css")
7+
public class MyComponent extends Div {
8+
9+
public MyComponent() {
10+
setText("Test CssImport from META-INF/resources with dev bundle");
11+
addClassName("my-component");
12+
}
13+
}

0 commit comments

Comments
 (0)