forked from webark/ember-component-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (136 loc) · 5.05 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* eslint-env node */
'use strict';
var Funnel = require('broccoli-funnel');
var Merge = require('broccoli-merge-trees');
var ProcessStyles = require('./lib/pod-style.js');
var ExtractNames = require('./lib/pod-names.js');
var StyleManifest = require('broccoli-style-manifest');
module.exports = {
_getStyleFunnel: function() {
return new Merge([this._getPodStyleFunnel(), this._getClassicStyleFunnel()], {
annotation: 'Merge (ember-component-css merge pod and classic styles)'
});
},
_getPodStyleFunnel: function() {
return new Funnel(this.projectRoot, {
srcDir: this._podDirectory(),
exclude: ['styles/**/*'],
include: ['**/*.{' + this.allowedStyleExtensions + ',}'],
allowEmpty: true,
annotation: 'Funnel (ember-component-css grab files)'
});
},
_getClassicStyleFunnel: function() {
return new Funnel(this.projectRoot, {
include: ['styles/' + this.classicStyleDir + '/**/*.{' + this.allowedStyleExtensions + ',}'],
allowEmpty: true,
annotation: 'Funnel (ember-component-css grab classic files)'
});
},
_podDirectory: function() {
return this.appConfig.podModulePrefix && !this._isAddon() ? this.appConfig.podModulePrefix.replace(this.appConfig.modulePrefix, '') : '';
},
_namespacingIsEnabled: function() {
return this.addonConfig.namespacing !== false;
},
_isAddon: function() {
return Boolean(this.parent.parent);
},
_allPodStyles: [],
_projectRoot: function(trees) {
var projectRoot;
if (this._isAddon()) {
projectRoot = this.parent.root + '/addon';
} else if (trees && trees.app) {
projectRoot = trees.app;
} else {
projectRoot = this.parent.root + '/app';
}
return projectRoot;
},
_getEnvironment: function() {
if (!this._findHost) {
this._findHost = function findHostShim() {
let current = this;
let app;
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
return app;
};
}
return this._findHost().env;
},
included: function(app) {
this._super.included.apply(this, arguments);
this.projectRoot = this._projectRoot(app.trees);
if (this._isAddon()) {
this.parent.treeForMethods['addon-styles'] = 'treeForParentAddonStyles';
this.parent.treeForParentAddonStyles = this.treeForParentAddonStyles.bind(this);
}
this.appConfig = app.project.config(this._getEnvironment());
this.addonConfig = this.appConfig['ember-component-css'] || {};
this.classicStyleDir = this.addonConfig.classicStyleDir || 'component-styles';
this.terseClassNames = Boolean(this.addonConfig.terseClassNames);
this.allowedStyleExtensions = app.registry.extensionsForType('css').filter(Boolean);
},
config: function(enviroment) {
var config = {
"ember-component-css": {
terseClassNames: false,
},
};
if (enviroment === 'production') {
config["ember-component-css"].terseClassNames = true;
}
return config;
},
treeForAddon: function(tree) {
if (this._namespacingIsEnabled()) {
var allPodStyles = new Merge(this._allPodStyles, {
overwrite: true, // there are times (specifically with ember engines) where we run over the tree for twice. Should revist and find a way to prevent that in the future.
annotation: 'Merge (ember-component-css merge all process styles for a complete list of styles)'
});
var podNames = new ExtractNames(allPodStyles, {
classicStyleDir: this.classicStyleDir,
terseClassNames: this.terseClassNames,
annotation: 'Walk (ember-component-css extract class names from style paths)'
});
tree = new Merge([tree, podNames], {
overwrite: true,
annotation: 'Merge (ember-component-css merge names with addon tree)'
});
}
return this._super.treeForAddon.call(this, tree);
},
treeForParentAddonStyles: function(tree) {
return this.processComponentStyles(tree);
},
treeForStyles: function(tree) {
if (!this._isAddon()) {
tree = this.processComponentStyles(tree);
}
return this._super.treeForStyles.call(this, tree);
},
processComponentStyles: function(tree) {
var podStyles = this._getStyleFunnel();
this._allPodStyles.push(podStyles);
if (this._namespacingIsEnabled()) {
podStyles = new ProcessStyles(podStyles, {
extensions: this.allowedStyleExtensions,
classicStyleDir: this.classicStyleDir,
terseClassNames: this.terseClassNames,
annotation: 'Filter (ember-component-css process :--component with class names)'
});
}
var styleManifest = new StyleManifest(podStyles, {
outputFileNameWithoutExtension: 'pod-styles',
annotation: 'StyleManifest (ember-component-css combining all style files that there are extensions for)'
});
tree = new Merge([podStyles, styleManifest, tree].filter(Boolean), {
annotation: 'Merge (ember-component-css merge namespacedStyles with style manafest)'
});
return tree;
},
name: 'ember-component-css'
};