-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
154 lines (120 loc) · 4.26 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
/* eslint-env node */
'use strict';
const path = require('path');
const Funnel = require('broccoli-funnel');
const intersection = require('./lib/intersection');
const difference = require('./lib/difference');
module.exports = {
name: 'ember-bulma',
init() {
this._super.init && this._super.init.apply(this, arguments);
this.options = this.options || {};
this.options.babel = this.options.babel || {};
this.options.babel.plugins = this.options.babel.plugins || [];
if (this.options.babel.plugins.indexOf('transform-decorators-legacy') === -1) {
this.options.babel.plugins.push('transform-decorators-legacy');
}
},
included() {
this._super.included.apply(this, arguments);
let app;
// Climb up the hierarchy of addons up to the host
// If the addon has the `_findHost()` method (in ember-cli >= 2.7.0) then use it
if (typeof this._findHost === 'function') {
app = this._findHost();
} else {
// Otherwise, use the copied `_findHost()` implementation
// https://github.com/ember-cli/ember-cli/blob/v2.15.1/lib/models/addon.js#L614-L625
let current = this;
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
}
this.app = app;
// see: http://ember-cli.com/extending/#broccoli-build-options-for-in-repo-addons
app.options = app.options || {};
// Build path to Bulma's sass paths
let bulmaPath = path.dirname(require.resolve('bulma'));
app.options.sassOptions = app.options.sassOptions || {};
app.options.sassOptions.includePaths = app.options.sassOptions.includePaths || [];
// Import sass dependencies
app.options.sassOptions.includePaths.push(bulmaPath);
let config = app.project.config(app.env) || {};
let addonConfig = config[this.name] || {};
this.whitelist = this.generateWhitelist(addonConfig);
this.blacklist = this.generateBlacklist(addonConfig);
},
/*
Tree Shaking borrowed from DockYard
https://github.com/DockYard/ember-composable-components/
https://github.com/DockYard/ember-composable-components#configuration
*/
treeForAddon() {
// see: https://github.com/ember-cli/ember-cli/issues/4463
var tree = this._super.treeForAddon.apply(this, arguments);
return this.filterComponents(tree, new RegExp('^modules\/' + this.name + '\/components\/', 'i'));
},
filterComponents(tree, regex) {
var whitelist = this.whitelist;
var blacklist = this.blacklist;
var _this = this;
// exit early if no opts defined
if (whitelist.length === 0 && blacklist.length === 0) {
return new Funnel(tree);
}
var funnelTree = new Funnel(tree, {
exclude: [function(name) {
return _this.exclusionFilter(name, regex, {
whitelist: whitelist,
blacklist: blacklist
});
}]
});
return funnelTree;
},
exclusionFilter(name, regex, lists) {
var whitelist = lists.whitelist || [];
var blacklist = lists.blacklist || [];
var isAddonComponent = regex.test(name);
var componentName = path.basename(name, '.js');
var isWhitelisted = whitelist.indexOf(componentName) !== -1;
var isBlacklisted = blacklist.indexOf(componentName) !== -1;
// non-component, don't exclude
if (!isAddonComponent) {
return false;
}
// don't exclude if both lists are empty
if (whitelist.length === 0 && blacklist.length === 0) {
return false;
}
// don't exclude if both whitelisted and blacklisted
if (isWhitelisted && isBlacklisted) {
return false;
}
// only whitelist defined
if (whitelist.length && blacklist.length === 0) {
return !isWhitelisted;
}
// only blacklist defined
if (blacklist.length && whitelist.length === 0) {
return isBlacklisted;
}
return !isWhitelisted || isBlacklisted;
},
generateWhitelist(addonConfig) {
var only = addonConfig.only || [];
var except = addonConfig.except || [];
if (except && except.length) {
return difference(only, except);
}
return only;
},
generateBlacklist(addonConfig) {
var only = addonConfig.only || [];
var except = addonConfig.except || [];
if (only && only.length) {
return intersection(except, only);
}
return except;
}
};