ember-repl 6.0.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +2 -404
  3. package/config/.try.mjs +87 -0
  4. package/config/addon-main.cjs +7 -0
  5. package/config/attw.json +7 -0
  6. package/config/babel.publish.config.cjs +29 -0
  7. package/config/ember-cli-update.json +21 -0
  8. package/config/rollup.config.mjs +44 -0
  9. package/config/testem.cjs +35 -0
  10. package/config/tsconfig.publish.json +18 -0
  11. package/config/vite.config.mjs +36 -0
  12. package/declarations/compile/Compiled.d.ts +7 -0
  13. package/declarations/compile/Compiled.d.ts.map +1 -0
  14. package/declarations/compile/compile.d.ts +17 -0
  15. package/declarations/compile/compile.d.ts.map +1 -0
  16. package/declarations/compile/state.d.ts +43 -0
  17. package/declarations/compile/state.d.ts.map +1 -0
  18. package/declarations/compile/types.d.ts +5 -12
  19. package/declarations/compile/types.d.ts.map +1 -1
  20. package/declarations/index.d.ts +6 -2
  21. package/declarations/index.d.ts.map +1 -1
  22. package/declarations/services/compiler.d.ts +94 -0
  23. package/declarations/services/compiler.d.ts.map +1 -0
  24. package/declarations/services/known-modules.d.ts +7 -0
  25. package/declarations/services/known-modules.d.ts.map +1 -0
  26. package/declarations/setup.d.ts +7 -0
  27. package/declarations/setup.d.ts.map +1 -0
  28. package/declarations/test-support.d.ts +20 -0
  29. package/declarations/test-support.d.ts.map +1 -0
  30. package/dist/_commonjsHelpers-BAGoDD49.js +37 -0
  31. package/dist/_commonjsHelpers-BAGoDD49.js.map +1 -0
  32. package/dist/babel-8wMrbxkT.js +110427 -0
  33. package/dist/babel-8wMrbxkT.js.map +1 -0
  34. package/dist/blank-line-Bzg2Qt4K.js +482 -0
  35. package/dist/blank-line-Bzg2Qt4K.js.map +1 -0
  36. package/dist/compile/Compiled.js +26 -0
  37. package/dist/compile/Compiled.js.map +1 -0
  38. package/dist/compile/compile.js +62 -0
  39. package/dist/compile/compile.js.map +1 -0
  40. package/dist/compile/state.js +75 -0
  41. package/dist/compile/state.js.map +1 -0
  42. package/dist/compile/utils.js +213 -2
  43. package/dist/compile/utils.js.map +1 -1
  44. package/dist/default-CoqAuVeH.js +4 -0
  45. package/dist/default-CoqAuVeH.js.map +1 -0
  46. package/dist/index-BTx1k6gT.js +323 -0
  47. package/dist/index-BTx1k6gT.js.map +1 -0
  48. package/dist/index-Bxzjtr16.js +87 -0
  49. package/dist/index-Bxzjtr16.js.map +1 -0
  50. package/dist/index-C371bO_b.js +1553 -0
  51. package/dist/index-C371bO_b.js.map +1 -0
  52. package/dist/index-C4AyeeIa.js +5721 -0
  53. package/dist/index-C4AyeeIa.js.map +1 -0
  54. package/dist/index-C8S2G0FH.js +1953 -0
  55. package/dist/index-C8S2G0FH.js.map +1 -0
  56. package/dist/index-CCcIVEUK.js +409 -0
  57. package/dist/index-CCcIVEUK.js.map +1 -0
  58. package/dist/index-CDSIcg03.js +9070 -0
  59. package/dist/index-CDSIcg03.js.map +1 -0
  60. package/dist/index-D8szzCn3.js +2 -0
  61. package/dist/index-D8szzCn3.js.map +1 -0
  62. package/dist/index-DBBNT106.js +2644 -0
  63. package/dist/index-DBBNT106.js.map +1 -0
  64. package/dist/index-DP_Su7Zc.js +362 -0
  65. package/dist/index-DP_Su7Zc.js.map +1 -0
  66. package/dist/index-DejgrVqh.js +11299 -0
  67. package/dist/index-DejgrVqh.js.map +1 -0
  68. package/dist/index-Dr5iYoKt.js +1551 -0
  69. package/dist/index-Dr5iYoKt.js.map +1 -0
  70. package/dist/index-DxolpiGq.js +3336 -0
  71. package/dist/index-DxolpiGq.js.map +1 -0
  72. package/dist/index-ZyJlPFQY.js +249 -0
  73. package/dist/index-ZyJlPFQY.js.map +1 -0
  74. package/dist/index-k6CfLgeq.js +26 -0
  75. package/dist/index-k6CfLgeq.js.map +1 -0
  76. package/dist/index.js +4 -1
  77. package/dist/index.js.map +1 -1
  78. package/dist/services/compiler.js +329 -0
  79. package/dist/services/compiler.js.map +1 -0
  80. package/dist/services/known-modules.js +123 -0
  81. package/dist/services/known-modules.js.map +1 -0
  82. package/dist/setup.js +15 -0
  83. package/dist/setup.js.map +1 -0
  84. package/dist/test-support.js +33 -0
  85. package/dist/test-support.js.map +1 -0
  86. package/package.json +117 -138
  87. package/src/compile/Compiled.ts +45 -0
  88. package/src/compile/compile.ts +89 -0
  89. package/src/compile/state.ts +88 -0
  90. package/src/compile/types.ts +14 -13
  91. package/src/index.ts +6 -2
  92. package/src/services/compiler.ts +401 -0
  93. package/src/services/known-modules.ts +130 -0
  94. package/src/setup.ts +26 -0
  95. package/src/test-support.ts +64 -0
  96. package/addon-main.cjs +0 -5
  97. package/declarations/__PRIVATE__.d.ts +0 -2
  98. package/declarations/__PRIVATE__.d.ts.map +0 -1
  99. package/declarations/compile/formats/gjs/babel.d.ts +0 -7
  100. package/declarations/compile/formats/gjs/babel.d.ts.map +0 -1
  101. package/declarations/compile/formats/gjs/eval.d.ts +0 -8
  102. package/declarations/compile/formats/gjs/eval.d.ts.map +0 -1
  103. package/declarations/compile/formats/gjs/index.d.ts +0 -24
  104. package/declarations/compile/formats/gjs/index.d.ts.map +0 -1
  105. package/declarations/compile/formats/gjs/known-modules.d.ts +0 -48
  106. package/declarations/compile/formats/gjs/known-modules.d.ts.map +0 -1
  107. package/declarations/compile/formats/hbs.d.ts +0 -17
  108. package/declarations/compile/formats/hbs.d.ts.map +0 -1
  109. package/declarations/compile/formats/markdown.d.ts +0 -22
  110. package/declarations/compile/formats/markdown.d.ts.map +0 -1
  111. package/declarations/compile/formats.d.ts +0 -17
  112. package/declarations/compile/formats.d.ts.map +0 -1
  113. package/declarations/compile/index.d.ts +0 -80
  114. package/declarations/compile/index.d.ts.map +0 -1
  115. package/declarations/test-support/index.d.ts +0 -2
  116. package/declarations/test-support/index.d.ts.map +0 -1
  117. package/dist/__PRIVATE__.js +0 -2
  118. package/dist/__PRIVATE__.js.map +0 -1
  119. package/dist/compile/formats/gjs/babel.js +0 -2
  120. package/dist/compile/formats/gjs/babel.js.map +0 -1
  121. package/dist/compile/formats/gjs/eval.js +0 -19
  122. package/dist/compile/formats/gjs/eval.js.map +0 -1
  123. package/dist/compile/formats/gjs/index.js +0 -122
  124. package/dist/compile/formats/gjs/index.js.map +0 -1
  125. package/dist/compile/formats/gjs/known-modules.js +0 -52
  126. package/dist/compile/formats/gjs/known-modules.js.map +0 -1
  127. package/dist/compile/formats/hbs.js +0 -93
  128. package/dist/compile/formats/hbs.js.map +0 -1
  129. package/dist/compile/formats/markdown.js +0 -266
  130. package/dist/compile/formats/markdown.js.map +0 -1
  131. package/dist/compile/formats.js +0 -173
  132. package/dist/compile/formats.js.map +0 -1
  133. package/dist/compile/index.js +0 -113
  134. package/dist/compile/index.js.map +0 -1
  135. package/dist/test-support/index.js +0 -8
  136. package/dist/test-support/index.js.map +0 -1
  137. package/src/__PRIVATE__.ts +0 -1
  138. package/src/compile/formats/gjs/babel.ts +0 -7
  139. package/src/compile/formats/gjs/eval.ts +0 -29
  140. package/src/compile/formats/gjs/index.ts +0 -153
  141. package/src/compile/formats/gjs/known-modules.ts +0 -49
  142. package/src/compile/formats/hbs.ts +0 -100
  143. package/src/compile/formats/markdown.ts +0 -345
  144. package/src/compile/formats.ts +0 -178
  145. package/src/compile/index.ts +0 -219
  146. package/src/test-support/index.ts +0 -5
@@ -1,100 +0,0 @@
1
- // import { precompileJSON } from '@glimmer/compiler';
2
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
- // @ts-ignore
4
- // These things are pre-bundled in the old system.
5
- // ember-template-compiler defines them in AMD/requirejs
6
- import { precompileJSON } from '@glimmer/compiler';
7
- import { getTemplateLocals } from '@glimmer/syntax';
8
- import { setComponentTemplate } from '@ember/component';
9
- import templateOnlyComponent from '@ember/component/template-only';
10
- import { array, concat, fn, get, hash } from '@ember/helper';
11
- import { on } from '@ember/modifier';
12
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
13
- // @ts-ignore
14
- import { createTemplateFactory } from '@ember/template-factory';
15
-
16
- import { nameFor } from '../utils.ts';
17
-
18
- import type { CompileResult } from '../types.ts';
19
- import type { ComponentLike } from '@glint/template';
20
-
21
- /**
22
- * compile a template with an empty scope
23
- * to use components, helpers, etc, you will need to compile with JS
24
- *
25
- * (templates alone do not have a way to import / define complex structures)
26
- */
27
- export function compileHBS(template: string, options: CompileTemplateOptions = {}): CompileResult {
28
- const name = nameFor(template);
29
- let component: undefined | ComponentLike;
30
- let error: undefined | Error;
31
-
32
- try {
33
- component = setComponentTemplate(
34
- compileTemplate(template, { moduleName: options.moduleName || name, ...options }),
35
- templateOnlyComponent(options.moduleName || name)
36
- ) as ComponentLike;
37
- } catch (e) {
38
- error = e as Error | undefined;
39
- }
40
-
41
- return { name, component, error };
42
- }
43
-
44
- interface CompileTemplateOptions {
45
- /**
46
- * Used for debug viewing
47
- */
48
- moduleName?: string;
49
- scope?: Record<string, unknown>;
50
- }
51
-
52
- /**
53
- * The reason why we can't use precompile directly is because of this:
54
- * https://github.com/glimmerjs/glimmer-vm/blob/master/packages/%40glimmer/compiler/lib/compiler.ts#L132
55
- *
56
- * Support for dynamically compiling templates in strict mode doesn't seem to be fully their yet.
57
- * That JSON.stringify (and the lines after) prevent us from easily setting the scope function,
58
- * which means that *everything* is undefined.
59
- */
60
- function compileTemplate(source: string, { moduleName, scope = {} }: CompileTemplateOptions) {
61
- const localScope = { array, concat, fn, get, hash, on, ...scope } as any;
62
- const locals = getTemplateLocals(source);
63
-
64
- const options = {
65
- strictMode: true,
66
- moduleName,
67
- locals,
68
- isProduction: false,
69
- meta: { moduleName },
70
- };
71
-
72
- // Copied from @glimmer/compiler/lib/compiler#precompile
73
- const [block, usedLocals] = precompileJSON(source, options);
74
-
75
- const usedScope = usedLocals.map((key: string) => {
76
- const value = localScope[key];
77
-
78
- if (!value) {
79
- throw new Error(
80
- `Attempt to use ${key} in compiled hbs, but it was not available in scope. ` +
81
- `Available scope includes: ${Object.keys(localScope)}`
82
- );
83
- }
84
-
85
- return value;
86
- });
87
-
88
- const blockJSON = JSON.stringify(block);
89
- const templateJSONObject = {
90
- id: moduleName,
91
- block: blockJSON,
92
- moduleName: moduleName ?? '(dynamically compiled component)',
93
- scope: () => usedScope,
94
- isStrictMode: true,
95
- };
96
-
97
- const factory = createTemplateFactory(templateJSONObject);
98
-
99
- return factory;
100
- }
@@ -1,345 +0,0 @@
1
- import rehypeRaw from 'rehype-raw';
2
- import rehypeStringify from 'rehype-stringify';
3
- import remarkGfm from 'remark-gfm';
4
- import remarkParse from 'remark-parse';
5
- import remarkRehype from 'remark-rehype';
6
- import { unified } from 'unified';
7
- import { visit } from 'unist-util-visit';
8
-
9
- import { invocationOf, nameFor } from '../utils.ts';
10
-
11
- import type { UnifiedPlugin } from '../types.ts';
12
- import type { Node } from 'hast';
13
- import type { Code, Text } from 'mdast';
14
- import type { Parent } from 'unist';
15
- import type { VFile } from 'vfile';
16
-
17
- export interface ExtractedCode {
18
- name: string;
19
- code: string;
20
- lang: string;
21
- }
22
-
23
- export interface LiveCodeExtraction {
24
- templateOnlyGlimdown: string;
25
- blocks: ExtractedCode[];
26
- }
27
- type LiveData = {
28
- liveCode?: ExtractedCode[];
29
- };
30
- type VFileWithMeta = VFile & {
31
- data: LiveData;
32
- };
33
-
34
- interface Options {
35
- snippets?: {
36
- classList?: string[];
37
- };
38
- demo?: {
39
- classList?: string[];
40
- };
41
- copyComponent?: string;
42
- shadowComponent?: string;
43
- }
44
-
45
- const GLIMDOWN_PREVIEW = Symbol('__GLIMDOWN_PREVIEW__');
46
- const GLIMDOWN_RENDER = Symbol('__GLIMDOWN_RENDER__');
47
- const ALLOWED_LANGUAGES = ['gjs', 'hbs'] as const;
48
-
49
- type AllowedLanguage = (typeof ALLOWED_LANGUAGES)[number];
50
- type RelevantCode = Omit<Code, 'lang'> & { lang: AllowedLanguage };
51
-
52
- function isLive(meta: string) {
53
- return meta.includes('live');
54
- }
55
-
56
- function isPreview(meta: string) {
57
- return meta.includes('preview');
58
- }
59
-
60
- function isBelow(meta: string) {
61
- return meta.includes('below');
62
- }
63
-
64
- // TODO: extract and publish remark plugin
65
- function liveCodeExtraction(options: Options = {}) {
66
- const { copyComponent, snippets, demo } = options;
67
- let { classList: snippetClasses } = snippets || {};
68
- let { classList: demoClasses } = demo || {};
69
-
70
- snippetClasses ??= [];
71
- demoClasses ??= [];
72
-
73
- function isRelevantCode(node: Code): node is RelevantCode {
74
- if (node.type !== 'code') return false;
75
-
76
- let { meta } = node;
77
- const { lang } = node;
78
-
79
- meta = meta?.trim();
80
-
81
- if (!meta || !lang) return false;
82
-
83
- if (!meta.includes('live')) {
84
- return false;
85
- }
86
-
87
- if (!(ALLOWED_LANGUAGES as unknown as string[]).includes(lang)) return false;
88
-
89
- return true;
90
- }
91
-
92
- const copyNode = {
93
- type: 'html',
94
- value: copyComponent,
95
- };
96
-
97
- function enhance(code: Code) {
98
- code.data ??= {};
99
- (code.data as any)['hProperties'] ??= {};
100
- // This is secret-to-us-only API, so we don't really care about the type
101
- (code.data as any)['hProperties'][GLIMDOWN_PREVIEW] = true;
102
-
103
- return {
104
- data: {
105
- hProperties: { className: snippetClasses },
106
- },
107
- type: 'div',
108
- hProperties: { className: snippetClasses },
109
- children: [code, copyNode],
110
- };
111
- }
112
-
113
- function flatReplaceAt<T>(array: T[], index: number, replacement: T[]) {
114
- array.splice(index, 1, ...replacement);
115
- }
116
-
117
- // because we mutate the tree as we iterate,
118
- // we need to make sure we don't loop forever
119
- const seen = new Set();
120
-
121
- return function transformer(tree: Parent, file: VFileWithMeta) {
122
- visit(tree, ['code'], function (node, index, parent) {
123
- if (parent === null || parent === undefined) return;
124
- if (index === null || index === undefined) return;
125
-
126
- if (!isRelevantCode(node as Code)) {
127
- const enhanced = enhance(node as Code);
128
-
129
- parent.children[index] = enhanced;
130
-
131
- return 'skip';
132
- }
133
-
134
- if (seen.has(node)) return 'skip';
135
-
136
- seen.add(node);
137
-
138
- const { meta, lang, value } = node as Code;
139
-
140
- if (!meta) return 'skip';
141
- if (!lang) return 'skip';
142
-
143
- file.data.liveCode ??= [];
144
-
145
- const code = value.trim();
146
- const name = nameFor(code);
147
- let invocation = invocationOf(name);
148
-
149
- const shadow = options.shadowComponent;
150
-
151
- const wrapInShadow = shadow && !meta?.includes('no-shadow');
152
-
153
- if (wrapInShadow) {
154
- invocation = `<${shadow}>${invocation}</${shadow}>`;
155
- }
156
-
157
- const invokeNode = {
158
- type: 'html',
159
- data: {
160
- hProperties: { [GLIMDOWN_RENDER]: true },
161
- },
162
- value: `<div class="${demoClasses}">${invocation}</div>`,
163
- };
164
-
165
- const wrapper = enhance(node as Code);
166
-
167
- file.data.liveCode.push({
168
- lang,
169
- name,
170
- code,
171
- });
172
-
173
- const live = isLive(meta);
174
- const preview = isPreview(meta);
175
- const below = isBelow(meta);
176
-
177
- if (live && preview && below) {
178
- flatReplaceAt(parent.children, index, [wrapper, invokeNode]);
179
-
180
- return 'skip';
181
- }
182
-
183
- if (live && preview) {
184
- flatReplaceAt(parent.children, index, [invokeNode, wrapper]);
185
-
186
- return 'skip';
187
- }
188
-
189
- if (live) {
190
- parent.children[index] = invokeNode;
191
-
192
- return 'skip';
193
- }
194
-
195
- parent.children[index] = wrapper;
196
-
197
- return;
198
- });
199
- };
200
- }
201
-
202
- function sanitizeForGlimmer(/* options */) {
203
- return (tree: Parent) => {
204
- visit(tree, 'element', (node: Parent) => {
205
- if ('tagName' in node) {
206
- if (!['pre', 'code'].includes(node.tagName as string)) return;
207
-
208
- visit(node, 'text', (textNode: Text) => {
209
- if ('value' in textNode && textNode.value) {
210
- textNode.value = textNode.value.replace(/{{/g, '\\{{');
211
- }
212
- });
213
-
214
- return 'skip';
215
- }
216
- });
217
- };
218
- }
219
-
220
- function buildCompiler(options: ParseMarkdownOptions) {
221
- let compiler = unified().use(remarkParse).use(remarkGfm);
222
-
223
- /**
224
- * If this were "use"d after `remarkRehype`,
225
- * remark is gone, and folks would need to work with rehype trees
226
- */
227
- if (options.remarkPlugins) {
228
- options.remarkPlugins.forEach((plugin) => {
229
- // Arrays are how plugins are passed options (for some reason?)
230
- // why not just invoke the the function?
231
- const p = Array.isArray(plugin) ? plugin : [plugin];
232
-
233
- compiler = compiler.use(...(p as [any]));
234
- });
235
- }
236
-
237
- // TODO: we only want to do this when we have pre > code.
238
- // code can exist inline.
239
- compiler = compiler.use(liveCodeExtraction, {
240
- snippets: {
241
- classList: ['glimdown-snippet', 'relative'],
242
- },
243
- demo: {
244
- classList: ['glimdown-render'],
245
- },
246
- copyComponent: options?.CopyComponent,
247
- shadowComponent: options?.ShadowComponent,
248
- });
249
-
250
- // .use(() => (tree) => visit(tree, (node) => console.log('i', node)))
251
- // remark rehype is needed to convert markdown to HTML
252
- // However, it also changes all the nodes, so we need another pass
253
- // to make sure our Glimmer-aware nodes are in tact
254
- compiler = compiler.use(remarkRehype, { allowDangerousHtml: true });
255
-
256
- // Convert invocables to raw format, so Glimmer can invoke them
257
- compiler = compiler.use(() => (tree: Node) => {
258
- visit(tree, function (node) {
259
- // We rely on an implicit transformation of data.hProperties => properties
260
- const properties = (node as any).properties;
261
-
262
- if (properties?.[GLIMDOWN_PREVIEW]) {
263
- return 'skip';
264
- }
265
-
266
- if (node.type === 'element' || ('tagName' in node && node.tagName === 'code')) {
267
- if (properties?.[GLIMDOWN_RENDER]) {
268
- node.type = 'glimmer_raw';
269
-
270
- return;
271
- }
272
-
273
- return 'skip';
274
- }
275
-
276
- if (node.type === 'text' || node.type === 'raw') {
277
- // definitively not the better way, but this is supposed to detect "glimmer" nodes
278
- if (
279
- 'value' in node &&
280
- typeof node.value === 'string' &&
281
- node.value.match(/<\/?[_A-Z:0-9].*>/g)
282
- ) {
283
- node.type = 'glimmer_raw';
284
- }
285
-
286
- node.type = 'glimmer_raw';
287
-
288
- return 'skip';
289
- }
290
-
291
- return;
292
- });
293
- });
294
-
295
- if (options.rehypePlugins) {
296
- options.rehypePlugins.forEach((plugin) => {
297
- // Arrays are how plugins are passed options (for some reason?)
298
- // why not just invoke the the function?
299
- const p = Array.isArray(plugin) ? plugin : [plugin];
300
-
301
- compiler = compiler.use(...(p as [any]));
302
- });
303
- }
304
-
305
- compiler = compiler.use(rehypeRaw, { passThrough: ['glimmer_raw', 'raw'] }).use(() => (tree) => {
306
- visit(tree, 'glimmer_raw', (node: Node) => {
307
- node.type = 'raw';
308
- });
309
- });
310
-
311
- compiler = compiler.use(sanitizeForGlimmer);
312
-
313
- // Finally convert to string! oofta!
314
- compiler = compiler.use(rehypeStringify, {
315
- collapseEmptyAttributes: true,
316
- closeSelfClosing: true,
317
- allowParseErrors: true,
318
- allowDangerousCharacters: true,
319
- allowDangerousHtml: true,
320
- }) as any;
321
-
322
- return compiler as ReturnType<typeof unified>;
323
- }
324
-
325
- interface ParseMarkdownOptions {
326
- CopyComponent?: string;
327
- ShadowComponent?: string;
328
- remarkPlugins?: UnifiedPlugin[];
329
- rehypePlugins?: UnifiedPlugin[];
330
- }
331
-
332
- /**
333
- * @internal not under semver
334
- */
335
- export async function parseMarkdown(
336
- input: string,
337
- options: ParseMarkdownOptions = {}
338
- ): Promise<LiveCodeExtraction> {
339
- const markdownCompiler = buildCompiler(options);
340
- const processed = await markdownCompiler.process(input);
341
- const liveCode = (processed.data as LiveData).liveCode || [];
342
- const templateOnly = processed.toString();
343
-
344
- return { templateOnlyGlimdown: templateOnly, blocks: liveCode };
345
- }
@@ -1,178 +0,0 @@
1
- import { invocationName } from './utils.ts';
2
-
3
- import type { ExtractedCode } from './formats/markdown.ts';
4
- import type { CompileResult, EvalImportMap, ScopeMap, UnifiedPlugin } from './types.ts';
5
-
6
- async function compileGJSArray(js: { code: string }[], importMap?: EvalImportMap) {
7
- const modules = await Promise.all(
8
- js.map(async ({ code }) => {
9
- return await compileGJS(code, importMap);
10
- })
11
- );
12
-
13
- return modules;
14
- }
15
-
16
- export async function compileGJS(
17
- gjsInput: string,
18
- importMap?: EvalImportMap
19
- ): Promise<CompileResult> {
20
- try {
21
- const { compileJS } = await import('./formats/gjs/index.ts');
22
-
23
- return await compileJS(gjsInput, importMap);
24
- } catch (error) {
25
- return { error: error as Error, name: 'unknown' };
26
- }
27
- }
28
-
29
- export async function compileHBS(
30
- hbsInput: string,
31
- options?: {
32
- moduleName?: string;
33
- scope?: Record<string, unknown>;
34
- }
35
- ): Promise<CompileResult> {
36
- try {
37
- const { compileHBS } = await import('./formats/hbs.ts');
38
-
39
- return compileHBS(hbsInput, options);
40
- } catch (error) {
41
- return { error: error as Error, name: 'unknown' };
42
- }
43
- }
44
-
45
- async function extractScope(
46
- liveCode: ExtractedCode[],
47
- options?: {
48
- importMap?: EvalImportMap;
49
- topLevelScope?: ScopeMap;
50
- }
51
- ): Promise<CompileResult[]> {
52
- const scope: CompileResult[] = [];
53
-
54
- const hbs = liveCode.filter((code) => code.lang === 'hbs');
55
- const js = liveCode.filter((code) => ['js', 'gjs'].includes(code.lang));
56
-
57
- if (js.length > 0) {
58
- const compiled = await compileGJSArray(js, options?.importMap);
59
-
60
- await Promise.all(
61
- compiled.map(async (info) => {
62
- // using web worker + import maps is not available yet (need firefox support)
63
- // (and to somehow be able to point at npm)
64
- //
65
- // if ('importPath' in info) {
66
- // return scope.push({
67
- // moduleName: name,
68
- // component: await import(/* webpackIgnore: true */ info.importPath),
69
- // });
70
- // }
71
-
72
- return scope.push(info);
73
- })
74
- );
75
- }
76
-
77
- for (const { code } of hbs) {
78
- const compiled = await compileHBS(code, { scope: options?.topLevelScope });
79
-
80
- scope.push(compiled);
81
- }
82
-
83
- return scope;
84
- }
85
-
86
- export async function compileMD(
87
- glimdownInput: string,
88
- options?: {
89
- importMap?: EvalImportMap;
90
- topLevelScope?: ScopeMap;
91
- remarkPlugins?: UnifiedPlugin[];
92
- rehypePlugins?: UnifiedPlugin[];
93
- CopyComponent?: string;
94
- ShadowComponent?: string;
95
- }
96
- ): Promise<CompileResult & { rootTemplate?: string }> {
97
- const topLevelScope = options?.topLevelScope ?? {};
98
- let rootTemplate: string;
99
- let liveCode: ExtractedCode[];
100
- let scope: CompileResult[] = [];
101
-
102
- /**
103
- * Step 1: Convert Markdown To HTML (Ember).
104
- *
105
- * The remark plugin, remark-code-extra also extracts
106
- * and transforms the code blocks we care about.
107
- *
108
- * These blocks will be compiled through babel and eval'd so the
109
- * compiled rootTemplate can invoke them
110
- */
111
- try {
112
- const { parseMarkdown } = await import('./formats/markdown.ts');
113
- const { templateOnlyGlimdown, blocks } = await parseMarkdown(glimdownInput, {
114
- CopyComponent: options?.CopyComponent,
115
- ShadowComponent: options?.ShadowComponent,
116
- remarkPlugins: options?.remarkPlugins,
117
- rehypePlugins: options?.rehypePlugins,
118
- });
119
-
120
- rootTemplate = templateOnlyGlimdown;
121
- liveCode = blocks;
122
- } catch (error) {
123
- return { error: error as Error, name: 'unknown' };
124
- }
125
-
126
- /**
127
- * Step 2: Compile the live code samples
128
- */
129
- if (liveCode.length > 0) {
130
- try {
131
- scope = await extractScope(liveCode, options);
132
- } catch (error) {
133
- console.info({ scope });
134
- console.error(error);
135
-
136
- return { error: error as Error, rootTemplate, name: 'unknown' };
137
- }
138
- }
139
-
140
- /**
141
- * Make sure non of our snippets errored
142
- *
143
- * TODO: for these errors, report them differently so that we
144
- * can render the 'Ember' and still highlight the correct line?
145
- * or maybe there is a way to highlight in the editor instead?
146
- */
147
- for (const { error, component } of scope) {
148
- if (!component) {
149
- if (error) {
150
- return { error, rootTemplate, name: 'unknown' };
151
- }
152
- }
153
- }
154
-
155
- /**
156
- * Step 4: Compile the Ember Template
157
- */
158
- try {
159
- const localScope = scope.reduce(
160
- (accum, { component, name }) => {
161
- accum[invocationName(name)] = component;
162
-
163
- return accum;
164
- },
165
- {} as Record<string, unknown>
166
- );
167
-
168
- return await compileHBS(rootTemplate, {
169
- moduleName: 'DynamicRootTemplate',
170
- scope: {
171
- ...topLevelScope,
172
- ...localScope,
173
- },
174
- });
175
- } catch (error) {
176
- return { error: error as Error, rootTemplate, name: 'unknown' };
177
- }
178
- }