From f0719378002cb8a07ffb43e343630be52cb10bfe Mon Sep 17 00:00:00 2001 From: Okiki Date: Thu, 9 Feb 2023 18:29:31 +0000 Subject: [PATCH] fix edge function api --- .gitignore | 1 + .gitpod.yml | 11 +++++++++++ api/index.ts | 28 ++++++++++++++++++++-------- package.json | 5 +++-- pnpm-lock.yaml | 29 +++++++++++++++++++++++++++++ tsconfig.json | 5 ++--- vite.config.ts | 7 +++++++ 7 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 .gitpod.yml create mode 100644 vite.config.ts diff --git a/.gitignore b/.gitignore index a547bf3..fc5ae9f 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ dist-ssr *.njsproj *.sln *.sw? +.vercel diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000..a681be1 --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,11 @@ +# This configuration file was automatically generated by Gitpod. +# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml) +# and commit this file to your remote git repository to share the goodness with others. + +# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart + +tasks: + - init: pnpm install && pnpm run build + command: pnpm run dev + + diff --git a/api/index.ts b/api/index.ts index 36d6115..7eda929 100644 --- a/api/index.ts +++ b/api/index.ts @@ -2,12 +2,22 @@ export const config = { runtime: 'edge', // this is a pre-requisite }; -import * as esbuild from 'esbuild-wasm'; -// @ts-ignore -import wasmModule from 'esbuild-wasm/esbuild.wasm?module'; -// import { TextEncoder as Encoder, TextDecoder } from 'text-encoding-shim'; -// const TextEncoder = Encoder as unknown as TextEncoder; +globalThis.performance = globalThis.performance ?? { now: Date.now } as Performance; + +import { TextEncoder as Encoder, TextDecoder as Decoder } from 'text-encoding-shim'; + +type EncoderType = typeof globalThis.TextEncoder; +type DecoderType = typeof globalThis.TextDecoder; + +globalThis.TextEncoder = globalThis.TextEncoder ?? Encoder as unknown as EncoderType; +globalThis.TextDecoder = globalThis.TextDecoder ?? Decoder as unknown as DecoderType; + +globalThis.location = globalThis.location ?? new URL("http://localhost:3000/") as unknown as Location; + +import * as esbuild from 'esbuild-wasm'; +// @ts-ignore Vercel Edge Function don't import WASM Moules that match `wasm instanceof WebAssembly.Module` +// import wasmModule from 'esbuild-wasm/esbuild.wasm?module'; export const inputModelResetValue = [ 'export default { complex: { boolean: true } }', @@ -16,15 +26,15 @@ export const inputModelResetValue = [ export default async function handler(req: Request) { try { await esbuild.initialize({ - wasmModule, + wasmURL: 'node_modules/esbuild-wasm/esbuild.wasm', worker: false, }); const start = Date.now(); const url = new URL(req.url); const result = await esbuild.build({ - entryPoints: { - '/index.tsx': inputModelResetValue, + stdin: { + 'contents': inputModelResetValue, }, treeShaking: true, @@ -40,6 +50,7 @@ export default async function handler(req: Request) { minify: true, platform: 'browser', + write: false, }); const end = Date.now(); @@ -49,6 +60,7 @@ export default async function handler(req: Request) { JSON.stringify({ query: url.search, input: inputModelResetValue, + output: result, rawTime: (end - start) / 1000, }), { diff --git a/package.json b/package.json index fe45846..33eccde 100644 --- a/package.json +++ b/package.json @@ -4,14 +4,15 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "vite", "build": "vite build", "preview": "vite preview" }, "devDependencies": { + "text-encoding-shim": "^1.0.5", "typescript": "^4.9.5", "vercel": "^28.15.3", - "vite": "^4.1.0" + "vite": "^4.1.0", + "vite-plugin-vercel": "^0.1.6" }, "dependencies": { "esbuild-wasm": "^0.17.6" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index caa501f..b45a7ee 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2,17 +2,21 @@ lockfileVersion: 5.4 specifiers: esbuild-wasm: ^0.17.6 + text-encoding-shim: ^1.0.5 typescript: ^4.9.5 vercel: ^28.15.3 vite: ^4.1.0 + vite-plugin-vercel: ^0.1.6 dependencies: esbuild-wasm: 0.17.6 devDependencies: + text-encoding-shim: 1.0.5 typescript: 4.9.5 vercel: 28.15.3 vite: 4.1.1 + vite-plugin-vercel: 0.1.6_vite@4.1.1 packages: @@ -22,6 +26,10 @@ packages: regenerator-runtime: 0.13.11 dev: true + /@brillout/libassert/0.5.8: + resolution: {integrity: sha512-u/fu+jTRUdNdbLONGq1plCfh+k2/XjSbGVTfnF3rHnSPZd+ABWp0XinR5ifrFkyGOzMbzv8IiQ44lZ4U6ZGrGA==} + dev: true + /@cspotcode/source-map-support/0.8.1: resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==} engines: {node: '>=12'} @@ -1428,6 +1436,10 @@ packages: yallist: 4.0.0 dev: true + /text-encoding-shim/1.0.5: + resolution: {integrity: sha512-H7yYW+jRn4yhu60ygZ2f/eMhXPITRt4QSUTKzLm+eCaDsdX8avmgWpmtmHAzesjBVUTAypz9odu5RKUjX5HNYA==} + dev: true + /time-span/4.0.0: resolution: {integrity: sha512-MyqZCTGLDZ77u4k+jqg4UlrzPTPZ49NDlaekU6uuFaJLzPIN1woaRXCbGeqOfxwc3Y37ZROGAJ614Rdv7Olt+g==} engines: {node: '>=10'} @@ -1542,6 +1554,19 @@ packages: - supports-color dev: true + /vite-plugin-vercel/0.1.6_vite@4.1.1: + resolution: {integrity: sha512-/Bdweik66AWNiqmwuQeW0I1qgrwivJCbo0AwBSx9xt4Cy2K24XoL8eZ+KDEDjj3a6VQATKlgKFkr4vKLFkfscg==} + peerDependencies: + vite: ^3.0.0 + dependencies: + '@brillout/libassert': 0.5.8 + '@vercel/routing-utils': 2.1.8 + esbuild: 0.16.17 + fast-glob: 3.2.12 + vite: 4.1.1 + zod: 3.20.6 + dev: true + /vite/4.1.1: resolution: {integrity: sha512-LM9WWea8vsxhr782r9ntg+bhSFS06FJgCvvB0+8hf8UWtvaiDagKYWXndjfX6kGl74keHJUcpzrQliDXZlF5yg==} engines: {node: ^14.18.0 || >=16.0.0} @@ -1608,3 +1633,7 @@ packages: resolution: {integrity: sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==} engines: {node: '>=6'} dev: true + + /zod/3.20.6: + resolution: {integrity: sha512-oyu0m54SGCtzh6EClBVqDDlAYRz4jrVtKwQ7ZnsEmMI9HnzuZFj8QFwAY1M5uniIYACdGvv0PBWPF2kO0aNofA==} + dev: true diff --git a/tsconfig.json b/tsconfig.json index 2d7ba43..6ec4ed3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,8 @@ { "compilerOptions": { "moduleResolution": "node", - "target": "ES2021", - "module": "ES2020", - "lib": ["ES2021", "DOM", "DOM.Iterable", "WebWorker"], + "target": "esnext", + "module": "esnext", "sourceMap": true, "outDir": "lib", "resolveJsonModule": true, diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..d410035 --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vite'; +import vercel from 'vite-plugin-vercel'; + +export default defineConfig({ + // @ts-ignore + plugins: [vercel()], +}); \ No newline at end of file -- knotmirror 0.0.1