Skip to content

Bundle dependencies #67

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

Merged
merged 9 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
!.env.example
.apps
.vercel
/content/tutorial/common/.svelte-kit
/content/tutorial/common/node_modules
/content/tutorial/common/package-lock.json
2 changes: 1 addition & 1 deletion content/tutorial/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"prepare": "svelte-kit sync"
},
"devDependencies": {
"@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next",
"esbuild-wasm": "^0.14.43",
"svelte": "^3.44.0"
},
"type": "module"
Expand Down
6 changes: 1 addition & 5 deletions content/tutorial/common/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import adapter from '@sveltejs/adapter-auto';

/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
}
kit: {}
};

export default config;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build && node scripts/create-middleware.js",
"build": "node scripts/create-common-bundle && svelte-kit build && node scripts/create-middleware",
"package": "svelte-kit package",
"preview": "svelte-kit preview",
"prepare": "svelte-kit sync",
Expand All @@ -17,16 +17,19 @@
"@sveltejs/kit": "next",
"@sveltejs/site-kit": "^2.1.0",
"diff": "^5.1.0",
"esbuild": "^0.14.43",
"prettier": "^2.6.2",
"prettier-plugin-svelte": "^2.7.0",
"svelte": "^3.48.0",
"svelte-check": "^2.7.2",
"tiny-glob": "^0.2.9",
"typescript": "~4.6.4"
},
"type": "module",
"dependencies": {
"@fontsource/roboto-mono": "^4.5.7",
"@webcontainer/api": "^0.0.1",
"adm-zip": "^0.5.9",
"base64-js": "^1.5.1",
"marked": "^4.0.16",
"monaco-editor": "^0.33.0",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions scripts/create-common-bundle/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'fs';
import AdmZip from 'adm-zip';

const zip = new AdmZip('common.zip');
zip.extractAllTo('.');

fs.mkdirSync('node_modules/.bin');

fs.symlinkSync('../@sveltejs/kit/svelte-kit.js', 'node_modules/.bin/svelte-kit');
fs.chmodSync('node_modules/.bin/svelte-kit', 0o777);

fs.symlinkSync('../esbuild/bin/esbuild', 'node_modules/.bin/esbuild');
fs.chmodSync('node_modules/.bin/esbuild', 0o777);
69 changes: 69 additions & 0 deletions scripts/create-common-bundle/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import fs from 'fs';
import esbuild from 'esbuild';
import AdmZip from 'adm-zip';
import glob from 'tiny-glob/sync.js';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';

const cwd = 'content/tutorial/common';

if (!fs.existsSync(`${cwd}/node_modules`)) {
execSync('npm install', { cwd });
}

const zip = new AdmZip();

// we selectively exclude certain files to minimise the bundle size.
// this is a bit ropey, but it works
const ignored_basenames = ['.DS_Store', 'LICENSE'];
const ignored_extensions = ['.d.ts', '.map', '.md'];
const ignored_directories = ['.svelte-kit', 'node_modules/.bin', 'node_modules/rollup/dist/shared'];

const ignored_files = new Set([
'node_modules/vite/dist/node/terser.js',
'node_modules/rollup/dist/es/rollup.browser.js',
'node_modules/rollup/dist/rollup.browser.js',
'node_modules/svelte/compiler.js'
]);

for (const file of glob('**', { cwd, filesOnly: true, dot: true })) {
if (ignored_extensions.find((ext) => file.endsWith(ext))) continue;
if (ignored_basenames.find((basename) => file.endsWith('/' + basename))) continue;
if (ignored_directories.find((dir) => file.startsWith(dir + '/'))) continue;

if (ignored_files.has(file)) {
ignored_files.delete(file);
continue;
}

// esbuild is a special case
if (file.startsWith('node_modules/esbuild-wasm/')) {
zip.addFile(
file.replace('node_modules/esbuild-wasm', 'node_modules/esbuild'),
fs.readFileSync(`${cwd}/${file}`)
);
continue;
} else if (file.startsWith('node_modules/esbuild')) {
continue;
}

zip.addFile(file, fs.readFileSync(`${cwd}/${file}`));
}

if (ignored_files.size > 0) {
throw new Error(`expected to find ${Array.from(ignored_files).join(', ')}`);
}

const out = zip.toBuffer();

fs.writeFileSync(`src/lib/client/adapters/common/common.zip`, out);

// bundle adm-zip so we can use it in the webcontainer
esbuild.buildSync({
entryPoints: [fileURLToPath(new URL('./boot.js', import.meta.url))],
bundle: true,
platform: 'node',
minify: true,
outfile: 'src/lib/client/adapters/common/boot.cjs',
format: 'cjs'
});
2 changes: 2 additions & 0 deletions src/lib/client/adapters/common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/boot.cjs
/common.zip
16 changes: 16 additions & 0 deletions src/lib/client/adapters/common/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import zipped from './common.zip?url';
import boot from './boot.cjs?url';

async function load() {
const result = await Promise.all([
fetch(zipped).then((r) => r.arrayBuffer()),
fetch(boot).then((r) => r.text())
]);

return {
zipped: result[0],
boot: result[1]
};
}

export const ready = load();
64 changes: 39 additions & 25 deletions src/lib/client/adapters/webcontainer/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { load } from '@webcontainer/api';

import base64 from 'base64-js';
import { ready } from '../common/index.js';

const WebContainer = await load();

Expand All @@ -8,29 +10,42 @@ const WebContainer = await load();
* @returns {Promise<import('$lib/types').Adapter>}
*/
export async function create(stubs) {
const vm = await WebContainer.boot();

const tree = convert_stubs_to_tree(stubs);

const common = await ready;
tree['common.zip'] = file(new Uint8Array(common.zipped));
tree['boot.cjs'] = file(common.boot);

const vm = await WebContainer.boot();
await vm.loadFiles(tree);

const boot = await vm.run(
{
command: 'node',
args: ['boot.cjs']
},
{
stderr: (data) => console.error(`[boot] ${data}`)
}
);

const code = await boot.onExit;

if (code !== 0) {
throw new Error('Failed to initialize WebContainer');
}

const base = await new Promise(async (fulfil, reject) => {
vm.on('server-ready', (port, base) => {
fulfil(base);
});

const install = await vm.run({
command: 'turbo',
args: ['install']
});

const code = await install.onExit;

if (code !== 0) {
reject(new Error('Installation failed'));
return;
}

await vm.run({ command: 'turbo', args: ['run', 'dev'] });
await vm.run(
{ command: 'turbo', args: ['run', 'dev'] },
{
stderr: (data) => console.error(`[dev] ${data}`)
}
);
});

let current = stubs;
Expand Down Expand Up @@ -97,11 +112,7 @@ export async function create(stubs) {
tree = /** @type {import('@webcontainer/api').DirectoryEntry} */ (tree[part]).directory;
}

tree[basename] = {
file: {
contents: stub.text ? stub.contents : base64.toByteArray(stub.contents)
}
};
tree[basename] = file(stub.text ? stub.contents : base64.toByteArray(stub.contents));
}

await vm.loadFiles(root);
Expand Down Expand Up @@ -132,14 +143,17 @@ function convert_stubs_to_tree(stubs, depth = 1) {
directory: convert_stubs_to_tree(children, depth + 1)
};
} else {
tree[stub.basename] = {
file: {
contents: stub.text ? stub.contents : base64.toByteArray(stub.contents)
}
};
tree[stub.basename] = file(stub.text ? stub.contents : base64.toByteArray(stub.contents));
}
}
}

return tree;
}

/** @param {string | Uint8Array} contents */
function file(contents) {
return {
file: { contents }
};
}
10 changes: 7 additions & 3 deletions src/lib/server/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const text_files = new Set([
'.md'
]);

const excluded = new Set(['.DS_Store', '.gitkeep']);
const excluded = new Set(['.DS_Store', '.gitkeep', '.svelte-kit']);

/** @param {string} file */
function json(file) {
Expand Down Expand Up @@ -104,7 +104,7 @@ export function get_section(slug) {
if (section.slug !== slug) continue;

const a = {
...walk('content/tutorial/common'),
...walk('content/tutorial/common', { exclude: ['node_modules'] }),
...walk(`content/tutorial/${part.slug}/common`),
...walk(`${section.dir}/app-a`)
};
Expand Down Expand Up @@ -163,8 +163,11 @@ function extract_frontmatter(markdown, dir) {
/**
* Get a list of all files in a directory
* @param {string} cwd - the directory to walk
* @param {{
* exclude?: string[]
* }} options
*/
export function walk(cwd) {
export function walk(cwd, options = {}) {
/** @type {Record<string, import('$lib/types').FileStub | import('$lib/types').DirectoryStub>} */
const result = {};

Expand All @@ -179,6 +182,7 @@ export function walk(cwd) {

for (const basename of files) {
if (excluded.has(basename)) continue;
if (options.exclude?.includes(basename)) continue;

const name = dir + basename;
const resolved = path.join(cwd, name);
Expand Down