forked from SAP/ui5-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththemeBuilder.js
106 lines (92 loc) · 2.75 KB
/
themeBuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const log = require("@ui5/logger").getLogger("builder:processors:themeBuilder");
const path = require("path");
const less = require("less-openui5");
const Resource = require("@ui5/fs").Resource;
const libraryMatchPattern = /^\/resources\/(.*)\/themes\/[^/]*\/library\.source\.less$/i;
/**
* Builds a library theme
*
* @public
* @memberof module:@ui5/builder.processors
*/
class ThemeBuilder {
/**
* Constructor
*
* @public
* @param {fs|module:@ui5/fs.fsInterface} fs Node fs or custom
* [fs interface]{@link module:resources/module:@ui5/fs.fsInterface}
*/
constructor({fs}) {
this.builder = new less.Builder({fs});
}
/**
* Starts the theme build
*
* @public
* @param {module:@ui5/fs.Resource[]} resources Library files
* @returns {Promise<module:@ui5/fs.Resource[]>} Resolving with array of created files
*/
build(resources) {
const files = [];
const compile = (resource) => {
log.verbose("Compiling %s", resource.getPath());
let libraryName;
const libraryMatch = libraryMatchPattern.exec(resource.getPath());
if (libraryMatch) {
libraryName = libraryMatch[1].replace(/\//g, ".");
}
return this.builder.build({
lessInputPath: resource.getPath(),
library: {
name: libraryName
}
}).then((result) => {
const themeDir = path.dirname(resource.getPath());
const libCss = new Resource({
path: themeDir + "/library.css",
string: result.css
});
const libCssRtl = new Resource({
path: themeDir + "/library-RTL.css",
string: result.cssRtl
});
const libParams = new Resource({
path: themeDir + "/library-parameters.json",
string: JSON.stringify(result.variables, null, "\t")
});
files.push(libCss, libCssRtl, libParams);
});
};
return Promise.all(resources.map(compile)).then(() => {
return files;
});
}
/**
* Clears all cached build results
*
* @public
* Use this method to prevent high memory consumption when building many themes within the same process.
*/
clearCache() {
this.builder.clearCache();
}
}
/**
* Builds a library theme.
*
* @public
* @alias module:@ui5/builder.processors.themeBuilder
* @param {Object} parameters Parameters
* @param {module:@ui5/fs.Resource[]} parameters.resources List of <code>library.source.less</code> resources to be processed
* @param {fs|module:@ui5/fs.fsInterface} parameters.fs Node fs or custom [fs interface]{@link module:resources/module:@ui5/fs.fsInterface}
* @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving with theme resources
*/
module.exports = ({resources, fs}) => {
const themeBuilder = new ThemeBuilder({fs});
return themeBuilder.build(resources).then((files) => {
themeBuilder.clearCache();
return files;
});
};
module.exports.ThemeBuilder = ThemeBuilder;