-
Notifications
You must be signed in to change notification settings - Fork 3
/
ember__template-compilation.ts
113 lines (104 loc) · 2.99 KB
/
ember__template-compilation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { transform } from './../../plugins/test';
import * as fns from '@lifeart/gxt';
import { format } from 'prettier/standalone';
import parserBabel from 'prettier/plugins/babel';
import estree from 'prettier/plugins/estree';
import { SYMBOLS } from '../../plugins/symbols';
import { TEMPLATE_META } from './ember__component';
Object.keys(fns).forEach((key) => {
if (key.startsWith('$')) {
// @ts-expect-error
globalThis[key] = fns[key];
}
});
// @ts-expect-error
globalThis.scopes = new Map();
// @ts-expect-error
globalThis.components = new Map();
// @ts-expect-error
globalThis.rawTemplates = new Map();
// @ts-expect-error
globalThis.compiledTemplate = new Map();
const emptyScope = () => ({});
export function precompileTemplate(
tpl: string,
args: {
strictMode: boolean;
scope: () => any;
},
) {
const scopeId = Math.random().toString(36).slice(2);
// @ts-expect-error
globalThis.scopes.set(scopeId, args.scope ?? emptyScope);
const keys =
typeof args.scope === 'function' ? Object.keys(args.scope()) : [];
// @ts-expect-error
globalThis.rawTemplates.set(scopeId, tpl);
const transformResult = transform(
`export function t${scopeId}() {
${SYMBOLS.$_GET_ARGS}(this, arguments);
this['$fw'] = ${SYMBOLS.$_GET_FW}(this, arguments);
let {${keys.join(', ')}} = globalThis.scopes.get('${scopeId}')();
return hbs\`${tpl}\`;
}`,
'name.js',
'development',
false,
{
IS_GLIMMER_COMPAT_MODE: true,
RUN_EVENT_DESTRUCTORS_FOR_SCOPED_NODES: false,
TRY_CATCH_ERROR_HANDLING: true,
SUPPORT_SHADOW_DOM: true,
REACTIVE_MODIFIERS: true,
WITH_HELPER_MANAGER: true,
WITH_MODIFIER_MANAGER: true,
},
);
let final = transformResult.split('export ').pop() ?? '';
// @ts-expect-error
globalThis.compiledTemplate.set(scopeId, final);
format(final, {
parser: 'babel',
filepath: 'index.js',
plugins: [parserBabel, estree],
})
.then((result) => {
// hasBottomBarBlock
if (final.includes('hasBottomBarBlock')) {
console.log(result);
}
// if (final.includes('euiTab') && !final.includes('euiTabs--expand')) {
// console.log(result);
// }
// @ts-expect-error
globalThis.compiledTemplate.set(scopeId, result);
})
.catch((e) => {
console.log('--------');
console.log(final);
console.log('--------');
console.log(e);
});
const fnId = Math.random().toString(36).slice(2);
try {
eval(`
let fn = ${final};
globalThis.components.set('${fnId}', fn);
// globalThis.components.set('${fnId}', function() {
// try {
// return fn.apply(this, Array.from(arguments));
// } catch (e) {
// // console.log(${JSON.stringify(tpl)});
// // console.log(${JSON.stringify(final)});
// throw e;
// }
// });
`);
} catch (e) {
console.log(tpl);
console.log(final);
throw e;
}
// @ts-expect-error
return globalThis.components.get(fnId);
}