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

Remove window.Ember global #19678

Merged
merged 1 commit into from
Aug 17, 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
16 changes: 0 additions & 16 deletions lib/overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,6 @@ module.exports = class Overrides {

toModule() {
return `
export let onEmberGlobalAccess;

${this.toJS()};
`;
}
Expand All @@ -347,20 +345,6 @@ module.exports = class Overrides {
}

${this.onDotAcces}

onEmberGlobalAccess = ${this.onEmberGlobalAccess};

if (!${this.showAllEmberGlobalDeprecations}) {
onEmberGlobalAccess = once(onEmberGlobalAccess);
}
`;
}

get onEmberGlobalAccess() {
return `
function onEmberGlobalAccess() {
return ${JSON.stringify(this.globalMessage)};
}
`;
}
};
56 changes: 1 addition & 55 deletions packages/@ember/-internals/bootstrap/index.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,10 @@
import { context } from '@ember/-internals/environment';
import { onEmberGlobalAccess } from '@ember/-internals/overrides';
import { deprecate } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import require from 'require';

(function bootstrap() {
let Ember: unknown;

let get = () => {
if (!Ember) {
// tslint:disable-next-line: no-require-imports
Ember = require('ember').default;
}

return Ember;
};

if (DEBUG) {
let defaultHandler = () => {
return 'Usage of the Ember Global is deprecated. You should import the Ember module or the specific API instead.';
};

let handler = onEmberGlobalAccess || defaultHandler;
let _get = get;

get = () => {
let message = handler();

if (message !== null) {
deprecate(message, false, {
id: 'ember-global',
until: '4.0.0',
url: 'https://deprecations.emberjs.com/v3.x/#toc_ember-global',
for: 'ember-source',
since: {
enabled: '3.27.0',
},
});
}

return _get();
};
}

function defineEmber(key: string) {
Object.defineProperty(context.exports, key, {
enumerable: true,
configurable: true,
get,
});
}

// Bootstrap the global
defineEmber('Ember');
defineEmber('Em');

// Bootstrap Node module
// eslint-disable-next-line no-undef
if (typeof module === 'object' && typeof module.require === 'function') {
// tslint:disable-next-line: no-require-imports
module.exports = Ember = require('ember').default;
module.exports = require('ember').default;
}
})();
9 changes: 0 additions & 9 deletions packages/@ember/-internals/overrides/index.d.ts

This file was deleted.

4 changes: 3 additions & 1 deletion tests/node/fastboot-sandbox-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function buildSandboxContext(precompile) {
URL,

// Convince jQuery not to assume it's in a browser
module: { exports: {} },
module: { exports: {}, require() {} },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is maybe the most suspicious change in here. As these tests are simply executing scripts in a context, they don't directly use any module system (CommonJS or ES). Adding require() {} to the exports fools Ember into thinking this is a CommonJS env (which is effectively what this line was doing anyway) and thus Ember sets .exports to be Ember. This hack only works for one this-which-exports-via-CommonJS in a context.

I'm not familiar enough with how this code runs in Fastboot to know if this is a meaningful test. cc @rwjblue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue suggested this should land, and that if Fastboot as a follow up we can address it during the beta.

};

// Set the global as `window`
Expand All @@ -69,6 +69,8 @@ var EmberENV = {
emberScript.runInContext(context);

let applicationSource = `
let Ember = module.exports;

class Router extends Ember.Router {}
Router.map(function() {
this.route('a');
Expand Down
47 changes: 0 additions & 47 deletions tests/node/overrides-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,6 @@ function infoForApp({
// };
// }

function evalJS(overrides) {
return eval(`
(function () {
let onEmberGlobalAccess;

${overrides.toJS()}

return {
onEmberGlobalAccess: onEmberGlobalAccess,
};
})()
`);
}

QUnit.module('Overrides', function () {
QUnit.module('.addonsInfoFor', function () {
// app
Expand Down Expand Up @@ -593,39 +579,6 @@ QUnit.module('Overrides', function () {
assert.strictEqual(overrides.hasActionableSuggestions, true, 'hasActionableSuggestions');
assert.strictEqual(overrides.hasCompatibleAddons, false, 'hasCompatibleAddons');
assert.strictEqual(overrides.hasDormantAddons, false, 'hasDormantAddons');
assert.strictEqual(
overrides.showAllEmberGlobalDeprecations,
false,
'showAllEmberGlobalDeprecations'
);
assert.deepEqual(overrides.suggestions, [
'Upgrade your `devDependencies` on `ember-cli-babel` to `^7.26.6`.',
]);
assert.equal(
overrides.outdated.length,
1 /* number of different old babel versions */,
'outdated.length'
);
assert.ok(
overrides.buildTimeWarning.startsWith(
'[DEPRECATION] Usage of the Ember Global is deprecated.'
),
'overrides.buildTimeWarning'
);
assert.ok(
overrides.globalMessage.startsWith('Usage of the Ember Global is deprecated.'),
'overrides.globalMessage'
);

let { onEmberGlobalAccess } = evalJS(overrides);

assert.equal(
onEmberGlobalAccess(),
overrides.globalMessage,
'onEmberGlobalAccess() (first call)'
);

assert.strictEqual(onEmberGlobalAccess(), null, 'onEmberGlobalAccess() (second call)');
});

// let project, env;
Expand Down