Skip to content

Commit 3cf4eaf

Browse files
authored
Add support for Vite (#2625)
2 parents ea55cfa + 659f1b7 commit 3cf4eaf

26 files changed

+318
-222
lines changed

ember_debug/lib/get-applications.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
/* eslint-disable ember/new-module-imports */
1+
import { Application, Namespace, guidFor } from '../utils/ember';
2+
23
/**
34
* Get all the Ember.Application instances from Ember.Namespace.NAMESPACES
45
* and add our own applicationId and applicationName to them
56
* @return {*}
67
*/
7-
export default function getApplications(Ember) {
8-
var namespaces = Ember.A(Ember.Namespace.NAMESPACES);
8+
export default function getApplications() {
9+
var namespaces = Namespace.NAMESPACES;
910

1011
var apps = namespaces.filter(function (namespace) {
11-
return namespace instanceof Ember.Application;
12+
return namespace instanceof Application;
1213
});
1314

1415
return apps.map(function (app) {
1516
// Add applicationId and applicationName to the app
16-
var applicationId = Ember.guidFor(app);
17+
var applicationId = guidFor(app);
1718
var applicationName =
1819
app.name || app.modulePrefix || `(unknown app - ${applicationId})`;
1920

ember_debug/lib/load-ember-debug-in-webpage.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default function loadEmberDebugInWebpage(callback) {
2424
*/
2525
if (window.Ember) return resolve();
2626

27+
if (globalThis.emberInspectorApps) return resolve();
28+
2729
window.addEventListener('Ember', resolve, { once: true });
2830
});
2931
waitForEmberLoad.then(callback);

ember_debug/lib/send-version-miss.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable ember/new-module-imports */
21
let channel = new MessageChannel();
32
let port = channel.port1;
43
window.postMessage('debugger-client', '*', [channel.port2]);
@@ -12,7 +11,7 @@ let registeredMiss = false;
1211
* It sends a message to the inspector app to redirect
1312
* to an inspector version that supports this Ember version.
1413
*/
15-
export default function sendVersionMiss(Ember) {
14+
export default function sendVersionMiss(VERSION) {
1615
if (registeredMiss) {
1716
return;
1817
}
@@ -32,7 +31,7 @@ export default function sendVersionMiss(Ember) {
3231
function sendVersionMismatch() {
3332
port.postMessage({
3433
name: 'version-mismatch',
35-
version: Ember.VERSION,
34+
version: VERSION,
3635
from: 'inspectedWindow',
3736
});
3837
}

ember_debug/lib/setup-instance-initializer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
/* eslint-disable ember/new-module-imports */
2-
export default function setupInstanceInitializer(Ember, app, callback) {
1+
import { guidFor } from '../utils/ember';
2+
3+
export default function setupInstanceInitializer(app, callback) {
34
if (!app.__inspector__setup) {
45
app.__inspector__setup = true;
56

67
// We include the app's guid in the initializer name because in Ember versions < 3
78
// registering an instance initializer with the same name, even if on a different app,
89
// triggers an error because instance initializers seem to be global instead of per app.
910
app.instanceInitializer({
10-
name: 'ember-inspector-app-instance-booted-' + Ember.guidFor(app),
11+
name: 'ember-inspector-app-instance-booted-' + guidFor(app),
1112
initialize: function (instance) {
1213
callback(instance);
1314
},

ember_debug/lib/start-inspector.js

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
/* eslint-disable ember/new-module-imports, ember/no-test-import-export */
2-
1+
/* eslint-disable ember/no-test-import-export */
32
import versionTest from './version-test';
43
import { EMBER_VERSIONS_SUPPORTED } from './versions';
54
import sendApps from './send-apps';
65
import getApplications from './get-applications';
76
import bootEmberInspector from './boot-ember-inspector';
87
import setupInstanceInitializer from './setup-instance-initializer';
98
import sendVersionMiss from './send-version-miss';
10-
11-
let Ember;
9+
import { guidFor, Application, VERSION } from '../utils/ember';
1210

1311
function onReady(callback) {
1412
if (
@@ -36,27 +34,6 @@ export function onEmberReady(callback) {
3634
return;
3735
}
3836

39-
if (!Ember) {
40-
try {
41-
Ember = requireModule('ember/barrel')['default'];
42-
} catch {
43-
// noop
44-
}
45-
try {
46-
Ember = Ember || requireModule('ember')['default'];
47-
} catch {
48-
Ember = window.Ember;
49-
}
50-
}
51-
52-
if (!Ember) {
53-
return;
54-
}
55-
// `Ember.Application` load hook triggers before all of Ember is ready.
56-
// In this case we ignore and wait for the `Ember` load hook.
57-
if (!Ember.RSVP) {
58-
return;
59-
}
6037
triggered = true;
6138
callback();
6239
};
@@ -74,21 +51,17 @@ export function startInspector(adapter) {
7451
// to determine when the application starts
7552
// but this definitely works
7653
function onApplicationStart(callback) {
77-
if (typeof Ember === 'undefined') {
78-
return;
79-
}
80-
8154
const adapterInstance = new adapter();
8255

8356
adapterInstance.onMessageReceived(function (message) {
8457
if (message.type === 'app-picker-loaded') {
85-
sendApps(adapterInstance, getApplications(Ember));
58+
sendApps(adapterInstance, getApplications());
8659
}
8760

8861
if (message.type === 'app-selected') {
8962
let current = window.EmberInspector._application;
90-
let selected = getApplications(Ember).find(
91-
(app) => Ember.guidFor(app) === message.applicationId,
63+
let selected = getApplications().find(
64+
(app) => guidFor(app) === message.applicationId,
9265
);
9366

9467
if (
@@ -101,7 +74,7 @@ export function startInspector(adapter) {
10174
}
10275
});
10376

104-
var apps = getApplications(Ember);
77+
var apps = getApplications();
10578

10679
sendApps(adapterInstance, apps);
10780

@@ -112,7 +85,7 @@ export function startInspector(adapter) {
11285
let instance = app.__deprecatedInstance__ || applicationInstances[0];
11386
if (instance) {
11487
// App started
115-
setupInstanceInitializer(Ember, app, callback);
88+
setupInstanceInitializer(app, callback);
11689
callback(instance);
11790
return true;
11891
}
@@ -143,7 +116,7 @@ export function startInspector(adapter) {
143116
},
144117
});
145118
}
146-
Ember.Application.initializer({
119+
Application.initializer({
147120
name: 'ember-inspector-booted',
148121
initialize: function (app) {
149122
setupInstanceInitializer(app, callback);
@@ -157,14 +130,25 @@ export function startInspector(adapter) {
157130
return;
158131
}
159132

160-
// If Ember doesn't exist, we should stop here to avoid issues with accessing `Ember.VERSION`
161-
if (!Ember) {
133+
/**
134+
* If we don't have a way to know the Ember version at this point
135+
* because the Ember app has not loaded and provided it somehow,
136+
* we can't continue (we need the version to know what version of
137+
* the Inspector to load).
138+
*/
139+
if (!VERSION) {
162140
return;
163141
}
164142

165-
if (!versionTest(Ember.VERSION, EMBER_VERSIONS_SUPPORTED)) {
143+
/**
144+
* This is used to redirect to an old snapshot of the Inspector if the
145+
* inspected app uses an older Ember version than supported versions.
146+
* The code fits the Inspector supporting Ember back to 3.16: any version
147+
* before 3.16 is necessarily a classic Ember app with Ember defined.
148+
*/
149+
if ((VERSION, !versionTest(VERSION, EMBER_VERSIONS_SUPPORTED))) {
166150
// Wrong inspector version. Redirect to the correct version.
167-
sendVersionMiss(Ember);
151+
sendVersionMiss(VERSION);
168152
return;
169153
}
170154

ember_debug/object-inspector.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ const OWNER_SYMBOL = '__owner__'; // can't use actual symbol because it can't be
3636

3737
// Try to use the most recent library (GlimmerValidator), else
3838
// fallback on the previous implementation (GlimmerReference).
39-
if (GlimmerValidator) {
39+
// The global checks if the inspected app is Vite, in that case
40+
// we can't execute that block because the properties it tries to
41+
// assign are readonly.
42+
if (GlimmerValidator && !globalThis.emberInspectorApps) {
4043
tagValue = GlimmerValidator.value || GlimmerValidator.valueForTag;
4144
tagValidate = GlimmerValidator.validate || GlimmerValidator.validateTag;
4245
track = GlimmerValidator.track;

ember_debug/utils/ember-object-names.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ const emberNames = new Map([
2727
[NativeArray, 'NativeArray Mixin'],
2828
[Observable, 'Observable Mixin'],
2929
[ControllerMixin, 'Controller Mixin'],
30-
[ActionHandler, 'ActionHandler Mixin'],
3130
[CoreObject, 'CoreObject'],
3231
[EmberObject, 'EmberObject'],
3332
[Component, 'Component'],
3433
]);
3534

35+
if (ActionHandler) {
36+
emberNames.set(ActionHandler, 'ActionHandler Mixin');
37+
}
38+
3639
if (compareVersion(VERSION, '3.27.0') === -1) {
3740
const TargetActionSupport = InternalsRuntime?.TargetActionSupport;
3841
emberNames.set(TargetActionSupport, 'TargetActionSupport Mixin');

0 commit comments

Comments
 (0)