Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use native promises instead of RSVP #623

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion assets/browser-fetch.js.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(function (originalGlobal) {
<%= moduleHeader %>
var Promise = RSVP.Promise;
var supportProps = [
'FormData',
'FileReader',
Expand Down
1 change: 1 addition & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
Expand Down
59 changes: 43 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down