diff --git a/assets/browser-fetch.js.t b/assets/browser-fetch.js.t index d762e0cd..7441dee1 100644 --- a/assets/browser-fetch.js.t +++ b/assets/browser-fetch.js.t @@ -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) { @@ -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) { @@ -49,6 +56,9 @@ } else { self['default'] = self.fetch; } + supportProps.forEach(function(prop) { + delete self[prop]; + }); }); define('fetch/ajax', ['exports'], function() { diff --git a/index.js b/index.js index c3107ec3..59c41568 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ module.exports = { */ included: function(app) { this._super.included.apply(this, arguments); - + let target = app; if (typeof this.import === 'function') { @@ -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: [ @@ -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; },