Skip to content

Commit 814f012

Browse files
authored
Merge pull request #172 from mydea/fix-in-engines
Make configuration work in engines
2 parents 3b8e4ad + e09bd95 commit 814f012

File tree

2 files changed

+205
-24
lines changed

2 files changed

+205
-24
lines changed

index.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,19 @@ module.exports = {
6363
* which allows us to use the `import()` method to tell it to include a file
6464
* from our `vendor` tree into the final built app.
6565
*/
66-
included: function(app) {
66+
included: function() {
6767
this._super.included.apply(this, arguments);
6868

69-
let target = app;
69+
let app = this._findApp();
70+
let importTarget = app;
7071

7172
if (typeof this.import === 'function') {
72-
target = this;
73-
} else {
74-
// If the addon has the _findHost() method (in ember-cli >= 2.7.0), we'll just
75-
// use that.
76-
if (typeof this._findHost === 'function') {
77-
target = this._findHost();
78-
} else {
79-
// Otherwise, we'll use this implementation borrowed from the _findHost()
80-
// method in ember-cli.
81-
// Keep iterating upward until we don't have a grandparent.
82-
// Has to do this grandparent check because at some point we hit the project.
83-
let current = this;
84-
do {
85-
target = current.app || app;
86-
} while (current.parent.parent && (current = current.parent));
87-
}
88-
// If this.import is not a function, app and target should point to the same EmberApp
89-
app = target;
73+
importTarget = this;
9074
}
9175

92-
this.buildConfig = app.options['ember-fetch'] || { preferNative: false };
76+
app._fetchBuildConfig = app.options['ember-fetch'] || { preferNative: false };
9377

94-
target.import('vendor/ember-fetch.js', {
78+
importTarget.import('vendor/ember-fetch.js', {
9579
exports: {
9680
default: [
9781
'default',
@@ -128,7 +112,8 @@ module.exports = {
128112
this.ui.writeWarnLine('[ember-fetch] Could not find `ember-cli-babel` addon, opting out of transpilation!')
129113
}
130114

131-
const preferNative = this.buildConfig.preferNative;
115+
const app = this._findApp();
116+
const preferNative = app._fetchBuildConfig.preferNative;
132117

133118
return debug(map(browserTree, (content) => `if (typeof FastBoot === 'undefined') {
134119
var preferNative = ${preferNative};
@@ -181,4 +166,22 @@ module.exports = {
181166
};
182167
}), 'browser-fetch');
183168
},
169+
170+
_findApp() {
171+
if (typeof this._findHost === 'function') {
172+
return this._findHost();
173+
} else {
174+
// Otherwise, we'll use this implementation borrowed from the _findHost()
175+
// method in ember-cli.
176+
// Keep iterating upward until we don't have a grandparent.
177+
// Has to do this grandparent check because at some point we hit the project.
178+
let app;
179+
let current = this;
180+
do {
181+
app = current.app || this;
182+
} while (current.parent && current.parent.parent && (current = current.parent));
183+
184+
return app;
185+
}
186+
},
184187
};

test/prefer-native-test.js

Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require('fetch-test');
2323
addon = Object.create(AddonFactory);
2424
Object.assign(addon, {
2525
addons: [],
26-
buildConfig: {
26+
_fetchBuildConfig: {
2727
preferNative
2828
},
2929
ui: {
@@ -95,4 +95,182 @@ require('fetch-test');
9595
}))
9696
}
9797
});
98+
99+
describe(`Build browser assets with preferNative = ${preferNative} in nested dependencies`, function() {
100+
let output, subject, addon;
101+
102+
beforeEach(function() {
103+
addon = Object.create(AddonFactory);
104+
105+
let app = {
106+
_fetchBuildConfig: {
107+
preferNative
108+
}
109+
};
110+
111+
Object.assign(addon, {
112+
addons: [],
113+
_findHost() {
114+
return app;
115+
},
116+
ui: {
117+
writeWarnLine() {},
118+
},
119+
});
120+
subject = addon.treeForVendor();
121+
output = helpers.createBuilder(subject);
122+
});
123+
124+
afterEach(co.wrap(function* () {
125+
yield output.dispose();
126+
}));
127+
128+
it('preferNative is built into vendor file', co.wrap(function*() {
129+
yield output.build();
130+
let files = output.read();
131+
expect(files).to.have.all.keys('ember-fetch.js');
132+
expect(files['ember-fetch.js']).to.include(`var preferNative = ${preferNative}`);
133+
}));
134+
135+
it(`${
136+
preferNative ? 'Prefers' : "Doesn't prefer"
137+
} native fetch as specified`, co.wrap(function*() {
138+
yield output.build();
139+
let emberFetchCode = output.read()['ember-fetch.js'];
140+
const amdLoaderCode = fs.readFileSync(require.resolve('loader.js'));
141+
const sandbox = {
142+
__result: false,
143+
window: {
144+
fetch: function() {
145+
sandbox.__result = true;
146+
},
147+
Ember: { RSVP }
148+
}
149+
};
150+
vm.createContext(sandbox);
151+
const code = amdLoaderCode + emberFetchCode + testCode;
152+
vm.runInContext(code, sandbox);
153+
expect(sandbox.__result).to.equal(preferNative);
154+
}));
155+
156+
if (preferNative === true) {
157+
it('Has fetch poly fill even if fetch is not there', co.wrap(function*() {
158+
yield output.build();
159+
let emberFetchCode = output.read()['ember-fetch.js'];
160+
const amdLoaderCode = fs.readFileSync(require.resolve('loader.js'));
161+
const sandbox = {
162+
console,
163+
window: {
164+
__result: false,
165+
// no fetch here
166+
// fetch: function() {},
167+
Ember: { RSVP }
168+
}
169+
};
170+
vm.createContext(sandbox);
171+
const testCodeForNonFetch = `
172+
define('fetch-test', ['fetch'], function(_fetch) {
173+
if (_fetch.default.polyfill) {
174+
window.__result = true
175+
}
176+
});
177+
require('fetch-test');
178+
`;
179+
const code = amdLoaderCode + emberFetchCode + testCodeForNonFetch;
180+
vm.runInContext(code, sandbox);
181+
expect(sandbox.window.__result).to.equal(true);
182+
}))
183+
}
184+
});
185+
186+
describe(`Build browser assets with preferNative = ${preferNative} in nested dependencies without _findHost`, function() {
187+
let output, subject, addon;
188+
189+
beforeEach(function() {
190+
addon = Object.create(AddonFactory);
191+
192+
let app = {
193+
_fetchBuildConfig: {
194+
preferNative
195+
}
196+
};
197+
198+
Object.assign(addon, {
199+
addons: [],
200+
app: this,
201+
parent: {
202+
parent: {
203+
app,
204+
parent: {}
205+
}
206+
},
207+
ui: {
208+
writeWarnLine() {},
209+
},
210+
});
211+
subject = addon.treeForVendor();
212+
output = helpers.createBuilder(subject);
213+
});
214+
215+
afterEach(co.wrap(function* () {
216+
yield output.dispose();
217+
}));
218+
219+
it('preferNative is built into vendor file', co.wrap(function*() {
220+
yield output.build();
221+
let files = output.read();
222+
expect(files).to.have.all.keys('ember-fetch.js');
223+
expect(files['ember-fetch.js']).to.include(`var preferNative = ${preferNative}`);
224+
}));
225+
226+
it(`${
227+
preferNative ? 'Prefers' : "Doesn't prefer"
228+
} native fetch as specified`, co.wrap(function*() {
229+
yield output.build();
230+
let emberFetchCode = output.read()['ember-fetch.js'];
231+
const amdLoaderCode = fs.readFileSync(require.resolve('loader.js'));
232+
const sandbox = {
233+
__result: false,
234+
window: {
235+
fetch: function() {
236+
sandbox.__result = true;
237+
},
238+
Ember: { RSVP }
239+
}
240+
};
241+
vm.createContext(sandbox);
242+
const code = amdLoaderCode + emberFetchCode + testCode;
243+
vm.runInContext(code, sandbox);
244+
expect(sandbox.__result).to.equal(preferNative);
245+
}));
246+
247+
if (preferNative === true) {
248+
it('Has fetch poly fill even if fetch is not there', co.wrap(function*() {
249+
yield output.build();
250+
let emberFetchCode = output.read()['ember-fetch.js'];
251+
const amdLoaderCode = fs.readFileSync(require.resolve('loader.js'));
252+
const sandbox = {
253+
console,
254+
window: {
255+
__result: false,
256+
// no fetch here
257+
// fetch: function() {},
258+
Ember: { RSVP }
259+
}
260+
};
261+
vm.createContext(sandbox);
262+
const testCodeForNonFetch = `
263+
define('fetch-test', ['fetch'], function(_fetch) {
264+
if (_fetch.default.polyfill) {
265+
window.__result = true
266+
}
267+
});
268+
require('fetch-test');
269+
`;
270+
const code = amdLoaderCode + emberFetchCode + testCodeForNonFetch;
271+
vm.runInContext(code, sandbox);
272+
expect(sandbox.window.__result).to.equal(true);
273+
}))
274+
}
275+
});
98276
});

0 commit comments

Comments
 (0)