Skip to content

Commit

Permalink
Merge pull request #26 from RuslanZavacky/dont-use-script-tag
Browse files Browse the repository at this point in the history
Don't use script tag to pass fingerprinted asset file name
  • Loading branch information
RuslanZavacky authored Oct 9, 2017
2 parents 70c91ff + 3104e3b commit bf6b1fa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
4 changes: 4 additions & 0 deletions addon/initializers/asset-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function initialize(app) {

let assetMapFile = window && window.__assetMapPlaceholder__;

if (!assetMapFile) {
assetMapFile = decodeURIComponent(document.querySelector('[property="ifa:placeholder"]').getAttribute('content'));
}

if (!assetMapFile) {
app.register('service:asset-map', AssetMap);
return;
Expand Down
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module.exports = function(/* environment, appConfig */) {
return {
ifa: {
enabled: false,
enabled: true,
inline: false
}
};
Expand Down
26 changes: 5 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,18 @@

let fs = require('fs');

const findRoot = (current) => {
let app;

// Keep iterating upward until we don't have a grandparent.
// Has to do this grandparent check because at some point we hit the project.
do {
app = current.app || app;
} while (current.parent && current.parent.parent && (current = current.parent));

return app;
};

module.exports = {
name: 'ember-cli-ifa',

isDevelopingAddon() {
return false;
},

included(app) {
this.app = findRoot(this);
this._super.included.apply(this, arguments);
},

postBuild(build) {
this._super.included.apply(this, arguments);

const env = process.env.EMBER_ENV;
const ifaConfig = this.app.project.config(env).ifa;
const ifaConfig = this.project.config(env).ifa;

if (!ifaConfig.enabled) {
return;
Expand Down Expand Up @@ -71,10 +54,9 @@ module.exports = {

if (ifaConfig.inline && fs.existsSync(assetFileNamePath)) {
assetMapPlaceholder = fs.readFileSync(assetFileNamePath, {encoding: 'utf-8'});

} else {
if (assetFileName) {
assetMapPlaceholder = `"${fingerprintPrepend}assets/${assetFileName}"`;
assetMapPlaceholder = encodeURIComponent(`${fingerprintPrepend}assets/${assetFileName}`);
}
}

Expand All @@ -86,7 +68,9 @@ module.exports = {
},

contentFor(type, config) {
if (type === 'head-footer' && config.ifa && config.ifa.enabled) {
if (type === 'head' && config.ifa && config.ifa.enabled && !config.ifa.inline) {
return '<meta property="ifa:placeholder" content="__asset_map_placeholder__">';
} else if (type === 'head-footer' && config.ifa && config.ifa.enabled && config.ifa.inline) {
return '<script>var __assetMapPlaceholder__ = __asset_map_placeholder__;</script>';
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ module.exports = function(environment) {
// when it is created
},
ifa: {
enabled: true
}
enabled: true,
inline: false
},
};

if (environment === 'development') {
Expand Down

0 comments on commit bf6b1fa

Please sign in to comment.