This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from adopted-ember-addons/ember-cli-ifa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (102 loc) · 3.71 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
122
123
124
125
126
127
128
129
130
'use strict';
const fs = require('fs');
const path = require('path');
const MetaPlaceholder = '__ember-cli-ifa__AssetMapPlaceholder__';
function replacePlaceholder(filePath, assetMap) {
const assetMapString = assetMap ? encodeURIComponent(JSON.stringify(assetMap)) : '';
const fileBody = fs.readFileSync(filePath, { encoding: 'utf-8' });
fs.writeFileSync(filePath, fileBody.replace(MetaPlaceholder, assetMapString));
}
module.exports = {
name: 'ember-cli-ifa',
isDevelopingAddon() {
return false;
},
treeForFastBoot(tree) {
this._isFastBoot = true;
if (this.project !== this.parent) {
this.ui.writeLine(
'ember-cli-ifa currently only supports being a top-level dependency! If you are seeing this message please open an issue on https://github.com/RuslanZavacky/ember-cli-ifa/issues'
);
}
return tree;
},
contentFor(type) {
if (type !== 'head') {
return;
}
if (!this._config().enabled) {
return '<meta name="ember-cli-ifa:assetMap">';
}
return `<meta name="ember-cli-ifa:assetMap" content="${MetaPlaceholder}">`;
},
/**
* By default, during runtime, the asset-map service reads the asset map
* information from a meta tag on the index.html. As we do not have access to
* global `document` when running in fastboot, we need to implement a
* different way to access this asset-map information. See
* `get-asset-map-data` where we require the `asset-map` module that is
* generated in the postBuild() below.
*/
updateFastBootManifest(manifest) {
manifest.vendorFiles.push('assets/assetMap.js');
return manifest;
},
postBuild(build) {
this._super.included.apply(this, arguments);
const ifaConfig = this._config();
if (!ifaConfig.enabled) {
return;
}
let fingerprintPrepend = '/';
const files = fs.readdirSync(path.join(build.directory, 'assets'));
const 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;
}
const assetFileNamePath = `${build.directory}/assets/${assetFileName}`;
// When using fastboot, always use the inline form
// As ajax is not so easily possible there
if (!ifaConfig.inline && this._isFastBoot) {
this.ui.writeLine('When running fastboot, ember-cli-ifa is forced into inline mode.');
}
const inline = ifaConfig.inline || this._isFastBoot;
let assetMap;
if (inline && fs.existsSync(assetFileNamePath)) {
let assetMapContent = fs.readFileSync(assetFileNamePath, { encoding: 'utf-8' });
assetMap = JSON.parse(assetMapContent);
if (this._isFastBoot) {
const assetModulePath = assetFileNamePath.replace(/\.json$/, '.js');
fs.writeFileSync(
assetModulePath,
`define('ember-cli-ifa/fastboot-asset-map', [], function () {
return {
'default': ${assetMapContent},
__esModule: true,
};
});`
);
}
} else if (assetFileName) {
assetMap = `${fingerprintPrepend}assets/${assetFileName}`;
}
replacePlaceholder(path.join(build.directory, 'index.html'), assetMap);
let testIndexPath = path.join(build.directory, 'tests/index.html');
if (fs.existsSync(testIndexPath)) {
replacePlaceholder(testIndexPath, assetMap);
}
},
_config() {
const env = process.env.EMBER_ENV;
return this.project.config(env).ifa;
},
};