forked from sandydoo/ember-google-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
347 lines (266 loc) · 8.69 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
338
339
340
341
342
343
344
345
346
347
/* eslint-disable prefer-template */
'use strict';
const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const path = require('path');
const chalk = require('chalk');
const Handlebars = require('handlebars');
const stripIndent = require('strip-indent');
const writeFile = require('broccoli-file-creator');
const BroccoliDebug = require('broccoli-debug');
const camelCase = require('camelcase');
const IS_COMPONENT = /components\//;
function intersection(a, b) {
let intersection = new Set();
for (let e of b) {
if (a.has(e)) {
intersection.add(e);
}
}
return intersection;
}
function difference(a, b) {
let difference = new Set(a);
for (let e of b) {
difference.delete(e);
}
return difference;
}
let dependencies = {
'circle': ['marker'],
};
function excludeComponent(included, excluded) {
let shouldExclude = excludeName(included, excluded);
return function(name) {
if (!IS_COMPONENT.test(name)) {
return false;
}
let baseName = path.basename(name).split('.').shift();
return shouldExclude(baseName);
}
}
function excludeName(included, excluded) {
return function(rawName) {
let name = camelCase(rawName),
isIncluded = included.indexOf(name) !== -1,
isExcluded = excluded.indexOf(name) !== -1;
if (included.length === 0 && excluded.length === 0) {
return false;
}
// Include if both included and excluded
if (isIncluded && isExcluded) {
return false;
}
// Only included
if (included.length && excluded.length === 0) {
return !isIncluded;
}
// Only excluded
if (excluded.length && included.length === 0) {
return isExcluded;
}
return !isIncluded || isExcluded;
}
}
module.exports = {
name: require('./package').name,
options: {
babel: {
plugins: ['@babel/plugin-proposal-object-rest-spread']
}
},
init() {
this._super.init.apply(this, arguments);
this.debugTree = BroccoliDebug.buildDebugCallback(`ember-google-maps:${this.name}`);
},
included() {
this._super.included.apply(this, arguments);
let app = this._findHost(),
config = app.options['ember-google-maps'] || {};
this.isProduction = app.isProduction;
this.isDevelopment = !this.isProduction;
let {
only = [],
except = [],
} = config;
only = only.map(camelCase);
except = except.map(camelCase);
let included = this.createIncludedList(only, except),
excluded = this.createExcludedList(only, except);
if (this.isProduction) {
excluded.push('warnMissingComponent');
}
// Ensure that we include the base map components.
if (included.length) {
included.push('gMap', 'canvas', 'mapComponent', 'addonFactory');
if (this.isDevelopment) {
included.push('warnMissingComponent');
}
included.forEach(name => {
let deps = dependencies[name];
if (deps) {
included = included.concat(deps);
}
});
}
this.excludeName = excludeName(included, excluded);
this.excludeComponent = excludeComponent(included, excluded);
this.skipTreeshaking =
(!included || included.length === 0) &&
(!excluded || excluded.length === 0);
},
config(env, config) {
let mapConfig = config['ember-google-maps'] || {};
mapConfig['src'] = this.buildGoogleMapsUrl(mapConfig);
return { 'ember-google-maps': mapConfig };
},
treeForAddon(tree) {
tree = this.debugTree(tree, 'addon-tree:input');
let addonFactoryTree = this.createAddonFactoryTree('templates/components');
tree = new MergeTrees([tree, addonFactoryTree], { overwrite: true });
tree = this.debugTree(tree, 'addon-tree:with-addon-factory');
tree = this.filterComponents(tree);
tree = this.debugTree(tree, 'addon-tree:post-filter');
// Run super now, which processes and removes `.hbs`` template files.
tree = this._super.treeForAddon.call(this, tree);
tree = this.debugTree(tree, 'addon-tree:post-super');
return tree;
},
// This hook is deprecated in Ember-CLI 3.13+.
// In older versions, it still runs and seems to overwrite our work in
// `treeForAddon`.
treeForAddonTemplates() {
let tree = this._super.treeForAddonTemplates.apply(this, arguments);
tree = this.debugTree(tree, 'addon-templates-tree:input');
let addonFactoryTree = this.createAddonFactoryTree('components');
tree = new MergeTrees([tree, addonFactoryTree], { overwrite: true });
tree = this.debugTree(tree, 'addon-templates-tree:with-addon-factory');
tree = this.filterComponents(tree);
tree = this.debugTree(tree, 'addon-templates-tree:post-filter');
return tree;
},
createAddonFactoryTree(templatePath) {
let AddonRegistry = require('./lib/broccoli/addon-registry');
let addons = new AddonRegistry(this.project).components.concat([
{ key: 'marker', component: 'g-map/marker' },
{ key: 'circle', component: 'g-map/circle' },
{ key: 'polyline', component: 'g-map/polyline' },
{ key: 'infoWindow', component: 'g-map/info-window' },
{ key: 'overlay', component: 'g-map/overlay' },
{ key: 'control', component: 'g-map/control' },
{ key: 'autocomplete', component: 'g-map/autocomplete' },
{ key: 'directions', component: 'g-map/directions' },
{ key: 'route', component: 'g-map/route' }
]);
if (this.isProduction) {
// Exclude components that we don't want in the production build.
addons = addons.filter(({ key }) => !this.excludeName(key));
} else {
// Replace an excluded component with a debug component in development and
// testing. This component should throw an assertion to warn the user of
// misconfigured treeshaking.
addons = addons.map(component => {
let { key } = component;
if (this.excludeName(key)) {
return {
key,
component: '-private-api/warn-missing-component',
};
}
return component;
});
}
let template = Handlebars.compile(stripIndent(`
\\{{yield
(hash
{{#each addons as |addon|}}
{{addon.key}}=(component "{{addon.component}}" map=map _internalAPI=_internalAPI gMap=gMap _name="{{addon.key}}")
{{/each}}
)
}}
`));
return writeFile(
`${templatePath}/-private-api/addon-factory.hbs`,
template({ addons })
);
},
filterComponents(tree) {
if (this.skipTreeshaking) {
return tree;
}
return new Funnel(tree, {
exclude: [this.excludeComponent]
});
},
createIncludedList(onlyList = [], exceptList = []) {
let only = new Set(onlyList),
except = new Set(exceptList);
if (except && except.length) {
return difference(only, except);
}
return Array.from(only);
},
createExcludedList(onlyList = [], exceptList = []) {
let only = new Set(onlyList),
except = new Set(exceptList);
if (only && only.length) {
return intersection(except, only);
}
return Array.from(except);
},
buildGoogleMapsUrl(config = {}) {
let {
baseUrl = '//maps.googleapis.com/maps/api/js',
channel,
client,
key,
language,
libraries,
protocol,
region,
version,
} = config;
if (!key && !client) {
// Since we allow configuring the URL at runtime, we don't throw an error
// here.
return '';
}
if (key && client) {
this.warn('You must specify either a Google Maps API key or a Google Maps Premium Plan Client ID, but not both. Learn more: https://ember-google-maps.sandydoo.me/docs/getting-started');
}
if (channel && !client) {
this.warn('The Google Maps API channel parameter is only available when using a client ID, not when using an API key. Learn more: https://ember-google-maps.sandydoo.me/docs/getting-started');
}
let src = baseUrl,
params = [];
if (version) {
params.push('v=' + encodeURIComponent(version));
}
if (client) {
params.push('client=' + encodeURIComponent(client));
}
if (channel) {
params.push('channel=' + encodeURIComponent(channel));
}
if (libraries && libraries.length) {
params.push('libraries=' + encodeURIComponent(libraries.join(',')));
}
if (region) {
params.push('region=' + encodeURIComponent(region));
}
if (language) {
params.push('language=' + encodeURIComponent(language));
}
if (key) {
params.push('key=' + encodeURIComponent(key));
}
if (protocol) {
src = protocol + ':' + src;
}
src += '?' + params.join('&');
return src;
},
warn(message) {
this.ui.writeLine(chalk.yellow(message));
}
};