-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
121 lines (109 loc) · 4.4 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
/* Copyright 2017 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
/* jshint node: true */
'use strict';
var path = require('path');
var MergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
function getAmChartsTree (destDir) {
// TODO: check this.options or env config to get location of amCharts build
// (i.e. a licensed build), for now just get free build installed from npm
var amchartsDir = path.dirname(require.resolve('amcharts3/amcharts/amcharts.js'));
// NOTE: this file is YUGE and will blow up ember production builds
// see https://github.com/Esri/ember-cli-cedar/issues/76
// it also doesn't appear to be needed b/c
// at runtime amCharts loads the minified version (pdfmake.min.js)
const exclude = [
'plugins/export/libs/pdfmake/pdfmake.js',
'plugins/animate/examples',
'plugins/dataloader/examples',
'plugins/export/examples',
'plugins/responsive/examples',
];
const amchartsTree = new Funnel(amchartsDir, {
destDir: destDir,
exclude: exclude
});
// copy cedar's calcite theme to the amCharts theme folder
const calciteThemeDir = path.dirname(require.resolve('@esri/cedar/dist/umd/themes/amCharts/calcite.js'));
const calciteThemeTree = new Funnel(calciteThemeDir, {
files: ['calcite.js'],
destDir: path.join(destDir, 'themes')
});
return MergeTrees([amchartsTree, calciteThemeTree]);
}
module.exports = {
name: 'ember-cli-cedar',
included(app) {
this._super.included.apply(this, arguments);
// parse amCharts options from ember-cli-build
const options = app && app.options;
this.amChartsOptions = (options && options.cedar && options.cedar.amCharts) || {};
// what amCharts files will we need to bundle inside vendor.js
this.amChartsImports = this.amChartsOptions.imports;
// NOTE: even when bundling scripts, need to also serve the amCharts assets
// that those scripts will dynamically load, so we also check for publicPath here
const publicPath = this.amChartsOptions.publicPath
if (publicPath && this.amChartsImports) {
// bundle specified amcharts files from the vendor folder
this.amChartsImports.forEach(function (resource) {
app.import(path.join('vendor', publicPath, resource));
});
}
},
treeForVendor (vendorTree) {
var treesToMerge = vendorTree ? [ vendorTree ] : [];
var publicPath = this._getPublicPath();
if (publicPath && this.amChartsImports) {
var amchartsTree = getAmChartsTree(publicPath);
treesToMerge.push(amchartsTree);
}
return new MergeTrees(treesToMerge);
},
_getPublicPath () {
let result = '';
if (this.amChartsOptions && this.amChartsOptions.publicPath) {
result = this.amChartsOptions.publicPath;
}
return result;
},
treeForPublic: function(publicNode) {
var node = this._super.treeForPublic(publicNode);
var publicPath = this._getPublicPath();
if (publicPath) {
// copy amCharts dist files to public folder so that
// it can dynamically load resources like images, styles, and scripts at runtime
// and so we can lazy load the scripts if configured to do so
var amchartsTree = getAmChartsTree(publicPath);
if (node) {
return new MergeTrees([node, amchartsTree]);
} else {
return amchartsTree;
}
} else {
return node;
}
},
contentFor(type, config) {
var content = '';
if (type === 'head') {
var publicPath = this._getPublicPath();
if (publicPath) {
var assetBaseUrl = (config.cedar && config.cedar.assetBaseUrl) || config.rootURL;
// concatenate path w/ '/'
var amChartsPath = [assetBaseUrl.replace(/\/$/, ''), publicPath.replace(/^\//, '')].join('/');
// tell AmCharts the base bath it should use when dynamically loading resources
content = `<script>var AmCharts_path = '${amChartsPath}';</script>`;
}
}
return content;
}
};