Skip to content

Commit fb8e4b5

Browse files
committed
Extend FastBoot.config to read other provided config as well.
1 parent 77f94d5 commit fb8e4b5

10 files changed

Lines changed: 87996 additions & 9 deletions

File tree

src/ember-app.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ class EmberApp {
3737
this.vendorFilePaths = config.vendorFiles;
3838
this.moduleWhitelist = config.moduleWhitelist;
3939
this.hostWhitelist = config.hostWhitelist;
40-
this.appConfig = config.appConfig;
40+
this.config = config.config;
41+
this.appName = config.appName;
4142

4243
if (process.env.APP_CONFIG) {
43-
this.appConfig = JSON.parse(process.env.APP_CONFIG);
44+
let appConfig = JSON.parse(process.env.APP_CONFIG);
45+
let appConfigKey = this.appName;
46+
if (!appConfig.hasOwnProperty(appConfigKey)) {
47+
this.config[appConfigKey] = appConfig;
48+
}
49+
}
50+
51+
if (process.env.ALL_CONFIG) {
52+
let allConfig = JSON.parse(process.env.ALL_CONFIG);
53+
this.config = allConfig;
4454
}
4555

4656
this.html = fs.readFileSync(config.htmlFile, 'utf8');
@@ -60,17 +70,27 @@ class EmberApp {
6070
*/
6171
buildSandbox(distPath, sandboxClass, sandboxGlobals) {
6272
let sandboxRequire = this.buildWhitelistedRequire(this.moduleWhitelist, distPath);
63-
let config = this.appConfig;
64-
function appConfig() {
65-
return { default: config };
73+
const config = this.config;
74+
const appName = this.appName;
75+
function fastbootConfig(key) {
76+
if (!key) {
77+
// default to app key
78+
key = appName;
79+
}
80+
81+
if (config) {
82+
return { default: config[key] };
83+
} else {
84+
return { default: undefined };
85+
}
6686
}
6787

6888
// add any additional user provided variables or override the default globals in the sandbox
6989
let globals = {
7090
najax,
7191
FastBoot: {
7292
require: sandboxRequire,
73-
config: appConfig
93+
config: fastbootConfig
7494
}
7595
};
7696

@@ -357,6 +377,17 @@ class EmberApp {
357377
manifest = this.transformManifestFiles(manifest);
358378
}
359379

380+
let config = pkg.fastboot.config;
381+
let appName = pkg.fastboot.appName;
382+
if (schemaVersion < FastBootSchemaVersions.configExtension) {
383+
// read from the appConfig tree
384+
if (pkg.fastboot.appConfig) {
385+
appName = pkg.fastboot.appConfig.modulePrefix;
386+
config = {};
387+
config[appName] = pkg.fastboot.appConfig;
388+
}
389+
}
390+
360391
debug("reading array of app file paths from manifest");
361392
var appFiles = manifest.appFiles.map(function(appFile) {
362393
return path.join(distPath, appFile);
@@ -373,7 +404,8 @@ class EmberApp {
373404
htmlFile: path.join(distPath, manifest.htmlFile),
374405
moduleWhitelist: pkg.fastboot.moduleWhitelist,
375406
hostWhitelist: pkg.fastboot.hostWhitelist,
376-
appConfig: pkg.fastboot.appConfig
407+
config: config,
408+
appName: appName,
377409
};
378410
}
379411

src/fastboot-schema-versions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
* should be added in fastboot lib) everytime fastboot addon schema version is bumped.
88
*/
99
const FastBootSchemaVersions = {
10-
'latest': 2, // latest schema version supported by fastboot library
10+
'latest': 3, // latest schema version supported by fastboot library
1111
'base': 1, // first schema version supported by fastboot library
12-
'manifestFileArrays': 2 // schema version when app and vendor in manifest supported an array of files
12+
'manifestFileArrays': 2, // schema version when app and vendor in manifest supported an array of files
13+
'configExtension': 3 // schema version when FastBoot.config can read arbitrary indexed config
1314
};
1415

1516
module.exports = FastBootSchemaVersions;

test/fastboot-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,17 @@ describe("FastBoot", function() {
365365
});
366366
});
367367

368+
it("can read multiple configs", function() {
369+
var fastboot = new FastBoot({
370+
distPath: fixture('app-with-multiple-config')
371+
});
372+
373+
return fastboot.visit('/')
374+
.then(r => r.html())
375+
.then(html => {
376+
expect(html).to.match(/App Name: app-with-multiple-configs/);
377+
expect(html).to.match(/Other Config {"default":"bar"}/);
378+
});
379+
});
380+
368381
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
define('app-with-multiple-configs/initializers/ajax', ['exports'], function (exports) {
2+
'use strict';
3+
4+
Object.defineProperty(exports, "__esModule", {
5+
value: true
6+
});
7+
var get = Ember.get;
8+
9+
10+
var nodeAjax = function nodeAjax(options) {
11+
var httpRegex = /^https?:\/\//;
12+
var protocolRelativeRegex = /^\/\//;
13+
var protocol = get(this, 'fastboot.request.protocol');
14+
15+
if (protocolRelativeRegex.test(options.url)) {
16+
options.url = protocol + options.url;
17+
} else if (!httpRegex.test(options.url)) {
18+
try {
19+
options.url = protocol + '//' + get(this, 'fastboot.request.host') + options.url;
20+
} catch (fbError) {
21+
throw new Error('You are using Ember Data with no host defined in your adapter. This will attempt to use the host of the FastBoot request, which is not configured for the current host of this request. Please set the hostWhitelist property for in your environment.js. FastBoot Error: ' + fbError.message);
22+
}
23+
}
24+
25+
if (najax) {
26+
najax(options);
27+
} else {
28+
throw new Error('najax does not seem to be defined in your app. Did you override it via `addOrOverrideSandboxGlobals` in the fastboot server?');
29+
}
30+
};
31+
32+
exports.default = {
33+
name: 'ajax-service',
34+
35+
initialize: function initialize(application) {
36+
application.register('ajax:node', nodeAjax, { instantiate: false });
37+
application.inject('adapter', '_ajaxRequest', 'ajax:node');
38+
application.inject('adapter', 'fastboot', 'service:fastboot');
39+
}
40+
};
41+
});
42+
define('app-with-multiple-configs/initializers/cat', ['exports'], function (exports) {
43+
'use strict';
44+
45+
Object.defineProperty(exports, "__esModule", {
46+
value: true
47+
});
48+
exports.default = {
49+
name: 'cat',
50+
51+
initialize: function initialize(application) {
52+
console.log('I got a cat');
53+
}
54+
};
55+
});
56+
define('app-with-multiple-configs/initializers/error-handler', ['exports'], function (exports) {
57+
'use strict';
58+
59+
Object.defineProperty(exports, "__esModule", {
60+
value: true
61+
});
62+
exports.default = {
63+
name: 'error-handler',
64+
65+
initialize: function initialize(application) {
66+
if (!Ember.onerror) {
67+
// if no onerror handler is defined, define one for fastboot environments
68+
Ember.onerror = function (err) {
69+
var errorMessage = 'There was an error running your app in fastboot. More info about the error: \n ' + (err.stack || err);
70+
Ember.Logger.error(errorMessage);
71+
};
72+
}
73+
}
74+
};
75+
});//# sourceMappingURL=app-with-multiple-configs-fastboot.map

0 commit comments

Comments
 (0)