@@ -8,6 +8,14 @@ const IMPORT_PATTERN = /@import .*"(.*)";/g;
88const BASE_LESS_PATTERN = / ^ \/ r e s o u r c e s \/ s a p \/ u i \/ c o r e \/ t h e m e s \/ ( [ ^ / ] + ) \/ b a s e \. l e s s $ / ;
99const GLOBAL_LESS_PATTERN = / ^ \/ r e s o u r c e s \/ s a p \/ u i \/ c o r e \/ t h e m e s \/ ( [ ^ / ] + ) \/ g l o b a l \. l e s s $ / ;
1010
11+ class ImportError extends Error {
12+ constructor ( message ) {
13+ super ( ) ;
14+ this . name = "ImportError" ;
15+ this . message = message ;
16+ }
17+ }
18+
1119class 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