Skip to content

Commit e24c71c

Browse files
authored
fix: add missing registerStyles import to generated app-shell-imports.js (#23975)
The generated `app-shell-imports.js` file was missing the `THEMABLE_MIXIN_IMPORT` line (`import { css, unsafeCSS, registerStyles }`), causing `ReferenceError: registerStyles is not defined` at startup in production mode when `@CssImport` with `themeFor` was used on an `AppShellConfigurator`. Also ensures import statements are moved to the top of the generated app shell file, consistent with other generated import files. Fixes #23689
1 parent 91c9896 commit e24c71c

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ private static List<String> removeDuplicateCssImports(List<String> lines) {
342342

343343
// Move all import lines to the top, before any non-import lines
344344
private static void moveImportsToTop(List<String> lines) {
345-
List<String> imports = new ArrayList<>(lines);
346-
imports.removeIf(line -> !line.startsWith("import "));
347-
lines.removeIf(line -> line.startsWith("import "));
348-
lines.addAll(0, imports);
345+
if (!lines.isEmpty()) {
346+
List<String> imports = new ArrayList<>(lines);
347+
imports.removeIf(line -> !line.startsWith("import "));
348+
lines.removeIf(line -> line.startsWith("import "));
349+
lines.addAll(0, imports);
350+
}
349351
}
350352

351353
private void writeWebComponentImports(List<String> lines) {
@@ -488,6 +490,7 @@ private Map<File, List<String>> process(Map<ChunkInfo, List<CssData>> css,
488490
cssLineOffset += appShellCssLines.size();
489491
if (!appShellCssLines.isEmpty()) {
490492
appShellLines.add(IMPORT_INJECT);
493+
appShellLines.add(THEMABLE_MIXIN_IMPORT);
491494
appShellLines.addAll(appShellCssLines);
492495
}
493496
if (FrontendBuildUtils.isTailwindCssEnabled(options)) {
@@ -499,6 +502,7 @@ private Map<File, List<String>> process(Map<ChunkInfo, List<CssData>> css,
499502
+ "/" + FrontendUtils.TAILWIND_JS;
500503
appShellLines.add(String.format(IMPORT_TEMPLATE, importPath));
501504
}
505+
moveImportsToTop(appShellLines);
502506
files.put(appShellImports, appShellLines);
503507
files.put(appShellDefinitions, Collections.singletonList("export {}"));
504508

flow-build-tools/src/test/java/com/vaadin/flow/server/frontend/AbstractUpdateImportsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public static class FooCssImport2 extends Component {
112112
public static class ThemeCssImport implements AppShellConfigurator {
113113
}
114114

115+
@CssImport("./foo.css")
116+
@CssImport("./bar.css")
117+
public static class MultiCssImportAppShell implements AppShellConfigurator {
118+
}
119+
115120
@CssImport("./foo.css")
116121
public static class CssImportExporter
117122
extends WebComponentExporter<FooCssImport> {
@@ -432,6 +437,8 @@ void generateLines_resultingLinesContainsThemeLinesAndExpectedImportsAndCssLines
432437
// AppShell and @Theme CSS imports are expected to be generated in
433438
// the dedicated file.
434439
List<String> expectedAppShellImports = List.of(
440+
"import \\{ injectGlobalCss \\}.*",
441+
"import \\{ css, unsafeCSS, registerStyles \\}.*",
435442
"import \\$cssFromFile_\\d from 'lumo-css-import.css\\?inline';",
436443
"injectGlobalCss\\(\\$cssFromFile_\\d.toString\\(\\), 'CSSImport end', document\\);");
437444

@@ -934,6 +941,19 @@ void generatedFlowWebComponentImports_importsAreOnTopBeforeOtherInstructions()
934941
assertImportsBeforeNonImportLines(lines);
935942
}
936943

944+
@Test
945+
void generatedAppShellImports_importsAreOnTopBeforeOtherInstructions()
946+
throws Exception {
947+
Class<?>[] testClasses = { MultiCssImportAppShell.class };
948+
ClassFinder classFinder = getClassFinder(testClasses);
949+
updater = new UpdateImports(getScanner(classFinder), options);
950+
updater.run();
951+
952+
List<String> lines = updater.getOutput().get(updater.appShellImports);
953+
assertNotNull(lines, "App shell imports should have been generated");
954+
assertImportsBeforeNonImportLines(lines);
955+
}
956+
937957
@Test
938958
void duplicateCssImportInWebComponentAndEager_importedOnlyOnce()
939959
throws Exception {

0 commit comments

Comments
 (0)