MCPcopy Index your code
hub / github.com/bytecodealliance/ComponentizeJS / setupComponent

Function setupComponent

test/util.js:28–77  ·  view source on GitHub ↗
(opts)

Source from the content-addressed store, hash-verified

26}
27
28export async function setupComponent(opts) {
29 const componentizeSrc = opts?.componentize?.src;
30 const componentizeOpts = opts?.componentize?.opts;
31 const transpileOpts = opts?.transpile?.opts;
32
33 let component;
34 if (componentizeSrc) {
35 const srcBuild = await componentize(componentizeSrc, componentizeOpts);
36 component = srcBuild.component;
37 } else if (!componentizeSrc && componentizeOpts) {
38 const optsBuild = await componentize(componentizeOpts);
39 component = optsBuild.component;
40 } else {
41 throw new Error('no componentize options or src provided');
42 }
43
44 const outputDir = join('./test/output', 'wasi-test');
45 await mkdir(outputDir, { recursive: true });
46
47 await writeFile(join(outputDir, 'wasi.component.wasm'), component);
48
49 const { files } = await transpile(component, transpileOpts);
50
51 const wasiDir = join(outputDir, 'wasi');
52 const interfacesDir = join(wasiDir, 'interfaces');
53 await mkdir(interfacesDir, { recursive: true });
54
55 for (const file of Object.keys(files)) {
56 await writeFile(join(wasiDir, file), files[file]);
57 }
58
59 const componentJsPath = join(wasiDir, 'component.js');
60
61 // NOTE: On Windows, vitest has stated suddenly hanging when processing code with an
62 // `await import(...)` in it, when the `...` is a bare identifier.
63 //
64 // This can be worked around in many ways:
65 // - doing a trivial tranformation on the identifier/argument
66 // - returning a more traditional promise
67 // - separating await and import() calls
68 //
69 // Here we choose to return the promise more traditionally
70 return import(componentJsPath)
71 .then(instance => {
72 return {
73 instance,
74 outputDir,
75 };
76 });
77}

Callers 2

api.jsFile · 0.90
wasi.jsFile · 0.90

Calls 1

componentizeFunction · 0.85

Tested by

no test coverage detected