repl-sdk 0.0.0 → 1.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 (61) hide show
  1. package/dist/assets/tar-worker-kdkltuRC.js +598 -0
  2. package/dist/assets/tar-worker-kdkltuRC.js.map +1 -0
  3. package/dist/codemirror-D4aIVflZ.js +110 -0
  4. package/dist/codemirror-D4aIVflZ.js.map +1 -0
  5. package/dist/gjs-CzFzkEFv.js +173 -0
  6. package/dist/gjs-CzFzkEFv.js.map +1 -0
  7. package/dist/gmd-D9OXs2v3.js +166 -0
  8. package/dist/gmd-D9OXs2v3.js.map +1 -0
  9. package/dist/hbs-CuhWjffM.js +62 -0
  10. package/dist/hbs-CuhWjffM.js.map +1 -0
  11. package/dist/index-CUWCqMoD.js +2133 -0
  12. package/dist/index-CUWCqMoD.js.map +1 -0
  13. package/dist/index.js +4 -104
  14. package/dist/index.js.map +1 -1
  15. package/dist/parse-aBKk9rfS.js +328 -0
  16. package/dist/parse-aBKk9rfS.js.map +1 -0
  17. package/dist/render-app-island-B-i8rvGi.js +61 -0
  18. package/dist/render-app-island-B-i8rvGi.js.map +1 -0
  19. package/package.json +82 -9
  20. package/src/cache.js +138 -0
  21. package/src/cdn.js +93 -0
  22. package/src/codemirror.js +161 -0
  23. package/src/compilers/ember/gjs.js +212 -0
  24. package/src/compilers/ember/gmd.js +190 -0
  25. package/src/compilers/ember/hbs.js +98 -0
  26. package/src/compilers/ember/render-app-island.js +83 -0
  27. package/src/compilers/ember.js +166 -0
  28. package/src/compilers/js.js +32 -0
  29. package/src/compilers/markdown/build-compiler.js +151 -0
  30. package/src/compilers/markdown/const.js +2 -0
  31. package/src/compilers/markdown/heading-id.js +75 -0
  32. package/src/compilers/markdown/live-code-extraction.js +198 -0
  33. package/src/compilers/markdown/parse.js +22 -0
  34. package/src/compilers/markdown/parse.test.ts +363 -0
  35. package/src/compilers/markdown/sanitize-for-glimmer.js +26 -0
  36. package/src/compilers/markdown/types.ts +21 -0
  37. package/src/compilers/markdown/utils.js +78 -0
  38. package/src/compilers/markdown.js +125 -0
  39. package/src/compilers/mermaid.js +35 -0
  40. package/src/compilers/react.js +47 -0
  41. package/src/compilers/svelte.js +116 -0
  42. package/src/compilers/vue.js +58 -0
  43. package/src/compilers.js +108 -0
  44. package/src/es-module-shim.js +53 -0
  45. package/src/index.d.ts +53 -4
  46. package/src/index.js +744 -89
  47. package/src/npm.js +58 -0
  48. package/src/request.Request.test.ts +59 -0
  49. package/src/request.js +140 -0
  50. package/src/resolve.fromImports.test.ts +35 -0
  51. package/src/resolve.fromInternalImport.test.ts +69 -0
  52. package/src/resolve.js +352 -0
  53. package/src/resolve.resolvePath.test.ts +24 -0
  54. package/src/resolve.test.ts +23 -0
  55. package/src/specifier.js +71 -0
  56. package/src/specifier.test.ts +90 -0
  57. package/src/tar-worker.js +61 -0
  58. package/src/tar.js +76 -0
  59. package/src/types.ts +335 -58
  60. package/src/utils.js +28 -1
  61. package/declarations/index.d.ts +0 -73
package/src/cache.js ADDED
@@ -0,0 +1,138 @@
1
+ import { assert } from './utils.js';
2
+
3
+ export const secretKey = '__repl-sdk__compiler__';
4
+
5
+ /**
6
+ * @typedef {object} ResolveIdValue
7
+ * @property {string} name
8
+ * @property {string} version
9
+ * @property {import('./types.ts').RequestAnswer} path
10
+ *
11
+ * @typedef {import('./request.js').Request} Request
12
+ *
13
+ * @typedef {typeof globalThis & { [secret]?: {
14
+ * requestCache?: Map<string, Request>,
15
+ * resolveId?: Map<string, ResolveIdValue>,
16
+ * tarballs?: Map<string, import('./types.ts').UntarredPackage>,
17
+ * resolves?: { [modulePath: string]: unknown },
18
+ * promiseCache?: Map<string, Promise<unknown>>,
19
+ * fileCache?: Map<string, { code: string, ext: string }>
20
+ * caches?: Caches
21
+ * } }} ExtendedWindow
22
+ */
23
+ const secret = Symbol.for(secretKey);
24
+
25
+ function getGlobal() {
26
+ const global = /** @type {ExtendedWindow} */ (globalThis);
27
+
28
+ return global;
29
+ }
30
+
31
+ assert(
32
+ `There is already an instance of repl-sdk, and there can only be one. Make sure that your dependency graph is correct.`,
33
+ !getGlobal()[secret]
34
+ );
35
+
36
+ class Caches {
37
+ clear() {
38
+ delete getGlobal()[secret];
39
+ }
40
+
41
+ /**
42
+ * Cache of resolved modulePaths to their module "value"
43
+ *
44
+ * @type {{ [modulePath: string]: unknown }}
45
+ */
46
+ get resolves() {
47
+ this.#root.resolves ||= {};
48
+
49
+ return this.#root.resolves;
50
+ }
51
+
52
+ /**
53
+ * Cache of untarred tarballs
54
+ *
55
+ * @type {Map<string, import('./types.ts').UntarredPackage>}
56
+ */
57
+ get tarballs() {
58
+ this.#root.tarballs ||= new Map();
59
+
60
+ return this.#root.tarballs;
61
+ }
62
+
63
+ /**
64
+ * Cache of request resolutions
65
+ *
66
+ * @type {Map<string, ResolveIdValue>}
67
+ */
68
+ get resolveId() {
69
+ this.#root.resolveId ||= new Map();
70
+
71
+ return this.#root.resolveId;
72
+ }
73
+
74
+ /**
75
+ * Cache of request Key to file content string
76
+ *
77
+ * @type {Map<string, { code: string, ext: string }>}
78
+ */
79
+ get fileCache() {
80
+ this.#root.fileCache ||= new Map();
81
+
82
+ return this.#root.fileCache;
83
+ }
84
+
85
+ /**
86
+ * For any key, store a promise for resolving later
87
+ *
88
+ * @type {Map<string, Promise<unknown>>}
89
+ */
90
+ get promiseCache() {
91
+ this.#root.promiseCache ||= new Map();
92
+
93
+ return this.#root.promiseCache;
94
+ }
95
+
96
+ /**
97
+ * @template Return
98
+ * @type {(key: string, callback: () => Promise<any>) => Promise<any>}
99
+ */
100
+ cachedPromise(key, callback) {
101
+ const existing = this.promiseCache.get(key);
102
+
103
+ if (existing) {
104
+ return /** @type {Promise<Return>} */ (existing);
105
+ }
106
+
107
+ const promise = callback();
108
+
109
+ this.promiseCache.set(key, promise);
110
+
111
+ return promise;
112
+ }
113
+
114
+ get requestCache() {
115
+ this.#root.requestCache ||= new Map();
116
+
117
+ return this.#root.requestCache;
118
+ }
119
+
120
+ get #root() {
121
+ const global = getGlobal();
122
+
123
+ global[secret] ||= {};
124
+ global[secret].caches ||= this;
125
+
126
+ return global[secret];
127
+ }
128
+ }
129
+
130
+ export function deleteCache() {
131
+ if (!getGlobal()?.[secret]) {
132
+ return;
133
+ }
134
+
135
+ delete getGlobal()[secret];
136
+ }
137
+
138
+ export const cache = new Caches();
package/src/cdn.js ADDED
@@ -0,0 +1,93 @@
1
+ import { parseSpecifier } from './specifier.js';
2
+
3
+ /**
4
+ * @param {string} importPath
5
+ * @returns {[string, string]}
6
+ */
7
+ function splitSubPath(importPath) {
8
+ const parsed = parseSpecifier(importPath);
9
+
10
+ return [parsed.name, parsed.path === '.' ? '' : parsed.path];
11
+ }
12
+
13
+ /**
14
+ * Generate an import URL for esm.sh
15
+ *
16
+ * @param {Record<string, string>} versions
17
+ * @param {string} importPath
18
+ */
19
+ export function esmSh(versions, importPath, allExternal = false) {
20
+ const [name, subPath] = splitSubPath(importPath);
21
+
22
+ const version = versions[name];
23
+ const subPathExport = subPath.length === 0 || subPath.startsWith('/') ? subPath : `/${subPath}`;
24
+
25
+ const externals = allExternal ? '*' : '';
26
+
27
+ return version
28
+ ? `https://esm.sh/${externals}${name}@${version}${subPathExport}`
29
+ : `https://esm.sh/${externals}${name}${subPathExport}`;
30
+ }
31
+
32
+ /**
33
+ * Generate an import URL for esm.run (jsdelivr)
34
+ *
35
+ * @param {Record<string, string>} versions
36
+ * @param {string} importPath
37
+ */
38
+ export function esmRun(versions, importPath) {
39
+ const [name, subPath] = splitSubPath(importPath);
40
+
41
+ const version = versions[name];
42
+ const subPathExport = subPath.length === 0 || subPath.startsWith('/') ? subPath : `/${subPath}`;
43
+
44
+ return version
45
+ ? `https://esm.run/${name}@latest${subPathExport}`
46
+ : `https://esm.run/${name}${subPathExport}`;
47
+ }
48
+
49
+ export const jsdelivr = {
50
+ /**
51
+ * @param {Record<string, string>} versions
52
+ * @param {string} importPath
53
+ */
54
+ async import(versions, importPath) {
55
+ const url = esmRun(versions, importPath);
56
+
57
+ return await import(/* @vite-ignore */ url);
58
+ },
59
+ /**
60
+ * @param {Record<string, string>} versions
61
+ * @param {string[]} deps the names of deps to pull from esm.sh
62
+ */
63
+ async importAll(versions, deps = []) {
64
+ return await Promise.all(
65
+ deps.map((dep) => {
66
+ return jsdelivr.import(versions, dep);
67
+ })
68
+ );
69
+ },
70
+ };
71
+
72
+ export const esmsh = {
73
+ /**
74
+ * @param {Record<string, string>} versions
75
+ * @param {string} importPath
76
+ */
77
+ async import(versions, importPath) {
78
+ const url = esmSh(versions, importPath);
79
+
80
+ return await import(/* @vite-ignore */ url);
81
+ },
82
+ /**
83
+ * @param {Record<string, string>} versions
84
+ * @param {string[]} deps the names of deps to pull from esm.sh
85
+ */
86
+ async importAll(versions, deps = []) {
87
+ return await Promise.all(
88
+ deps.map((dep) => {
89
+ return esmsh.import(versions, dep);
90
+ })
91
+ );
92
+ },
93
+ };
@@ -0,0 +1,161 @@
1
+ import { completionKeymap } from '@codemirror/autocomplete';
2
+ import { indentWithTab } from '@codemirror/commands';
3
+ import { markdownKeymap } from '@codemirror/lang-markdown';
4
+ import { Compartment, EditorSelection, EditorState } from '@codemirror/state';
5
+ import { keymap } from '@codemirror/view';
6
+ import { basicSetup, EditorView } from 'codemirror';
7
+ // @ts-ignore
8
+ import { foldByIndent } from 'codemirror-lang-mermaid';
9
+
10
+ /**
11
+ * Builds and creates a codemirror instance for the given element
12
+ *
13
+ * @typedef {any} Extension
14
+ *
15
+ * @typedef {object} CodemirrorOptions
16
+ * @property {HTMLElement} element
17
+ * @property {string} text
18
+ * @property {string} format
19
+ * @property {Extension[]} [ extensions ]
20
+ * @property {(text: string) => void} handleUpdate
21
+ * @property {(format: string) => Promise<Extension>} getLang
22
+ * @property {(format: string) => Promise<Extension>} getSupport
23
+ *
24
+ * @param {CodemirrorOptions} options
25
+ */
26
+ export async function buildCodemirror({
27
+ element,
28
+ text,
29
+ format,
30
+ extensions,
31
+ handleUpdate,
32
+ getLang,
33
+ getSupport,
34
+ }) {
35
+ const languageConf = new Compartment();
36
+ const supportConf = new Compartment();
37
+ const tabSize = new Compartment();
38
+
39
+ const updateListener = EditorView.updateListener.of(({ state, docChanged }) => {
40
+ if (docChanged) {
41
+ handleUpdate(state.doc.toString());
42
+ }
43
+ });
44
+
45
+ /**
46
+ * @param {string} format
47
+ */
48
+ async function languageForFormat(format) {
49
+ switch (format) {
50
+ case 'glimdown':
51
+ case 'gdm':
52
+ case 'gmd':
53
+ return getLang('gmd');
54
+ case 'jsx':
55
+ case 'jsx|react':
56
+ return getLang('jsx|react');
57
+ default:
58
+ return getLang(format);
59
+ }
60
+ }
61
+
62
+ /**
63
+ * @param {string} format
64
+ */
65
+ async function supportForFormat(format) {
66
+ const support = await getSupport(format);
67
+
68
+ if (!support) {
69
+ return [];
70
+ }
71
+
72
+ return Array.isArray(support) ? support : [support];
73
+ }
74
+
75
+ const [language, support] = await Promise.all([
76
+ languageForFormat(format),
77
+ supportForFormat(format),
78
+ ]);
79
+
80
+ const editorExtensions = [
81
+ // features
82
+ basicSetup,
83
+ foldByIndent(),
84
+ // Language
85
+ languageConf.of(language),
86
+ supportConf.of(support),
87
+
88
+ updateListener,
89
+ EditorView.lineWrapping,
90
+ keymap.of([indentWithTab, ...completionKeymap, ...markdownKeymap]),
91
+
92
+ // TODO: lsp,
93
+
94
+ ...(extensions ?? []),
95
+ ].filter(Boolean);
96
+
97
+ const view = new EditorView({
98
+ parent: element,
99
+ state: EditorState.create({
100
+ extensions: editorExtensions,
101
+ }),
102
+ });
103
+
104
+ /**
105
+ * Called from the host app to update the editor.
106
+ *
107
+ * @param {string} text
108
+ * @param {string} format
109
+ */
110
+ const setText = async (text, format) => {
111
+ const [language, support] = await Promise.all([
112
+ languageForFormat(format),
113
+ supportForFormat(format),
114
+ ]);
115
+
116
+ console.debug(`Codemirror changing to ${format}: ${language ? 'ok' : 'not ok'}`);
117
+
118
+ view.dispatch({
119
+ changes: {
120
+ from: 0,
121
+ to: view.state.doc.length,
122
+ insert: text,
123
+ },
124
+ effects: [languageConf.reconfigure(language), supportConf.reconfigure(support)],
125
+ });
126
+ };
127
+
128
+ /**
129
+ * Changes just the format of the editor.
130
+ *
131
+ * @param {string} format
132
+ */
133
+ const setFormat = async (format) => {
134
+ const [language, support] = await Promise.all([
135
+ languageForFormat(format),
136
+ supportForFormat(format),
137
+ ]);
138
+
139
+ console.debug(`Codemirror changing to ${format}: ${language ? 'ok' : 'not ok'}`);
140
+
141
+ view.dispatch({
142
+ effects: [languageConf.reconfigure(language), supportConf.reconfigure(support)],
143
+ });
144
+ };
145
+
146
+ view.dispatch(
147
+ view.state.changeByRange((range) => ({
148
+ changes: [{ from: range.from, insert: text }],
149
+ range: EditorSelection.range(range.from, range.to),
150
+ }))
151
+ );
152
+
153
+ view.dispatch({
154
+ effects: [
155
+ tabSize.reconfigure(EditorState.tabSize.of(2)),
156
+ // languageConf.reconfigure(languageForFormat(format)),
157
+ ],
158
+ });
159
+
160
+ return { view, setText, setFormat };
161
+ }
@@ -0,0 +1,212 @@
1
+ import { cache } from '../../cache.js';
2
+ import { renderApp } from './render-app-island.js';
3
+
4
+ let elementId = 0;
5
+
6
+ const buildDependencies = [
7
+ /**
8
+ * The only version of babel that is easily runnable in the browser
9
+ * This includes way too much stuff.
10
+ */
11
+ '@babel/standalone',
12
+ /**
13
+ * We will be using this decorator transform
14
+ * instead of the babel one.
15
+ * The babel transform does way too much transforming.
16
+ */
17
+ 'decorator-transforms',
18
+
19
+ /**
20
+ * Babel plugin that understands all the different ways
21
+ * which templates have been authored and what they need to
22
+ * compile to over the years.
23
+ */
24
+ 'babel-plugin-ember-template-compilation',
25
+ /**
26
+ * The actual template-compiler is ember-sounce-dependent,
27
+ * because the underlying format / bytecodes / etc is private,
28
+ * and can change between versions of ember-source.
29
+ */
30
+ 'ember-source/dist/ember-template-compiler.js',
31
+ /**
32
+ * Converts gjs/gts to standard js/ts
33
+ */
34
+ 'content-tag',
35
+ /**
36
+ * Older-style build macros
37
+ * (before import.meta.env was even a thing)
38
+ *
39
+ * These remove `@glimmer/env` and DEBUG usages
40
+ */
41
+ 'babel-plugin-debug-macros',
42
+
43
+ /**
44
+ * build macros, because the ecosystem isn't standardized on imprt.meta.env?.X
45
+ * Also, @embroider/macros does dead-code-elimination, which is handy.
46
+ */
47
+ // '@embroider/macros/babel',
48
+ ];
49
+
50
+ /**
51
+ * @type {import('../../types.ts').CompilerConfig['compiler']}
52
+ */
53
+ export async function compiler(config, api) {
54
+ const [
55
+ _babel,
56
+ _decoratorTransforms,
57
+ _emberTemplateCompilation,
58
+ compiler,
59
+ contentTag,
60
+ { default: DebugMacros },
61
+ // embroiderMacros,
62
+ ] = await api.tryResolveAll(buildDependencies);
63
+
64
+ // These libraries are compiled incorrectly for cjs<->ESM compat
65
+ const decoratorTransforms =
66
+ 'default' in _decoratorTransforms ? _decoratorTransforms.default : _decoratorTransforms;
67
+
68
+ const emberTemplateCompilation =
69
+ 'default' in _emberTemplateCompilation
70
+ ? _emberTemplateCompilation.default
71
+ : _emberTemplateCompilation;
72
+
73
+ const babel = 'availablePlugins' in _babel ? _babel : _babel.default;
74
+
75
+ // let macros = embroiderMacros.buildMacros();
76
+
77
+ /**
78
+ * @param {string} text
79
+ */
80
+ async function transform(text) {
81
+ return babel.transform(text, {
82
+ filename: `dynamic-repl.js`,
83
+ plugins: [
84
+ [
85
+ emberTemplateCompilation,
86
+ {
87
+ compiler,
88
+ transforms: [
89
+ // ...macros.templateMacros
90
+ ],
91
+ targetFormat: 'wire',
92
+ },
93
+ ],
94
+ [
95
+ // @ts-ignore - we don't care about types here..
96
+ decoratorTransforms,
97
+ {
98
+ runtime: {
99
+ import: 'decorator-transforms/runtime',
100
+ },
101
+ },
102
+ ],
103
+ // ...macros.babelMacros,
104
+ [
105
+ DebugMacros,
106
+ {
107
+ flags: [
108
+ {
109
+ source: '@glimmer/env',
110
+ flags: {
111
+ DEBUG: true,
112
+ CI: false,
113
+ },
114
+ },
115
+ ],
116
+ debugTools: {
117
+ isDebug: true,
118
+ source: '@ember/debug',
119
+ assertPredicateIndex: 1,
120
+ },
121
+ externalizeHelpers: {
122
+ module: '@ember/debug',
123
+ },
124
+ },
125
+ '@ember/debug stripping',
126
+ ],
127
+ [
128
+ DebugMacros,
129
+ {
130
+ externalizeHelpers: {
131
+ module: '@ember/application/deprecations',
132
+ },
133
+ debugTools: {
134
+ isDebug: true,
135
+ source: '@ember/application/deprecations',
136
+ assertPredicateIndex: 1,
137
+ },
138
+ },
139
+ '@ember/application/deprecations stripping',
140
+ ],
141
+ ],
142
+ presets: [],
143
+ });
144
+ }
145
+
146
+ const preprocessor = new contentTag.Preprocessor();
147
+
148
+ /**
149
+ * @type {import('../../types.ts').Compiler}
150
+ */
151
+ const gjsCompiler = {
152
+ compile: async (text, options) => {
153
+ const { code: preprocessed } = preprocessor.process(text, { filename: 'dynamic-repl.js' });
154
+ const transformed = await transform(preprocessed);
155
+
156
+ const code = transformed.code;
157
+
158
+ return code;
159
+ },
160
+ render: async (element, compiled, extra, compiler) => {
161
+ /**
162
+ *
163
+ * TODO: These will make things easier:
164
+ * https://github.com/emberjs/rfcs/pull/1099
165
+ * https://github.com/ember-cli/ember-addon-blueprint/blob/main/files/tests/test-helper.js
166
+ */
167
+ const attribute = `data-repl-sdk-ember-gjs-${elementId++}`;
168
+
169
+ element.setAttribute(attribute, '');
170
+
171
+ const [application, destroyable, resolver, router, route, testWaiters, runloop] =
172
+ await compiler.tryResolveAll([
173
+ '@ember/application',
174
+ '@ember/destroyable',
175
+ 'ember-resolver',
176
+ '@ember/routing/router',
177
+ '@ember/routing/route',
178
+ '@ember/test-waiters',
179
+ '@ember/runloop',
180
+ ]);
181
+
182
+ // We don't want to await here, because we need to early
183
+ // return the element so that the app can render in to it.
184
+ // (Ember will only render in to an element if it's present in the DOM)
185
+ return renderApp({
186
+ element,
187
+ selector: `[${attribute}]`,
188
+ component: compiled,
189
+ log: compiler.announce,
190
+ modules: {
191
+ application,
192
+ destroyable,
193
+ resolver,
194
+ router,
195
+ route,
196
+ testWaiters,
197
+ runloop,
198
+ },
199
+ });
200
+ },
201
+ handlers: {
202
+ js: async (text) => {
203
+ return gjsCompiler.compile(text, {});
204
+ },
205
+ mjs: async (text) => {
206
+ return gjsCompiler.compile(text, {});
207
+ },
208
+ },
209
+ };
210
+
211
+ return gjsCompiler;
212
+ }