forked from adopted-ember-addons/ember-cli-ifa
/
index.js
54 lines (44 loc) · 1.39 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
/* jshint node: true */
'use strict';
let fs = require('fs');
module.exports = {
name: 'ember-cli-ifa',
isDevelopingAddon: function() {
return false;
},
included: function(app) {
this.app = app;
this._super.included.apply(this, arguments);
},
postBuild: function (build) {
let fingerprintPrepend = '/';
let indexFilePath = build.directory + '/index.html';
let indexFileBuffer = fs.readFileSync(indexFilePath);
let indexFile = indexFileBuffer.toString('utf8');
let files = fs.readdirSync(build.directory + '/assets');
let totalFiles = files.length;
let assetFileName = null;
for (let i = 0; i < totalFiles; i++) {
if (files[i].match(/^assetMap/i)) {
assetFileName = files[i];
break;
}
}
// Prepend the URL of the assetMap with the location defined in fingerprint
// options.
if (this.app && this.app.options && this.app.options.fingerprint) {
fingerprintPrepend = this.app.options.fingerprint.prepend;
}
if (assetFileName) {
fs.writeFileSync(
indexFilePath,
indexFile.replace(/__asset_map_placeholder__/, fingerprintPrepend + 'assets/' + assetFileName)
);
}
},
contentFor(type, config) {
if (type === 'head-footer' && config.ifa && config.ifa.enabled) {
return '<script>var __assetMapFilename__ = "__asset_map_placeholder__";</script>';
}
}
};