Skip to content

Commit d53ead8

Browse files
committed
feat(optimization): ability to use terse class names to reduce css size
1 parent e22a404 commit d53ead8

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,22 @@ module.exports = {
7373
this.appConfig = app.project.config(app.env);
7474
this.addonConfig = this.appConfig['ember-component-css'] || {};
7575
this.classicStyleDir = this.addonConfig.classicStyleDir || 'component-styles';
76+
this.terseClassNames = Boolean(this.addonConfig.terseClassNames);
7677
this.allowedStyleExtensions = app.registry.extensionsForType('css').filter(Boolean);
7778
},
7879

80+
config: function(enviroment) {
81+
var config = {
82+
"ember-component-css": {
83+
terseClassNames: false,
84+
},
85+
};
86+
if (enviroment === 'production') {
87+
config["ember-component-css"].terseClassNames = true;
88+
}
89+
return config;
90+
},
91+
7992
treeForAddon: function(tree) {
8093
if (this._namespacingIsEnabled()) {
8194
var allPodStyles = new Merge(this._allPodStyles, {
@@ -85,6 +98,7 @@ module.exports = {
8598

8699
var podNames = new ExtractNames(allPodStyles, {
87100
classicStyleDir: this.classicStyleDir,
101+
terseClassNames: this.terseClassNames,
88102
annotation: 'Walk (ember-component-css extract class names from style paths)'
89103
});
90104

@@ -116,6 +130,7 @@ module.exports = {
116130
podStyles = new ProcessStyles(podStyles, {
117131
extensions: this.allowedStyleExtensions,
118132
classicStyleDir: this.classicStyleDir,
133+
terseClassNames: this.terseClassNames,
119134
annotation: 'Filter (ember-component-css process :--component with class names)'
120135
});
121136
}

lib/component-names.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ module.exports = {
1515

1616
return actualPath.substr(0, actualPath.lastIndexOf(terminator)).replace(pathSegementToRemove, '');
1717
},
18-
class: function(modifiedPath, classicStyleDir) {
19-
var seperator = '__',
20-
className = seperator + this.path(modifiedPath, classicStyleDir).replace(/\//g, seperator);
21-
return className + seperator + md5(className).slice(-5);
18+
19+
class: function(modifiedPath, classicStyleDir, terseClassNames) {
20+
var seperator = '__';
21+
var componentPath = this.path(modifiedPath, classicStyleDir);
22+
var className = seperator + md5(componentPath).slice(-5);
23+
24+
if (!terseClassNames) {
25+
className = seperator + componentPath.replace(/\//g, seperator) + className;
26+
}
27+
28+
return className;
2229
}
2330
};

lib/pod-names.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function PodNames(inputNode, options) {
2323
this.currentTree = new FSTree();
2424
this.podNameJson = {};
2525
this.classicStyleDir = options.classicStyleDir;
26+
this.terseClassNames = options.terseClassNames;
2627
}
2728

2829
PodNames.prototype.build = function() {
@@ -56,7 +57,7 @@ PodNames.prototype.writePodStyleName = function(patches) {
5657

5758
PodNames.prototype.addClass = function(stylePath) {
5859
var componentPath = componentNames.path(stylePath, this.classicStyleDir),
59-
componentClass = componentNames.class(stylePath, this.classicStyleDir);
60+
componentClass = componentNames.class(stylePath, this.classicStyleDir, this.terseClassNames);
6061
this.podNameJson[componentPath] = componentClass;
6162
}
6263

lib/pod-style.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ function PodStyles(inputTree, options) {
1717
});
1818
this.extensions = options.extensions;
1919
this.classicStyleDir = options.classicStyleDir;
20+
this.terseClassNames = options.terseClassNames;
2021
}
2122

2223
PodStyles.prototype.processString = function(contents, stylePath) {
2324
var extension = path.extname(stylePath),
24-
className = componentNames.class(stylePath, this.classicStyleDir),
25+
className = componentNames.class(stylePath, this.classicStyleDir, this.terseClassNames),
2526
strategy = 'default';
2627

2728
switch (extension) {

0 commit comments

Comments
 (0)