-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.js
155 lines (126 loc) · 4.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
'use strict';
const StripClassCallCheckPlugin = require.resolve('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');
module.exports = {
name: require('./package').name,
init() {
this._super.init && this._super.init.apply(this, arguments);
this.options = this.options || {};
},
getOutputDirForVersion() {
return '';
},
// Borrowed from ember-cli-babel
_emberVersionRequiresModulesAPIPolyfill() {
let checker = this.checker.for('ember-source', 'npm');
if (!checker.exists()) {
return true;
}
return checker.lt('3.27.0-alpha.1');
},
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'
],
destDir: '@html-next/vertical-collection'
});
let privateTree = babel.transpileTree(withPrivate, {
babel: this.options.babel,
'ember-cli-babel': {
// we leave our output as valid ES
// for the consuming app's config to transpile as desired
// so we don't want to compileModules to amd here
compileModules: false,
disableEmberModulesAPIPolyfill: !this._emberVersionRequiresModulesAPIPolyfill(),
// TODO for the embroider world we want to leave our -private module
// as an es module and only transpile the few things we genuinely care about.
// ideally this would occur as a pre-publish step so that consuming apps would
// just see a `-private.js` file and not pay any additional costs.
// CURRENTLY we transpile the -private module fully acccording to the
// consuming app's config, so we must leave these enabled.
disablePresetEnv: false,
disableDebugTooling: false,
disableDecoratorTransforms: false,
enableTypeScriptTransform: true,
// consuming app will take care of this if needed,
// we don't need to also include
includePolyfill: false,
extensions: ['js', 'ts'],
},
});
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(id) {
return (
id.startsWith('@ember/') ||
['ember', 'ember-raf-scheduler'].includes(id)
);
},
},
});
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)) {
this.import('vendor/debug.css');
}
}
};