Skip to content

Commit 0e25b59

Browse files
committed
[FIX] processors/libraryLessGenerator: Don't throw in case of import errors
To be compatible with the previous behavior (Maven), the processor should not throw an error in case an import can't be handled. Instead the error is logged and a comment is added to the resulting library.less file.
1 parent 08a34ae commit 0e25b59

File tree

2 files changed

+153
-45
lines changed

2 files changed

+153
-45
lines changed

lib/processors/libraryLessGenerator.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const IMPORT_PATTERN = /@import .*"(.*)";/g;
88
const BASE_LESS_PATTERN = /^\/resources\/sap\/ui\/core\/themes\/([^/]+)\/base\.less$/;
99
const GLOBAL_LESS_PATTERN = /^\/resources\/sap\/ui\/core\/themes\/([^/]+)\/global\.less$/;
1010

11+
class ImportError extends Error {
12+
constructor(message) {
13+
super();
14+
this.name = "ImportError";
15+
this.message = message;
16+
}
17+
}
18+
1119
class LibraryLessGenerator {
1220
constructor({fs}) {
1321
const readFile = promisify(fs.readFile);
@@ -33,7 +41,17 @@ class LibraryLessGenerator {
3341
const replacements = await Promise.all(imports.map(async (importMatch) => {
3442
const baseDir = posixPath.dirname(filePath);
3543
const resolvedFilePath = posixPath.resolve(baseDir, importMatch.path);
36-
importMatch.content = await this.resolveLessImport(importMatch.path, resolvedFilePath, baseDir);
44+
try {
45+
importMatch.content = await this.resolveLessImport(importMatch.path, resolvedFilePath, baseDir);
46+
} catch (error) {
47+
if (error instanceof ImportError) {
48+
// Add message of import errors after the import statements
49+
// Currently those errors should not break the build (see comments in resolveLessImport)
50+
importMatch.content = importMatch.fullMatch + ` /* ${error} */`;
51+
} else {
52+
throw error;
53+
}
54+
}
3755
return importMatch;
3856
}));
3957

@@ -78,19 +96,26 @@ class LibraryLessGenerator {
7896
}
7997

8098
/*
81-
* Throw error in case of files which are not in the same directory as the current file because
99+
* Log error in case of files which are not in the same directory as the current file because
82100
* inlining them would break relative URLs.
83-
* Keeping the import is also not possible since only "library.less" and "global.less" are
101+
* A possible solution would be to rewrite relative URLs when inlining the content.
102+
*
103+
* Keeping the import will cause errors since only "library.less" and "global.less" are
84104
* configured to be available to the Theme Designer (.theming generated in generateThemeDesignerResources).
105+
* However, the previous implementation did not break the build.
106+
* In many cases the library.less file is currently not relevant so breaking the build would cause
107+
* unnecessary issues.
85108
*
86-
* A possible solution would be to rewrite relative URLs when inlining the content.
87109
*/
88110
const relativeFilePath = posixPath.relative(baseDir, resolvedFilePath);
89111
if (relativeFilePath.includes(posixPath.sep)) {
90-
throw new Error(
91-
`libraryLessGenerator: Unsupported import of file '${resolvedFilePath}'. ` +
92-
`Stylesheets must be located in the theme directory '${baseDir}' (no sub-directories)`
112+
log.error(
113+
`Could not inline import '${resolvedFilePath}' outside of theme directory '${baseDir}'. ` +
114+
`Stylesheets must be located in the theme directory (no sub-directories). ` +
115+
`The generated '${baseDir}/library.less' will cause errors when compiled with the Theme Designer.`
93116
);
117+
// Throw error to be added as comment to the import statement
118+
throw new ImportError("Could not inline import outside of theme directory");
94119
}
95120

96121
let importedFileContent;
@@ -119,6 +144,7 @@ class LibraryLessGenerator {
119144
while ((match = IMPORT_PATTERN.exec(fileContent)) !== null) {
120145
imports.push({
121146
path: match[1],
147+
fullMatch: match[0],
122148
matchStart: match.index,
123149
matchLength: match[0].length
124150
});

0 commit comments

Comments
 (0)