Skip to content

Commit 2458159

Browse files
authored
Merge pull request #752 from zonkyio/fastboot-config
Add support for custom configuration
2 parents 715c5b1 + 40b6634 commit 2458159

7 files changed

Lines changed: 132 additions & 6 deletions

File tree

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,40 @@ visit your app at `http://localhost:4200/?fastboot=false`. If you want to turn o
6060

6161
You can even disable serving fastboot with `ember serve` using an environment flag: `FASTBOOT_DISABLED=true ember serve`. If you have disabled building fastboot assets using the same flag as described [here](https://github.com/ember-fastboot/ember-cli-fastboot#double-build-times-and-no-incremental-builds), remember to also disable serving fastboot assets when using `ember serve`.
6262

63+
### FastBoot Configuration
64+
65+
When running locally using `ember serve` you can pass options into FastBoot instance via `config/fastboot.js` file. The configuration file is applicable only for applications, addons are not supported.
66+
67+
```js
68+
module.exports = function(environment) {
69+
let myGlobal = environment === 'production' ? process.env.MY_GLOBAL : 'testing';
70+
71+
return {
72+
sandboxGlobals: {
73+
myGlobal;
74+
}
75+
};
76+
}
77+
```
78+
79+
There are several options available, see FastBoot's [README](https://github.com/ember-fastboot/fastboot/tree/v2.0.3#usage) for more information, but be aware that `distPath` is provided internally by `ember-cli-fastboot`, hence it can not be modified by this file.
80+
81+
### FastBoot App Server Configuration
82+
83+
When using FastBoot App Server for production environment you have to manually pass options from `config/fastboot.js` file.
84+
85+
```js
86+
const FastBootAppServer = require('fastboot-app-server');
87+
const config = require('./config/fastboot')(process.env.NODE_ENV);
88+
89+
let server = new FastBootAppServer({
90+
distPath: 'dist',
91+
...config,
92+
});
93+
94+
server.start();
95+
```
96+
6397
## Using Node/npm Dependencies
6498

6599
### Whitelisting Packages

index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ module.exports = {
7979
this._appRegistry = app.registry;
8080
this._name = app.name;
8181

82+
this.fastbootOptions = this._fastbootOptionsFor(app.env, app.project);
83+
8284
migrateInitializers(this.project);
8385
},
8486

@@ -323,13 +325,14 @@ module.exports = {
323325
if (!this.fastboot) {
324326
const broccoliHeader = req.headers['x-broccoli'];
325327
const outputPath = broccoliHeader['outputPath'];
328+
const fastbootOptions = Object.assign(
329+
{},
330+
this.fastbootOptions,
331+
{ distPath: outputPath }
332+
);
326333

327-
// TODO(future): make this configurable for allowing apps to pass sandboxGlobals
328-
// and custom sandbox class
329334
this.ui.writeLine(chalk.green('App is being served by FastBoot'));
330-
this.fastboot = new FastBoot({
331-
distPath: outputPath
332-
});
335+
this.fastboot = new FastBoot(fastbootOptions);
333336
}
334337

335338
let fastbootMiddleware = FastBootExpressMiddleware({
@@ -373,8 +376,26 @@ module.exports = {
373376

374377
return checker.for('ember', 'bower');
375378
},
376-
379+
377380
_isModuleUnification() {
378381
return (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification();
382+
},
383+
384+
/**
385+
* Reads FastBoot configuration from application's `config/fastboot.js` file if present,
386+
* otherwise returns empty object.
387+
*
388+
* The configuration file is expected to export a function with `environment` as an argument,
389+
* which is same as a how `config/environment.js` works.
390+
*
391+
* TODO Allow add-ons to provide own options and merge them with the application's options.
392+
*/
393+
_fastbootOptionsFor(environment, project) {
394+
const configPath = path.join(path.dirname(project.configPath()), 'fastboot.js');
395+
396+
if (fs.existsSync(configPath)) {
397+
return require(configPath)(environment);
398+
}
399+
return {};
379400
}
380401
};

test/fastboot-config-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const expect = require('chai').use(require('chai-string')).expect;
4+
const RSVP = require('rsvp');
5+
const request = RSVP.denodeify(require('request'));
6+
7+
const AddonTestApp = require('ember-cli-addon-tests').AddonTestApp;
8+
9+
describe('FastBoot config', function() {
10+
this.timeout(400000);
11+
12+
let app;
13+
14+
before(function() {
15+
app = new AddonTestApp();
16+
17+
return app.create('fastboot-config')
18+
.then(function() {
19+
return app.startServer({
20+
command: 'serve'
21+
});
22+
});
23+
});
24+
25+
after(function() {
26+
return app.stopServer();
27+
});
28+
29+
it('provides sandbox globals', function() {
30+
return request({
31+
url: 'http://localhost:49741/',
32+
headers: {
33+
'Accept': 'text/html'
34+
}
35+
})
36+
.then(function(response) {
37+
expect(response.statusCode).to.equal(200);
38+
expect(response.headers['content-type']).to.equalIgnoreCase('text/html; charset=utf-8');
39+
expect(response.body).to.contain('<h1>My Global</h1>');
40+
});
41+
});
42+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Ember from 'ember';
2+
import config from './config/environment';
3+
4+
const Router = Ember.Router.extend({
5+
location: config.locationType,
6+
rootURL: config.rootURL
7+
});
8+
9+
Router.map(function() {
10+
});
11+
12+
export default Router;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Route.extend({
4+
model() {
5+
if (typeof FastBoot !== 'undefined') {
6+
return window.myGlobal;
7+
}
8+
}
9+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>{{this.model}}</h1>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = function(environment) {
2+
return {
3+
sandboxGlobals: {
4+
myGlobal: 'My Global'
5+
}
6+
};
7+
}

0 commit comments

Comments
 (0)