forked from html-next/vertical-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (121 loc) · 4.06 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
/* eslint-env node */
'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');
const path = require('path');
const BlockScopingTransform = (function() {
let plugin = require('babel-plugin-transform-es2015-block-scoping');
// adding `baseDir` ensures that broccoli-babel-transpiler does not
// issue a warning and opt out of caching
let pluginPath = require.resolve('babel-plugin-transform-es2015-block-scoping/package');
let pluginBaseDir = path.dirname(pluginPath);
plugin.baseDir = () => pluginBaseDir;
return plugin;
})();
function isProductionEnv() {
const isProd = /production/.test(process.env.EMBER_ENV);
const isTest = process.env.EMBER_CLI_TEST_COMMAND;
return isProd && !isTest;
}
module.exports = {
name: 'vertical-collection',
init() {
this._super.init && this._super.init.apply(this, arguments);
this.options = this.options || {};
},
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: '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: {
entry: '-private/index.js',
targets: [
{ dest: 'vertical-collection/-private.js', format: 'amd', moduleId: 'vertical-collection/-private' }
],
external: ['ember', 'ember-raf-scheduler']
}
});
// the output of treeForAddon is required to be modules/<your files>
publicTree = new Funnel(publicTree, { destDir: 'modules' });
privateTree = new Funnel(privateTree, { destDir: 'modules' });
return merge([
addonTemplateTree,
publicTree,
privateTree
]);
},
_hasSetupBabelOptions: false,
buildBabelOptions(originalOptions) {
const plugins = originalOptions.plugins || [];
const opts = {
loose: true,
plugins,
postTransformPlugins: [StripClassCallCheckPlugin],
exclude: [
'transform-es2015-block-scoping',
'transform-es2015-typeof-symbol'
]
};
opts.plugins.push(
[BlockScopingTransform, { 'throwIfClosureRequired': true }]
);
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)) {
app.import('vendor/debug.css');
}
},
treeForApp() {
const tree = this._super.treeForApp.apply(this, arguments);
const exclude = [];
if (isProductionEnv()) {
exclude.push('initializers/debug.js');
}
if (this.checker.forEmber().isAbove('1.13.0')) {
exclude.push('initializers/vertical-collection-legacy-compat.js');
}
return new Funnel(tree, { exclude });
}
};