@dxos/plugin-script 0.7.5-labs.8a82073 → 0.7.5-labs.c0e040f

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 (95) hide show
  1. package/dist/lib/browser/{ScriptContainer-QI4NQT7V.mjs → ScriptContainer-SPKDYRXU.mjs} +8 -8
  2. package/dist/lib/browser/ScriptContainer-SPKDYRXU.mjs.map +7 -0
  3. package/dist/lib/browser/{artifact-3UZZBCZ2.mjs → artifact-YCIVHFSS.mjs} +3 -3
  4. package/dist/lib/browser/{artifact-3UZZBCZ2.mjs.map → artifact-YCIVHFSS.mjs.map} +3 -3
  5. package/dist/lib/browser/{chunk-VIX22OMB.mjs → chunk-GB5OS4JQ.mjs} +13 -4
  6. package/dist/lib/browser/chunk-GB5OS4JQ.mjs.map +7 -0
  7. package/dist/lib/browser/{chunk-F22QPQ6G.mjs → chunk-XTD6J6PC.mjs} +2 -2
  8. package/dist/lib/browser/{compiler-K3FNRDDN.mjs → compiler-YY2XSKHQ.mjs} +2 -4
  9. package/dist/lib/browser/compiler-YY2XSKHQ.mjs.map +7 -0
  10. package/dist/lib/browser/index.mjs +5 -11
  11. package/dist/lib/browser/index.mjs.map +1 -1
  12. package/dist/lib/browser/{intent-resolver-CMKQTLBG.mjs → intent-resolver-IKIQWAL4.mjs} +2 -2
  13. package/dist/lib/browser/meta.json +1 -1
  14. package/dist/lib/browser/{react-surface-YR4YO746.mjs → react-surface-ZP2FXWYA.mjs} +2 -2
  15. package/dist/lib/node/{ScriptContainer-FSKWJR6Z.cjs → ScriptContainer-M3PIEFIA.cjs} +13 -11
  16. package/dist/lib/node/ScriptContainer-M3PIEFIA.cjs.map +7 -0
  17. package/dist/lib/node/{artifact-A2QXHFGS.cjs → artifact-IPGGY2Z2.cjs} +6 -6
  18. package/dist/lib/node/{artifact-A2QXHFGS.cjs.map → artifact-IPGGY2Z2.cjs.map} +2 -2
  19. package/dist/lib/node/{chunk-PKT5DD3O.cjs → chunk-FGJJPY2L.cjs} +12 -5
  20. package/dist/lib/node/chunk-FGJJPY2L.cjs.map +7 -0
  21. package/dist/lib/node/{chunk-JSANQCTA.cjs → chunk-HEDZVP6A.cjs} +5 -5
  22. package/dist/lib/node/{compiler-P2HQB22H.cjs → compiler-UIBLKJKG.cjs} +6 -6
  23. package/dist/lib/node/compiler-UIBLKJKG.cjs.map +7 -0
  24. package/dist/lib/node/index.cjs +9 -14
  25. package/dist/lib/node/index.cjs.map +1 -1
  26. package/dist/lib/node/{intent-resolver-V5ASU5DW.cjs → intent-resolver-BLLSSUIM.cjs} +6 -6
  27. package/dist/lib/node/meta.json +1 -1
  28. package/dist/lib/node/{react-surface-MFTICFI5.cjs → react-surface-BI5NMAJM.cjs} +9 -9
  29. package/dist/lib/node-esm/{ScriptContainer-MNETNTNQ.mjs → ScriptContainer-6GCPTLOV.mjs} +8 -8
  30. package/dist/lib/node-esm/ScriptContainer-6GCPTLOV.mjs.map +7 -0
  31. package/dist/lib/node-esm/{artifact-RFZQ2AJJ.mjs → artifact-7H4HGX5W.mjs} +3 -3
  32. package/dist/lib/node-esm/{artifact-RFZQ2AJJ.mjs.map → artifact-7H4HGX5W.mjs.map} +3 -3
  33. package/dist/lib/node-esm/{chunk-FGO5DVO2.mjs → chunk-DWVYAASV.mjs} +2 -2
  34. package/dist/lib/node-esm/{chunk-NBAL5DXN.mjs → chunk-RFVLVTYI.mjs} +13 -4
  35. package/dist/lib/node-esm/chunk-RFVLVTYI.mjs.map +7 -0
  36. package/dist/lib/node-esm/{compiler-PDN46H4Y.mjs → compiler-XTXFGZJM.mjs} +2 -4
  37. package/dist/lib/node-esm/compiler-XTXFGZJM.mjs.map +7 -0
  38. package/dist/lib/node-esm/index.mjs +5 -11
  39. package/dist/lib/node-esm/index.mjs.map +1 -1
  40. package/dist/lib/node-esm/{intent-resolver-75BHFRUK.mjs → intent-resolver-QFIYWOWQ.mjs} +2 -2
  41. package/dist/lib/node-esm/meta.json +1 -1
  42. package/dist/lib/node-esm/{react-surface-NAK6LMCY.mjs → react-surface-N4Z5HGAC.mjs} +2 -2
  43. package/dist/types/src/capabilities/artifact.d.ts.map +1 -1
  44. package/dist/types/src/components/FrameContainer/FrameContainer.d.ts +2 -2
  45. package/dist/types/src/components/FrameContainer/FrameContainer.d.ts.map +1 -1
  46. package/dist/types/src/hooks/deploy.d.ts.map +1 -1
  47. package/dist/types/src/index.d.ts +0 -1
  48. package/dist/types/src/index.d.ts.map +1 -1
  49. package/dist/types/src/templates/discord.d.ts +3 -0
  50. package/dist/types/src/templates/discord.d.ts.map +1 -0
  51. package/dist/types/src/templates/forex-effect.d.ts.map +1 -1
  52. package/dist/types/src/templates/index.d.ts.map +1 -1
  53. package/package.json +41 -44
  54. package/src/capabilities/artifact.ts +2 -2
  55. package/src/capabilities/compiler.ts +1 -1
  56. package/src/components/FrameContainer/FrameContainer.tsx +4 -3
  57. package/src/components/TypescriptEditor/TypescriptEditor.tsx +2 -2
  58. package/src/hooks/deploy.ts +3 -3
  59. package/src/index.ts +0 -1
  60. package/src/templates/discord.ts +86 -0
  61. package/src/templates/forex-effect.ts +16 -14
  62. package/src/templates/index.ts +10 -2
  63. package/dist/lib/browser/ScriptContainer-QI4NQT7V.mjs.map +0 -7
  64. package/dist/lib/browser/chunk-SDHKTS6H.mjs +0 -237
  65. package/dist/lib/browser/chunk-SDHKTS6H.mjs.map +0 -7
  66. package/dist/lib/browser/chunk-VIX22OMB.mjs.map +0 -7
  67. package/dist/lib/browser/compiler-K3FNRDDN.mjs.map +0 -7
  68. package/dist/lib/node/ScriptContainer-FSKWJR6Z.cjs.map +0 -7
  69. package/dist/lib/node/chunk-PKT5DD3O.cjs.map +0 -7
  70. package/dist/lib/node/chunk-T4TXCRN5.cjs +0 -260
  71. package/dist/lib/node/chunk-T4TXCRN5.cjs.map +0 -7
  72. package/dist/lib/node/compiler-P2HQB22H.cjs.map +0 -7
  73. package/dist/lib/node-esm/ScriptContainer-MNETNTNQ.mjs.map +0 -7
  74. package/dist/lib/node-esm/chunk-G76M6O46.mjs +0 -239
  75. package/dist/lib/node-esm/chunk-G76M6O46.mjs.map +0 -7
  76. package/dist/lib/node-esm/chunk-NBAL5DXN.mjs.map +0 -7
  77. package/dist/lib/node-esm/compiler-PDN46H4Y.mjs.map +0 -7
  78. package/dist/types/src/bundler/bundler.d.ts +0 -39
  79. package/dist/types/src/bundler/bundler.d.ts.map +0 -1
  80. package/dist/types/src/bundler/bundler.test.d.ts +0 -2
  81. package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
  82. package/dist/types/src/bundler/index.d.ts +0 -2
  83. package/dist/types/src/bundler/index.d.ts.map +0 -1
  84. package/src/bundler/bundler.test.ts +0 -55
  85. package/src/bundler/bundler.ts +0 -250
  86. package/src/bundler/index.ts +0 -5
  87. /package/dist/lib/browser/{chunk-F22QPQ6G.mjs.map → chunk-XTD6J6PC.mjs.map} +0 -0
  88. /package/dist/lib/browser/{intent-resolver-CMKQTLBG.mjs.map → intent-resolver-IKIQWAL4.mjs.map} +0 -0
  89. /package/dist/lib/browser/{react-surface-YR4YO746.mjs.map → react-surface-ZP2FXWYA.mjs.map} +0 -0
  90. /package/dist/lib/node/{chunk-JSANQCTA.cjs.map → chunk-HEDZVP6A.cjs.map} +0 -0
  91. /package/dist/lib/node/{intent-resolver-V5ASU5DW.cjs.map → intent-resolver-BLLSSUIM.cjs.map} +0 -0
  92. /package/dist/lib/node/{react-surface-MFTICFI5.cjs.map → react-surface-BI5NMAJM.cjs.map} +0 -0
  93. /package/dist/lib/node-esm/{chunk-FGO5DVO2.mjs.map → chunk-DWVYAASV.mjs.map} +0 -0
  94. /package/dist/lib/node-esm/{intent-resolver-75BHFRUK.mjs.map → intent-resolver-QFIYWOWQ.mjs.map} +0 -0
  95. /package/dist/lib/node-esm/{react-surface-NAK6LMCY.mjs.map → react-surface-N4Z5HGAC.mjs.map} +0 -0
@@ -1,250 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { type BuildOptions } from 'esbuild';
6
- import { build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';
7
-
8
- import { subtleCrypto } from '@dxos/crypto';
9
- import { invariant } from '@dxos/invariant';
10
- import { log } from '@dxos/log';
11
-
12
- export type Import = {
13
- moduleUrl: string;
14
- defaultImport: boolean;
15
- namedImports: string[];
16
- };
17
-
18
- export type BundlerResult = {
19
- timestamp: number;
20
- sourceHash: Buffer;
21
- imports?: Import[];
22
- bundle?: string;
23
- error?: any;
24
- };
25
-
26
- export type BundlerOptions = {
27
- platform: BuildOptions['platform'];
28
- sandboxedModules: string[];
29
- remoteModules: Record<string, string>;
30
- };
31
-
32
- let initialized: Promise<void>;
33
- export const initializeBundler = async (options: { wasmUrl: string }) => {
34
- await (initialized ??= initialize({
35
- wasmURL: options.wasmUrl,
36
- }));
37
- };
38
-
39
- /**
40
- * ESBuild bundler.
41
- */
42
- export class Bundler {
43
- constructor(private readonly _options: BundlerOptions) {}
44
-
45
- async bundle(source: string): Promise<BundlerResult> {
46
- const { sandboxedModules: providedModules, ...options } = this._options;
47
-
48
- const createResult = async (result?: Partial<BundlerResult>) => {
49
- return {
50
- timestamp: Date.now(),
51
- sourceHash: Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))),
52
- ...result,
53
- };
54
- };
55
-
56
- if (this._options.platform === 'browser') {
57
- invariant(initialized, 'Compiler not initialized.');
58
- await initialized;
59
- }
60
-
61
- const imports = analyzeSourceFileImports(source);
62
-
63
- // https://esbuild.github.io/api/#build
64
- try {
65
- const result = await build({
66
- platform: options.platform,
67
- conditions: ['workerd', 'browser'],
68
- metafile: true,
69
- write: false,
70
- entryPoints: ['memory:main.tsx'],
71
- bundle: true,
72
- format: 'esm',
73
- plugins: [
74
- {
75
- name: 'memory',
76
- setup: (build) => {
77
- build.onResolve({ filter: /^\.\/runtime\.js$/ }, ({ path }) => {
78
- return { path, external: true };
79
- });
80
-
81
- build.onResolve({ filter: /^dxos:functions$/ }, ({ path }) => {
82
- return { path: './runtime.js', external: true };
83
- });
84
-
85
- build.onResolve({ filter: /^memory:/ }, ({ path }) => {
86
- return { path: path.split(':')[1], namespace: 'memory' };
87
- });
88
-
89
- build.onLoad({ filter: /.*/, namespace: 'memory' }, ({ path }) => {
90
- if (path === 'main.tsx') {
91
- return {
92
- contents: source,
93
- loader: 'tsx',
94
- };
95
- }
96
- });
97
-
98
- for (const module of providedModules) {
99
- build.onResolve({ filter: new RegExp(`^${module}$`) }, ({ path }) => {
100
- return { path, namespace: 'injected-module' };
101
- });
102
- }
103
-
104
- build.onLoad({ filter: /.*/, namespace: 'injected-module' }, ({ path }) => {
105
- const namedImports = imports.find((entry) => entry.moduleIdentifier === path)?.namedImports ?? [];
106
- return {
107
- contents: `
108
- const { ${namedImports.join(',')} } = window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}];
109
- export { ${namedImports.join(',')} };
110
- export default window.__DXOS_SANDBOX_MODULES__[${JSON.stringify(path)}].default;
111
- `,
112
- loader: 'tsx',
113
- };
114
- });
115
- },
116
- },
117
- httpPlugin,
118
- ],
119
- });
120
-
121
- log('compile complete', result.metafile);
122
-
123
- return await createResult({
124
- imports: this.analyzeImports(result),
125
- bundle: result.outputFiles![0].text,
126
- });
127
- } catch (err) {
128
- return await createResult({ error: err });
129
- }
130
- }
131
-
132
- // TODO(dmaretskyi): In the future we can replace the compiler with SWC with plugins running in WASM.
133
- analyzeImports(result: BuildResult): Import[] {
134
- invariant(result.outputFiles);
135
-
136
- // TODO(dmaretskyi): Support import aliases and wildcard imports.
137
- const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
138
- return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {
139
- const namedImports: string[] = [];
140
-
141
- const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
142
- if (parsedImport?.[2]) {
143
- NAMED_IMPORTS_REGEX.lastIndex = 0;
144
- const namedImportsMatch = NAMED_IMPORTS_REGEX.exec(parsedImport[2]);
145
- if (namedImportsMatch) {
146
- namedImportsMatch[1].split(',').forEach((importName) => {
147
- namedImports.push(importName.trim());
148
- });
149
- }
150
- }
151
-
152
- return {
153
- moduleUrl: entry.path,
154
- defaultImport: !!parsedImport?.[1],
155
- namedImports,
156
- };
157
- });
158
- }
159
-
160
- analyzeSourceFileImports(code: string) {
161
- // TODO(dmaretskyi): Support import aliases and wildcard imports.
162
- const parsedImports = allMatches(IMPORT_REGEX, code);
163
- return parsedImports.map((capture) => {
164
- return {
165
- defaultImportName: capture[1],
166
- namedImports: capture[2]?.split(',').map((importName) => importName.trim()),
167
- wildcardImportName: capture[3],
168
- moduleIdentifier: capture[4],
169
- quotes: capture[5],
170
- };
171
- });
172
- }
173
- }
174
-
175
- // https://regex101.com/r/FEN5ks/1
176
- // https://stackoverflow.com/a/73265022
177
- // $1 = default import name (can be non-existent)
178
- // $2 = destructured exports (can be non-existent)
179
- // $3 = wildcard import name (can be non-existent)
180
- // $4 = module identifier
181
- // $5 = quotes used (either ' or ")
182
- const IMPORT_REGEX =
183
- /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;
184
-
185
- const NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
186
-
187
- const allMatches = (regex: RegExp, str: string) => {
188
- regex.lastIndex = 0;
189
-
190
- let match;
191
- const matches = [];
192
- while ((match = regex.exec(str))) {
193
- matches.push(match);
194
- }
195
-
196
- return matches;
197
- };
198
-
199
- type ParsedImport = {
200
- defaultImportName?: string;
201
- namedImports: string[];
202
- wildcardImportName?: string;
203
- moduleIdentifier: string;
204
- quotes: string;
205
- };
206
-
207
- const analyzeSourceFileImports = (code: string): ParsedImport[] => {
208
- // TODO(dmaretskyi): Support import aliases and wildcard imports.
209
- const parsedImports = allMatches(IMPORT_REGEX, code);
210
- return parsedImports.map((capture) => {
211
- return {
212
- defaultImportName: capture[1],
213
- namedImports: capture[2]
214
- ?.trim()
215
- .slice(1, -1)
216
- .split(',')
217
- .map((importName) => importName.trim()),
218
- wildcardImportName: capture[3],
219
- moduleIdentifier: capture[4],
220
- quotes: capture[5],
221
- };
222
- });
223
- };
224
-
225
- const httpPlugin: Plugin = {
226
- name: 'http',
227
- setup: (build) => {
228
- // Intercept import paths starting with "http:" and "https:" so esbuild doesn't attempt to map them to a file system location.
229
- // Tag them with the "http-url" namespace to associate them with this plugin.
230
- build.onResolve({ filter: /^https?:\/\// }, (args) => ({
231
- path: args.path,
232
- namespace: 'http-url',
233
- }));
234
-
235
- // We also want to intercept all import paths inside downloaded files and resolve them against the original URL.
236
- // All of these files will be in the "http-url" namespace.
237
- // Make sure to keep the newly resolved URL in the "http-url" namespace so imports inside it will also be resolved as URLs recursively.
238
- build.onResolve({ filter: /.*/, namespace: 'http-url' }, (args) => ({
239
- path: new URL(args.path, args.importer).toString(),
240
- namespace: 'http-url',
241
- }));
242
-
243
- // When a URL is loaded, we want to actually download the content from the internet.
244
- // 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.
245
- build.onLoad({ filter: /.*/, namespace: 'http-url' }, async (args) => {
246
- const response = await fetch(args.path);
247
- return { contents: await response.text(), loader: 'jsx' };
248
- });
249
- },
250
- };
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- export * from './bundler';