Skip to content

Commit

Permalink
Feat: Check that npm resources are available (#9617)
Browse files Browse the repository at this point in the history
Check that we have named npm resources
available or throw an exception informing
the user to install it using a NpmPackage
annotation or manually with (p)npm i

Fixes #9615
# Conflicts:
#	flow-tests/test-embedding/test-embedding-application-theme/src/main/java/com/vaadin/flow/webcomponent/ThemedComponentExporter.java
  • Loading branch information
caalador committed Mar 4, 2021
1 parent dc794b7 commit 3ce7e16
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const fs = require('fs');
const path = require('path');
const generateThemeFile = require('./theme-generator');
const copyStaticAssets = require('./theme-copy');
const { copyStaticAssets } = require('./theme-copy');

let logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ function copyStaticAssets(themeName, themeProperties, projectStaticAssetsOutputF
fs.mkdirSync(projectStaticAssetsOutputFolder, {
recursive: true
});
const missingModules = checkModules(Object.keys(assets));
if (missingModules.length > 0) {
throw Error("Missing npm modules '" + missingModules.join("', '")
+ "' for assets marked in 'theme.json'.\n" +
"Install package(s) by adding a @NpmPackage annotation or install it using 'npm/pnpm i'");
}
Object.keys(assets).forEach((module) => {

const copyRules = assets[module];
Expand All @@ -84,4 +90,16 @@ function copyStaticAssets(themeName, themeProperties, projectStaticAssetsOutputF
});
};

module.exports = copyStaticAssets;
function checkModules(modules) {
const missing = [];

modules.forEach((module) => {
if (!fs.existsSync(path.resolve('node_modules/', module))) {
missing.push(module);
}
});

return missing;
}

module.exports = {checkModules, copyStaticAssets};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const { checkModules } = require('./theme-copy');

// Special folder inside a theme for component themes that go inside the component shadow root
const themeComponentsFolder = 'components';
Expand Down Expand Up @@ -82,6 +84,13 @@ function generateThemeFile(themeFolder, themeName, themeProperties) {

let i = 0;
if (themeProperties.importCss) {
const missingModules = checkModules(themeProperties.importCss);
if(missingModules.length > 0) {
throw Error("Missing npm modules or files '" + missingModules.join("', '")
+ "' for importCss marked in 'theme.json'.\n" +
"Install or update package(s) by adding a @NpmPackage annotation or install it using 'npm/pnpm i'");

}
themeProperties.importCss.forEach((cssPath) => {
const variable = 'module' + i++;
imports.push(`import ${variable} from '${cssPath}';\n`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package com.vaadin.flow.webcomponent;

import com.vaadin.flow.component.WebComponentExporter;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.component.webcomponent.WebComponent;
import com.vaadin.flow.theme.Theme;

@Theme(themeFolder = "embedded-theme")
@NpmPackage(value = "@fortawesome/fontawesome-free", version = "5.15.1")
public class ThemedComponentExporter
extends WebComponentExporter<ThemedComponent> {
public ThemedComponentExporter() {
Expand Down
2 changes: 2 additions & 0 deletions flow-tests/test-themes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
To test #9615 change the importCss and/or asset to a non installed npm module,
execute `mvn verify` and verify the exception message thrown for failing webpack build.

0 comments on commit 3ce7e16

Please sign in to comment.