Skip to content

Commit 4d3c806

Browse files
authored
Merge pull request #13 from lifeart/ember-inspector-support
ember inspector support
2 parents 0715704 + efa40df commit 4d3c806

16 files changed

+1121
-79
lines changed

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"scripts": {
1717
"dev": "vite",
18-
"build": "tsc && vite build",
18+
"build": "tsc && vite build --mode development",
19+
"build:prod": "tsc && vite build",
1920
"build-lib": "tsc && vite build -- --lib",
2021
"preview": "vite preview",
2122
"prepublishOnly": "npm run build-lib && node ./utils/write-compiler-types.js",
@@ -28,6 +29,7 @@
2829
"dist/index.d.ts",
2930
"dist/gxt.index.es.js",
3031
"dist/gxt.compiler.es.js",
32+
"dist/gxt.ember-inspector.es.js",
3133
"compiler.d.ts"
3234
],
3335
"module": "./dist/gxt.index.es.js",
@@ -40,6 +42,10 @@
4042
"./compiler": {
4143
"import": "./dist/gxt.compiler.es.js",
4244
"types": "./dist/compiler.d.ts"
45+
},
46+
"./ember-inspector": {
47+
"import": "./dist/gxt.ember-inspector.es.js",
48+
"types": "./dist/src/utils/ember-inspector.d.ts"
4349
}
4450
},
4551
"devDependencies": {
@@ -70,6 +76,7 @@
7076
"@babel/core": "^7.23.6",
7177
"@babel/preset-typescript": "^7.23.3",
7278
"@glimmer/syntax": "^0.87.1",
79+
"backburner.js": "^2.8.0",
7380
"content-tag": "^1.2.2"
7481
}
7582
}

plugins/compiler.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ import { type Plugin } from 'vite';
22
import { Preprocessor } from 'content-tag';
33
import { transform } from './test';
44
import { MAIN_IMPORT } from './symbols';
5+
import { flags } from './flags.ts';
6+
57
const p = new Preprocessor();
68

79
export function compiler(mode: string): Plugin {
810
return {
911
enforce: 'pre',
1012
name: 'glimmer-next',
11-
13+
config(config, mode) {
14+
const isLibBuild = config.build?.lib !== undefined;
15+
const defineValues: Record<string, boolean> = flags;
16+
if (!isLibBuild) {
17+
defineValues['IS_DEV_MODE'] = mode.mode === 'development';
18+
}
19+
return {
20+
define: defineValues,
21+
};
22+
},
1223
transform(code: string, file: string) {
1324
const ext = file.split('.').pop();
1425
if (ext === 'gjs' || ext === 'gts') {

pnpm-lock.yaml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import '@glint/environment-glimmerx';
22
import '@glint/environment-ember-template-imports';
3-
43
import './style.css';
54

65
import { createBenchmark } from '@/utils/benchmark';
@@ -17,6 +16,9 @@ import {
1716
// https://github.com/glimmerjs/glimmer-vm/issues/1540
1817

1918
export default async function render() {
19+
if (IS_DEV_MODE) {
20+
await import('@lifeart/gxt/ember-inspector');
21+
}
2022
const benchmark = createBenchmark();
2123

2224
// starting app

src/utils/component.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class Component<T extends Props = any>
120120
template!: ComponentReturnType;
121121
}
122122
async function destroyNode(node: Node) {
123-
if (import.meta.env.DEV) {
123+
if (IS_DEV_MODE) {
124124
if (node === undefined) {
125125
console.warn(`Trying to destroy undefined`);
126126
return;
@@ -248,14 +248,16 @@ export async function destroyElement(
248248

249249
var $newDestructors = new WeakMap<any, Destructors>();
250250

251-
window['getDestructors'] = () => $newDestructors;
251+
if (IS_DEV_MODE) {
252+
window['getDestructors'] = () => $newDestructors;
253+
}
252254

253255
export function associateDestroyable(ctx: any, destructors: Destructors) {
254256
if (destructors.length === 0) {
255257
return;
256258
}
257259

258-
if (import.meta.env.DEV) {
260+
if (IS_DEV_MODE) {
259261
if (ctx.ctx && ctx.ctx !== ctx) {
260262
throw new Error(`Invalid context`);
261263
}
@@ -266,7 +268,7 @@ export function associateDestroyable(ctx: any, destructors: Destructors) {
266268
}
267269

268270
export function removeDestructor(ctx: any, destructor: DestructorFn) {
269-
if (import.meta.env.DEV) {
271+
if (IS_DEV_MODE) {
270272
if (ctx.ctx && ctx.ctx !== ctx) {
271273
throw new Error(`Invalid context`);
272274
}

src/utils/dom.ts

+62-23
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
$eventsProp,
3434
addToTree,
3535
RENDER_TREE,
36+
setBounds,
3637
} from './shared';
3738

3839
// EMPTY DOM PROPS
@@ -43,6 +44,10 @@ const $_className = 'className';
4344

4445
let ROOT: Component<any> | null = null;
4546

47+
export function getRoot() {
48+
return ROOT;
49+
}
50+
4651
type ModifierFn = (
4752
element: HTMLElement,
4853
...args: unknown[]
@@ -296,7 +301,7 @@ export function $_unstableChildComponentWrapper(
296301
) {
297302
return component(
298303
function UnstableChildWrapper(this: Component<any>) {
299-
if (import.meta.env.DEV) {
304+
if (IS_DEV_MODE) {
300305
// @ts-expect-error construct signature
301306
this.debugName = `UnstableChildWrapper-${unstableWrapperId++}`;
302307
}
@@ -308,29 +313,36 @@ export function $_unstableChildComponentWrapper(
308313
);
309314
}
310315

311-
function buildGraph(obj: Record<string, unknown>, root: any, children: any[]) {
312-
const name =
313-
root.debugName || root?.constructor?.name || root?.tagName || 'unknown';
314-
if (children.length === 0) {
315-
obj[name] = null;
316+
if (IS_DEV_MODE) {
317+
function buildGraph(
318+
obj: Record<string, unknown>,
319+
root: any,
320+
children: any[],
321+
) {
322+
const name =
323+
root.debugName || root?.constructor?.name || root?.tagName || 'unknown';
324+
if (children.length === 0) {
325+
obj[name] = null;
326+
return obj;
327+
}
328+
obj[name] = children.map((child) => {
329+
return buildGraph({}, child, RENDER_TREE.get(child) ?? []);
330+
});
316331
return obj;
317332
}
318-
obj[name] = children.map((child) => {
319-
return buildGraph({}, child, RENDER_TREE.get(child) ?? []);
320-
});
321-
return obj;
322-
}
323333

324-
function drawTreeToConsole() {
325-
const ref = buildGraph(
326-
{} as Record<string, unknown>,
327-
ROOT,
328-
RENDER_TREE.get(ROOT!) ?? [],
329-
);
330-
console.log(JSON.stringify(ref, null, 2));
331-
console.log(RENDER_TREE);
334+
function drawTreeToConsole() {
335+
const ref = buildGraph(
336+
{} as Record<string, unknown>,
337+
ROOT,
338+
RENDER_TREE.get(ROOT!) ?? [],
339+
);
340+
console.log(JSON.stringify(ref, null, 2));
341+
console.log(RENDER_TREE);
342+
}
343+
window.drawTreeToConsole = drawTreeToConsole;
332344
}
333-
window.drawTreeToConsole = drawTreeToConsole;
345+
334346
// hello, basic component manager
335347
function component(
336348
comp: ComponentReturnType | Component,
@@ -353,12 +365,18 @@ function component(
353365
// here is workaround for simple components @todo - figure out how to show context-less components in tree
354366
// for now we don't adding it
355367
addToTree(ctx, result.ctx);
368+
if (IS_DEV_MODE) {
369+
setBounds(result);
370+
}
356371
}
357372
return result;
358373
}
359374
if (instance.ctx !== null) {
360375
// for now we adding only components with context
361376
addToTree(ctx, instance.ctx);
377+
if (IS_DEV_MODE) {
378+
setBounds(instance);
379+
}
362380
}
363381
return instance;
364382
}
@@ -378,7 +396,7 @@ function mergeComponents(
378396
const nodes: Array<Node> = [];
379397
const contexts: Array<Component> = [];
380398
components.forEach((component) => {
381-
if (import.meta.env.DEV) {
399+
if (IS_DEV_MODE) {
382400
if (typeof component === 'boolean' || typeof component === 'undefined') {
383401
throw new Error(`
384402
Woops, looks like we trying to render boolean or undefined to template, check used helpers.
@@ -583,7 +601,28 @@ const ArgProxyHandler = {
583601
};
584602
export function $_args(args: Record<string, unknown>) {
585603
if (IS_GLIMMER_COMPAT_MODE) {
586-
return new Proxy(args, ArgProxyHandler);
604+
if (IS_DEV_MODE) {
605+
const newArgs: Record<string, () => unknown> = {};
606+
Object.keys(args).forEach((key) => {
607+
try {
608+
Object.defineProperty(newArgs, key, {
609+
get() {
610+
if (!isFn(args[key])) {
611+
return args[key];
612+
}
613+
// @ts-expect-error function signature
614+
return args[key]();
615+
},
616+
enumerable: true,
617+
});
618+
} catch (e) {
619+
console.error(e);
620+
}
621+
});
622+
return newArgs;
623+
} else {
624+
return new Proxy(args, ArgProxyHandler);
625+
}
587626
} else {
588627
return args;
589628
}
@@ -613,7 +652,7 @@ export function $_fin(
613652
}
614653
});
615654
if (!isStable) {
616-
if (import.meta.env.DEV) {
655+
if (IS_DEV_MODE) {
617656
nodes.unshift(
618657
api.comment(`unstable root enter node: ${ctx?.constructor.name}`),
619658
);

0 commit comments

Comments
 (0)