Skip to content

Commit

Permalink
Fix Ember global deprecation
Browse files Browse the repository at this point in the history
A minimal fix for #600. We should probably follow through with the
refactor suggested by @rwjblue in #600 but that can be done later.

This is probably not very embroider friendly, but neither is the
current code. It's probably easier to just rewrite the shims in
terms of ES modules and embroider macros, i.e. what was suggested
in #337.
  • Loading branch information
chancancode committed Jun 2, 2021
1 parent 3e02036 commit 157172f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
8 changes: 3 additions & 5 deletions assets/browser-fetch.js.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
(function (originalGlobal) {
define('fetch', ['exports'], function(exports) {
'use strict';
var Promise = originalGlobal.Ember.RSVP.Promise;
<%= moduleHeader %>
var supportProps = [
'FormData',
'FileReader',
Expand Down Expand Up @@ -49,8 +47,8 @@
return result;
}

if (originalGlobal.Ember.Test) {
originalGlobal.Ember.Test.registerWaiter(function() {
if (Ember.Test) {
Ember.Test.registerWaiter(function() {
return pending === 0;
});

Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const map = stew.map;
const Rollup = require('broccoli-rollup');
const BroccoliDebug = require('broccoli-debug');
const calculateCacheKeyForTree = require('calculate-cache-key-for-tree');
const VersionChecker = require('ember-cli-version-checker');

const debug = BroccoliDebug.buildDebugCallback('ember-fetch');

Expand Down Expand Up @@ -73,6 +74,9 @@ module.exports = {
let hasEmberFetch = !!this.project.findAddonByName('ember-fetch');
let hasEmberCliFastboot = !!this.project.findAddonByName('ember-cli-fastboot');

let emberSource = new VersionChecker(this.project).for('ember-source');
let hasEmberSourceModules = emberSource.exists() && emberSource.gte('3.27.0');

if(isApp && hasEmberCliFastboot && !hasEmberFetch) {
throw new Error(`Ember fetch is not installed as top-level dependency of the application using fastboot. Add ember-fetch as dependecy in application's package.json.
For details check here - https://github.com/ember-cli/ember-fetch#top-level-addon`);
Expand All @@ -88,6 +92,7 @@ module.exports = {
app._fetchBuildConfig = Object.assign({
preferNative: false,
alwaysIncludePolyfill: false,
hasEmberSourceModules,
browsers: this.project.targets && this.project.targets.browsers
}, app.options['ember-fetch']);

Expand Down Expand Up @@ -216,8 +221,19 @@ 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');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"caniuse-api": "^3.0.0",
"ember-cli-babel": "^7.23.1",
"ember-cli-typescript": "^3.1.3",
"ember-cli-version-checker": "^5.1.2",
"node-fetch": "^2.6.1",
"whatwg-fetch": "^3.6.2"
},
Expand Down

0 comments on commit 157172f

Please sign in to comment.