Skip to content

Commit

Permalink
feat: runtime code
Browse files Browse the repository at this point in the history
  • Loading branch information
hikerpig committed Jan 1, 2024
1 parent 04bd066 commit 49ea494
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as path from 'path'
import * as fs from 'fs'
import { Plugin } from 'esbuild'

const packageDir = path.resolve(__dirname, '../node_modules')

const RUNTIME_CODE_NS = 'pintora-runtime-code'

export function makeESBuildNodePolyfillsPlugin(opts: { runtimeLibPath: string }) {
return {
name: 'ESBuildNodePolyfillsPlugin',
setup(build) {
build.onResolve({ filter: /virtual:pintora/ }, args => {
return { namespace: RUNTIME_CODE_NS, path: opts.runtimeLibPath }
})
build.onLoad({ filter: /.*/, namespace: RUNTIME_CODE_NS }, args => {
const contents = fs.readFileSync(opts.runtimeLibPath)
return {
contents,
}
})
},
} as Plugin
}
36 changes: 28 additions & 8 deletions packages/pintora-target-wintercg/build/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as path from 'path'
import * as fs from 'fs'
import { build, BuildOptions } from 'esbuild'
import { ESBuildNodePolyfillsPlugin } from './ESBuildNodePolyfillsPlugin'

// const packageDir = path.resolve(__dirname)
const packageDir = path.resolve(__dirname, '..')
const aliasDir = path.resolve(__dirname, '../aliases')

const runtimeOutFilePath = path.join(packageDir, 'dist/runtime.js')

const options: BuildOptions = {
entryPoints: ['runtime/index.ts'],
bundle: true,
outfile: 'dist/runtime.js',
outfile: runtimeOutFilePath,
format: 'iife',
globalName: 'pintoraTarget',
sourcemap: 'external',
Expand All @@ -21,11 +24,28 @@ const options: BuildOptions = {
plugins: [ESBuildNodePolyfillsPlugin],
loader: {
'.ttf': 'binary',
}
},
write: true,
}

const umdOptions: BuildOptions = { ...options }
build({
...umdOptions,
write: true,
})
build(options).then(afterLibEsbuild)

async function afterLibEsbuild() {
console.log('afterLibEsbuild, generate platform code')
const plugBuild = await build({
entryPoints: ['src/platforms/edge-handler.ts'],
outdir: path.join(packageDir, 'dist/platforms'),
bundle: true,
format: 'iife',
sourcemap: false,
write: false,
// plugins: [makeESBuildNodePolyfillsPlugin({ runtimeLibPath: runtimeOutFilePath })],
})
const runtimeLibCode = fs.readFileSync(runtimeOutFilePath, 'utf-8').toString()
const handlerCode = `
${runtimeLibCode}
// separation
${plugBuild.outputFiles[0].text}
`
fs.writeFileSync(path.join(packageDir, 'dist/platforms/edge-handler.js'), handlerCode)
}
6 changes: 6 additions & 0 deletions packages/pintora-target-wintercg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"publishConfig": {
"access": "public"
},
"files": [
"dist",
"CHANGELOG.md",
"README.md"
],
"dependencies": {
"@pintora/core": "workspace:*",
"@pintora/renderer": "workspace:*",
Expand All @@ -41,6 +46,7 @@
"path-browserify": "^1.0.1",
"punycode": "^2.3.1",
"readable-stream": "^3.5.0",
"signal-exit": "^4.1.0",
"stream": "^0.0.2",
"stream-browserify": "^3.0.0",
"ts-node": "^10.9.2",
Expand Down
3 changes: 3 additions & 0 deletions packages/pintora-target-wintercg/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* pintora target for wintercg, this module will be bundled into one file. It will be combined with other handler code runs inside edge runtime
*/
import { render, RuntimeRenderOptions } from './render'

async function pintoraMain(opts: RuntimeRenderOptions) {
Expand Down
4 changes: 3 additions & 1 deletion packages/pintora-target-wintercg/src/edge-runtime-pintora.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// test edge runtime
// node lib/edge-runtime-server.js
import { EdgeRuntime } from 'edge-runtime'
import * as fs from 'fs'
import * as path from 'path'
Expand All @@ -7,7 +9,7 @@ const runtime = new EdgeRuntime()

const dir = __dirname
async function main() {
const runtimeCode = fs.readFileSync(path.join(dir, '../dist/runtime.js'), 'utf-8').toString()
const runtimeCode = fs.readFileSync(path.join(dir, '../dist/platforms/edge-handler.js'), 'utf-8').toString()

const codeToRun = `
${runtimeCode}
Expand Down
21 changes: 21 additions & 0 deletions packages/pintora-target-wintercg/src/edge-runtime-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EdgeRuntime, runServer } from 'edge-runtime'
import { onExit } from 'signal-exit'
import * as fs from 'fs'
import * as path from 'path'

const dir = __dirname

async function main() {
const initialCode = fs.readFileSync(path.join(dir, '../dist/platforms/edge-handler.js'), 'utf-8').toString()

const edgeRuntime = new EdgeRuntime({ initialCode })

const server = await runServer({ runtime: edgeRuntime, port: 9000 })
console.log(`> Edge server running at ${server.url}`)
onExit(() => {
server.close()
return
})
}

main()
2 changes: 1 addition & 1 deletion packages/pintora-target-wintercg/src/node-pintora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function main() {
fs.writeFileSync(path.join(process.cwd(), 'dist/code-to-run.js'), codeToRun)
}
await runInThisContext(codeToRun)
const target = globalThis.pintoraTarget as PintoraTarget
const target = pintoraTarget as PintoraTarget
let pintoraDsl = ''
const inputFilePath = process.argv[2]
if (inputFilePath) {
Expand Down
27 changes: 27 additions & 0 deletions packages/pintora-target-wintercg/src/platforms/edge-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// this module runs inside edge runtime, and pintoraTarget will be prepended by bundler
/* eslint-disable @typescript-eslint/triple-slash-reference */
/// <reference path="../../types/index.d.ts" />

const target = pintoraTarget

addEventListener('fetch', async event => {
const requestText = await event.request.text()

const code =
requestText ||
`
sequenceDiagram
title: Sequence Diagram Example
autonumber
User->>Pintora: render this
`
const result = await target.pintoraMain({
code,
})
const response = new Response(result.data, {
headers: {
'Content-Type': 'image/svg+xml',
},
})
return event.respondWith(response)
})
7 changes: 5 additions & 2 deletions packages/pintora-target-wintercg/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
"checkJs": true,
"allowJs": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": false,
"module": "CommonJS",
"target": "esnext",
"esModuleInterop": false,
"types": ["@edge-runtime/types", "node", "svgdom"]
},
"references": [
{ "path": "../pintora-standalone" }
{ "path": "../pintora-standalone" },
{ "path": "../pintora-core" }
],
"include": [
"./src/**/*",
"./types/*.d.ts"
"./types/*"
]
}
6 changes: 5 additions & 1 deletion packages/pintora-target-wintercg/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ export type PintoraTarget = {
}>
}

declare const pintoraTarget: PintoraTarget
export const pintoraTarget: PintoraTarget

declare global {
export const pintoraTarget: PintoraTarget
}
19 changes: 3 additions & 16 deletions pnpm-lock.yaml

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

0 comments on commit 49ea494

Please sign in to comment.