Skip to content

Commit

Permalink
Add option forcePolyfill
Browse files Browse the repository at this point in the history
If set to false, native `fetch` access will be enabled.
  • Loading branch information
xg-wang committed May 17, 2018
1 parent 3765c6b commit 42691b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
48 changes: 29 additions & 19 deletions assets/browser-fetch.js.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@
define('fetch', ['exports'], function(self) {
'use strict';
var Promise = global.Ember.RSVP.Promise;
var window = self;
if (global.FormData) {
self.FormData = global.FormData;
}
if (global.FileReader) {
self.FileReader = global.FileReader;
}
if (global.Blob) {
self.Blob = global.Blob;
}
if (global.ArrayBuffer) {
self.ArrayBuffer = global.ArrayBuffer;
}
if (global.Symbol) {
self.Symbol = global.Symbol;
}
if (global.URLSearchParams) {
self.URLSearchParams = global.URLSearchParams;
var supportProps = [
'FormData',
'FileReader',
'Blob',
'URLSearchParams',
'Symbol',
'ArrayBuffer'
];
var polyfillProps = [
'fetch',
'Headers',
'Request',
'Response'
];
var combinedProps = supportProps;
if (!forcePolyfill) {
combinedProps = supportProps.concat(polyfillProps);
}
combinedProps.forEach(function(prop) {
if (global[prop]) {
self[prop] = global[prop];
}
});

var window = self;

<%= moduleBody %>
debugger;

var pending = 0;
function decrement(result) {
Expand All @@ -38,7 +45,7 @@
self['default'] = function() {
pending++;

return self.fetch.apply(self, arguments).then(function(response){
return self.fetch.apply(forcePolyfill ? self : global, arguments).then(function(response){
response.clone().blob().then(decrement, decrement);
return response;
}, function(reason) {
Expand All @@ -49,6 +56,9 @@
} else {
self['default'] = self.fetch;
}
supportProps.forEach(function(prop) {
delete self[prop];
});
});

define('fetch/ajax', ['exports'], function() {
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = {
*/
included: function(app) {
this._super.included.apply(this, arguments);

let target = app;

if (typeof this.import === 'function') {
Expand All @@ -75,6 +75,8 @@ module.exports = {
} while (current.parent.parent && (current = current.parent));
}

this.buildConfig = target.options['ember-fetch'] || {forcePolyfill: true};

target.import('vendor/ember-fetch.js', {
exports: {
default: [
Expand All @@ -92,12 +94,14 @@ module.exports = {
* directory is kind of a junk drawer; nothing we put in it is used unless we
* explicitly `import()` a file (which we do in the `included` hook, above).
*
* To build our tree, we first detect whether we're in a FastBoot build or
* not. Based on that, we return a tree that contains the correct version of
* the polyfill at the `vendor/fetch.js` path.
* To build our tree, we first pass in option flags and detect whether we're
* in a FastBoot build or not. Based on that, we return a tree that contains
* the correct version of the polyfill at the `vendor/fetch.js` path.
*/
treeForVendor: function() {
var browserTree = treeForBrowserFetch();
var forcePolyfill = this.buildConfig.forcePolyfill;
browserTree = map(browserTree, (content) => `var forcePolyfill = ${forcePolyfill}; ${content}`);
browserTree = map(browserTree, (content) => `if (typeof FastBoot === 'undefined') { ${content} }`);
return browserTree;
},
Expand Down

0 comments on commit 42691b8

Please sign in to comment.