forked from ember-fastboot/ember-cli-fastboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
365 lines (300 loc) · 10.9 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* eslint-env node */
'use strict';
const path = require('path');
const fs = require('fs');
const MergeTrees = require('broccoli-merge-trees');
const FastBootExpressMiddleware = require('fastboot-express-middleware');
const FastBoot = require('fastboot');
const chalk = require('chalk');
const fastbootAppModule = require('./lib/utilities/fastboot-app-module');
const FastBootConfig = require('./lib/broccoli/fastboot-config');
const migrateInitializers = require('./lib/build-utilities/migrate-initializers');
const SilentError = require('silent-error');
const Concat = require('broccoli-concat');
const Funnel = require('broccoli-funnel');
const p = require('ember-cli-preprocess-registry/preprocessors');
const fastbootTransform = require('fastboot-transform');
const existsSync = fs.existsSync;
let checker;
function getVersionChecker(context) {
if (!checker) {
const VersionChecker = require('ember-cli-version-checker');
checker = new VersionChecker(context);
}
return checker;
}
/*
* Main entrypoint for the Ember CLI addon.
*/
module.exports = {
name: 'ember-cli-fastboot',
init() {
this._super.init && this._super.init.apply(this, arguments);
},
/**
* Called at the start of the build process to let the addon know it will be
* used. Sets the auto run on app to be false so that we create and route app
* automatically only in browser.
*
* See: https://ember-cli.com/user-guide/#integration
*/
included(app) {
let assetRev = this.project.addons.find(addon => addon.name === 'broccoli-asset-rev');
if(assetRev && !assetRev.supportsFastboot) {
throw new SilentError("This version of ember-cli-fastboot requires a newer version of broccoli-asset-rev");
}
// set autoRun to false since we will conditionally include creating app when app files
// is eval'd in app-boot
app.options.autoRun = false;
app.import('vendor/experimental-render-mode-rehydrate.js');
// get the app registry object and app name so that we can build the fastboot
// tree
this._appRegistry = app.registry;
this._name = app.name;
migrateInitializers(this.project);
},
/**
* Registers the fastboot shim that allows apps and addons to wrap non-compatible
* libraries in Node with a FastBoot check using `app.import`.
*
*/
importTransforms() {
return {
fastbootShim: fastbootTransform
}
},
/**
* Inserts placeholders into index.html that are used by the FastBoot server
* to insert the rendered content into the right spot. Also injects a module
* for FastBoot application boot.
*/
contentFor(type, config, contents) {
if (type === 'body') {
return "<!-- EMBER_CLI_FASTBOOT_BODY -->";
}
if (type === 'head') {
return "<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->";
}
if (type === 'app-boot') {
const isModuleUnification = (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification();
return fastbootAppModule(config.modulePrefix, JSON.stringify(config.APP || {}), isModuleUnification);
}
// if the fastboot addon is installed, we overwrite the config-module so that the config can be read
// from meta tag/directly for browser build and from Fastboot config for fastboot target.
if (type === 'config-module') {
const originalContents = contents.join('');
const appConfigModule = `${config.modulePrefix}`;
contents.splice(0, contents.length);
contents.push(
'if (typeof FastBoot !== \'undefined\') {',
'return FastBoot.config(\'' + appConfigModule + '\');',
'} else {',
originalContents,
'}'
);
return;
}
},
treeForFastBoot(tree) {
let fastbootHtmlBarsTree;
// check the ember version and conditionally patch the DOM api
if (this._getEmberVersion().lt('2.10.0-alpha.1')) {
fastbootHtmlBarsTree = this.treeGenerator(path.resolve(__dirname, 'fastboot-app-lt-2-9'));
return tree ? new MergeTrees([tree, fastbootHtmlBarsTree]) : fastbootHtmlBarsTree;
}
return tree;
},
_processAddons(addons, fastbootTrees) {
addons.forEach((addon) => {
this._processAddon(addon, fastbootTrees);
});
},
_processAddon(addon, fastbootTrees) {
// walk through each addon and grab its fastboot tree
const currentAddonFastbootPath = path.join(addon.root, 'fastboot');
let fastbootTree;
if (existsSync(currentAddonFastbootPath)) {
fastbootTree = this.treeGenerator(currentAddonFastbootPath);
}
// invoke addToFastBootTree for every addon
if (addon.treeForFastBoot) {
let additionalFastBootTree = addon.treeForFastBoot(fastbootTree);
if (additionalFastBootTree) {
fastbootTrees.push(additionalFastBootTree);
}
} else if (fastbootTree !== undefined) {
fastbootTrees.push(fastbootTree);
}
this._processAddons(addon.addons, fastbootTrees);
},
/**
* Function that builds the fastboot tree from all fastboot complaint addons
* and project and transpiles it into appname-fastboot.js
*/
_getFastbootTree() {
const appName = this._name;
let fastbootTrees = [];
this._processAddons(this.project.addons, fastbootTrees);
// check the parent containing the fastboot directory
const projectFastbootPath = path.join(this.project.root, 'fastboot');
if (existsSync(projectFastbootPath)) {
let fastbootTree = this.treeGenerator(projectFastbootPath);
fastbootTrees.push(fastbootTree);
}
// transpile the fastboot JS tree
let mergedFastBootTree = new MergeTrees(fastbootTrees, {
overwrite: true
});
let funneledFastbootTrees = new Funnel(mergedFastBootTree, {
destDir: appName
});
const processExtraTree = p.preprocessJs(funneledFastbootTrees, '/', this._name, {
registry: this._appRegistry
});
function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
}
let appFilePath = stripLeadingSlash(this.app.options.outputPaths.app.js);
let finalFastbootTree = new Concat(processExtraTree, {
outputFile: appFilePath.replace(/\.js$/, '-fastboot.js')
});
return finalFastbootTree;
},
treeForPublic(tree) {
let fastbootTree = this._getFastbootTree();
let trees = [];
if (tree) {
trees.push(tree);
}
trees.push(fastbootTree);
let newTree = new MergeTrees(trees);
return newTree;
},
/**
* After the entire Broccoli tree has been built for the `dist` directory,
* adds the `fastboot-config.json` file to the root.
*
*/
postprocessTree(type, tree) {
if (type === 'all') {
let fastbootConfigTree = this._buildFastbootConfigTree(tree);
// Merge the package.json with the existing tree
return new MergeTrees([tree, fastbootConfigTree], {overwrite: true});
}
return tree;
},
/**
* Need to handroll our own clone algorithm since JSON.stringy changes regex
* to empty objects which breaks hostWhiteList property of fastboot.
*
* @param {Object} config
*/
_cloneConfigObject(config) {
if (config === null || typeof config !== 'object') {
return config;
}
if (config instanceof Array) {
let copy = [];
for (let i=0; i< config.length; i++) {
copy[i] = this._cloneConfigObject(config[i]);
}
return copy;
}
if (config instanceof RegExp) {
// converting explicitly to string since we create a new regex object
// in fastboot: https://github.com/ember-fastboot/fastboot/blob/master/src/fastboot-request.js#L28
return config.toString();
}
if (config instanceof Object) {
let copy = {};
for (let attr in config) {
if (config.hasOwnProperty(attr)) {
copy[attr] = this._cloneConfigObject(config[attr]);
}
}
return copy;
}
throw new Error('App config cannot be cloned for FastBoot.');
},
_getHostAppConfig() {
let env = this.app.env;
// clone the config object
let appConfig = this._cloneConfigObject(this.project.config(env));
// do not boot the app automatically in fastboot. The instance is booted and
// lives for the lifetime of the request.
let APP = appConfig.APP;
if (APP) {
APP.autoboot = false;
} else {
appConfig.APP = { autoboot: false };
}
return appConfig;
},
_buildFastbootConfigTree(tree) {
let appConfig = this._getHostAppConfig();
let fastbootAppConfig = appConfig.fastboot;
return new FastBootConfig(tree, {
project: this.project,
name: this.app.name,
outputPaths: this.app.options.outputPaths,
ui: this.ui,
fastbootAppConfig: fastbootAppConfig,
appConfig: appConfig
});
},
serverMiddleware(options) {
let emberCliVersion = this._getEmberCliVersion();
let app = options.app;
if (emberCliVersion.gte('2.12.0-beta.1')) {
// only run the middleware when ember-cli version for app is above 2.12.0-beta.1 since
// that version contains API to hook fastboot into ember-cli
app.use((req, resp, next) => {
const fastbootQueryParam = (req.query.hasOwnProperty('fastboot') && req.query.fastboot === 'false') ? false : true;
const enableFastBootServe = !process.env.FASTBOOT_DISABLED && fastbootQueryParam;
const broccoliHeader = req.headers['x-broccoli'];
const outputPath = broccoliHeader['outputPath'];
if (req.serveUrl && enableFastBootServe) {
// if it is a base page request, then have fastboot serve the base page
if (!this.fastboot) {
// TODO(future): make this configurable for allowing apps to pass sandboxGlobals
// and custom sandbox class
this.ui.writeLine(chalk.green('App is being served by FastBoot'));
this.fastboot = new FastBoot({
distPath: outputPath
});
}
let fastbootMiddleware = FastBootExpressMiddleware({
fastboot: this.fastboot
});
fastbootMiddleware(req, resp, next);
} else {
// forward the request to the next middleware (example other assets, proxy etc)
next();
}
});
}
},
postBuild(result) {
if (this.fastboot) {
// should we reload fastboot if there are only css changes? Seems it maynot be needed.
// TODO(future): we can do a smarter reload here by running fs-tree-diff on files loaded
// in sandbox.
this.ui.writeLine(chalk.blue('Reloading FastBoot...'));
this.fastboot.reload({
distPath: result.directory
});
}
},
_getEmberCliVersion() {
const checker = getVersionChecker(this);
return checker.for('ember-cli', 'npm');
},
_getEmberVersion() {
const checker = getVersionChecker(this);
const emberVersionChecker = checker.for('ember-source', 'npm');
if (emberVersionChecker.version) {
return emberVersionChecker;
}
return checker.for('ember', 'bower');
},
};