From ad8be4e490d6d9dd1eb76e03a35590c18f4498bc Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Thu, 10 Jun 2021 17:00:48 +0200 Subject: [PATCH] Allow to use native promises instead of RSVP --- .travis.yml | 1 + README.md | 14 ++++++++++ assets/browser-fetch.js.t | 1 - ember-cli-build.js | 1 + index.js | 59 ++++++++++++++++++++++++++++----------- 5 files changed, 59 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1c7472bb..5c4e110f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,7 @@ jobs: - yarn lint:js - yarn test - PREFER_NATIVE=true yarn test + - NATIVE_PROMISE=true yarn test - npm run test:node - name: "Floating Dependencies" diff --git a/README.md b/README.md index 29f98f52..dd751b33 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,20 @@ If all your [browser targets](https://guides.emberjs.com/release/configuring-emb The way you do import remains same. +### Use native promise instead of RSVP + +If you do not want to use RSVP, but native Promises, you can specify this build config flag: + +```js +// ember-cli-build.js +let app = new EmberAddon(defaults, { + // Add options here + 'ember-fetch': { + nativePromise: true + } +}); +``` + ### Error Handling A `fetch` response is successful if `response.ok` is true, diff --git a/assets/browser-fetch.js.t b/assets/browser-fetch.js.t index 480fb6f4..777b1cf2 100644 --- a/assets/browser-fetch.js.t +++ b/assets/browser-fetch.js.t @@ -1,6 +1,5 @@ (function (originalGlobal) { <%= moduleHeader %> - var Promise = RSVP.Promise; var supportProps = [ 'FormData', 'FileReader', diff --git a/ember-cli-build.js b/ember-cli-build.js index e6243bfb..cf6dd07c 100644 --- a/ember-cli-build.js +++ b/ember-cli-build.js @@ -6,6 +6,7 @@ module.exports = function(defaults) { let app = new EmberAddon(defaults, { // Add options here 'ember-fetch': { + nativePromise: process.env.NATIVE_PROMISE ? true : false, preferNative: process.env.PREFER_NATIVE ? true : false } }); diff --git a/index.js b/index.js index 66b14847..00f29664 100644 --- a/index.js +++ b/index.js @@ -91,6 +91,7 @@ module.exports = { app._fetchBuildConfig = Object.assign({ preferNative: false, + nativePromise: false, alwaysIncludePolyfill: false, hasEmberSourceModules, browsers: this.project.targets && this.project.targets.browsers @@ -221,22 +222,48 @@ module.exports = { sourceMapConfig: { enabled: false } }), 'after-concat'); - const moduleHeader = options.hasEmberSourceModules ? ` - define('fetch', ['exports', 'ember', 'rsvp'], function(exports, Ember__module, RSVP__module) { - 'use strict'; - var Ember = 'default' in Ember__module ? Ember__module['default'] : Ember__module; - var RSVP = 'default' in RSVP__module ? RSVP__module['default'] : RSVP__module;` : ` - define('fetch', ['exports'], function(exports) { - 'use strict'; - var Ember = originalGlobal.Ember; - var RSVP = Ember.RSVP;` - - return debug(new Template(polyfillNode, TEMPLATE_PATH, function(content) { - return { - moduleHeader, - moduleBody: content - }; - }), 'browser-fetch'); + const moduleHeader = this._getModuleHeader(options); + + return debug( + new Template(polyfillNode, TEMPLATE_PATH, function (content) { + return { + moduleHeader, + moduleBody: content, + }; + }), + "browser-fetch" + ); + }, + + _getModuleHeader({ hasEmberSourceModules, nativePromise }) { + if (hasEmberSourceModules && nativePromise) { + return ` +define('fetch', ['exports', 'ember'], function(exports, Ember__module) { + 'use strict'; + var Ember = 'default' in Ember__module ? Ember__module['default'] : Ember__module;`; + } + + if (hasEmberSourceModules) { + return ` +define('fetch', ['exports', 'ember', 'rsvp'], function(exports, Ember__module, RSVP__module) { + 'use strict'; + var Ember = 'default' in Ember__module ? Ember__module['default'] : Ember__module; + var RSVP = 'default' in RSVP__module ? RSVP__module['default'] : RSVP__module; + var Promise = RSVP.Promise;`; + } + + if (nativePromise) { + return ` +define('fetch', ['exports'], function(exports) { + 'use strict'; + var Ember = originalGlobal.Ember;`; + } + + return ` +define('fetch', ['exports'], function(exports) { + 'use strict'; + var Ember = originalGlobal.Ember; + var Promise = Ember.RSVP.Promise;`; }, _checkSupports(featureName, browsers) {