Skip to content

Commit ac643c4

Browse files
authored
fix: Enable use of external css using @import (#10953)
Add fix for using a external css file by having the `@import url(...)` in a css file. Also supports imports with media queries. fixes #10228
1 parent e06014a commit ac643c4

8 files changed

Lines changed: 127 additions & 5 deletions

File tree

‎flow-server/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ The full information would be preferred:
5252

5353
For creating a plugin see [Writing a plugin](https://webpack.js.org/contribute/writing-a-plugin/)
5454

55+
## Working with plugin in a project
56+
57+
Testing a plugin is often easier by using it in a project.
58+
As by default all plugins are re-copied from the `flow-server.jar` making changes
59+
would mean building the server module each time.
60+
61+
The plugins are copied to `target/plugins` from there they are "installed" to `node_modules` with (p)npm.
62+
To make editing and testing easier one can add `"update": false,` to the plugin package json that will make it not be overwritten.
63+
5564
## Using a Flow webpack plugin
5665

5766
The flow plugins get installed to `node_modules/@vaadin` which means that using them we should use the for `@vaadin/${plugin-name}`

‎flow-server/src/main/resources/plugins/application-theme-plugin/theme-generator.js‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,44 @@ const getStyleModule = (id) => {
4747
return cssText;
4848
};
4949
`;
50+
51+
const createLinkReferences = `
52+
const createLinkReferences = (css, target) => {
53+
// Unresolved urls are written as '@import url(text);' to the css
54+
// [0] is the full match
55+
// [1] matches the media query
56+
// [2] matches the url
57+
const importMatcher = /(?:@media\\s(.+?))?(?:\\s{)?\\@import\\surl\\((.+?)\\);(?:})?/g;
58+
59+
var match;
60+
var styleCss = css;
61+
62+
// For each external url import add a link reference
63+
while((match = importMatcher.exec(css)) !== null) {
64+
styleCss = styleCss.replace(match[0], "");
65+
const link = document.createElement('link');
66+
link.rel = 'stylesheet';
67+
link.href = match[2];
68+
if (match[1]) {
69+
link.media = match[1];
70+
}
71+
// For target document append to head else append to target
72+
if (target === document) {
73+
document.head.appendChild(link);
74+
} else {
75+
target.appendChild(link);
76+
}
77+
};
78+
return styleCss;
79+
};
80+
`;
81+
5082
const injectGlobalCssMethod = `
5183
// target: Document | ShadowRoot
5284
export const injectGlobalCss = (css, target, first) => {
85+
5386
const sheet = new CSSStyleSheet();
54-
sheet.replaceSync(css);
87+
sheet.replaceSync(createLinkReferences(css,target));
5588
if (first) {
5689
target.adoptedStyleSheets = [sheet, ...target.adoptedStyleSheets];
5790
} else {
@@ -105,6 +138,7 @@ function generateThemeFile(themeFolder, themeName, themeProperties, productionMo
105138
themeFile += `import {applyTheme as applyBaseTheme} from './theme-${themeProperties.parent}.generated.js';`;
106139
}
107140

141+
themeFile += createLinkReferences;
108142
themeFile += injectGlobalCssMethod;
109143
themeFile += addCssBlockMethod;
110144
themeFile += addStyleIncludeMethod;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.global {
2+
color: blue;
3+
}

‎flow-tests/test-embedding/test-embedding-application-theme/frontend/themes/embedded-theme/document.css‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
@import url('https://fonts.googleapis.com/css?family=Poppins');
2+
@import url('https://fonts.googleapis.com/css?family=Oswald') screen and (orientation:landscape);
3+
4+
@import url("docImport.css");
5+
16
@font-face {
27
font-family: "Ostrich";
38
src: url("./font/ostrich-sans-regular.ttf") format("TrueType");
49
}
5-
6-
.global {
7-
color: blue;
8-
}

‎flow-tests/test-embedding/test-embedding-application-theme/frontend/themes/embedded-theme/styles.css‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@import url('https://fonts.googleapis.com/css?family=Itim');
2+
13
:host {
24
width: 1200px;
35
height: 600px;

‎flow-tests/test-embedding/test-embedding-application-theme/src/test/java/com/vaadin/flow/webcomponent/ApplicationThemeComponentIT.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.vaadin.flow.webcomponent;
1717

18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
1821
import org.junit.Assert;
1922
import org.junit.Ignore;
2023
import org.junit.Test;
@@ -172,4 +175,50 @@ public void documentCssFonts_fromLocalCssFile_fontNotAppliedToEmbeddedComponent(
172175
"Expected no Ostrich font to be applied to embedded component",
173176
(Boolean) ostrichFontStylesFoundForEmbedded);
174177
}
178+
179+
@Test
180+
public void documentCssImport_onlyExternalAddedToHeadAsLink() {
181+
open();
182+
checkLogsForErrors();
183+
184+
final WebElement documentHead = getDriver()
185+
.findElement(By.xpath("/html/head"));
186+
final List<WebElement> links = documentHead
187+
.findElements(By.tagName("link"));
188+
189+
List<String> linkUrls = links.stream()
190+
.map(link -> link.getAttribute("href"))
191+
.collect(Collectors.toList());
192+
Assert.assertTrue("Missing link for external url", linkUrls
193+
.contains("https://fonts.googleapis.com/css?family=Poppins"));
194+
Assert.assertTrue("Link with media query was not found", linkUrls
195+
.contains("https://fonts.googleapis.com/css?family=Oswald"));
196+
Assert.assertFalse("Found import that webpack should have resolved",
197+
linkUrls.contains("docImport.css"));
198+
199+
final List<WebElement> mediaLinks = links.stream()
200+
.filter(link -> link.getAttribute("href").equals(
201+
"https://fonts.googleapis.com/css?family=Oswald"))
202+
.collect(Collectors.toList());
203+
Assert.assertFalse("No expected media link found",
204+
mediaLinks.isEmpty());
205+
Assert.assertEquals("Wrong media attribute contents",
206+
"screen and (orientation:landscape)",
207+
mediaLinks.get(0).getAttribute("media"));
208+
}
209+
210+
@Test
211+
public void stylesCssImport_externalLinkAddedToShadowroot() {
212+
open();
213+
checkLogsForErrors();
214+
final TestBenchElement themedComponent = $("themed-component").first();
215+
final List<WebElement> links = findInShadowRoot(themedComponent,
216+
By.tagName("link"));
217+
218+
List<String> linkUrls = links.stream()
219+
.map(link -> link.getAttribute("href"))
220+
.collect(Collectors.toList());
221+
Assert.assertTrue("Missing link for external url", linkUrls
222+
.contains("https://fonts.googleapis.com/css?family=Itim"));
223+
}
175224
}

‎flow-tests/test-themes/frontend/themes/app-theme/styles.css‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import url('https://fonts.googleapis.com/css?family=Itim');
12
@import 'sub-css/sub.css';
23

34
@font-face {

‎flow-tests/test-themes/src/test/java/com/vaadin/flow/uitest/ui/theme/ThemeIT.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.vaadin.flow.uitest.ui.theme;
1717

18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
1821
import org.junit.Assert;
1922
import org.junit.Test;
2023
import org.openqa.selenium.By;
@@ -261,6 +264,26 @@ public void themeRulesOverrideLumo() {
261264

262265
}
263266

267+
@Test
268+
public void documentCssImport_onlyExternalAddedToHeadAsLink() {
269+
open();
270+
checkLogsForErrors();
271+
272+
final WebElement documentHead = getDriver()
273+
.findElement(By.xpath("/html/head"));
274+
final List<WebElement> links = documentHead
275+
.findElements(By.tagName("link"));
276+
277+
List<String> linkUrls = links.stream()
278+
.map(link -> link.getAttribute("href"))
279+
.collect(Collectors.toList());
280+
281+
Assert.assertTrue("Missing link for external url", linkUrls
282+
.contains("https://fonts.googleapis.com/css?family=Itim"));
283+
Assert.assertFalse("Found import that webpack should have resolved",
284+
linkUrls.contains("sub-css/sub.css"));
285+
}
286+
264287
@Override
265288
protected String getTestPath() {
266289
String path = super.getTestPath();

0 commit comments

Comments
 (0)