ember-repl 8.2.3 → 8.3.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 (50) hide show
  1. package/declarations/compile/Compiled.d.ts +11 -0
  2. package/declarations/compile/Compiled.d.ts.map +1 -1
  3. package/declarations/compile/compile.d.ts +9 -0
  4. package/declarations/compile/compile.d.ts.map +1 -1
  5. package/declarations/compile/state.d.ts +0 -5
  6. package/declarations/compile/state.d.ts.map +1 -1
  7. package/declarations/compile/types.d.ts +1 -1
  8. package/declarations/compile/types.d.ts.map +1 -1
  9. package/declarations/services/compiler.d.ts +22 -1
  10. package/declarations/services/compiler.d.ts.map +1 -1
  11. package/declarations/services/known-modules.d.ts +9 -0
  12. package/declarations/services/known-modules.d.ts.map +1 -1
  13. package/declarations/setup.d.ts.map +1 -1
  14. package/dist/{_commonjsHelpers-BAGoDD49.js → _commonjsHelpers-Bssqqw1Q.js} +2 -4
  15. package/dist/_commonjsHelpers-Bssqqw1Q.js.map +1 -0
  16. package/dist/compile/Compiled.js +7 -1
  17. package/dist/compile/Compiled.js.map +1 -1
  18. package/dist/compile/compile.js +0 -5
  19. package/dist/compile/compile.js.map +1 -1
  20. package/dist/compile/state.js +1 -13
  21. package/dist/compile/state.js.map +1 -1
  22. package/dist/compile/utils.js.map +1 -1
  23. package/dist/{index-DNPwtadt.js → index-CPzChcIg.js} +2 -2
  24. package/dist/{index-DNPwtadt.js.map → index-CPzChcIg.js.map} +1 -1
  25. package/dist/{index-C0sat7kP.js → index-D55gYNjO.js} +29 -8
  26. package/dist/index-D55gYNjO.js.map +1 -0
  27. package/dist/index-DNctqogm.js +70615 -0
  28. package/dist/index-DNctqogm.js.map +1 -0
  29. package/dist/{plugin-ZDtYFKJg.js → plugin-Deq-PxMR.js} +58 -37
  30. package/dist/plugin-Deq-PxMR.js.map +1 -0
  31. package/dist/services/compiler.js +90 -36
  32. package/dist/services/compiler.js.map +1 -1
  33. package/dist/services/known-modules.js +72 -60
  34. package/dist/services/known-modules.js.map +1 -1
  35. package/dist/setup.js +1 -4
  36. package/dist/setup.js.map +1 -1
  37. package/dist/test-support.js.map +1 -1
  38. package/package.json +35 -36
  39. package/src/compile/Compiled.ts +17 -0
  40. package/src/compile/compile.ts +9 -7
  41. package/src/compile/state.ts +0 -15
  42. package/src/compile/types.ts +2 -0
  43. package/src/services/compiler.ts +117 -31
  44. package/src/services/known-modules.ts +87 -56
  45. package/src/setup.ts +1 -8
  46. package/dist/_commonjsHelpers-BAGoDD49.js.map +0 -1
  47. package/dist/babel-DbH-RlNa.js +0 -106895
  48. package/dist/babel-DbH-RlNa.js.map +0 -1
  49. package/dist/index-C0sat7kP.js.map +0 -1
  50. package/dist/plugin-ZDtYFKJg.js.map +0 -1
@@ -1,14 +1,12 @@
1
1
  /* eslint-disable getter-return */
2
2
 
3
3
  // @ts-ignore
4
- import { tracked } from '@glimmer/tracking';
5
4
  import { setComponentTemplate } from '@ember/component';
6
5
  import templateOnly from '@ember/component/template-only';
7
6
  import { assert } from '@ember/debug';
8
7
  import { registerDestructor } from '@ember/destroyable';
9
- import { array, concat, fn, get, hash } from '@ember/helper';
10
- import { on } from '@ember/modifier';
11
8
  import { getOwner } from '@ember/owner';
9
+ import { trackedArray, trackedMap } from '@ember/reactive/collections';
12
10
  import { precompileTemplate } from '@ember/template-compilation';
13
11
  import { waitFor } from '@ember/test-waiters';
14
12
 
@@ -18,7 +16,7 @@ import { Compiler } from 'repl-sdk';
18
16
  import { visit } from 'unist-util-visit';
19
17
 
20
18
  import { nameFor } from '../compile/utils.ts';
21
- import { modules } from './known-modules.ts';
19
+ import { modules, packagesFor } from './known-modules.ts';
22
20
 
23
21
  import type { CompileResult, ModuleMap } from '../compile/types.ts';
24
22
  import type { ComponentLike } from '@glint/template';
@@ -75,6 +73,9 @@ interface CompilerOptions {
75
73
  gjs?: {
76
74
  owner?: unknown;
77
75
  };
76
+ gts?: {
77
+ owner?: unknown;
78
+ };
78
79
  gmd?: {
79
80
  scope?: Record<string, unknown>;
80
81
  remarkPlugins?: unknown[];
@@ -87,14 +88,6 @@ interface CompilerOptions {
87
88
  * HBS isn't used in real apps (that are fully up to date)
88
89
  */
89
90
  const standardScope = {
90
- // These are only added here because it's convenient for hbs
91
- // to have them
92
- array,
93
- concat,
94
- fn,
95
- get,
96
- hash,
97
- on,
98
91
  // The default available scope for gjs:
99
92
  //
100
93
  // We don't use gjs transpilation here, because hbs transpilation
@@ -153,6 +146,21 @@ const standardScope = {
153
146
  isSecureContext,
154
147
  };
155
148
 
149
+ /**
150
+ * A spread would read the getters of `standardScope` once, before any compile.
151
+ */
152
+ function withStandardScope(extra: Record<string, unknown> | undefined) {
153
+ const scope = Object.defineProperties(
154
+ {},
155
+ Object.getOwnPropertyDescriptors(standardScope)
156
+ );
157
+
158
+ return Object.defineProperties(
159
+ scope,
160
+ Object.getOwnPropertyDescriptors(extra ?? {})
161
+ );
162
+ }
163
+
156
164
  export default class CompilerService {
157
165
  #compiler: Compiler | undefined;
158
166
 
@@ -172,7 +180,46 @@ export default class CompilerService {
172
180
  });
173
181
  }
174
182
 
175
- @tracked messages: Message[] = [];
183
+ readonly messages = trackedArray<Message>();
184
+
185
+ /**
186
+ * The packages that the last compile imported, as name → version.
187
+ * The version is `undefined` for modules that ember-repl or the host app provides.
188
+ */
189
+ readonly dependencies = trackedMap<string, string | undefined>();
190
+
191
+ /**
192
+ * The packages of the compile in progress.
193
+ * They replace `dependencies` when the compile ends,
194
+ * so that the list does not empty out while a compile runs.
195
+ */
196
+ #pending: Map<string, string | undefined> | undefined;
197
+
198
+ #dependencyListeners = new Set<
199
+ (entries: [string, string | undefined][]) => void
200
+ >();
201
+
202
+ /**
203
+ * Calls `listener` with all of `dependencies` after each change,
204
+ * for a page that shows them in another window.
205
+ *
206
+ * Returns a function that stops the calls.
207
+ */
208
+ onDependencies(listener: (entries: [string, string | undefined][]) => void) {
209
+ this.#dependencyListeners.add(listener);
210
+
211
+ return () => this.#dependencyListeners.delete(listener);
212
+ }
213
+
214
+ #reportDependencies() {
215
+ if (this.#dependencyListeners.size === 0) return;
216
+
217
+ const entries = Array.from(this.dependencies);
218
+
219
+ for (const listener of this.#dependencyListeners) {
220
+ listener(entries);
221
+ }
222
+ }
176
223
 
177
224
  get lastInfo(): InfoMessage | undefined {
178
225
  const m = this.messages;
@@ -232,15 +279,26 @@ export default class CompilerService {
232
279
 
233
280
  this.#compiler = new Compiler({
234
281
  logging: location.search.includes('debug'),
235
- resolve: {
236
- ...localModules,
237
- },
282
+ resolve: localModules,
283
+ packages: packagesFor(Object.keys(localModules)),
238
284
  on: {
239
- log: (type: Message['type'], message: string) => {
240
- this.messages.push({ type, message });
241
- // Waiting on better array primitive
242
- // eslint-disable-next-line no-self-assign
243
- this.messages = this.messages;
285
+ log: (type: Message['type'], message: string, error?: unknown) => {
286
+ this.messages.push(
287
+ type === 'error' ? { type, message, error } : { type, message }
288
+ );
289
+ },
290
+ dependency: (name: string, version: string | undefined) => {
291
+ if (this.#pending) {
292
+ this.#pending.set(name, version);
293
+
294
+ return;
295
+ }
296
+
297
+ /**
298
+ * An `import()` that runs after the compile.
299
+ */
300
+ this.dependencies.set(name, version);
301
+ this.#reportDependencies();
244
302
  },
245
303
  },
246
304
  options: {
@@ -249,13 +307,14 @@ export default class CompilerService {
249
307
  owner,
250
308
  ...(options.gjs ?? {}),
251
309
  },
310
+ gts: {
311
+ owner,
312
+ ...(options.gts ?? {}),
313
+ },
252
314
  gmd: {
253
315
  owner,
254
316
  ...(options.gmd ?? {}),
255
- scope: {
256
- ...standardScope,
257
- ...(options.gmd?.scope ?? {}),
258
- },
317
+ scope: withStandardScope(options.gmd?.scope),
259
318
  remarkPlugins: [
260
319
  function defaultHbsToEmber() {
261
320
  return function transformer(tree: any) {
@@ -281,10 +340,7 @@ export default class CompilerService {
281
340
  ember: {
282
341
  owner,
283
342
  ...(options.hbs ?? {}),
284
- scope: {
285
- ...standardScope,
286
- ...(options.hbs?.scope ?? {}),
287
- },
343
+ scope: withStandardScope(options.hbs?.scope),
288
344
  },
289
345
  },
290
346
  },
@@ -320,6 +376,12 @@ export default class CompilerService {
320
376
  view: EditorView;
321
377
  setText: (text: string, format: string) => Promise<void>;
322
378
  setFormat: (format: string) => Promise<void>;
379
+ /**
380
+ * Shows another file, with its own undo history.
381
+ * `format` is undefined for a file with no language support.
382
+ */
383
+ openDocument: (key: string, text: string, format: string | undefined) => Promise<void>;
384
+ forgetDocument: (key: string) => void;
323
385
  }> {
324
386
  return this.compiler.createEditor(element, options);
325
387
  }
@@ -334,9 +396,33 @@ export default class CompilerService {
334
396
  */
335
397
  await Promise.resolve();
336
398
 
337
- this.messages = [];
399
+ this.messages.length = 0;
400
+
401
+ const pending = new Map<string, string | undefined>();
402
+
403
+ this.#pending = pending;
404
+
405
+ try {
406
+ return await this.compiler.compile(ext, text, options ?? {});
407
+ } finally {
408
+ /**
409
+ * A newer compile owns the list now.
410
+ */
411
+ if (this.#pending === pending) {
412
+ this.#pending = undefined;
413
+ this.#publishDependencies(pending);
414
+ }
415
+ }
416
+ }
417
+
418
+ #publishDependencies(entries: Map<string, string | undefined>) {
419
+ this.dependencies.clear();
420
+
421
+ for (const [name, version] of entries) {
422
+ this.dependencies.set(name, version);
423
+ }
338
424
 
339
- return this.compiler.compile(ext, text, options ?? {});
425
+ this.#reportDependencies();
340
426
  }
341
427
 
342
428
  /**
@@ -1,4 +1,7 @@
1
+ import { VERSION } from '@ember/version';
2
+
1
3
  import type { ModuleMap } from '../compile/types.ts';
4
+ import type { Options } from 'repl-sdk';
2
5
 
3
6
  const frameworkModules = {
4
7
  '@ember/application': () => import('@ember/application'),
@@ -43,9 +46,11 @@ const frameworkModules = {
43
46
  };
44
47
 
45
48
  const coreLibraries = {
46
- 'ember-resolver': () => import('ember-resolver'),
47
49
  'ember-resources': () => import('ember-resources'),
48
50
  'ember-primitives': () => import('ember-primitives'),
51
+ // The prebuilt entry has its dependencies inlined and loads each diagram type on demand.
52
+ // @ts-ignore
53
+ mermaid: () => import('mermaid/dist/mermaid.esm.min.mjs'),
49
54
  'repl-sdk': () => import('repl-sdk'),
50
55
  };
51
56
 
@@ -59,7 +64,7 @@ const emberCompilationModules = {
59
64
  'ember-source/ember-template-compiler/index.js'
60
65
  ),
61
66
  // Direct Dependencies
62
- '@babel/standalone': () => import('@babel/standalone'),
67
+ '@glimdown/babel-8-lite': () => import('@glimdown/babel-8-lite'),
63
68
  'content-tag': () => import('content-tag'),
64
69
  'decorator-transforms': () => import('decorator-transforms'),
65
70
  'decorator-transforms/runtime': () => import('decorator-transforms/runtime'),
@@ -73,59 +78,6 @@ const emberCompilationModules = {
73
78
 
74
79
  // @ts-ignore
75
80
  'babel-plugin-debug-macros': () => import('babel-plugin-debug-macros'),
76
- '@embroider/macros': () => ({
77
- // passthrough, we are not doing dead-code-elimination
78
- macroCondition: (x: boolean) => x,
79
- // I *could* actually implement this
80
- appEmberSatisfies: () => true,
81
- // I *could* actually implement this
82
- dependencySatisfies: () => true,
83
- isDevelopingApp: () => true,
84
- // Trying to use warp-drive in a REPL environment may be impossible, since they
85
- // encourage choosing your own adventure without a buildless recommended path.
86
- // The use of nested configs (specifically env) is also problematic for
87
- // "falling back to false" as what all other macros-using libraries use.
88
- // (many of us have seen "Cannot access DEBUG on undefined" in build errors)
89
- // Even with this config, I have not successfully been able to use warp-drive
90
- // in any of my REPL-based projects.
91
- //
92
- // (Its also perfectly fine for warp-drive to decide they don't care about the same things I do)
93
- getGlobalConfig: () => ({
94
- WarpDrive: {
95
- debug: false,
96
- env: {
97
- DEBUG: false,
98
- TESTING: false,
99
- PRODUCTION: true,
100
- },
101
- activeLogging: false,
102
- compatWith: '99.0',
103
- features: {},
104
- deprecations: {},
105
- polyfillUUID: false,
106
- includeDataAdapter: false,
107
- },
108
- }),
109
- // Private
110
-
111
- // @ts-ignore
112
- importSync: (x: string) =>
113
- // @ts-expect-error
114
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
115
- window[Symbol.for('__repl-sdk__compiler__') as any]!.resolves[x],
116
- moduleExists: () => false,
117
- }),
118
- };
119
-
120
- const markdownCompilationModules = {
121
- // Provided directly in repl-sdk, but could be overridden
122
- // 'rehype-raw': () => import('rehype-raw'),
123
- // 'rehype-stringify': () => import('rehype-stringify'),
124
- // 'remark-gfm': () => import('remark-gfm'),
125
- // 'remark-parse': () => import('remark-parse'),
126
- // 'remark-rehype': () => import('remark-rehype'),
127
- // unified: () => import('unified'),
128
- // 'unist-util-visit': () => import('unist-util-visit'),
129
81
  };
130
82
 
131
83
  /**
@@ -137,5 +89,84 @@ export const modules = (extraModules: ModuleMap): ModuleMap => ({
137
89
  ...frameworkModules,
138
90
  ...extraModules,
139
91
  ...emberCompilationModules,
140
- ...markdownCompilationModules,
141
92
  });
93
+
94
+ const EMBER_SOURCE = 'ember-source';
95
+
96
+ /**
97
+ * Framework modules that are packages of their own.
98
+ * Every other framework module comes from ember-source.
99
+ */
100
+ const ownPackages = new Set([
101
+ '@glimmer/component',
102
+ '@ember/test-helpers',
103
+ '@ember/test-waiters',
104
+ ]);
105
+
106
+ /**
107
+ * The npm package of a bundled module.
108
+ *
109
+ * @ember/helper → ember-source
110
+ * @glimmer/component → @glimmer/component
111
+ * ember-primitives/x → ember-primitives
112
+ */
113
+ function packageOf(specifier: string): string {
114
+ if (specifier in frameworkModules && !ownPackages.has(specifier)) {
115
+ return EMBER_SOURCE;
116
+ }
117
+
118
+ if (
119
+ specifier.startsWith('@ember/template-compiler') ||
120
+ specifier.startsWith('ember-source/')
121
+ ) {
122
+ return EMBER_SOURCE;
123
+ }
124
+
125
+ const parts = specifier.split('/');
126
+
127
+ return specifier.startsWith('@')
128
+ ? `${parts[0]}/${parts[1]}`
129
+ : (parts[0] ?? specifier);
130
+ }
131
+
132
+ /**
133
+ * Compiler tools that work with any Ember, such as babel and content-tag.
134
+ * Every other bundled module is tied to the bundled Ember.
135
+ */
136
+ function isEmberIndependent(specifier: string): boolean {
137
+ return (
138
+ specifier in emberCompilationModules &&
139
+ packageOf(specifier) !== EMBER_SOURCE
140
+ );
141
+ }
142
+
143
+ /**
144
+ * What the bundled modules are, for repl-sdk's `packages` option.
145
+ *
146
+ * A compile that pins a package loads it from npm in place of these modules.
147
+ * Everything except the compiler tools imports the bundled Ember,
148
+ * so a pin of ember-source loads those from npm too.
149
+ */
150
+ export function packagesFor(
151
+ specifiers: string[]
152
+ ): NonNullable<Options['packages']> {
153
+ const emberSource = { version: VERSION, modules: [] as string[] };
154
+ const packages: NonNullable<Options['packages']> = {
155
+ [EMBER_SOURCE]: emberSource,
156
+ };
157
+
158
+ for (const specifier of specifiers) {
159
+ const name = packageOf(specifier);
160
+
161
+ if (name === EMBER_SOURCE) {
162
+ emberSource.modules.push(specifier);
163
+ continue;
164
+ }
165
+
166
+ packages[name] ??= isEmberIndependent(specifier)
167
+ ? {}
168
+ : { builtWith: [EMBER_SOURCE] };
169
+ }
170
+
171
+ return packages;
172
+ }
package/src/setup.ts CHANGED
@@ -5,8 +5,6 @@ import { getCompiler } from './services/compiler.ts';
5
5
  import type { ModuleMap } from './compile/types.ts';
6
6
  import type CompilerService from './services/compiler.ts';
7
7
 
8
- const NO_MODULES = Symbol.for('no custom modules configured');
9
-
10
8
  export function setup(
11
9
  context: object,
12
10
  options?: { modules?: ModuleMap; options?: object }
@@ -15,12 +13,7 @@ export function setup(
15
13
 
16
14
  assert(`Could not find the compiler service.`, compiler);
17
15
 
18
- compiler.setup(
19
- options?.modules ?? {
20
- [NO_MODULES]: true,
21
- },
22
- options?.options
23
- );
16
+ compiler.setup(options?.modules ?? {}, options?.options);
24
17
 
25
18
  return compiler;
26
19
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"_commonjsHelpers-BAGoDD49.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}