Skip to content

Commit

Permalink
Merge pull request #24 from magne4000/og
Browse files Browse the repository at this point in the history
fix: wasm import
  • Loading branch information
magne4000 committed Sep 25, 2023
2 parents 400e464 + d891bc0 commit e8d8ab5
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 6 deletions.
33 changes: 33 additions & 0 deletions examples/demo/endpoints/og.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { ImageResponse } from '@vercel/og';

import type { VercelRequest, VercelResponse } from '@vercel/node';

export default async function handler(
request: VercelRequest,
response: VercelResponse,
) {
return new ImageResponse(
(
<div
style={{
fontSize: 40,
color: 'black',
background: 'white',
width: '100%',
height: '100%',
padding: '50px 200px',
textAlign: 'center',
justifyContent: 'center',
alignItems: 'center',
}}
>
👋 Hello
</div>
),
{
width: 1200,
height: 630,
},
);
}
1 change: 1 addition & 0 deletions examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"@vercel/edge-config": "^0.2.1",
"@vercel/og": "^0.5.17",
"@vite-plugin-vercel/vike": "workspace:*",
"@vitejs/plugin-react-swc": "^3.3.2",
"cross-fetch": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions examples/demo/renderer/PageShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function PageShell({
<Link href="/catch-all/a/b/c">Catch-all</Link>
<Link href="/function/a">Function</Link>
<Link href="/edge">Edge Function endpoint</Link>
<Link href="/og">Edge OG endpoint</Link>
</Sidebar>
<Content>{children}</Content>
</Layout>
Expand Down
5 changes: 5 additions & 0 deletions examples/demo/tests/05-vite-plugin-ssr/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ prepareTestJsonFileContent('config.json', (context) => {
dest: '/edge/$1',
check: true,
},
{
src: '^/og(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?$',
dest: '/og/$1',
check: true,
},
{
check: true,
src: '^/api/page$',
Expand Down
5 changes: 5 additions & 0 deletions examples/demo/tests/05-vite-plugin-ssr/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('fs', function () {
'/functions/api/post.func/.vc-config.json',
'/functions/edge.func/index.js',
'/functions/edge.func/.vc-config.json',
'/functions/og.func/index.js',
'/functions/og.func/.vc-config.json',
new RegExp('/functions/og.func/.*.ttf'),
'/functions/og.func/resvg.wasm',
'/functions/og.func/yoga.wasm',
'/static/vite-plugin-ssr.json',
'/static/manifest.json',
// ISR + Static pages
Expand Down
6 changes: 6 additions & 0 deletions examples/demo/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export default {
edge: true,
addRoute: true,
},
{
source: 'endpoints/og.tsx',
destination: `og`,
edge: true,
addRoute: true,
},
],
},
// We manually add a list of dependencies to be pre-bundled, in order to avoid a page reload at dev start which breaks vite-plugin-ssr's CI
Expand Down
60 changes: 57 additions & 3 deletions packages/vercel/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ResolvedConfig } from 'vite';
import glob from 'fast-glob';
import path from 'path';
import path, { basename, dirname } from 'path';
import { getOutput, getRoot, pathRelativeTo } from './utils';
import { build, BuildOptions } from 'esbuild';
import { build, BuildOptions, type Plugin } from 'esbuild';
import { ViteVercelApiEntry } from './types';
import { assert } from './assert';
import { vercelOutputVcConfigSchema } from './schemas/config/vc-config';
import fs from 'fs/promises';
import fs, { copyFile } from 'fs/promises';
import type { Rewrite } from '@vercel/routing-utils';

export function getAdditionalEndpoints(resolvedConfig: ResolvedConfig) {
Expand Down Expand Up @@ -57,13 +57,43 @@ export function getEntries(
}, getAdditionalEndpoints(resolvedConfig));
}

const wasmPlugin: Plugin = {
name: 'wasm',
setup(build) {
build.onResolve({ filter: /\.wasm/ }, (args) => {
return {
path: args.path.replace(/\.wasm\?module$/i, '.wasm'),
external: true,
};
});
},
};

const vercelOgPlugin = (ctx: { found: boolean; index: string }): Plugin => {
return {
name: 'vercel-og',
setup(build) {
build.onResolve({ filter: /@vercel\/og/ }, () => {
ctx.found = true;
return undefined;
});

build.onLoad({ filter: /@vercel\/og/ }, (args) => {
ctx.index = args.path;
return undefined;
});
},
};
};

const standardBuildOptions: BuildOptions = {
bundle: true,
target: 'es2020',
format: 'cjs',
platform: 'node',
logLevel: 'info',
minify: true,
plugins: [wasmPlugin],
};

export async function buildFn(
Expand Down Expand Up @@ -114,9 +144,33 @@ export async function buildFn(
'import',
'require',
];
options.format = 'esm';
}

const ctx = { found: false, index: '' };
options.plugins!.push(vercelOgPlugin(ctx));

await build(options);

// Special case for @vercel/og
// See https://github.com/magne4000/vite-plugin-vercel/issues/23
// and https://github.com/magne4000/vite-plugin-vercel/issues/25
if (ctx.found && ctx.index) {
const dir = dirname(ctx.index);
const externalFiles = await glob(`${dir}/*.{ttf,wasm}`);

for (const f of externalFiles) {
await copyFile(
f,
path.join(
getOutput(resolvedConfig, 'functions'),
entry.destination,
basename(f),
),
);
}
}

await writeVcConfig(resolvedConfig, entry.destination, Boolean(entry.edge));
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vercel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2020",
"lib": ["ES2020"],
"target": "ES2021",
"lib": ["es2021", "dom"],
"module": "ESNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
Expand Down
Loading

1 comment on commit e8d8ab5

@vercel
Copy link

@vercel vercel bot commented on e8d8ab5 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.