forked from ember-fastboot/ember-cli-fastboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
295 lines (246 loc) · 8.91 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
/* 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 Concat = require('broccoli-concat');
const Funnel = require('broccoli-funnel');
const p = require('ember-cli-preprocess-registry/preprocessors');
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) {
// set autoRun to false since we will conditionally include creating app when app files
// is eval'd in app-boot
app.options.autoRun = false;
if (app.options.fingerprint) {
// set generateAssetMap to be true so that manifest files can be correctly written
// in package.json
app.options.fingerprint.generateAssetMap = true;
}
// 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);
},
/**
* 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') {
return fastbootAppModule(config.modulePrefix, JSON.stringify(config.APP || {}));
}
// 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') {
var originalContents = contents.join('');
contents.splice(0, contents.length);
contents.push(
'if (typeof FastBoot !== \'undefined\') {',
'return FastBoot.config();',
'} 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
});
let fileAppName = path.basename(this.app.options.outputPaths.app.js).split('.')[0];
let finalFastbootTree = new Concat(processExtraTree, {
outputFile: 'assets/' + fileAppName + '-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;
},
_buildFastbootConfigTree(tree) {
let env = this.app.env;
let config = this.project.config(env);
let fastbootConfig = config.fastboot;
// do not boot the app automatically in fastboot. The instance is booted and
// lives for the lifetime of the request.
let APP = config.APP;
if (APP) {
APP.autoboot = false;
} else {
config.APP = { autoboot: false };
}
return new FastBootConfig(tree, {
assetMapPath: this.assetMapPath,
project: this.project,
name: this.app.name,
outputPaths: this.app.options.outputPaths,
ui: this.ui,
fastbootAppConfig: fastbootConfig,
appConfig: config
});
},
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');
},
};