Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): preprocessTree to treeForAddon #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions addon/services/thumbor-image.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import Service, { inject as service } from '@ember/service';
import Service from '@ember/service';

export default class ThumborImageService extends Service {
@service('-document') document;
meta = '__ember_thumbor_image_meta__';

getImage(imageName, size) {
let { meta } = this;

meta = meta || this.cacheImagesFromMeta();

if (!meta || !meta[imageName]) {
return imageName;
}
Expand All @@ -22,20 +20,4 @@ export default class ThumborImageService extends Service {
getSizes() {
return this.meta && this.meta.sizes;
}

cacheImagesFromMeta() {
if (typeof Fastboot === undefined) {
return;
}

let thumborConfig = window.thumborConfig();

if (!thumborConfig) {
return;
}

this.meta = thumborConfig;

return this.meta;
}
}
44 changes: 15 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,26 @@ module.exports = {
this.app = (parentAddon || app);
},

postprocessTree(type, tree) {
if (type === 'all') {
let trees = [];
let imageTree = this.generateThumborURLs(tree, this.addonOptions);
treeForAddon() {
let trees = [];

trees.push(imageTree);
let imageTree = this.generateThumborURLs(this.addonOptions);

let pattern = /["']__ember_thumbor_image_meta__["']/;
let mapMeta = (content) => content.replace(pattern, JSON.stringify(this.metaData));
trees.push(imageTree);

trees = trees.concat([
tree,
map(find(tree, '**/*.js'), mapMeta),
map(find(tree, '**/index.html'), mapMeta)
]);
let pattern = /["']__ember_thumbor_image_meta__["']/;
let mapMeta = (content) => content.replace(pattern, JSON.stringify(this.metaData));

return mergeTrees(trees, { overwrite: true });
}
trees = trees.concat([
map(find(this._super.treeForAddon.apply(this, arguments), '**/*.js'), mapMeta)
]);

return tree;
return mergeTrees(trees, { overwrite: true });
},

generateThumborURLs(tree, options) {
let extensions = options.extensions.join('|');
let funnel = new Funnel(tree, {
generateThumborURLs(options) {
let extensions = (options.extensions || []).join('|');
let funnel = new Funnel('public', {
srcDir: options.sourceDir,
include: [`**/*.+(${extensions})`],
allowEmpty: true,
Expand All @@ -80,16 +75,7 @@ module.exports = {
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");
}
isDevelopingAddon() {
return true;
}
};
Loading