forked from vishwasraj-thyagaraj/vertical-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (127 loc) · 4.03 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
'use strict';
const StripClassCallCheckPlugin = require('babel6-plugin-strip-class-callcheck');
const Funnel = require('broccoli-funnel');
const Rollup = require('broccoli-rollup');
const merge = require('broccoli-merge-trees');
const VersionChecker = require('ember-cli-version-checker');
function isProductionEnv() {
const isProd = /production/.test(process.env.EMBER_ENV);
const isTest = process.env.EMBER_CLI_TEST_COMMAND;
return isProd && !isTest;
}
module.exports = {
name: require('./package').name,
init() {
this._super.init && this._super.init.apply(this, arguments);
this.options = this.options || {};
},
getOutputDirForVersion() {
let VersionChecker = require('ember-cli-version-checker');
let checker = new VersionChecker(this);
let emberCli = checker.for('ember-cli', 'npm');
let requiresModulesDir = emberCli.satisfies('< 3.0.0');
return requiresModulesDir ? 'modules' : '';
},
treeForAddon(tree) {
let babel = this.addons.find((addon) => addon.name === 'ember-cli-babel');
let withPrivate = new Funnel(tree, { include: ['-private/**'] });
let withoutPrivate = new Funnel(tree, {
exclude: [
'**/**.hbs',
'-private',
isProductionEnv() ? '-debug' : false
].filter(Boolean),
destDir: '@html-next/vertical-collection'
});
let privateTree = babel.transpileTree(withPrivate, {
babel: this.options.babel,
'ember-cli-babel': {
compileModules: false
}
});
const templateTree = new Funnel(tree, {
include: ['**/**.hbs']
});
// use the default options
const addonTemplateTree = this._super(templateTree);
let publicTree = babel.transpileTree(withoutPrivate);
privateTree = new Rollup(privateTree, {
rollup: {
input: '-private/index.js',
output: [
{
file: '@html-next/vertical-collection/-private.js',
format: 'amd',
amd: {
id: '@html-next/vertical-collection/-private'
}
}
],
external: ['ember', 'ember-raf-scheduler']
}
});
let destDir = this.getOutputDirForVersion();
publicTree = new Funnel(publicTree, { destDir });
privateTree = new Funnel(privateTree, { destDir });
return merge([
addonTemplateTree,
publicTree,
privateTree
]);
},
_hasSetupBabelOptions: false,
buildBabelOptions(originalOptions) {
const plugins = originalOptions.plugins || [];
const opts = {
loose: true,
plugins,
postTransformPlugins: [StripClassCallCheckPlugin]
};
return opts;
},
_setupBabelOptions() {
if (this._hasSetupBabelOptions) {
return;
}
this.options.babel = this.buildBabelOptions(this.options.babel);
this._hasSetupBabelOptions = true;
},
included(app) {
this._super.included.apply(this, arguments);
this.checker = new VersionChecker(app);
while (typeof app.import !== 'function' && app.app) {
app = app.app;
}
if (typeof app.import !== 'function') {
throw new Error('vertical-collection is being used within another addon or engine '
+ 'and is having trouble registering itself to the parent application.');
}
this._env = app.env;
this._setupBabelOptions(app.env);
if (!/production/.test(app.env) && !/test/.test(app.env)) {
findImporter(this).import('vendor/debug.css');
}
},
treeForApp() {
const tree = this._super.treeForApp.apply(this, arguments);
const exclude = [];
if (isProductionEnv()) {
exclude.push('initializers/debug.js');
}
return new Funnel(tree, { exclude });
}
};
function findImporter(addon) {
if (typeof addon.import === 'function') {
// If addon.import() is present (CLI 2.7+) use that
return addon;
} else {
// Otherwise, reuse the _findHost implementation that would power addon.import()
let current = addon;
let app;
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
return app;
}
}