Skip to content

Commit

Permalink
feat: configurable theme.json autoInjectComponents property (#15154)
Browse files Browse the repository at this point in the history
* feat: added support for theme.json autoInjectComponents flag

Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
  • Loading branch information
MarcinVaadin and vursen committed Nov 18, 2022
1 parent 0d98cf7 commit d846c8b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,22 @@ export const injectGlobalCss = (css, target, first) => {
function generateThemeFile(themeFolder, themeName, themeProperties, productionMode) {
const styles = path.resolve(themeFolder, stylesCssFile);
const document = path.resolve(themeFolder, documentCssFile);
const componentsFiles = glob.sync('*.css', {
cwd: path.resolve(themeFolder, themeComponentsFolder),
nodir: true
});

const autoInjectComponents = themeProperties.autoInjectComponents ?? true;
let themeFile = headerImport;
var componentsFiles;

if (autoInjectComponents) {
componentsFiles = glob.sync('*.css', {
cwd: path.resolve(themeFolder, themeComponentsFolder),
nodir: true
});

if (componentsFiles.length > 0) {
themeFile += "import { unsafeCSS, registerStyles } from '@vaadin/vaadin-themable-mixin/register-styles';\n";
if (componentsFiles.length > 0) {
themeFile += "import { unsafeCSS, registerStyles } from '@vaadin/vaadin-themable-mixin/register-styles';\n";
}
}


if (themeProperties.parent) {
themeFile += `import {applyTheme as applyBaseTheme} from './theme-${themeProperties.parent}.generated.js';\n`;
}
Expand Down Expand Up @@ -213,19 +218,21 @@ function generateThemeFile(themeFolder, themeName, themeProperties, productionMo
});
}

componentsFiles.forEach((componentCss) => {
const filename = path.basename(componentCss);
const tag = filename.replace('.css', '');
const variable = camelCase(filename);
imports.push(`import ${variable} from 'themes/${themeName}/${themeComponentsFolder}/${filename}?inline';\n`);
// Don't format as the generated file formatting will get wonky!
const componentString = `registerStyles(
'${tag}',
unsafeCSS(${variable}.toString())
);
`;
componentCssCode.push(componentString);
});
if (autoInjectComponents) {
componentsFiles.forEach((componentCss) => {
const filename = path.basename(componentCss);
const tag = filename.replace('.css', '');
const variable = camelCase(filename);
imports.push(`import ${variable} from 'themes/${themeName}/${themeComponentsFolder}/${filename}?inline';\n`);
// Don't format as the generated file formatting will get wonky!
const componentString = `registerStyles(
'${tag}',
unsafeCSS(${variable}.toString())
);
`;
componentCssCode.push(componentString);
});
}

themeFile += imports.join('');
themeFile += `
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[part="input-field"] {
border: 10px solid rgb(255, 0, 0);
}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
{"lumoImports":["typography","color","spacing","badge","utility"]}
{
"lumoImports":["typography","color","spacing","badge","utility"],
"autoInjectComponents": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public void cssImportAnnotationForComponent() {
Assert.assertEquals("rgb(173, 216, 230)", fieldBackground);
}

@Test
public void autoInjectComponentsIsFalse_cssNotImported() {
String fieldBorder = (String) executeScript(
"return getComputedStyle(document.querySelector('#themedfield').shadowRoot.querySelector('[part=input-field]')).border");
Assert.assertNotEquals("10px solid rgb(255, 0, 0)", fieldBorder);
}

@Test
public void documentCssImport_externalAddedToHeadAsLink() {
checkLogsForErrors();
Expand Down

0 comments on commit d846c8b

Please sign in to comment.