Fallback to TypeScript compilation for wasm.ts
This commit is contained in:
parent
576e191631
commit
a8bfe73321
|
|
@ -9,7 +9,7 @@ import lightningcss from 'bun-lightningcss';
|
|||
|
||||
import { getGames } from './isGame';
|
||||
|
||||
export async function buildHTML(game: string, production = false) {
|
||||
export async function buildHTML(game: string, production = false, portable = false) {
|
||||
const html = await Bun.file(path.resolve(import.meta.dir, '..', 'assets', 'index.html')).text();
|
||||
const bundle = await Bun.build({
|
||||
outdir: '/tmp',
|
||||
|
|
@ -23,7 +23,7 @@ export async function buildHTML(game: string, production = false) {
|
|||
plugins: [
|
||||
imagePlugin,
|
||||
fontPlugin,
|
||||
wasmPlugin,
|
||||
wasmPlugin({ production, portable }),
|
||||
lightningcss(),
|
||||
]
|
||||
});
|
||||
|
|
|
|||
|
|
@ -15,7 +15,11 @@ Bun.serve({
|
|||
case '/':
|
||||
case 'index.html':
|
||||
try {
|
||||
const html = await buildHTML(game);
|
||||
const html = await buildHTML(
|
||||
game,
|
||||
url.searchParams.get('production') === 'true', // to debug production builds
|
||||
url.searchParams.get('portable') === 'true', // to skip AssemblyScript compilation
|
||||
);
|
||||
if (html) {
|
||||
return new Response(html, {
|
||||
headers: {
|
||||
|
|
|
|||
|
|
@ -2,18 +2,33 @@ import { plugin, type BunPlugin } from "bun";
|
|||
import path from 'path';
|
||||
import asc from 'assemblyscript/asc';
|
||||
|
||||
const wasmPlugin: BunPlugin = {
|
||||
interface WasmLoaderConfig {
|
||||
production?: boolean;
|
||||
portable?: boolean;
|
||||
}
|
||||
|
||||
const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin => {
|
||||
const p: BunPlugin = {
|
||||
name: "WASM loader",
|
||||
async setup(build) {
|
||||
build.onLoad({ filter: /\.wasm\.ts$/ }, async (args) => {
|
||||
if (portable) {
|
||||
const contents = await Bun.file(args.path).text();
|
||||
return {
|
||||
contents: `import "assemblyscript/std/portable/index.js";\n${contents}`,
|
||||
loader: 'tsx',
|
||||
}
|
||||
}
|
||||
const wasmPath = path.resolve(import.meta.dir, '..', '..', 'dist', 'tmp.wasm');
|
||||
const jsPath = wasmPath.replace(/\.wasm$/, '.js');
|
||||
const { error, stderr } = await asc.main([
|
||||
const ascArgs = [
|
||||
args.path,
|
||||
'--outFile', wasmPath,
|
||||
'--textFile', wasmPath.replace(/\.wasm$/, '.wat'),
|
||||
'--optimize', '--bindings', 'esm',
|
||||
]);
|
||||
'--bindings', 'esm',
|
||||
];
|
||||
ascArgs.push(production ? '--optimize' : '--debug');
|
||||
|
||||
const { error, stderr } = await asc.main(ascArgs);
|
||||
|
||||
if (error) {
|
||||
console.error(stderr.toString(), error.message);
|
||||
|
|
@ -33,7 +48,8 @@ const wasmPlugin: BunPlugin = {
|
|||
});
|
||||
}
|
||||
};
|
||||
|
||||
plugin(wasmPlugin);
|
||||
plugin(p);
|
||||
return p;
|
||||
};
|
||||
|
||||
export default wasmPlugin;
|
||||
Loading…
Reference in New Issue