Skip to content

Commit 3aa1678

Browse files
tobiasso85RandomByte
authored andcommitted
[INTERNAL] introduce clean css
WIP
1 parent ab6562f commit 3aa1678

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

lib/processors/cssOptimizer.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const CleanCSS = require("clean-css");
2+
const log = require("@ui5/logger").getLogger("builder:processors:cssOptimizer");
3+
4+
/**
5+
* Optimizes the supplied resources.
6+
*
7+
* @public
8+
* @alias module:@ui5/builder.processors.cssOptimizer
9+
* @param {Object} parameters Parameters
10+
* @param {module:@ui5/fs.Resource[]} parameters.resources List of resources to be processed
11+
* @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving with optimized resources
12+
*/
13+
module.exports = function({resources}) {
14+
return Promise.all(resources.map((resource) => {
15+
return resource.getString().then((css) => {
16+
const options = {
17+
compatibility: {
18+
properties: {
19+
colors: false
20+
}
21+
},
22+
returnPromise: true
23+
};
24+
return new CleanCSS(options).minify(css).then((result) => {
25+
if (Array.isArray(result.warnings) && result.warnings.length > 0) {
26+
log.warn(`Warnings occurred: ${result.warnings.join(", ")}`);
27+
}
28+
resource.setString(result.styles);
29+
return resource;
30+
}, (error) => {
31+
throw new Error(`Errors occurred: ${error.join(", ")}`);
32+
});
33+
});
34+
}));
35+
};

lib/tasks/buildThemes.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const themeBuilder = require("../processors/themeBuilder");
2+
const cssOptimizer = require("../processors/cssOptimizer");
23
const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritized;
34
const fsInterface = require("@ui5/fs").fsInterface;
45

@@ -17,7 +18,7 @@ const fsInterface = require("@ui5/fs").fsInterface;
1718
* @param {boolean} [parameters.options.compress=true]
1819
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
1920
*/
20-
module.exports = function({workspace, dependencies, options}) {
21+
module.exports = async function({workspace, dependencies, options}) {
2122
const combo = new ReaderCollectionPrioritized({
2223
name: `theme - prioritize workspace over dependencies: ${options.projectName}`,
2324
readers: [workspace, dependencies]
@@ -30,13 +31,13 @@ module.exports = function({workspace, dependencies, options}) {
3031
promises.push(combo.byGlob(options.librariesPattern));
3132
}
3233

33-
const compress = options.compress === undefined ? true : options.compress;
34+
const [allResources, availableLibraries] = await Promise.all(promises);
3435

35-
return Promise.all(promises).then(([allResources, availableLibraries]) => {
36-
if (!availableLibraries || availableLibraries.length === 0) {
37-
// Try to build all themes
38-
return allResources;
39-
}
36+
let resources;
37+
if (!availableLibraries || availableLibraries.length === 0) {
38+
// Try to build all themes
39+
resources = allResources;
40+
} else {
4041
/* Don't try to build themes for libraries that are not available
4142
(maybe replace this with something more aware of which dependencies are optional and therefore
4243
legitimately missing and which not (fault case))
@@ -54,18 +55,22 @@ module.exports = function({workspace, dependencies, options}) {
5455
return false;
5556
};
5657

57-
return allResources.filter(isAvailable);
58-
}).then((resources) => {
59-
return themeBuilder({
60-
resources,
61-
fs: fsInterface(combo),
62-
options: {
63-
compress
64-
}
65-
});
66-
}).then((processedResources) => {
67-
return Promise.all(processedResources.map((resource) => {
68-
return workspace.write(resource);
69-
}));
58+
resources = await allResources.filter(isAvailable);
59+
}
60+
const processedResources = await themeBuilder({
61+
resources,
62+
fs: fsInterface(combo)
7063
});
64+
65+
const compress = options.compress === undefined ? true : options.compress;
66+
if (compress) {
67+
const cssResources = processedResources.filter((resource) => {
68+
return resource.getPath().endsWith(".css");
69+
});
70+
await cssOptimizer({resources: cssResources,
71+
fs: fsInterface(combo)});
72+
}
73+
return Promise.all(processedResources.map((resource) => {
74+
return workspace.write(resource);
75+
}));
7176
};

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"@ui5/fs": "^1.1.2",
101101
"@ui5/logger": "^1.0.2",
102102
"cheerio": "^0.22.0",
103+
"clean-css": "^4.2.1",
103104
"escape-unicode": "^0.2.0",
104105
"escodegen": "^1.12.1",
105106
"escope": "^3.6.0",

0 commit comments

Comments
 (0)