-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
173 lines (150 loc) · 5.5 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* eslint-env node */
'use strict';
const fs = require('fs');
const path = require('path');
const express = require('express');
const http = require('http');
const critical = require('critical');
const chromeLauncher = require('chrome-launcher');
const chromeInterface = require('chrome-remote-interface');
const stringUtil = require('ember-cli-string-utils');
const DEFAULT_OPTIONS = {
visitPath: '/app-shell',
outputFile: 'index.html',
enabled: true,
chromeFlags: []
};
const DEFAULT_CRITICAL_OPTIONS = {
minify: true
};
const PLACEHOLDER = '<!-- EMBER_APP_SHELL_PLACEHOLDER -->';
const SERVER_PORT = process.env.APP_SHELL_EXPRESS_PORT || '4321';
module.exports = {
name: 'ember-app-shell',
included(app) {
this._super.included && this._super.included.apply(this, arguments);
this.app = app;
this.app.options = this.app.options || {};
this.app.options['ember-app-shell'] = Object.assign({}, DEFAULT_OPTIONS, this.app.options['ember-app-shell']);
if (process.env.APP_SHELL_DISABLED) {
this.app.options['ember-app-shell'].enabled = false;
}
},
postBuild({ directory }) {
if (!this.app.options['ember-app-shell'].enabled || this.app.env === 'test') {
return;
}
let { outputFile, visitPath } = this.app.options['ember-app-shell'];
return this._launchAppServer(directory)
.then((server) => {
return this._launchChrome().then(({ client, chrome }) => {
const { Page, Runtime } = client;
const kill = () => {
server.close();
client.close();
return chrome.kill();
}
const url = path.join(`http://localhost:${SERVER_PORT}`, visitPath);
const navigate = Page.enable()
.then(() => Page.addScriptToEvaluateOnNewDocument({source: `
window.addEventListener('application', ({ detail: Application }) => {
Application.autoboot = false;
});
`}))
.then(() => Page.navigate({ url }))
.then(() => Page.loadEventFired());
return navigate
.then(() => Runtime.evaluate({ awaitPromise: true, expression: `
// Wrap inside a native promise since appGlobal.visit returns
// RSVP.Promise which doesn't play well with awaitPromise.
new Promise((resolve, reject) => {
// Fail after 30 seconds.
setTimeout(reject, 30000);
const getOuterHTML = () => {
try {
window['${this._appGlobal()}'].visit('${visitPath}')
.then((application) => {
return resolve(document.body.querySelector('.ember-view').outerHTML);
});
} catch(e) {
// Log errors for future debugging.
return reject(e);
}
};
if (window['${this._appGlobal()}']) {
getOuterHTML();
} else {
// In case the global has not yet been defined.
Object.defineProperty(window, '${this._appGlobal()}', {
configurable: true,
enumerable: true,
writeable: true,
get: function() {
return this._appGlobal;
},
set: function(val) {
this._appGlobal = val;
// Get the outerHTML after global has been set.
getOuterHTML();
}
});
}
});
`}))
.then((html) => {
let indexHTML = fs.readFileSync(path.join(directory, 'index.html')).toString();
let appShellHTML = indexHTML.replace(PLACEHOLDER, html.result.value.toString());
let criticalOptions = Object.assign(DEFAULT_CRITICAL_OPTIONS, {
inline: true,
base: directory,
folder: directory,
html: appShellHTML,
dest: outputFile
}, this.app.options['ember-app-shell'].criticalCSSOptions);
return critical.generate(criticalOptions);
})
.catch((error) => console.log(error))
.then(kill, kill);
});
});
},
contentFor(type) {
if (type === "body-footer" && this.app.env !== "test" && this.app.options["ember-app-shell"].enabled) {
return PLACEHOLDER;
}
},
_launchAppServer(directory) {
return new Promise((resolve, reject) => {
let app = express();
let server = http.createServer(app);
app.use(express.static(directory));
app.get('*', function (req, res) {
res.sendFile('/index.html', { root: directory });
});
server.listen(SERVER_PORT, () => {
resolve(server);
});
});
},
_launchChrome() {
let chromeFlags = [
'--disable-gpu',
'--headless',
...this.app.options['ember-app-shell'].chromeFlags
];
return chromeLauncher.launch({ chromeFlags }).then(chrome => {
return chromeInterface({ port: chrome.port }).then((client) => {
return { client, chrome };
});
});
},
_appGlobal() {
let config = require(path.join(this.app.project.root, 'config/environment'))(this.app.env);
var value = config.exportApplicationGlobal;
if (typeof value === 'string') {
return value;
} else {
return stringUtil.classify(config.modulePrefix);
}
}
};