Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit b086df6

Browse files
author
Wenjie Xia
committed
feat: rewrite plugin system
1 parent a515e0a commit b086df6

File tree

10 files changed

+190
-209
lines changed

10 files changed

+190
-209
lines changed

framework/core/routing.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { DependencyDescriptor } from '../../server/types.ts'
21
import { reModuleExt } from '../../shared/constants.ts'
32
import util from '../../shared/util.ts'
4-
import type { RouterURL } from '../../types.ts'
3+
import type { DependencyDescriptor, RouterURL } from '../../types.ts'
54
import events from './events.ts'
65

76
export interface Route {

framework/react/renderer.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,20 @@ import { createElement } from 'https://esm.sh/react'
33
import { renderToString } from 'https://esm.sh/react-dom/server'
44
import { hashShort, reHttp } from '../../shared/constants.ts'
55
import util from '../../shared/util.ts'
6-
import type { RouterURL } from '../../types.ts'
6+
import type { RenderResult, RouterURL } from '../../types.ts'
77
import events from '../core/events.ts'
88
import { RendererContext, RouterContext } from './context.ts'
99
import { AsyncUseDenoError, E400MissingComponent, E404Page } from './error.ts'
1010
import { serverStyles } from './style.ts'
1111
import { createPageProps, isLikelyReactComponent } from './util.ts'
1212

13-
interface RenderResult {
14-
head: string[]
15-
body: string
16-
data: Record<string, any> | null
17-
scripts: Record<string, any>[]
18-
}
19-
20-
export async function renderPage(
13+
export async function render(
2114
url: RouterURL,
2215
App: ComponentType<any> | undefined,
2316
E404: ComponentType | undefined,
2417
pageComponentTree: { url: string, Component?: any }[],
2518
styles?: { url: string, hash: string }[]
26-
): Promise<RenderResult> {
19+
) {
2720
let el: ReactElement
2821
const pageProps = createPageProps(pageComponentTree)
2922
if (App) {
@@ -48,7 +41,7 @@ export async function renderPage(
4841
}
4942
}
5043

51-
const ret: RenderResult = {
44+
const ret: Omit<RenderResult, 'url' | 'status'> = {
5245
head: [],
5346
body: '',
5447
data: null,
@@ -58,7 +51,7 @@ export async function renderPage(
5851
headElements: new Map(),
5952
scriptsElements: new Map()
6053
}
61-
const buildMode = Deno.env.get('__buildMode')
54+
const buildMode = Deno.env.get('BUILD_MODE')
6255
const data: Record<string, any> = {}
6356
const useDenUrl = `useDeno://${url.pathname}`
6457
const useDenoAsyncCalls: Array<Promise<any>> = []
@@ -98,7 +91,6 @@ export async function renderPage(
9891
if (error instanceof AsyncUseDenoError) {
9992
continue
10093
}
101-
console.log(error)
10294
Object.assign(window, { [`__asyncData_${useDenUrl}`]: null })
10395
throw error
10496
}

plugins/sass.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { LoaderPlugin } from '../types.ts'
33

44
const pluginFactory = (opts: Options = {}): LoaderPlugin => ({
55
type: 'loader',
6-
name: 'sass-loader',
6+
loader: 'css',
77
test: /.(sass|scss)$/,
88
acceptHMR: true,
9-
transform(content: Uint8Array, path: string) {
9+
precompile(content: Uint8Array, path: string) {
1010
const ret = renderSync({
1111
indentedSyntax: path.endsWith('.sass'),
1212
...opts,
@@ -17,16 +17,15 @@ const pluginFactory = (opts: Options = {}): LoaderPlugin => ({
1717
return {
1818
code: (new TextDecoder).decode(ret.css),
1919
map: ret.map ? (new TextDecoder).decode(ret.map) : undefined,
20-
loader: 'css'
2120
}
2221
}
2322
})
24-
const defaultPlugin = pluginFactory()
2523

26-
// make the `pluginFactory` as a plugin
27-
pluginFactory.type = defaultPlugin.type
24+
// make the `pluginFactory` function as a plugin
25+
const defaultPlugin = pluginFactory()
26+
pluginFactory.loader = defaultPlugin.loader
2827
pluginFactory.test = defaultPlugin.test
2928
pluginFactory.acceptHMR = defaultPlugin.acceptHMR
30-
pluginFactory.transform = defaultPlugin.transform
29+
pluginFactory.precompile = defaultPlugin.precompile
3130

3231
export default pluginFactory

plugins/sass_test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
22
import plugin from './sass.ts'
33

44
Deno.test('project scss loader plugin', async () => {
5-
const { code, loader } = await plugin.transform(
5+
const { code } = await plugin.precompile!(
66
(new TextEncoder).encode('$someVar: 123px; .some-selector { width: $someVar; }'),
77
'test.scss'
88
)
99
assertEquals(plugin.test.test('test.sass'), true)
1010
assertEquals(plugin.test.test('test.scss'), true)
11+
assertEquals(plugin.loader, 'css')
1112
assertEquals(plugin.acceptHMR, true)
1213
assertEquals(code, '.some-selector {\n width: 123px;\n}')
13-
assertEquals(loader, 'css')
1414
})
1515

1616
Deno.test('project sass loader plugin', async () => {
17-
let ret = await plugin.transform(
17+
let ret = await plugin.precompile!(
1818
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
1919
'test.sass'
2020
)
2121
assertEquals(ret.code, '.some-selector {\n width: 123px;\n}')
22-
ret = await plugin({ indentType: 'tab', indentWidth: 2 }).transform(
22+
ret = await plugin({ indentType: 'tab', indentWidth: 2 }).precompile!(
2323
(new TextEncoder).encode('$someVar: 123px\n.some-selector\n width: 123px'),
2424
'test.sass'
2525
)

plugins/wasm.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import type { LoaderPlugin } from '../types.ts'
22

33
const wasmLoader: LoaderPlugin = {
44
type: 'loader',
5-
name: 'wasm-loader',
5+
loader: 'js',
66
test: /.wasm$/,
7-
transform: (content: Uint8Array, path: string) => ({
7+
precompile: (content: Uint8Array, path: string) => ({
88
code: `
99
const wasmBytes = new Uint8Array([${content.join(',')}])
1010
const wasmModule = new WebAssembly.Module(wasmBytes)
1111
const { exports } = new WebAssembly.Instance(wasmModule)
1212
export default exports
1313
`,
14-
loader: 'js'
1514
})
1615
}
1716

plugins/wasm_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Deno.test('project wasm loader plugin', async () => {
1010
105, 110, 0, 0, 10, 138, 128, 128, 128, 0, 1, 132, 128, 128, 128, 0, 0,
1111
65, 42, 11
1212
])
13-
const { code, loader } = await plugin.transform(wasmBytes, '42.wasm')
13+
const { code } = await plugin.precompile!(wasmBytes, '42.wasm')
1414
const jsfile = (await Deno.makeTempFile()) + '.js'
1515
await Deno.writeTextFile(jsfile, code)
1616
const { default: wasm } = await import('file://' + jsfile)
1717
assertEquals(plugin.test.test('test.wasm'), true)
18+
assertEquals(plugin.loader, 'js')
1819
assertEquals(wasm.main(), 42)
19-
assertEquals(loader, 'js')
2020
})

0 commit comments

Comments
 (0)