forked from ember-bootstrap/ember-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
337 lines (278 loc) · 10.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
'use strict';
const path = require('path');
const mergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
const stew = require('broccoli-stew');
const mv = stew.mv;
const rm = stew.rm;
const map = stew.map;
const rename = stew.rename;
const BroccoliDebug = require('broccoli-debug');
const chalk = require('chalk');
const SilentError = require('silent-error'); // From ember-cli
const VersionChecker = require('ember-cli-version-checker');
const resolve = require('resolve');
const defaultOptions = {
importBootstrapTheme: false,
importBootstrapCSS: true,
importBootstrapFont: false,
insertEmberWormholeElementToDom: true,
bootstrapVersion: 4
};
const supportedPreprocessors = [
'less',
'sass'
];
const componentDependencies = {
'bs-button-group': ['bs-button'],
'bs-accordion': ['bs-collapse'],
'bs-dropdown': ['bs-button'],
'bs-modal-simple': ['bs-modal'],
'bs-navbar': ['bs-nav', 'bs-button'],
'bs-popover': ['bs-contextual-help'],
'bs-tab': ['bs-nav'],
'bs-tooltip': ['bs-contextual-help']
};
const minimumBS4Version = '4.0.0-beta';
module.exports = {
name: require('./package').name,
init() {
this._super.init.apply(this, arguments);
this.debugTree = BroccoliDebug.buildDebugCallback(`ember-bootstrap:${this.name}`);
},
includedCommands() {
return {
'bootstrap:info': require('./lib/commands/info')
};
},
included() {
this._super.included.apply(this, arguments);
let app = this._findHost(this);
this.app = app;
let options = Object.assign({}, defaultOptions, app.options['ember-bootstrap']);
if (options.bootstrapVersion === 4 && app.options['ember-bootstrap'].importBootstrapFont) {
this.warn(
'Inclusion of the Glyphicon font is only supported for Bootstrap 3. ' +
'Set Ember Bootstrap\'s `importBootstrapFont` option to `false` to hide this warning.'
);
}
if (process.env.BOOTSTRAPVERSION) {
// override bootstrapVersion config when environment variable is set
options.bootstrapVersion = parseInt(process.env.BOOTSTRAPVERSION);
}
this.bootstrapOptions = options;
this.validateDependencies();
this.preprocessor = this.findPreprocessor();
// static Bootstrap CSS is mapped to vendor tree, independent of BS version, so import from there
let vendorPath = path.join('vendor', 'ember-bootstrap');
if (!this.hasPreprocessor()) {
// / Import css from bootstrap
if (options.importBootstrapCSS) {
this.import(path.join(vendorPath, 'bootstrap.css'));
this.import(path.join(vendorPath, 'bootstrap.css.map'), { destDir: 'assets' });
}
if (options.importBootstrapTheme) {
this.import(path.join(vendorPath, 'bootstrap-theme.css'));
this.import(path.join(vendorPath, 'bootstrap-theme.css.map'), { destDir: 'assets' });
}
}
// import custom addon CSS
this.import(path.join(vendorPath, `bs${options.bootstrapVersion}.css`));
// register library version
this.import(path.join(vendorPath, 'register-version.js'));
},
validateDependencies() {
let bowerDependencies = this.app.project.bowerDependencies();
if (this.getBootstrapVersion() === 4) {
let checker = new VersionChecker(this);
let dep = checker.for('bootstrap');
if (!dep.gte(minimumBS4Version)) {
this.warn(`For Bootstrap 4 support this version of ember-bootstrap requires at least Bootstrap ${minimumBS4Version}, but you have ${dep.version}. Please run \`ember generate ember-bootstrap\` to update your dependencies!`);
}
}
if ('bootstrap' in bowerDependencies || 'bootstrap-sass' in bowerDependencies) {
this.warn('The dependencies for ember-bootstrap may be outdated. Please run `ember generate ember-bootstrap` to install appropriate dependencies!');
}
},
findPreprocessor() {
return supportedPreprocessors.find((name) => !!this.app.project.findAddonByName(`ember-cli-${name}`) && this.validatePreprocessor(name));
},
validatePreprocessor(name) {
let dependencies = this.app.project.dependencies();
switch (name) {
case 'sass':
if (!('bootstrap-sass' in dependencies) && this.getBootstrapVersion() === 3) {
this.warn('Npm package "bootstrap-sass" is missing, but is typically required for SASS support. Please run `ember generate ember-bootstrap` to install the missing dependencies!');
}
break;
case 'less':
if (this.getBootstrapVersion() === 4) {
throw new SilentError('There is no Less support for Bootstrap 4! Falling back to importing static CSS. Consider switching to Sass for preprocessor support!');
}
if (!('bootstrap' in dependencies)) {
this.warn('Npm package "bootstrap" is missing, but is typically required for Less support. Please run `ember generate ember-bootstrap` to install the missing dependencies!');
}
break;
}
return true;
},
getBootstrapStylesPath() {
switch (this.preprocessor) {
case 'sass':
if (this.getBootstrapVersion() === 4) {
return this.resolvePackagePath('bootstrap/scss');
} else {
return this.resolvePackagePath('bootstrap-sass/assets/stylesheets');
}
case 'less':
return this.resolvePackagePath('bootstrap/less');
default:
return this.resolvePackagePath('bootstrap/dist/css');
}
},
getBootstrapFontPath() {
switch (this.preprocessor) {
case 'sass':
return this.resolvePackagePath('bootstrap-sass/assets/fonts');
case 'less':
default:
return this.resolvePackagePath('bootstrap/fonts');
}
},
resolvePackagePath(pkgPath) {
let parts = pkgPath.split('/');
let pkg = parts[0];
let result = path.dirname(resolve.sync(`${pkg}/package.json`, { basedir: this.app.project.root }));
// add sub folders to path
if (parts.length > 1) {
let args = parts.map((part, i) => i === 0 ? result : part);
result = path.join.apply(path, args);
}
return result;
},
hasPreprocessor() {
return !!this.preprocessor;
},
treeForStyles() {
if (this.hasPreprocessor()) {
return new Funnel(this.getBootstrapStylesPath(), {
destDir: 'ember-bootstrap'
});
}
},
treeForPublic() {
if (this.getBootstrapVersion() === 3 && this.bootstrapOptions.importBootstrapFont) {
return new Funnel(this.getBootstrapFontPath(), {
destDir: 'fonts'
});
}
},
treeForVendor(tree) {
let trees = [tree];
let versionTree = rename(
map(tree, 'ember-bootstrap/register-version.template', (c) => c.replace('###VERSION###', require('./package.json').version)),
'register-version.template',
'register-version.js'
);
trees.push(versionTree);
if (!this.hasPreprocessor()) {
trees.push(new Funnel(this.getBootstrapStylesPath(), {
destDir: 'ember-bootstrap'
}));
}
return mergeTrees(trees, { overwrite: true });
},
getBootstrapVersion() {
return parseInt(this.bootstrapOptions.bootstrapVersion);
},
getOtherBootstrapVersion() {
return this.getBootstrapVersion() === 3 ? 4 : 3;
},
treeForApp(tree) {
tree = this.filterComponents(tree);
return this._super.treeForApp.call(this, tree);
},
treeForAddon(tree) {
let bsVersion = this.getBootstrapVersion();
let otherBsVersion = this.getOtherBootstrapVersion();
let componentsPath = 'components/';
tree = this.debugTree(tree, 'addon-tree:input');
tree = mv(tree, `${componentsPath}bs${bsVersion}/`, componentsPath);
tree = rm(tree, `${componentsPath}bs${otherBsVersion}/**/*`);
tree = this.debugTree(tree, 'addon-tree:bootstrap-version');
tree = this.filterComponents(tree);
tree = this.debugTree(tree, 'addon-tree:tree-shaken');
return this._super.treeForAddon.call(this, tree);
},
treeForAddonTemplates(tree) {
let bsVersion = this.getBootstrapVersion();
let otherBsVersion = this.getOtherBootstrapVersion();
let templatePath = 'components/';
tree = this.debugTree(tree, 'addon-templates-tree:input');
tree = mv(tree, `${templatePath}common/`, templatePath);
tree = mv(tree, `${templatePath}bs${bsVersion}/`, templatePath);
tree = rm(tree, `${templatePath}bs${otherBsVersion}/**/*`);
tree = this.debugTree(tree, 'addon-templates-tree:bootstrap-version');
tree = this.filterComponents(tree);
tree = this.debugTree(tree, 'addon-templates-tree:tree-shaken');
return this._super.treeForAddonTemplates.call(this, tree);
},
contentFor(type, config) {
if (type === 'body-footer' && config.environment !== 'test' && this.bootstrapOptions.insertEmberWormholeElementToDom !== false) {
return '<div id="ember-bootstrap-wormhole"></div>';
}
},
warn(message) {
this.ui.writeLine(chalk.yellow(message));
},
filterComponents(tree) {
let whitelist = this.generateWhitelist(this.bootstrapOptions.whitelist);
let blacklist = this.bootstrapOptions.blacklist || [];
// exit early if no opts defined
if (whitelist.length === 0 && blacklist.length === 0) {
return tree;
}
return new Funnel(tree, {
exclude: [(name) => this.excludeComponent(name, whitelist, blacklist)]
});
},
excludeComponent(name, whitelist, blacklist) {
let regex = /^(templates\/)?components\/(base\/)?/;
let isComponent = regex.test(name);
if (!isComponent) {
return false;
}
let baseName = name.replace(regex, '');
let firstSeparator = baseName.indexOf('/');
if (firstSeparator !== -1) {
baseName = baseName.substring(0, firstSeparator);
} else {
baseName = baseName.substring(0, baseName.lastIndexOf('.'));
}
let isWhitelisted = whitelist.indexOf(baseName) !== -1;
let isBlacklisted = blacklist.indexOf(baseName) !== -1;
if (whitelist.length === 0 && blacklist.length === 0) {
return false;
}
if (whitelist.length && blacklist.length === 0) {
return !isWhitelisted;
}
return isBlacklisted;
},
generateWhitelist(whitelist) {
let list = [];
if (!whitelist) {
return list;
}
function _addToWhitelist(item) {
if (list.indexOf(item) === -1) {
list.push(item);
if (componentDependencies[item]) {
componentDependencies[item].forEach(_addToWhitelist);
}
}
}
whitelist.forEach(_addToWhitelist);
return list;
}
};