forked from webark/ember-component-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
228 lines (193 loc) · 7.15 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'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');
var Replace = require('broccoli-replace');
let VersionChecker = require("ember-cli-version-checker");
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;
},
_getHostApp: 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();
},
_getEnvironment: function() {
return this._getHostApp().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);
}
var hostapp = this._getHostApp();
if (!hostapp._allPodStyles) {
hostapp._allPodStyles = [];
}
this._allPodStyles = hostapp._allPodStyles;
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)'
});
}
let checker = new VersionChecker(this);
let ember = checker.forEmber();
let superTree = this._super.treeForAddon.call(this, tree);
if (ember.isAbove('3.6.0')) {
return new Funnel(superTree, {
exclude: ['ember-component-css/initializers/route-styles.js'],
annotation:
"Funnel (ember-component-css exclude addon/initializers/route-styles.js in 3.6+)"
});
} else {
return new Funnel(superTree, {
exclude: ["ember-component-css/instance-initializers/route-styles.js"],
annotation:
"Funnel (ember-component-css exclude addon/instance-initializers/route-styles.js in < 3.6)"
});
}
},
treeForApp: function(tree) {
let checker = new VersionChecker(this);
let ember = checker.forEmber();
if (ember.isAbove('3.6.0')) {
return new Funnel(tree, {
exclude: ["initializers/route-styles.js"],
annotation:
"Funnel (ember-component-css exclude app/initializers/route-styles.js in 3.6+)"
});
} else {
return new Funnel(tree, {
exclude: ["instance-initializers/route-styles.js"],
annotation:
"Funnel (ember-component-css exclude app/instance-initializers/route-styles.js in < 3.6)"
});
}
},
treeForParentAddonStyles: function(tree) {
let defaultTree = tree;
if (this.parent.treeForAddonStyles) {
defaultTree = this.parent.treeForAddonStyles.apply(this.parent, arguments);
}
return this.processComponentStyles(defaultTree);
},
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 podStylesWithoutExcluded = new Funnel(podStyles, {
exclude: this.addonConfig.excludeFromManifest || [],
annotation: 'Funnel (ember-component-css exclude style files from manifest)'
});
var styleManifest = new StyleManifest(podStylesWithoutExcluded, {
outputFileNameWithoutExtension: 'pod-styles',
annotation: 'StyleManifest (ember-component-css combining all style files that there are extensions for)'
});
// this is due to sass spcifically not allowing for ANY semicolons.
styleManifest = new Replace(styleManifest, {
files: ['**/*.sass'],
patterns: [{
match: /;/g,
replacement: '',
}],
});
tree = new Merge([podStyles, styleManifest, tree].filter(Boolean), {
annotation: 'Merge (ember-component-css merge namespacedStyles with style manifest)'
});
return tree;
},
name: require('./package').name
};