Skip to content

Commit d77b020

Browse files
authored
Merge pull request #470 from kratiahuja/new-import-api
Register fastboot transform
2 parents a2540bf + 1d1a578 commit d77b020

File tree

17 files changed

+530
-520
lines changed

17 files changed

+530
-520
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ By default, FastBoot waits for the `beforeModel`, `model`, and
131131
client. If you have asynchrony that runs outside of those contexts, your
132132
response may not reflect the state that you want.
133133

134-
To solve this, the `fastboot` service has `deferRendering` method that accepts
134+
To solve this, the `fastboot` service has `deferRendering` method that accepts
135135
a promise. It will chain all promises passed to it, and the FastBoot server will
136136
wait until all of these promises resolve before sending the response to
137137
the client. These promises must be chained before the rendering is
@@ -142,7 +142,7 @@ defer the rendering of the page.
142142

143143
The following example demonstrates how the `deferRendering` method can be
144144
used to ensure posts data has been loaded asynchronously by a component before
145-
rendering the entire page. Note how the call should be wrapped in a `fastboot.isFastboot`
145+
rendering the entire page. Note how the call should be wrapped in a `fastboot.isFastboot`
146146
check since the method will throw an exception outside of that context:
147147

148148
```js
@@ -383,8 +383,20 @@ export default Ember.Route.extend({
383383
There are two places where the inclusion of incompatible JavaScript libraries could
384384
occur:
385385

386-
1. `app.import` in the application's `ember-cli-build.js`
387-
2. `app.import` in an addon's `included` hook
386+
#### `app.import` in the application's `ember-cli-build.js`
387+
388+
If your Ember application is importing an incompatible Javascript library,you can use `app.import` with the `using` API.
389+
390+
```js
391+
app.import('vendor/fastboot-incompatible.js', {
392+
using: [
393+
{
394+
transformation: 'fastbootShim'
395+
}
396+
]
397+
});
398+
```
399+
#### `app.import` in an addon's `included` hook
388400

389401
You can include the incompatible Javascript libraries by wrapping them with a `FastBoot` variable check. In the browser, `FastBoot` global variable is not defined.
390402

index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const migrateInitializers = require('./lib/build-utilities/migrate-initializers'
1616
const Concat = require('broccoli-concat');
1717
const Funnel = require('broccoli-funnel');
1818
const p = require('ember-cli-preprocess-registry/preprocessors');
19+
const fastbootTransform = require('fastboot-transform');
1920
const existsSync = fs.existsSync;
2021

2122
let checker;
@@ -27,6 +28,8 @@ function getVersionChecker(context) {
2728
return checker;
2829
}
2930

31+
32+
3033
/*
3134
* Main entrypoint for the Ember CLI addon.
3235
*/
@@ -63,6 +66,17 @@ module.exports = {
6366
migrateInitializers(this.project);
6467
},
6568

69+
/**
70+
* Registers the fastboot shim that allows apps and addons to wrap non-compatible
71+
* libraries in Node with a FastBoot check using `app.import`.
72+
*
73+
*/
74+
importTransforms() {
75+
return {
76+
fastbootShim: fastbootTransform
77+
}
78+
},
79+
6680
/**
6781
* Inserts placeholders into index.html that are used by the FastBoot server
6882
* to insert the rendered content into the right spot. Also injects a module
@@ -206,8 +220,8 @@ module.exports = {
206220
/**
207221
* Need to handroll our own clone algorithm since JSON.stringy changes regex
208222
* to empty objects which breaks hostWhiteList property of fastboot.
209-
*
210-
* @param {Object} config
223+
*
224+
* @param {Object} config
211225
*/
212226
_cloneConfigObject(config) {
213227
if (config === null || typeof config !== 'object') {

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"ember-cli-version-checker": "^2.0.0",
3030
"fastboot": "^1.1.0",
3131
"fastboot-express-middleware": "^1.1.0",
32-
"fs-extra": "^4.0.0",
3332
"json-stable-stringify": "^1.0.1",
3433
"md5-hex": "^2.0.0",
3534
"silent-error": "^1.0.0"
@@ -42,7 +41,7 @@
4241
"chai-fs": "^1.0.0",
4342
"co": "^4.6.0",
4443
"ember-ajax": "^3.0.0",
45-
"ember-cli": "~2.15.1",
44+
"ember-cli": "~2.16.2",
4645
"ember-cli-addon-tests": "^0.11.0",
4746
"ember-cli-dependency-checker": "^2.0.1",
4847
"ember-cli-eslint": "^4.1.0",
@@ -59,6 +58,8 @@
5958
"ember-resolver": "^4.3.0",
6059
"ember-sinon": "^1.0.0",
6160
"ember-source": "~2.16.0",
61+
"fastboot-transform": "^0.1.2",
62+
"fs-extra": "^4.0.0",
6263
"glob": "^7.0.0",
6364
"loader.js": "^4.2.3",
6465
"mocha": "^4.0.0",

test/fixtures/dummy/app/router.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Router = Ember.Router.extend({
88
Router.map(function() {
99
this.route('posts');
1010
this.route('boom');
11+
this.route('imports');
1112
});
1213

1314
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: function() {
5+
return {
6+
importStatus: window.browserImportStatus || 'FastBoot default default value'
7+
};
8+
}
9+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FastBoot compatible vendor file: {{model.importStatus}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*jshint node:true*/
2+
/* global require, module */
3+
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
4+
5+
module.exports = function(defaults) {
6+
var app = new EmberApp(defaults, {});
7+
8+
app.import('vendor/fastboot-incompatible.js', {
9+
using: [
10+
{
11+
transformation: 'fastbootShim'
12+
}
13+
]
14+
});
15+
16+
return app.toTree();
17+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.browserImportStatus = 'This file was never imported in FastBoot';

test/simple-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ describe('simple acceptance', function() {
111111
});
112112
});
113113

114+
it('/imports HTML contents', function() {
115+
return request({
116+
url: 'http://localhost:49741/imports',
117+
headers: {
118+
'Accept': 'text/html'
119+
}
120+
})
121+
.then(function(response) {
122+
console.log(response.body);
123+
expect(response.statusCode).to.equal(200);
124+
expect(response.headers["content-type"]).to.eq("text/html; charset=utf-8");
125+
expect(response.body).to.contain("FastBoot compatible vendor file: FastBoot default default value");
126+
});
127+
});
128+
114129
it('/assets/vendor.js', function() {
115130
return request('http://localhost:49741/assets/vendor.js')
116131
.then(function(response) {

testem.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ module.exports = {
99
'Chrome'
1010
],
1111
browser_args: {
12-
Chrome: [
12+
Chrome: {
13+
mode: 'ci',
14+
args: [
1315
'--disable-gpu',
1416
'--headless',
1517
'--remote-debugging-port=9222',
1618
'--window-size=1440,900'
17-
]
19+
]
20+
}
1821
}
1922
};

0 commit comments

Comments
 (0)