Skip to content

Instantly share code, notes, and snippets.

@himself65
Created July 29, 2023 16:31
Show Gist options
  • Save himself65/3c09230454612846873a2c0155be6d69 to your computer and use it in GitHub Desktop.
Save himself65/3c09230454612846873a2c0155be6d69 to your computer and use it in GitHub Desktop.
import * as React from 'react'
const mockA = new Map([
[
'react',
React
],
]);
const module = new StaticModuleRecord(`
import { useState } from 'react';
export const entry = () => {
const [state, setState] = useState(null)
return null;
}
`);
const c1 = new Compartment(
{},
{},
{
name: 'first compartment',
}
);
const imports = new Map<string, Map<string, any>>();
imports.set('a', mockA);
const f = c1.evaluate(module.__syncModuleProgram__);
f({
imports: newUpdaters => {
for (const [module, moduleUpdaters] of newUpdaters) {
const moduleImports = imports.get(module);
if (moduleImports) {
for (const [importName, importUpdaters] of moduleUpdaters) {
const updateImport = (value: any) => {
for (const importUpdater of importUpdaters) {
importUpdater(value);
}
};
if (moduleImports.has(importName)) {
updateImport(moduleImports.get(importName));
} else {
console.log('import not found', importName);
}
}
}
}
},
onceVar: {
entry: (f: any) => {
console.log('onceVar', f);
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment