@dxos/plugin-script 0.6.11 → 0.6.12-main.5cc132e
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.
- package/dist/lib/node-esm/ScriptEditor-5Y3EROG3.mjs +513 -0
- package/dist/lib/node-esm/ScriptEditor-5Y3EROG3.mjs.map +7 -0
- package/dist/lib/node-esm/TypescriptEditor-GPE72Y6I.mjs +9 -0
- package/dist/lib/node-esm/TypescriptEditor-GPE72Y6I.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-DL5XQRQM.mjs +241 -0
- package/dist/lib/node-esm/chunk-DL5XQRQM.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-JBACGDOJ.mjs +21 -0
- package/dist/lib/node-esm/chunk-JBACGDOJ.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-MRGOT5RF.mjs +78 -0
- package/dist/lib/node-esm/chunk-MRGOT5RF.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-O4LWBX6A.mjs +46 -0
- package/dist/lib/node-esm/chunk-O4LWBX6A.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-RHEZ5LUU.mjs +229 -0
- package/dist/lib/node-esm/chunk-RHEZ5LUU.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +19 -0
- package/dist/lib/node-esm/edge/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +291 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/lib/node-esm/meta.mjs +9 -0
- package/dist/lib/node-esm/meta.mjs.map +7 -0
- package/dist/lib/node-esm/types/index.mjs +12 -0
- package/dist/lib/node-esm/types/index.mjs.map +7 -0
- package/package.json +51 -47
- package/src/bundler/bundler.test.ts +12 -2
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// packages/plugins/experimental/plugin-script/src/bundler/bundler.ts
|
|
2
|
+
import { build, initialize } from "esbuild-wasm";
|
|
3
|
+
import { subtleCrypto } from "@dxos/crypto";
|
|
4
|
+
import { invariant } from "@dxos/invariant";
|
|
5
|
+
import { log } from "@dxos/log";
|
|
6
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/experimental/plugin-script/src/bundler/bundler.ts";
|
|
7
|
+
var initialized;
|
|
8
|
+
var initializeBundler = async (options) => {
|
|
9
|
+
await (initialized ??= initialize({
|
|
10
|
+
wasmURL: options.wasmUrl
|
|
11
|
+
}));
|
|
12
|
+
};
|
|
13
|
+
var Bundler = class {
|
|
14
|
+
constructor(_options) {
|
|
15
|
+
this._options = _options;
|
|
16
|
+
}
|
|
17
|
+
async bundle(source) {
|
|
18
|
+
const { sandboxedModules: providedModules, ...options } = this._options;
|
|
19
|
+
const createResult = async (result) => {
|
|
20
|
+
return {
|
|
21
|
+
timestamp: Date.now(),
|
|
22
|
+
sourceHash: Buffer.from(await subtleCrypto.digest("SHA-256", Buffer.from(source))),
|
|
23
|
+
...result
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
if (this._options.platform === "browser") {
|
|
27
|
+
invariant(initialized, "Compiler not initialized.", {
|
|
28
|
+
F: __dxlog_file,
|
|
29
|
+
L: 57,
|
|
30
|
+
S: this,
|
|
31
|
+
A: [
|
|
32
|
+
"initialized",
|
|
33
|
+
"'Compiler not initialized.'"
|
|
34
|
+
]
|
|
35
|
+
});
|
|
36
|
+
await initialized;
|
|
37
|
+
}
|
|
38
|
+
const imports = analyzeSourceFileImports(source);
|
|
39
|
+
try {
|
|
40
|
+
const result = await build({
|
|
41
|
+
platform: options.platform,
|
|
42
|
+
conditions: [
|
|
43
|
+
"workerd",
|
|
44
|
+
"browser"
|
|
45
|
+
],
|
|
46
|
+
metafile: true,
|
|
47
|
+
write: false,
|
|
48
|
+
entryPoints: [
|
|
49
|
+
"memory:main.tsx"
|
|
50
|
+
],
|
|
51
|
+
bundle: true,
|
|
52
|
+
format: "esm",
|
|
53
|
+
plugins: [
|
|
54
|
+
{
|
|
55
|
+
name: "memory",
|
|
56
|
+
setup: (build2) => {
|
|
57
|
+
build2.onResolve({
|
|
58
|
+
filter: /^\.\/runtime\.js$/
|
|
59
|
+
}, ({ path }) => {
|
|
60
|
+
return {
|
|
61
|
+
path,
|
|
62
|
+
external: true
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
build2.onResolve({
|
|
66
|
+
filter: /^memory:/
|
|
67
|
+
}, ({ path }) => {
|
|
68
|
+
return {
|
|
69
|
+
path: path.split(":")[1],
|
|
70
|
+
namespace: "memory"
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
build2.onLoad({
|
|
74
|
+
filter: /.*/,
|
|
75
|
+
namespace: "memory"
|
|
76
|
+
}, ({ path }) => {
|
|
77
|
+
if (path === "main.tsx") {
|
|
78
|
+
return {
|
|
79
|
+
contents: source,
|
|
80
|
+
loader: "tsx"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
for (const module of providedModules) {
|
|
85
|
+
build2.onResolve({
|
|
86
|
+
filter: new RegExp(`^${module}$`)
|
|
87
|
+
}, ({ path }) => {
|
|
88
|
+
return {
|
|
89
|
+
path,
|
|
90
|
+
namespace: "injected-module"
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
build2.onLoad({
|
|
95
|
+
filter: /.*/,
|
|
96
|
+
namespace: "injected-module"
|
|
97
|
+
}, ({ path }) => {
|
|
98
|
+
const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];
|
|
99
|
+
return {
|
|
100
|
+
contents: `
|
|
101
|
+
const { ${namedImports.join(",")} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];
|
|
102
|
+
export { ${namedImports.join(",")} };
|
|
103
|
+
export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;
|
|
104
|
+
`,
|
|
105
|
+
loader: "tsx"
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
httpPlugin
|
|
111
|
+
]
|
|
112
|
+
});
|
|
113
|
+
log("compile complete", result.metafile, {
|
|
114
|
+
F: __dxlog_file,
|
|
115
|
+
L: 117,
|
|
116
|
+
S: this,
|
|
117
|
+
C: (f, a) => f(...a)
|
|
118
|
+
});
|
|
119
|
+
return await createResult({
|
|
120
|
+
imports: this.analyzeImports(result),
|
|
121
|
+
bundle: result.outputFiles[0].text
|
|
122
|
+
});
|
|
123
|
+
} catch (err) {
|
|
124
|
+
return await createResult({
|
|
125
|
+
error: err
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.
|
|
130
|
+
analyzeImports(result) {
|
|
131
|
+
invariant(result.outputFiles, void 0, {
|
|
132
|
+
F: __dxlog_file,
|
|
133
|
+
L: 130,
|
|
134
|
+
S: this,
|
|
135
|
+
A: [
|
|
136
|
+
"result.outputFiles",
|
|
137
|
+
""
|
|
138
|
+
]
|
|
139
|
+
});
|
|
140
|
+
const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
|
|
141
|
+
return Object.values(result.metafile.outputs)[0].imports.map((entry) => {
|
|
142
|
+
const namedImports = [];
|
|
143
|
+
const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
|
|
144
|
+
if (parsedImport?.[2]) {
|
|
145
|
+
NAMED_IMPORTS_REGEX.lastIndex = 0;
|
|
146
|
+
const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);
|
|
147
|
+
if (namedImportsMatch) {
|
|
148
|
+
namedImportsMatch[1].split(",").forEach((importName) => {
|
|
149
|
+
namedImports.push(importName.trim());
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
moduleUrl: entry.path,
|
|
155
|
+
defaultImport: !!parsedImport?.[1],
|
|
156
|
+
namedImports
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
analyzeSourceFileImports(code) {
|
|
161
|
+
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
162
|
+
return parsedImports.map((capture) => {
|
|
163
|
+
return {
|
|
164
|
+
defaultImportName: capture[1],
|
|
165
|
+
namedImports: capture[2]?.split(",").map((importName) => importName.trim()),
|
|
166
|
+
wildcardImportName: capture[3],
|
|
167
|
+
moduleIdentifier: capture[4],
|
|
168
|
+
quotes: capture[5]
|
|
169
|
+
};
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var IMPORT_REGEX = /import(?:(?:(?:[ \n\t]+([^ *\n\t{},]+)[ \n\t]*(?:,|[ \n\t]+))?([ \n\t]*{(?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+})?[ \n\t]*)|[ \n\t]*\*[ \n\t]*as[ \n\t]+([^ \n\t{}]+)[ \n\t]+)from[ \n\t]*(?:['"])([^'"\n]+)(['"])/gm;
|
|
174
|
+
var NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
|
|
175
|
+
var allMatches = (regex, str) => {
|
|
176
|
+
regex.lastIndex = 0;
|
|
177
|
+
let match;
|
|
178
|
+
const matches = [];
|
|
179
|
+
while (match = regex.exec(str)) {
|
|
180
|
+
matches.push(match);
|
|
181
|
+
}
|
|
182
|
+
return matches;
|
|
183
|
+
};
|
|
184
|
+
var analyzeSourceFileImports = (code) => {
|
|
185
|
+
const parsedImports = allMatches(IMPORT_REGEX, code);
|
|
186
|
+
return parsedImports.map((capture) => {
|
|
187
|
+
return {
|
|
188
|
+
defaultImportName: capture[1],
|
|
189
|
+
namedImports: capture[2]?.trim().slice(1, -1).split(",").map((importName) => importName.trim()),
|
|
190
|
+
wildcardImportName: capture[3],
|
|
191
|
+
moduleIdentifier: capture[4],
|
|
192
|
+
quotes: capture[5]
|
|
193
|
+
};
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
var httpPlugin = {
|
|
197
|
+
name: "http",
|
|
198
|
+
setup: (build2) => {
|
|
199
|
+
build2.onResolve({
|
|
200
|
+
filter: /^https?:\/\//
|
|
201
|
+
}, (args) => ({
|
|
202
|
+
path: args.path,
|
|
203
|
+
namespace: "http-url"
|
|
204
|
+
}));
|
|
205
|
+
build2.onResolve({
|
|
206
|
+
filter: /.*/,
|
|
207
|
+
namespace: "http-url"
|
|
208
|
+
}, (args) => ({
|
|
209
|
+
path: new URL(args.path, args.importer).toString(),
|
|
210
|
+
namespace: "http-url"
|
|
211
|
+
}));
|
|
212
|
+
build2.onLoad({
|
|
213
|
+
filter: /.*/,
|
|
214
|
+
namespace: "http-url"
|
|
215
|
+
}, async (args) => {
|
|
216
|
+
const response = await fetch(args.path);
|
|
217
|
+
return {
|
|
218
|
+
contents: await response.text(),
|
|
219
|
+
loader: "jsx"
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
export {
|
|
226
|
+
initializeBundler,
|
|
227
|
+
Bundler
|
|
228
|
+
};
|
|
229
|
+
//# sourceMappingURL=chunk-RHEZ5LUU.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/bundler/bundler.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type BuildOptions } from 'esbuild';\nimport { build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';\n\nimport { subtleCrypto } from '@dxos/crypto';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nexport type Import = {\n moduleUrl: string;\n defaultImport: boolean;\n namedImports: string[];\n};\n\nexport type BundlerResult = {\n timestamp: number;\n sourceHash: Buffer;\n imports?: Import[];\n bundle?: string;\n error?: any;\n};\n\nexport type BundlerOptions = {\n platform: BuildOptions['platform'];\n sandboxedModules: string[];\n remoteModules: Record<string, string>;\n};\n\nlet initialized: Promise<void>;\nexport const initializeBundler = async (options: { wasmUrl: string }) => {\n await (initialized ??= initialize({\n wasmURL: options.wasmUrl,\n }));\n};\n\n/**\n * ESBuild bundler.\n */\nexport class Bundler {\n constructor(private readonly _options: BundlerOptions) {}\n\n async bundle(source: string): Promise<BundlerResult> {\n const { sandboxedModules: providedModules, ...options } = this._options;\n\n const createResult = async (result?: Partial<BundlerResult>) => {\n return {\n timestamp: Date.now(),\n sourceHash: Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))),\n ...result,\n };\n };\n\n if (this._options.platform === 'browser') {\n invariant(initialized, 'Compiler not initialized.');\n await initialized;\n }\n\n const imports = analyzeSourceFileImports(source);\n\n // https://esbuild.github.io/api/#build\n try {\n const result = await build({\n platform: options.platform,\n conditions: ['workerd', 'browser'],\n metafile: true,\n write: false,\n entryPoints: ['memory:main.tsx'],\n bundle: true,\n format: 'esm',\n plugins: [\n {\n name: 'memory',\n setup: (build) => {\n build.onResolve({ filter: /^\\.\\/runtime\\.js$/ }, ({ path }) => {\n return { path, external: true };\n });\n\n build.onResolve({ filter: /^memory:/ }, ({ path }) => {\n return { path: path.split(':')[1], namespace: 'memory' };\n });\n\n build.onLoad({ filter: /.*/, namespace: 'memory' }, ({ path }) => {\n if (path === 'main.tsx') {\n return {\n contents: source,\n loader: 'tsx',\n };\n }\n });\n\n for (const module of providedModules) {\n build.onResolve({ filter: new RegExp(`^${module}$`) }, ({ path }) => {\n return { path, namespace: 'injected-module' };\n });\n }\n\n build.onLoad({ filter: /.*/, namespace: 'injected-module' }, ({ path }) => {\n const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];\n return {\n contents: `\n const { ${namedImports.join(',')} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];\n export { ${namedImports.join(',')} };\n export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;\n `,\n loader: 'tsx',\n };\n });\n },\n },\n httpPlugin,\n ],\n });\n\n log('compile complete', result.metafile);\n\n return await createResult({\n imports: this.analyzeImports(result),\n bundle: result.outputFiles![0].text,\n });\n } catch (err) {\n return await createResult({ error: err });\n }\n }\n\n // TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.\n analyzeImports(result: BuildResult): Import[] {\n invariant(result.outputFiles);\n\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);\n return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {\n const namedImports: string[] = [];\n\n const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);\n if (parsedImport?.[2]) {\n NAMED_IMPORTS_REGEX.lastIndex = 0;\n const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);\n if (namedImportsMatch) {\n namedImportsMatch[1].split(',').forEach((importName) => {\n namedImports.push(importName.trim());\n });\n }\n }\n\n return {\n moduleUrl: entry.path,\n defaultImport: !!parsedImport?.[1],\n namedImports,\n };\n });\n }\n\n analyzeSourceFileImports(code: string) {\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, code);\n return parsedImports.map((capture) => {\n return {\n defaultImportName: capture[1],\n namedImports: capture[2]?.split(',').map((importName) => importName.trim()),\n wildcardImportName: capture[3],\n moduleIdentifier: capture[4],\n quotes: capture[5],\n };\n });\n }\n}\n\n// https://regex101.com/r/FEN5ks/1\n// https://stackoverflow.com/a/73265022\n// $1 = default import name (can be non-existent)\n// $2 = destructured exports (can be non-existent)\n// $3 = wildcard import name (can be non-existent)\n// $4 = module identifier\n// $5 = quotes used (either ' or \")\nconst IMPORT_REGEX =\n /import(?:(?:(?:[ \\n\\t]+([^ *\\n\\t{},]+)[ \\n\\t]*(?:,|[ \\n\\t]+))?([ \\n\\t]*{(?:[ \\n\\t]*[^ \\n\\t\"'{}]+[ \\n\\t]*,?)+})?[ \\n\\t]*)|[ \\n\\t]*\\*[ \\n\\t]*as[ \\n\\t]+([^ \\n\\t{}]+)[ \\n\\t]+)from[ \\n\\t]*(?:['\"])([^'\"\\n]+)(['\"])/gm;\n\nconst NAMED_IMPORTS_REGEX = /[ \\n\\t]*{((?:[ \\n\\t]*[^ \\n\\t\"'{}]+[ \\n\\t]*,?)+)}[ \\n\\t]*/gm;\n\nconst allMatches = (regex: RegExp, str: string) => {\n regex.lastIndex = 0;\n\n let match;\n const matches = [];\n while ((match = regex.exec(str))) {\n matches.push(match);\n }\n\n return matches;\n};\n\ntype ParsedImport = {\n defaultImportName?: string;\n namedImports: string[];\n wildcardImportName?: string;\n moduleIdentifier: string;\n quotes: string;\n};\n\nconst analyzeSourceFileImports = (code: string): ParsedImport[] => {\n // TODO(dmaretskyi): Support import aliases and wildcard imports.\n const parsedImports = allMatches(IMPORT_REGEX, code);\n return parsedImports.map((capture) => {\n return {\n defaultImportName: capture[1],\n namedImports: capture[2]\n ?.trim()\n .slice(1, -1)\n .split(',')\n .map((importName) => importName.trim()),\n wildcardImportName: capture[3],\n moduleIdentifier: capture[4],\n quotes: capture[5],\n };\n });\n};\n\nconst httpPlugin: Plugin = {\n name: 'http',\n setup: (build) => {\n // Intercept import paths starting with \"http:\" and \"https:\" so esbuild doesn't attempt to map them to a file system location.\n // Tag them with the \"http-url\" namespace to associate them with this plugin.\n build.onResolve({ filter: /^https?:\\/\\// }, (args) => ({\n path: args.path,\n namespace: 'http-url',\n }));\n\n // We also want to intercept all import paths inside downloaded files and resolve them against the original URL.\n // All of these files will be in the \"http-url\" namespace.\n // Make sure to keep the newly resolved URL in the \"http-url\" namespace so imports inside it will also be resolved as URLs recursively.\n build.onResolve({ filter: /.*/, namespace: 'http-url' }, (args) => ({\n path: new URL(args.path, args.importer).toString(),\n namespace: 'http-url',\n }));\n\n // When a URL is loaded, we want to actually download the content from the internet.\n // This has just enough logic to be able to handle the example import from unpkg.com but in reality this would probably need to be more complex.\n build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {\n const response = await fetch(args.path);\n return { contents: await response.text(), loader: 'jsx' };\n });\n },\n};\n"],
|
|
5
|
+
"mappings": ";AAKA,SAASA,OAAOC,kBAAiD;AAEjE,SAASC,oBAAoB;AAC7B,SAASC,iBAAiB;AAC1B,SAASC,WAAW;;AAsBpB,IAAIC;AACG,IAAMC,oBAAoB,OAAOC,YAAAA;AACtC,SAAOF,gBAAgBJ,WAAW;IAChCO,SAASD,QAAQE;EACnB,CAAA;AACF;AAKO,IAAMC,UAAN,MAAMA;EACXC,YAA6BC,UAA0B;SAA1BA,WAAAA;EAA2B;EAExD,MAAMC,OAAOC,QAAwC;AACnD,UAAM,EAAEC,kBAAkBC,iBAAiB,GAAGT,QAAAA,IAAY,KAAKK;AAE/D,UAAMK,eAAe,OAAOC,WAAAA;AAC1B,aAAO;QACLC,WAAWC,KAAKC,IAAG;QACnBC,YAAYC,OAAOC,KAAK,MAAMtB,aAAauB,OAAO,WAAWF,OAAOC,KAAKV,MAAAA,CAAAA,CAAAA;QACzE,GAAGI;MACL;IACF;AAEA,QAAI,KAAKN,SAASc,aAAa,WAAW;AACxCvB,gBAAUE,aAAa,6BAAA;;;;;;;;;AACvB,YAAMA;IACR;AAEA,UAAMsB,UAAUC,yBAAyBd,MAAAA;AAGzC,QAAI;AACF,YAAMI,SAAS,MAAMlB,MAAM;QACzB0B,UAAUnB,QAAQmB;QAClBG,YAAY;UAAC;UAAW;;QACxBC,UAAU;QACVC,OAAO;QACPC,aAAa;UAAC;;QACdnB,QAAQ;QACRoB,QAAQ;QACRC,SAAS;UACP;YACEC,MAAM;YACNC,OAAO,CAACpC,WAAAA;AACNA,cAAAA,OAAMqC,UAAU;gBAAEC,QAAQ;cAAoB,GAAG,CAAC,EAAEC,KAAI,MAAE;AACxD,uBAAO;kBAAEA;kBAAMC,UAAU;gBAAK;cAChC,CAAA;AAEAxC,cAAAA,OAAMqC,UAAU;gBAAEC,QAAQ;cAAW,GAAG,CA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AAMX,KAAKY,GAAAA,GAAO;AAChCE,YAAQX,KAAKU,KAAAA;EACf;AAEA,SAAOC;AACT;AAUA,IAAM3D,2BAA2B,CAACoD,SAAAA;AAEhC,QAAMnB,gBAAgBC,WAAWC,cAAciB,IAAAA;AAC/C,SAAOnB,cAAcM,IAAI,CAACE,YAAAA;AACxB,WAAO;MACLY,mBAAmBZ,QAAQ,CAAA;MAC3BrB,cAAcqB,QAAQ,CAAA,GAClBQ,KAAAA,EACDW,MAAM,GAAG,EAAC,EACV/C,MAAM,GAAA,EACN0B,IAAI,CAACQ,eAAeA,WAAWE,KAAI,CAAA;MACtCK,oBAAoBb,QAAQ,CAAA;MAC5BlB,kBAAkBkB,QAAQ,CAAA;MAC1Bc,QAAQd,QAAQ,CAAA;IAClB;EACF,CAAA;AACF;AAEA,IAAMd,aAAqB;EACzBpB,MAAM;EACNC,OAAO,CAACpC,WAAAA;AAGNA,IAAAA,OAAMqC,UAAU;MAAEC,QAAQ;IAAe,GAAG,CAACmD,UAAU;MACrDlD,MAAMkD,KAAKlD;MACXG,WAAW;IACb,EAAA;AAKA1C,IAAAA,OAAMqC,UAAU;MAAEC,QAAQ;MAAMI,WAAW;IAAW,GAAG,CAAC+C,UAAU;MAClElD,MAAM,IAAImD,IAAID,KAAKlD,MAAMkD,KAAKE,QAAQ,EAAEC,SAAQ;MAChDlD,WAAW;IACb,EAAA;AAIA1C,IAAAA,OAAM2C,OAAO;MAAEL,QAAQ;MAAMI,WAAW;IAAW,GAAG,OAAO+C,SAAAA;AAC3D,YAAMI,WAAW,MAAMC,MAAML,KAAKlD,IAAI;AACtC,aAAO;QAAEK,UAAU,MAAMiD,SAASnC,KAAI;QAAIb,QAAQ;MAAM;IAC1D,CAAA;EACF;AACF;",
|
|
6
|
+
"names": ["build", "initialize", "subtleCrypto", "invariant", "log", "initialized", "initializeBundler", "options", "wasmURL", "wasmUrl", "Bundler", "constructor", "_options", "bundle", "source", "sandboxedModules", "providedModules", "createResult", "result", "timestamp", "Date", "now", "sourceHash", "Buffer", "from", "digest", "platform", "imports", "analyzeSourceFileImports", "conditions", "metafile", "write", "entryPoints", "format", "plugins", "name", "setup", "onResolve", "filter", "path", "external", "split", "namespace", "onLoad", "contents", "loader", "module", "RegExp", "namedImports", "find", "entry", "moduleIdentifier", "join", "JSON", "stringify", "httpPlugin", "analyzeImports", "outputFiles", "text", "err", "error", "parsedImports", "allMatches", "IMPORT_REGEX", "Object", "values", "outputs", "map", "parsedImport", "capture", "NAMED_IMPORTS_REGEX", "lastIndex", "namedImportsMatch", "exec", "forEach", "importName", "push", "trim", "moduleUrl", "defaultImport", "code", "defaultImportName", "wildcardImportName", "quotes", "regex", "str", "match", "matches", "slice", "args", "URL", "importer", "toString", "response", "fetch"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HeaderCodec,
|
|
3
|
+
codec,
|
|
4
|
+
getUserFunctionUrlInMetadata,
|
|
5
|
+
matchServiceCredential,
|
|
6
|
+
publicKeyToDid,
|
|
7
|
+
setUserFunctionUrlInMetadata,
|
|
8
|
+
uploadWorkerFunction
|
|
9
|
+
} from "../chunk-DL5XQRQM.mjs";
|
|
10
|
+
export {
|
|
11
|
+
HeaderCodec,
|
|
12
|
+
codec,
|
|
13
|
+
getUserFunctionUrlInMetadata,
|
|
14
|
+
matchServiceCredential,
|
|
15
|
+
publicKeyToDid,
|
|
16
|
+
setUserFunctionUrlInMetadata,
|
|
17
|
+
uploadWorkerFunction
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Bundler,
|
|
3
|
+
initializeBundler
|
|
4
|
+
} from "./chunk-RHEZ5LUU.mjs";
|
|
5
|
+
import {
|
|
6
|
+
FunctionType,
|
|
7
|
+
ScriptAction,
|
|
8
|
+
ScriptType
|
|
9
|
+
} from "./chunk-O4LWBX6A.mjs";
|
|
10
|
+
import {
|
|
11
|
+
SCRIPT_PLUGIN,
|
|
12
|
+
meta_default
|
|
13
|
+
} from "./chunk-JBACGDOJ.mjs";
|
|
14
|
+
|
|
15
|
+
// packages/plugins/experimental/plugin-script/src/ScriptPlugin.tsx
|
|
16
|
+
import { Code } from "@phosphor-icons/react";
|
|
17
|
+
import wasmUrl from "esbuild-wasm/esbuild.wasm?url";
|
|
18
|
+
import React2 from "react";
|
|
19
|
+
import { parseIntentPlugin, resolvePlugin, NavigationAction } from "@dxos/app-framework";
|
|
20
|
+
import { create } from "@dxos/echo-schema";
|
|
21
|
+
import { parseClientPlugin } from "@dxos/plugin-client";
|
|
22
|
+
import { createExtension, isActionGroup } from "@dxos/plugin-graph";
|
|
23
|
+
import { TextType } from "@dxos/plugin-markdown/types";
|
|
24
|
+
import { SpaceAction } from "@dxos/plugin-space";
|
|
25
|
+
import { loadObjectReferences } from "@dxos/react-client/echo";
|
|
26
|
+
|
|
27
|
+
// packages/plugins/experimental/plugin-script/src/compiler/compiler.ts
|
|
28
|
+
import { createDefaultMapFromCDN, createSystem, createVirtualTypeScriptEnvironment } from "@typescript/vfs";
|
|
29
|
+
import ts from "typescript";
|
|
30
|
+
import { invariant } from "@dxos/invariant";
|
|
31
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/plugins/experimental/plugin-script/src/compiler/compiler.ts";
|
|
32
|
+
var defaultOptions = {
|
|
33
|
+
lib: [
|
|
34
|
+
"DOM",
|
|
35
|
+
"es2022"
|
|
36
|
+
],
|
|
37
|
+
target: ts.ScriptTarget.ES2022
|
|
38
|
+
};
|
|
39
|
+
var Compiler = class {
|
|
40
|
+
constructor(_options = defaultOptions) {
|
|
41
|
+
this._options = _options;
|
|
42
|
+
}
|
|
43
|
+
async initialize() {
|
|
44
|
+
if (this._env) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this._fsMap = await createDefaultMapFromCDN(this._options, "5.5.4", true, ts);
|
|
48
|
+
const system = createSystem(this._fsMap);
|
|
49
|
+
this._env = createVirtualTypeScriptEnvironment(system, [], ts, this._options);
|
|
50
|
+
}
|
|
51
|
+
get environment() {
|
|
52
|
+
invariant(this._env, "Compiler environment not initialized.", {
|
|
53
|
+
F: __dxlog_file,
|
|
54
|
+
L: 39,
|
|
55
|
+
S: this,
|
|
56
|
+
A: [
|
|
57
|
+
"this._env",
|
|
58
|
+
"'Compiler environment not initialized.'"
|
|
59
|
+
]
|
|
60
|
+
});
|
|
61
|
+
return this._env;
|
|
62
|
+
}
|
|
63
|
+
setFile(fileName, content) {
|
|
64
|
+
invariant(this._fsMap, "File system map not initialized.", {
|
|
65
|
+
F: __dxlog_file,
|
|
66
|
+
L: 44,
|
|
67
|
+
S: this,
|
|
68
|
+
A: [
|
|
69
|
+
"this._fsMap",
|
|
70
|
+
"'File system map not initialized.'"
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
this.environment.createFile(fileName, content);
|
|
74
|
+
}
|
|
75
|
+
compile(fileName) {
|
|
76
|
+
return this.environment.languageService.getEmitOutput(fileName);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// packages/plugins/experimental/plugin-script/src/components/index.ts
|
|
81
|
+
import React from "react";
|
|
82
|
+
var ScriptEditor = React.lazy(() => import("./ScriptEditor-5Y3EROG3.mjs"));
|
|
83
|
+
var TypescriptEditor = React.lazy(() => import("./TypescriptEditor-GPE72Y6I.mjs"));
|
|
84
|
+
|
|
85
|
+
// packages/plugins/experimental/plugin-script/src/translations.ts
|
|
86
|
+
var translations_default = [
|
|
87
|
+
{
|
|
88
|
+
"en-US": {
|
|
89
|
+
[SCRIPT_PLUGIN]: {
|
|
90
|
+
"plugin name": "Scripts",
|
|
91
|
+
"object title placeholder": "New script",
|
|
92
|
+
"create object label": "Create script",
|
|
93
|
+
"create stack section label": "Create script",
|
|
94
|
+
"binding placeholder": "Function name",
|
|
95
|
+
"function request placeholder": "Function request",
|
|
96
|
+
"deploy label": "Deploy",
|
|
97
|
+
"deployed label": "Current version of the script is deployed",
|
|
98
|
+
"upload failed label": "Upload failed",
|
|
99
|
+
"copy link label": "Copy function link",
|
|
100
|
+
"format label": "Format",
|
|
101
|
+
"toggle details label": "Toggle details panel"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// packages/plugins/experimental/plugin-script/src/ScriptPlugin.tsx
|
|
108
|
+
var ScriptPlugin = () => {
|
|
109
|
+
const compiler = new Compiler();
|
|
110
|
+
return {
|
|
111
|
+
meta: meta_default,
|
|
112
|
+
initialize: async () => {
|
|
113
|
+
await compiler.initialize();
|
|
114
|
+
compiler.setFile("/src/typings.d.ts", "declare module 'https://*';");
|
|
115
|
+
compiler.setFile("/src/runtime.ts", `
|
|
116
|
+
export const Filter: any = {};
|
|
117
|
+
export type FunctionHandler = ({ event, context }: { event: any; context: any }) => Promise<Response>;
|
|
118
|
+
export const functionHandler = (handler: FunctionHandler) => handler;
|
|
119
|
+
`);
|
|
120
|
+
await initializeBundler({
|
|
121
|
+
wasmUrl
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
provides: {
|
|
125
|
+
metadata: {
|
|
126
|
+
records: {
|
|
127
|
+
[ScriptType.typename]: {
|
|
128
|
+
placeholder: [
|
|
129
|
+
"object title placeholder",
|
|
130
|
+
{
|
|
131
|
+
ns: SCRIPT_PLUGIN
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
icon: (props) => /* @__PURE__ */ React2.createElement(Code, props),
|
|
135
|
+
iconSymbol: "ph--code--regular",
|
|
136
|
+
// TODO(wittjosiah): Move out of metadata.
|
|
137
|
+
loadReferences: (script) => loadObjectReferences(script, (script2) => [
|
|
138
|
+
script2.source
|
|
139
|
+
])
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
translations: translations_default,
|
|
144
|
+
echo: {
|
|
145
|
+
schema: [
|
|
146
|
+
ScriptType,
|
|
147
|
+
FunctionType
|
|
148
|
+
]
|
|
149
|
+
},
|
|
150
|
+
graph: {
|
|
151
|
+
builder: (plugins) => {
|
|
152
|
+
const client = resolvePlugin(plugins, parseClientPlugin)?.provides.client;
|
|
153
|
+
const dispatch = resolvePlugin(plugins, parseIntentPlugin)?.provides.intent.dispatch;
|
|
154
|
+
if (!client || !dispatch) {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
return createExtension({
|
|
158
|
+
id: ScriptAction.CREATE,
|
|
159
|
+
filter: (node) => isActionGroup(node) && node.id.startsWith(SpaceAction.ADD_OBJECT),
|
|
160
|
+
actions: ({ node }) => {
|
|
161
|
+
const id = node.id.split("/").at(-1);
|
|
162
|
+
const [spaceId, objectId] = id?.split(":") ?? [];
|
|
163
|
+
const space = client.spaces.get().find((space2) => space2.id === spaceId);
|
|
164
|
+
const object = objectId && space?.db.getObjectById(objectId);
|
|
165
|
+
const target = objectId ? object : space;
|
|
166
|
+
if (!target) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
return [
|
|
170
|
+
{
|
|
171
|
+
id: `${SCRIPT_PLUGIN}/create/${node.id}`,
|
|
172
|
+
data: async () => {
|
|
173
|
+
await dispatch([
|
|
174
|
+
{
|
|
175
|
+
plugin: SCRIPT_PLUGIN,
|
|
176
|
+
action: ScriptAction.CREATE
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
action: SpaceAction.ADD_OBJECT,
|
|
180
|
+
data: {
|
|
181
|
+
target
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
action: NavigationAction.OPEN
|
|
186
|
+
}
|
|
187
|
+
]);
|
|
188
|
+
},
|
|
189
|
+
properties: {
|
|
190
|
+
label: [
|
|
191
|
+
"create object label",
|
|
192
|
+
{
|
|
193
|
+
ns: SCRIPT_PLUGIN
|
|
194
|
+
}
|
|
195
|
+
],
|
|
196
|
+
icon: (props) => /* @__PURE__ */ React2.createElement(Code, props),
|
|
197
|
+
iconSymbol: "ph--code--regular",
|
|
198
|
+
testId: "scriptPlugin.createObject"
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
];
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
stack: {
|
|
207
|
+
creators: [
|
|
208
|
+
{
|
|
209
|
+
id: "create-stack-section-script",
|
|
210
|
+
testId: "scriptPlugin.createSection",
|
|
211
|
+
type: [
|
|
212
|
+
"plugin name",
|
|
213
|
+
{
|
|
214
|
+
ns: SCRIPT_PLUGIN
|
|
215
|
+
}
|
|
216
|
+
],
|
|
217
|
+
label: [
|
|
218
|
+
"create stack section label",
|
|
219
|
+
{
|
|
220
|
+
ns: SCRIPT_PLUGIN
|
|
221
|
+
}
|
|
222
|
+
],
|
|
223
|
+
icon: (props) => /* @__PURE__ */ React2.createElement(Code, props),
|
|
224
|
+
intent: {
|
|
225
|
+
plugin: SCRIPT_PLUGIN,
|
|
226
|
+
action: ScriptAction.CREATE
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
]
|
|
230
|
+
},
|
|
231
|
+
surface: {
|
|
232
|
+
component: ({ data, role }) => {
|
|
233
|
+
if (role && ![
|
|
234
|
+
"article",
|
|
235
|
+
"section"
|
|
236
|
+
].includes(role)) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
if (data.object instanceof ScriptType) {
|
|
240
|
+
return /* @__PURE__ */ React2.createElement(ScriptEditor, {
|
|
241
|
+
env: compiler.environment,
|
|
242
|
+
script: data.object,
|
|
243
|
+
role
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
intent: {
|
|
250
|
+
resolver: (intent) => {
|
|
251
|
+
switch (intent.action) {
|
|
252
|
+
case ScriptAction.CREATE: {
|
|
253
|
+
return {
|
|
254
|
+
data: create(ScriptType, {
|
|
255
|
+
source: create(TextType, {
|
|
256
|
+
content: example
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
var example = [
|
|
268
|
+
"export default async ({ ",
|
|
269
|
+
" event: { data: { request } },",
|
|
270
|
+
" context: { space, ai } ",
|
|
271
|
+
"}: any) => {",
|
|
272
|
+
" return new Response('Hello DXOS!');",
|
|
273
|
+
"};",
|
|
274
|
+
""
|
|
275
|
+
].join("\n");
|
|
276
|
+
|
|
277
|
+
// packages/plugins/experimental/plugin-script/src/index.ts
|
|
278
|
+
var src_default = ScriptPlugin;
|
|
279
|
+
export {
|
|
280
|
+
Bundler,
|
|
281
|
+
Compiler,
|
|
282
|
+
FunctionType,
|
|
283
|
+
ScriptAction,
|
|
284
|
+
ScriptEditor,
|
|
285
|
+
ScriptPlugin,
|
|
286
|
+
ScriptType,
|
|
287
|
+
TypescriptEditor,
|
|
288
|
+
src_default as default,
|
|
289
|
+
initializeBundler
|
|
290
|
+
};
|
|
291
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/ScriptPlugin.tsx", "../../../src/compiler/compiler.ts", "../../../src/components/index.ts", "../../../src/translations.ts", "../../../src/index.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { Code, type IconProps } from '@phosphor-icons/react';\n// @ts-ignore\nimport wasmUrl from 'esbuild-wasm/esbuild.wasm?url';\nimport React from 'react';\n\nimport { parseIntentPlugin, type PluginDefinition, resolvePlugin, NavigationAction } from '@dxos/app-framework';\nimport { create } from '@dxos/echo-schema';\nimport { parseClientPlugin } from '@dxos/plugin-client';\nimport { type ActionGroup, createExtension, isActionGroup } from '@dxos/plugin-graph';\nimport { TextType } from '@dxos/plugin-markdown/types';\nimport { SpaceAction } from '@dxos/plugin-space';\nimport { loadObjectReferences } from '@dxos/react-client/echo';\n\nimport { initializeBundler } from './bundler';\nimport { Compiler } from './compiler';\nimport { ScriptEditor } from './components';\nimport meta, { SCRIPT_PLUGIN } from './meta';\nimport translations from './translations';\nimport { FunctionType, ScriptType } from './types';\nimport { ScriptAction, type ScriptPluginProvides } from './types';\n\nexport const ScriptPlugin = (): PluginDefinition<ScriptPluginProvides> => {\n const compiler = new Compiler();\n\n return {\n meta,\n initialize: async () => {\n await compiler.initialize();\n // TODO(wittjosiah): Fetch types for https modules.\n compiler.setFile('/src/typings.d.ts', \"declare module 'https://*';\");\n // TODO(wittjosiah): Proper function handler types.\n // TODO(wittjosiah): Remove.\n compiler.setFile(\n '/src/runtime.ts',\n `\n export const Filter: any = {};\n export type FunctionHandler = ({ event, context }: { event: any; context: any }) => Promise<Response>;\n export const functionHandler = (handler: FunctionHandler) => handler;\n `,\n );\n await initializeBundler({ wasmUrl });\n },\n provides: {\n metadata: {\n records: {\n [ScriptType.typename]: {\n placeholder: ['object title placeholder', { ns: SCRIPT_PLUGIN }],\n icon: (props: IconProps) => <Code {...props} />,\n iconSymbol: 'ph--code--regular',\n // TODO(wittjosiah): Move out of metadata.\n loadReferences: (script: ScriptType) => loadObjectReferences(script, (script) => [script.source]),\n },\n },\n },\n translations,\n echo: {\n schema: [ScriptType, FunctionType],\n },\n graph: {\n builder: (plugins) => {\n const client = resolvePlugin(plugins, parseClientPlugin)?.provides.client;\n const dispatch = resolvePlugin(plugins, parseIntentPlugin)?.provides.intent.dispatch;\n if (!client || !dispatch) {\n return [];\n }\n\n return createExtension({\n id: ScriptAction.CREATE,\n filter: (node): node is ActionGroup => isActionGroup(node) && node.id.startsWith(SpaceAction.ADD_OBJECT),\n actions: ({ node }) => {\n const id = node.id.split('/').at(-1);\n const [spaceId, objectId] = id?.split(':') ?? [];\n const space = client.spaces.get().find((space) => space.id === spaceId);\n const object = objectId && space?.db.getObjectById(objectId);\n const target = objectId ? object : space;\n if (!target) {\n return;\n }\n\n return [\n {\n id: `${SCRIPT_PLUGIN}/create/${node.id}`,\n data: async () => {\n await dispatch([\n { plugin: SCRIPT_PLUGIN, action: ScriptAction.CREATE },\n { action: SpaceAction.ADD_OBJECT, data: { target } },\n { action: NavigationAction.OPEN },\n ]);\n },\n properties: {\n label: ['create object label', { ns: SCRIPT_PLUGIN }],\n icon: (props: IconProps) => <Code {...props} />,\n iconSymbol: 'ph--code--regular',\n testId: 'scriptPlugin.createObject',\n },\n },\n ];\n },\n });\n },\n },\n stack: {\n creators: [\n {\n id: 'create-stack-section-script',\n testId: 'scriptPlugin.createSection',\n type: ['plugin name', { ns: SCRIPT_PLUGIN }],\n label: ['create stack section label', { ns: SCRIPT_PLUGIN }],\n icon: (props: any) => <Code {...props} />,\n intent: {\n plugin: SCRIPT_PLUGIN,\n action: ScriptAction.CREATE,\n },\n },\n ],\n },\n surface: {\n component: ({ data, role }) => {\n if (role && !['article', 'section'].includes(role)) {\n return null;\n }\n\n if (data.object instanceof ScriptType) {\n return <ScriptEditor env={compiler.environment} script={data.object} role={role} />;\n }\n\n return null;\n },\n },\n intent: {\n resolver: (intent) => {\n switch (intent.action) {\n case ScriptAction.CREATE: {\n return { data: create(ScriptType, { source: create(TextType, { content: example }) }) };\n }\n }\n },\n },\n },\n };\n};\n\n// TODO(burdon): Import.\nconst example = [\n 'export default async ({ ',\n ' event: { data: { request } },',\n ' context: { space, ai } ',\n '}: any) => {',\n \" return new Response('Hello DXOS!');\",\n '};',\n '',\n].join('\\n');\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport {\n createDefaultMapFromCDN,\n createSystem,\n createVirtualTypeScriptEnvironment,\n type VirtualTypeScriptEnvironment,\n} from '@typescript/vfs';\nimport ts from 'typescript';\n\nimport { invariant } from '@dxos/invariant';\n\nconst defaultOptions: ts.CompilerOptions = {\n lib: ['DOM', 'es2022'],\n target: ts.ScriptTarget.ES2022,\n};\n\nexport class Compiler {\n private _env: VirtualTypeScriptEnvironment | undefined;\n private _fsMap: Map<string, string> | undefined;\n\n constructor(private readonly _options: ts.CompilerOptions = defaultOptions) {}\n\n async initialize() {\n if (this._env) {\n return;\n }\n\n // TODO(wittjosiah): Figure out how to get workers working in plugin packages.\n // https://github.com/val-town/codemirror-ts?tab=readme-ov-file#setup-worker\n this._fsMap = await createDefaultMapFromCDN(this._options, '5.5.4', true, ts);\n const system = createSystem(this._fsMap);\n this._env = createVirtualTypeScriptEnvironment(system, [], ts, this._options);\n }\n\n get environment() {\n invariant(this._env, 'Compiler environment not initialized.');\n return this._env;\n }\n\n setFile(fileName: string, content: string) {\n invariant(this._fsMap, 'File system map not initialized.');\n this.environment.createFile(fileName, content);\n }\n\n compile(fileName: string) {\n return this.environment.languageService.getEmitOutput(fileName);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport React from 'react';\n\nexport const ScriptEditor = React.lazy(() => import('./ScriptEditor'));\nexport const TypescriptEditor = React.lazy(() => import('./TypescriptEditor'));\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { SCRIPT_PLUGIN } from './meta';\n\nexport default [\n {\n 'en-US': {\n [SCRIPT_PLUGIN]: {\n 'plugin name': 'Scripts',\n 'object title placeholder': 'New script',\n 'create object label': 'Create script',\n 'create stack section label': 'Create script',\n 'binding placeholder': 'Function name',\n 'function request placeholder': 'Function request',\n 'deploy label': 'Deploy',\n 'deployed label': 'Current version of the script is deployed',\n 'upload failed label': 'Upload failed',\n 'copy link label': 'Copy function link',\n 'format label': 'Format',\n 'toggle details label': 'Toggle details panel',\n },\n },\n },\n];\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { ScriptPlugin } from './ScriptPlugin';\n\nexport default ScriptPlugin;\n\nexport * from './bundler';\nexport * from './compiler';\nexport * from './components';\nexport * from './types';\n\nexport * from './ScriptPlugin';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;AAIA,SAASA,YAA4B;AAErC,OAAOC,aAAa;AACpB,OAAOC,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,cAAAA;cAAaC,KAAK5E,SAAS6E;cAAaxD,QAAQuC,KAAKJ;cAAQiB;;UACvE;AAEA,iBAAO;QACT;MACF;MACArC,QAAQ;QACN0C,UAAU,CAAC1C,WAAAA;AACT,kBAAQA,OAAO0B,QAAM;YACnB,KAAKvB,aAAaC,QAAQ;AACxB,qBAAO;gBAAEoB,MAAMmB,OAAOrE,YAAY;kBAAEa,QAAQwD,OAAOC,UAAU;oBAAEC,SAASC;kBAAQ,CAAA;gBAAG,CAAA;cAAG;YACxF;UACF;QACF;MACF;IACF;EACF;AACF;AAGA,IAAMA,UAAU;EACd;EACA;EACA;EACA;EACA;EACA;EACA;EACAC,KAAK,IAAA;;;AIrJP,IAAA,cAAeC;",
|
|
6
|
+
"names": ["Code", "wasmUrl", "React", "parseIntentPlugin", "resolvePlugin", "NavigationAction", "create", "parseClientPlugin", "createExtension", "isActionGroup", "TextType", "SpaceAction", "loadObjectReferences", "createDefaultMapFromCDN", "createSystem", "createVirtualTypeScriptEnvironment", "ts", "invariant", "defaultOptions", "lib", "target", "ScriptTarget", "ES2022", "Compiler", "constructor", "_options", "initialize", "_env", "_fsMap", "system", "environment", "setFile", "fileName", "content", "createFile", "compile", "languageService", "getEmitOutput", "React", "ScriptEditor", "React", "lazy", "TypescriptEditor", "SCRIPT_PLUGIN", "ScriptPlugin", "compiler", "Compiler", "meta", "initialize", "setFile", "initializeBundler", "wasmUrl", "provides", "metadata", "records", "ScriptType", "typename", "placeholder", "ns", "SCRIPT_PLUGIN", "icon", "props", "React", "Code", "iconSymbol", "loadReferences", "script", "loadObjectReferences", "source", "translations", "echo", "schema", "FunctionType", "graph", "builder", "plugins", "client", "resolvePlugin", "parseClientPlugin", "dispatch", "parseIntentPlugin", "intent", "createExtension", "id", "ScriptAction", "CREATE", "filter", "node", "isActionGroup", "startsWith", "SpaceAction", "ADD_OBJECT", "actions", "split", "at", "spaceId", "objectId", "space", "spaces", "get", "find", "object", "db", "getObjectById", "target", "data", "plugin", "action", "NavigationAction", "OPEN", "properties", "label", "testId", "stack", "creators", "type", "surface", "component", "role", "includes", "ScriptEditor", "env", "environment", "resolver", "create", "TextType", "content", "example", "join", "ScriptPlugin"]
|
|
7
|
+
}
|