-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
236 lines (183 loc) · 5.66 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
229
230
231
232
233
234
235
236
"use strict";
const path = require("path");
const funnel = require("broccoli-funnel");
const merge = require("broccoli-merge-trees");
const map = require("broccoli-stew").map;
const DEFAULT_OPTIONS = {
importUIkitCSS: true,
importUIkitJS: true,
importUIkitIcons: true,
importUIkitAssets: true,
useIcons: true,
whitelist: [],
blacklist: [],
};
const COMPONENT_DEPENDENCIES = {
"uk-switcher": ["uk-tab", "uk-subnav"],
};
module.exports = {
name: require("./package").name,
treeForPublic(tree) {
const uikitAssets =
this.uikitOptions.importUIkitAssets &&
funnel(this._getAssetsPath(), {
destDir: "/assets/images/components",
});
const uikitIcons =
this.uikitOptions.useIcons &&
this.uikitOptions.importUIkitIcons &&
funnel(this._getIconsPath(), {
destDir: "/assets/images/icons",
});
const appTree = this._super.treeForPublic.call(this, tree);
return merge([uikitAssets, uikitIcons, appTree].filter(Boolean));
},
treeForStyles(tree) {
const uikitStyles =
this._hasSass() &&
this.uikitOptions.importUIkitCSS &&
funnel(this._getStylesPath(), {
destDir: "ember-uikit",
});
const appTree = this._super.treeForStyles.call(this, tree);
return merge([uikitStyles, appTree].filter(Boolean));
},
treeForVendor(tree) {
let uikitScripts = funnel(path.join(this._getUikitPath(), "dist", "js"), {
include: ["uikit.js", "uikit-icons.js"],
});
uikitScripts = map(
uikitScripts,
(content) => `if (typeof FastBoot === 'undefined') { ${content} }`,
);
const appTree = this._super.treeForVendor.call(this, tree);
return merge([appTree, uikitScripts].filter(Boolean));
},
included(...args) {
this._super.included.apply(this, args);
this.app = this._findHost();
this._registerCodeCoveragePlugin();
const options = Object.assign(
Object.assign({}, DEFAULT_OPTIONS),
this.app.options["ember-uikit"],
);
this.uikitOptions = options;
if (
this.uikitOptions.whitelist.length &&
this.uikitOptions.blacklist.length
) {
this.ui.writeWarnLine(
"[ember-uikit]: `blacklist` and `whitelist` should not be used simultaneously - ignoring whitelist.",
);
}
if (!this.uikitOptions.useIcons && !this.uikitOptions.whitelist.length) {
this.uikitOptions.blacklist.push("uk-icon");
}
if (!this._hasSass() && this.uikitOptions.importUIkitCSS) {
// use compiled css version of uikit
this.app.import(path.join(this._getStylesPath(), "uikit.css"));
}
if (this.uikitOptions.importUIkitJS) {
this.app.import(path.join("vendor", "uikit.js"));
if (this.uikitOptions.useIcons) {
this.app.import(path.join("vendor", "uikit-icons.js"));
}
}
},
_generateWhitelist(whitelist) {
const list = [];
if (!whitelist) {
return list;
}
function _addToWhitelist(item) {
if (list.indexOf(item) === -1) {
list.push(item);
if (COMPONENT_DEPENDENCIES[item]) {
COMPONENT_DEPENDENCIES[item].forEach(_addToWhitelist);
}
}
}
whitelist.forEach(_addToWhitelist);
return list;
},
treeForAddon(tree) {
return this._super.treeForAddon.call(this, this._filterComponents(tree));
},
treeForAddonTemplates(tree) {
return this._super.treeForAddonTemplates.call(
this,
this._filterComponents(tree),
);
},
/**
* Treeshaking stolen from ember-bootstrap all credits to @kaliber5
*/
_filterComponents(tree) {
const whitelist = this._generateWhitelist(this.uikitOptions.whitelist);
const blacklist = this.uikitOptions.blacklist || [];
// exit early if no opts defined
if (whitelist.length === 0 && blacklist.length === 0) {
return tree;
}
return funnel(tree, {
exclude: [(name) => this._excludeComponent(name, whitelist, blacklist)],
});
},
_excludeComponent(name, whitelist, blacklist) {
const regex = /^(templates\/)?components\//;
const isComponent = regex.test(name);
if (!isComponent) {
return false;
}
let baseName = name.replace(regex, "");
const firstSeparator = baseName.indexOf("/");
if (firstSeparator !== -1) {
baseName = baseName.substring(0, firstSeparator);
} else {
baseName = baseName.substring(0, baseName.lastIndexOf("."));
}
const isWhitelisted = whitelist.indexOf(baseName) !== -1;
const isBlacklisted = blacklist.indexOf(baseName) !== -1;
if (whitelist.length === 0 && blacklist.length === 0) {
return false;
}
if (whitelist.length && blacklist.length === 0) {
return !isWhitelisted;
}
return isBlacklisted;
},
_hasSass() {
return !!this.app.project.findAddonByName("ember-cli-sass");
},
_getUikitPath() {
return path.dirname(
require.resolve("uikit/package.json", {
basedir: this.project.root,
}),
);
},
_getIconsPath() {
return path.join(this._getUikitPath(), "src", "images", "icons");
},
_getAssetsPath() {
return path.join(this._getUikitPath(), "src", "images", "components");
},
_getStylesPath() {
const uikitPath = this._getUikitPath();
if (this._hasSass()) {
return path.join(uikitPath, "src", "scss");
}
return path.join(uikitPath, "dist", "css");
},
_registerCodeCoveragePlugin() {
if (this.app.project.pkg.name === this.name) {
this.options.babel.plugins = [
...(this.options.babel.plugins ?? []),
// eslint-disable-next-line n/no-unpublished-require
...require("ember-cli-code-coverage").buildBabelPlugin({
embroider: true,
}),
];
}
},
};