Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Inline WebView bundle #2528

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions packages/snaps-execution-environments/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const ENTRY_POINTS = {
webview: {
entryPoint: './src/webview/index.ts',
html: true,
inlineBundle: true,
},
};

Expand Down Expand Up @@ -99,18 +100,30 @@ async function main() {
<html>
<head>
<meta charset="utf-8" />
<title>MetaMask Snaps Iframe Execution Environment</title>
<title>MetaMask Snaps Execution Environment</title>
<script>${lavaMoatRuntimeBrowser}</script>
</head>
<body>
<script src="bundle.js"></script>
</body>
</html>`;

const createInlinedHTML = (bundleSource) => `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>MetaMask Snaps Execution Environment</title>
<script>${lavaMoatRuntimeBrowser}</script>
</head>
<body>
<script>${bundleSource}</script>
</body>
</html>`;

await Promise.all(
Object.entries(ENTRY_POINTS).map(async ([key, config]) => {
console.log('Bundling', key);
const { html, node, worker, entryPoint } = config;
const { html, node, worker, entryPoint, inlineBundle } = config;
const insertGlobalVars = node
? { process: undefined, ...LavaMoatBrowserify.args.insertGlobalVars }
: LavaMoatBrowserify.args.insertGlobalVars;
Expand Down Expand Up @@ -218,14 +231,19 @@ async function main() {
outputBundle = `${runtime}\n${outputBundle}`;
}

const bundlePath = path.join(OUTPUT_PATH, key, OUTPUT_BUNDLE);
await fs.mkdir(path.dirname(bundlePath), { recursive: true });
await fs.writeFile(bundlePath, outputBundle);
if (!inlineBundle) {
const bundlePath = path.join(OUTPUT_PATH, key, OUTPUT_BUNDLE);
await fs.mkdir(path.dirname(bundlePath), { recursive: true });
await fs.writeFile(bundlePath, outputBundle);
}

if (html) {
const htmlPath = path.join(OUTPUT_PATH, key, OUTPUT_HTML);
await fs.mkdir(path.dirname(htmlPath), { recursive: true });
await fs.writeFile(htmlPath, htmlFile);
await fs.writeFile(
htmlPath,
inlineBundle ? createInlinedHTML(outputBundle) : htmlFile,
);
}

const outputBytes = stringToBytes(outputBundle);
Expand Down
Loading