-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (75 loc) · 2.37 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
'use strict';
const Funnel = require('broccoli-funnel');
const map = require('broccoli-stew').map;
const find = require('broccoli-stew').find;
const mergeTrees = require('broccoli-merge-trees');
const ThumborWriter = require('./broccoli-thumbor-writer');
function defaultConfig(env) {
let defaultConfig = {
enabled: env === 'production',
sourceDir: 'assets/images/',
sizes: [1024],
extensions: ['jpg', 'jpeg', 'png', 'gif'],
host: '',
assetPrepend: ''
};
return defaultConfig;
}
module.exports = {
name: require('./package').name,
addonOptions: {},
metaData: {},
config(env, baseConfig) {
if (!env) {
return;
}
let config = baseConfig['thumbor'];
let url = baseConfig.rootURL || baseConfig.baseURL || '';
config.rootURL = url;
this.addonOptions = Object.assign({}, defaultConfig(env), config);
this.metaData.sizes = this.addonOptions.sizes;
this.metaData.host = this.addonOptions.host;
},
included(app, parentAddon) {
this._super.included.apply(this, arguments);
this.app = (parentAddon || app);
},
postprocessTree(type, tree) {
if (type === 'all') {
let trees = [];
let imageTree = this.generateThumborURLs(tree, this.addonOptions);
trees.push(imageTree);
let pattern = /["']__ember_thumbor_image_meta__["']/;
let mapMeta = (content) => content.replace(pattern, JSON.stringify(this.metaData));
trees = trees.concat([
tree,
map(find(tree, '**/*.js'), mapMeta),
map(find(tree, '**/index.html'), mapMeta)
]);
return mergeTrees(trees, { overwrite: true });
}
return tree;
},
generateThumborURLs(tree, options) {
let extensions = options.extensions.join('|');
let funnel = new Funnel(tree, {
srcDir: options.sourceDir,
include: [`**/*.+(${extensions})`],
allowEmpty: true,
destDir: '/'
});
return new ThumborWriter([funnel], options, this.metaData, this.ui);
},
contentFor(type) {
// TODO:: Figure our if its possible to push this as a separate file and optimise build times
// from that instead of writing this everytime.
if (type === 'head-footer') {
let txt = [
'<script type="text/javascript">',
'window.thumborConfig = () => { return \'__ember_thumbor_image_meta__\'; }',
'</script>'
];
return txt.join("\n");
}
}
};