mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
esbuild almost works!
This commit is contained in:
6 files changed
+210
-49
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import type { Plugin } from 'esbuild-wasm';
|
||||
|
||||
export function esmShPlugin(): Plugin {
|
||||
return {
|
||||
name: 'esm-sh',
|
||||
setup(build) {
|
||||
// Handle bare imports like "react"
|
||||
build.onResolve({ filter: /^[^./].*/ }, (args) => ({
|
||||
path: `https://esm.sh/${args.path}`,
|
||||
namespace: 'http',
|
||||
}));
|
||||
|
||||
// Handle relative or absolute imports inside esm.sh modules
|
||||
build.onResolve({ filter: /.*/, namespace: 'http' }, (args) => {
|
||||
const baseURL = new URL(args.importer); // e.g., https://esm.sh/react-dom/client
|
||||
const fullURL = new URL(args.path, baseURL).toString(); // resolve "/x" → full URL
|
||||
return {
|
||||
path: fullURL,
|
||||
namespace: 'http',
|
||||
};
|
||||
});
|
||||
|
||||
// Load HTTP modules
|
||||
build.onLoad({ filter: /.*/, namespace: 'http' }, async (args) => {
|
||||
const res = await fetch(args.path);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch ${args.path}`);
|
||||
}
|
||||
|
||||
const contents = await res.text();
|
||||
|
||||
return {
|
||||
contents,
|
||||
loader: 'js',
|
||||
};
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import * as esbuild from 'esbuild-wasm';
|
||||
|
||||
import { esmShPlugin } from './esmShPlugin';
|
||||
import { lightningFsPlugin } from './lightningFsPlugin';
|
||||
|
||||
import type { JSRuntimeFS } from '@/lib/JSRuntime';
|
||||
|
||||
await esbuild.initialize({
|
||||
worker: false,
|
||||
wasmURL: 'https://unpkg.com/esbuild-wasm/esbuild.wasm',
|
||||
});
|
||||
|
||||
export async function buildProject(fs: JSRuntimeFS, projectId: string): Promise<Record<string, Uint8Array>> {
|
||||
const indexHtml = await fs.readFile(`/projects/${projectId}/index.html`, 'utf8');
|
||||
|
||||
// TODO: parse indexHtml to find entryPoints
|
||||
// The file will have an absolute path like `/src/main.tsx` which must be converted to a relative path like `main.tsx`
|
||||
|
||||
const results = await esbuild.build({
|
||||
entryPoints: ['main.tsx'],
|
||||
bundle: true,
|
||||
write: false,
|
||||
format: 'esm',
|
||||
target: 'esnext',
|
||||
outdir: 'dist',
|
||||
plugins: [
|
||||
lightningFsPlugin(fs, `/projects/${projectId}/src`),
|
||||
esmShPlugin(),
|
||||
],
|
||||
});
|
||||
|
||||
const dist: Record<string, Uint8Array> = {};
|
||||
|
||||
for (const file of results.outputFiles) {
|
||||
const relativePath = file.path.replace(/^\/dist\//, '');
|
||||
dist[relativePath] = file.contents;
|
||||
}
|
||||
|
||||
// FIXME: We need to ensure that dist files are imported inside the index.html. If a file was processed from an existing tag, its path might need to be updated, otherwise it might need to be imported (eg a css output file due to css import in js)
|
||||
dist['index.html'] = new TextEncoder().encode(indexHtml);
|
||||
|
||||
return dist;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
import path from 'path-browserify';
|
||||
|
||||
import type { Loader, Plugin } from 'esbuild-wasm';
|
||||
import type { JSRuntimeFS } from '@/lib/JSRuntime';
|
||||
|
||||
export function lightningFsPlugin(fs: JSRuntimeFS, cwd: string): Plugin {
|
||||
return {
|
||||
name: 'lightning-fs',
|
||||
setup(build) {
|
||||
build.onResolve({ filter: /.*/ }, async (args) => {
|
||||
let resolved: string;
|
||||
|
||||
if (args.path.startsWith('@/')) {
|
||||
resolved = path.join(cwd, args.path.slice(2));
|
||||
} else if (args.path.startsWith('/')) {
|
||||
resolved = args.path;
|
||||
} else {
|
||||
resolved = path.join(cwd, args.resolveDir, args.path);
|
||||
}
|
||||
|
||||
try {
|
||||
resolved = await tryFileVariants(fs, resolved);
|
||||
} catch {
|
||||
return; // Let other plugins handle this
|
||||
}
|
||||
|
||||
return {
|
||||
path: resolved,
|
||||
namespace: 'lightning-fs',
|
||||
};
|
||||
});
|
||||
|
||||
build.onLoad({ filter: /.*/, namespace: 'lightning-fs' }, async (args) => {
|
||||
const ext = path.extname(args.path).slice(1);
|
||||
|
||||
if (!['ts', 'tsx', 'js', 'jsx', 'css'].includes(ext)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return {
|
||||
contents: await fs.readFile(args.path, 'utf8'),
|
||||
loader: ext as Loader,
|
||||
};
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function tryFileVariants(fs: JSRuntimeFS, basePath: string): Promise<string> {
|
||||
const extensions = ['.ts', '.tsx', '.js', '.jsx', '.css'];
|
||||
|
||||
try {
|
||||
await fs.stat(basePath);
|
||||
return basePath;
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
// Try direct file with extensions
|
||||
for (const ext of extensions) {
|
||||
const full = basePath + ext;
|
||||
try {
|
||||
await fs.stat(full);
|
||||
return full;
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
// Try directory index files
|
||||
for (const ext of extensions) {
|
||||
const indexFile = path.join(basePath, 'index' + ext);
|
||||
try {
|
||||
await fs.stat(indexFile);
|
||||
return indexFile;
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('File not found');
|
||||
}
|
||||
Reference in new issue
Block a user