Skip to content

Commit 49f8f43

Browse files
committed
Fix fastboot-config test and add test for basepagewriter
1 parent e3f89b3 commit 49f8f43

File tree

6 files changed

+296
-173
lines changed

6 files changed

+296
-173
lines changed

packages/ember-cli-fastboot/index.js

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const chalk = require('chalk');
1111

1212
const fastbootAppBoot = require('./lib/utilities/fastboot-app-boot');
1313
const FastBootConfig = require('./lib/broccoli/fastboot-config');
14-
const HTMLWriter = require('./lib/broccoli/html-writer');
14+
const BasePageWriter = require('./lib/broccoli/html-writer');
1515
const fastbootAppFactoryModule = require('./lib/utilities/fastboot-app-factory-module');
1616
const migrateInitializers = require('./lib/build-utilities/migrate-initializers');
1717
const SilentError = require('silent-error');
@@ -61,7 +61,9 @@ module.exports = {
6161
* See: https://ember-cli.com/user-guide/#integration
6262
*/
6363
included(app) {
64-
let assetRev = this.project.addons.find(addon => addon.name === 'broccoli-asset-rev');
64+
let assetRev = this.project.addons.find(
65+
(addon) => addon.name === 'broccoli-asset-rev'
66+
);
6567
if (assetRev && !assetRev.supportsFastboot) {
6668
throw new SilentError(
6769
'This version of ember-cli-fastboot requires a newer version of broccoli-asset-rev'
@@ -110,7 +112,10 @@ module.exports = {
110112
}
111113

112114
if (type === 'app-boot') {
113-
return fastbootAppBoot(config.modulePrefix, JSON.stringify(config.APP || {}));
115+
return fastbootAppBoot(
116+
config.modulePrefix,
117+
JSON.stringify(config.APP || {})
118+
);
114119
}
115120

116121
// if the fastboot addon is installed, we overwrite the config-module so that the config can be read
@@ -135,15 +140,19 @@ module.exports = {
135140

136141
// check the ember version and conditionally patch the DOM api
137142
if (this._getEmberVersion().lt('2.10.0-alpha.1')) {
138-
fastbootHtmlBarsTree = this.treeGenerator(path.resolve(__dirname, 'fastboot-app-lt-2-9'));
139-
return tree ? new MergeTrees([tree, fastbootHtmlBarsTree]) : fastbootHtmlBarsTree;
143+
fastbootHtmlBarsTree = this.treeGenerator(
144+
path.resolve(__dirname, 'fastboot-app-lt-2-9')
145+
);
146+
return tree
147+
? new MergeTrees([tree, fastbootHtmlBarsTree])
148+
: fastbootHtmlBarsTree;
140149
}
141150

142151
return tree;
143152
},
144153

145154
_processAddons(addons, fastbootTrees) {
146-
addons.forEach(addon => {
155+
addons.forEach((addon) => {
147156
this._processAddon(addon, fastbootTrees);
148157
});
149158
},
@@ -183,7 +192,10 @@ module.exports = {
183192
// check the parent containing the fastboot directory
184193
const projectFastbootPath = path.join(this.project.root, 'fastboot');
185194
// ignore the project's fastboot folder if we are an addon, as that is already handled above
186-
if (!this.project.isEmberCLIAddon() && this.existsSync(projectFastbootPath)) {
195+
if (
196+
!this.project.isEmberCLIAddon() &&
197+
this.existsSync(projectFastbootPath)
198+
) {
187199
let fastbootTree = this.treeGenerator(projectFastbootPath);
188200
fastbootTrees.push(fastbootTree);
189201
}
@@ -196,17 +208,28 @@ module.exports = {
196208
let funneledFastbootTrees = new Funnel(mergedFastBootTree, {
197209
destDir: appName,
198210
});
199-
const processExtraTree = p.preprocessJs(funneledFastbootTrees, '/', this._name, {
200-
registry: this._appRegistry,
201-
});
211+
const processExtraTree = p.preprocessJs(
212+
funneledFastbootTrees,
213+
'/',
214+
this._name,
215+
{
216+
registry: this._appRegistry,
217+
}
218+
);
202219

203220
// FastBoot app factory module
204221
const writeFile = require('broccoli-file-creator');
205-
let appFactoryModuleTree = writeFile('app-factory.js', fastbootAppFactoryModule(appName));
206-
207-
let newProcessExtraTree = new MergeTrees([processExtraTree, appFactoryModuleTree], {
208-
overwrite: true,
209-
});
222+
let appFactoryModuleTree = writeFile(
223+
'app-factory.js',
224+
fastbootAppFactoryModule(appName)
225+
);
226+
227+
let newProcessExtraTree = new MergeTrees(
228+
[processExtraTree, appFactoryModuleTree],
229+
{
230+
overwrite: true,
231+
}
232+
);
210233

211234
function stripLeadingSlash(filePath) {
212235
return filePath.replace(/^\//, '');
@@ -231,7 +254,8 @@ module.exports = {
231254

232255
let newTree = new MergeTrees(trees);
233256

234-
let fastbootConfigTree = (this._fastbootConfigTree = this._buildFastbootConfigTree(newTree));
257+
let fastbootConfigTree = (this._fastbootConfigTree =
258+
this._buildFastbootConfigTree(newTree));
235259

236260
// Merge the package.json with the existing tree
237261
return new MergeTrees([newTree, fastbootConfigTree], { overwrite: true });
@@ -296,15 +320,23 @@ module.exports = {
296320

297321
_buildFastbootConfigTree(tree) {
298322
let appConfig = this._getHostAppConfig();
299-
let fastbootAppConfig = appConfig.fastboot;
300323

301324
return new FastBootConfig(tree, {
302325
project: this.project,
303-
name: this.app.name,
304326
outputPaths: this.app.options.outputPaths,
305327
ui: this.ui,
306-
fastbootAppConfig: fastbootAppConfig,
307-
appConfig: appConfig,
328+
appConfig,
329+
});
330+
},
331+
332+
_buildHTMLWriter(tree) {
333+
let appConfig = this._getHostAppConfig();
334+
335+
return new BasePageWriter(tree, {
336+
project: this.project,
337+
appConfig,
338+
appJsPath: this.app.options.outputPaths.app.js,
339+
outputPaths: this.app.options.outputPaths,
308340
});
309341
},
310342

@@ -314,16 +346,7 @@ module.exports = {
314346
postprocessTree(type, tree) {
315347
this._super(...arguments);
316348
if (type === 'all') {
317-
let { fastbootConfig, appName, manifest } = this._fastbootConfigTree;
318-
debugger;
319-
let fastbootHTMLTree = new HTMLWriter(tree, {
320-
annotation: 'FastBoot HTML Writer',
321-
fastbootConfig,
322-
appName,
323-
manifest,
324-
appJsPath: this.app.options.outputPaths.app.js,
325-
outputPaths: this.app.options.outputPaths,
326-
});
349+
let fastbootHTMLTree = this._buildHTMLWriter(tree);
327350

328351
// Merge the package.json with the existing tree
329352
return new MergeTrees([tree, fastbootHTMLTree], { overwrite: true });
@@ -342,8 +365,11 @@ module.exports = {
342365

343366
app.use((req, resp, next) => {
344367
const fastbootQueryParam =
345-
req.query.hasOwnProperty('fastboot') && req.query.fastboot === 'false' ? false : true;
346-
const enableFastBootServe = !process.env.FASTBOOT_DISABLED && fastbootQueryParam;
368+
req.query.hasOwnProperty('fastboot') && req.query.fastboot === 'false'
369+
? false
370+
: true;
371+
const enableFastBootServe =
372+
!process.env.FASTBOOT_DISABLED && fastbootQueryParam;
347373

348374
if (req.serveUrl && enableFastBootServe) {
349375
// if it is a base page request, then have fastboot serve the base page
@@ -410,7 +436,10 @@ module.exports = {
410436
* TODO Allow add-ons to provide own options and merge them with the application's options.
411437
*/
412438
_fastbootOptionsFor(environment, project) {
413-
const configPath = path.join(path.dirname(project.configPath()), 'fastboot.js');
439+
const configPath = path.join(
440+
path.dirname(project.configPath()),
441+
'fastboot.js'
442+
);
414443

415444
if (fs.existsSync(configPath)) {
416445
return require(configPath)(environment);

packages/ember-cli-fastboot/lib/broccoli/fastboot-config.js

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,35 @@
44
const fs = require('fs');
55
const fmt = require('util').format;
66
const uniq = require('ember-cli-lodash-subset').uniq;
7-
const merge = require('ember-cli-lodash-subset').merge;
87
const md5Hex = require('md5-hex');
98
const path = require('path');
109
const Plugin = require('broccoli-plugin');
10+
const SilentError = require('silent-error');
1111

1212
const stringify = require('json-stable-stringify');
1313

1414
const LATEST_SCHEMA_VERSION = 5;
1515

1616
module.exports = class FastBootConfig extends Plugin {
17-
constructor(inputNode, options) {
17+
constructor(inputNode, { project, ui, appConfig, outputPaths }) {
1818
super([inputNode], {
1919
annotation: 'Generate: FastBoot package.json',
20-
persistentOutput: true
20+
persistentOutput: true,
2121
});
2222

23-
this.project = options.project;
23+
this.project = project;
2424

25-
this.name = options.name;
26-
this.ui = options.ui;
27-
this.fastbootAppConfig = options.fastbootAppConfig;
28-
this.outputPaths = options.outputPaths;
29-
this.appName = options.appConfig.modulePrefix;
30-
const appConfigModule = `${this.appName}`;
31-
this.fastbootConfig = {};
32-
this.fastbootConfig[appConfigModule] = options.appConfig;
25+
this.ui = ui;
26+
this.fastbootAppConfig = appConfig.fastboot;
27+
this.outputPaths = outputPaths;
28+
this.appName = appConfig.modulePrefix;
3329
this._fileToChecksumMap = {};
3430

3531
if (this.fastbootAppConfig && this.fastbootAppConfig.htmlFile) {
3632
this.htmlFile = this.fastbootAppConfig.htmlFile;
3733
} else {
3834
this.htmlFile = 'index.html';
3935
}
40-
41-
this.prepareConfig();
42-
this.prepareDependencies();
4336
}
4437

4538
/**
@@ -48,18 +41,12 @@ module.exports = class FastBootConfig extends Plugin {
4841
* and write it to `package.json`.
4942
*/
5043
build() {
44+
this.prepareDependencies();
5145
this.buildHostWhitelist();
5246
let outputPath = path.join(this.outputPath, 'package.json');
5347
this.writeFileIfContentChanged(outputPath, this.toJSONString());
5448
}
5549

56-
get manifest() {
57-
if (!this._manifest) {
58-
this._manifest = this.buildManifest();
59-
}
60-
return this._manifest;
61-
}
62-
6350
writeFileIfContentChanged(outputPath, content) {
6451
let previous = this._fileToChecksumMap[outputPath];
6552
let next = md5Hex(content);
@@ -70,39 +57,26 @@ module.exports = class FastBootConfig extends Plugin {
7057
}
7158
}
7259

73-
prepareConfig() {
74-
// we only walk the host app's addons to grab the config since ideally
75-
// addons that have dependency on other addons would never define
76-
// this advance hook.
77-
this.project.addons.forEach(addon => {
78-
if (addon.fastbootConfigTree) {
79-
let configFromAddon = addon.fastbootConfigTree();
80-
81-
if (!configFromAddon) {
82-
throw new Error('`fastbootConfigTree` requires a map to be returned');
83-
}
84-
85-
merge(this.fastbootConfig, configFromAddon);
86-
}
87-
});
88-
}
89-
9060
prepareDependencies() {
9161
let dependencies = {};
9262
let moduleWhitelist = [];
9363
let ui = this.ui;
9464

95-
eachAddonPackage(this.project, pkg => {
65+
eachAddonPackage(this.project, (pkg) => {
9666
let deps = getFastBootDependencies(pkg);
9767

9868
if (deps) {
99-
deps.forEach(dep => {
69+
deps.forEach((dep) => {
10070
let version = getDependencyVersion(pkg, dep);
10171

10272
if (dep in dependencies) {
10373
version = dependencies[dep];
10474
ui.writeLine(
105-
fmt('Duplicate FastBoot dependency %s. Versions may mismatch. Using range %s.', dep, version),
75+
fmt(
76+
'Duplicate FastBoot dependency %s. Versions may mismatch. Using range %s.',
77+
dep,
78+
version
79+
),
10680
ui.WARNING
10781
);
10882
return;
@@ -121,7 +95,7 @@ module.exports = class FastBootConfig extends Plugin {
12195
let projectDeps = pkg.fastbootDependencies;
12296

12397
if (projectDeps) {
124-
projectDeps.forEach(dep => {
98+
projectDeps.forEach((dep) => {
12599
moduleWhitelist.push(dep);
126100

127101
let version = pkg.dependencies && pkg.dependencies[dep];
@@ -136,12 +110,14 @@ module.exports = class FastBootConfig extends Plugin {
136110
}
137111

138112
updateFastBootManifest(manifest) {
139-
this.project.addons.forEach(addon => {
113+
this.project.addons.forEach((addon) => {
140114
if (addon.updateFastBootManifest) {
141115
manifest = addon.updateFastBootManifest(manifest);
142116

143117
if (!manifest) {
144-
throw new Error(`${addon.name} did not return the updated manifest from updateFastBootManifest hook.`);
118+
throw new Error(
119+
`${addon.name} did not return the updated manifest from updateFastBootManifest hook.`
120+
);
145121
}
146122
}
147123
});
@@ -161,7 +137,7 @@ module.exports = class FastBootConfig extends Plugin {
161137
let manifest = {
162138
appFiles: [appFilePath, appFastbootFilePath],
163139
vendorFiles: [vendorFilePath],
164-
htmlFile: this.htmlFile
140+
htmlFile: this.htmlFile,
165141
};
166142

167143
return this.updateFastBootManifest(manifest);
@@ -174,6 +150,7 @@ module.exports = class FastBootConfig extends Plugin {
174150
}
175151

176152
toJSONString() {
153+
let manifest = this.buildManifest();
177154
return stringify(
178155
{
179156
name: this.appName,
@@ -184,9 +161,9 @@ module.exports = class FastBootConfig extends Plugin {
184161
hostWhitelist: this.normalizeHostWhitelist(),
185162
// We can't drop manifest until broccoli-asset-rev also supports v5 HTML based manifest
186163
// https://github.com/ember-cli/broccoli-asset-rev/blob/78f6047c15acb3bd348611f658b03bdd1041911f/lib/fastboot-manifest-rewrite.js
187-
manifest: this.manifest,
188-
htmlEntrypoint: this.manifest.htmlFile
189-
}
164+
manifest,
165+
htmlEntrypoint: manifest.htmlFile,
166+
},
190167
},
191168
null,
192169
2
@@ -198,7 +175,7 @@ module.exports = class FastBootConfig extends Plugin {
198175
return;
199176
}
200177

201-
return this.hostWhitelist.map(function(entry) {
178+
return this.hostWhitelist.map(function (entry) {
202179
// Is a regex
203180
if (entry.source) {
204181
return '/' + entry.source + '/';
@@ -210,7 +187,7 @@ module.exports = class FastBootConfig extends Plugin {
210187
};
211188

212189
function eachAddonPackage(project, cb) {
213-
project.addons.map(addon => cb(addon.pkg));
190+
project.addons.map((addon) => cb(addon.pkg));
214191
}
215192

216193
function getFastBootDependencies(pkg) {
@@ -232,7 +209,13 @@ function getFastBootDependencies(pkg) {
232209

233210
function getDependencyVersion(pkg, dep) {
234211
if (!pkg.dependencies) {
235-
throw new Error(fmt("Could not find FastBoot dependency '%s' in %s/package.json dependencies.", dep, pkg.name));
212+
throw new Error(
213+
fmt(
214+
"Could not find FastBoot dependency '%s' in %s/package.json dependencies.",
215+
dep,
216+
pkg.name
217+
)
218+
);
236219
}
237220

238221
return pkg.dependencies[dep];

0 commit comments

Comments
 (0)