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

Commit 9e6a22c

Browse files
author
Wenjie Xia
committed
refactor: improve server plugin
1 parent 369cf05 commit 9e6a22c

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

server/app.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export class Appliaction {
3131
#renderCache: Map<string, Map<string, RenderResult>> = new Map()
3232
#postcssPlugins: Record<string, AcceptedPlugin> = {}
3333
#cleanCSS = new CleanCSS({ compatibility: '*' /* Internet Explorer 10+ */ })
34-
#swcReady: Promise<void> | null = null
35-
#postcssReady: Promise<void[]> | null = null
34+
#compilerReady: Promise<void> | boolean = false
35+
#postcssReady: Promise<void[]> | boolean = false
3636
#reloading = false
3737

3838
constructor(workingDir = '.', mode: 'development' | 'production' = 'production', reload = false) {
@@ -52,6 +52,7 @@ export class Appliaction {
5252
plugins: [],
5353
postcss: {
5454
plugins: [
55+
'postcss-nested',
5556
'autoprefixer'
5657
]
5758
}
@@ -145,9 +146,21 @@ export class Appliaction {
145146
return null
146147
}
147148

148-
/** add a new module by given url and source code. */
149-
async addModule(source: { url: string, code: string }): Promise<Module> {
150-
return await this.compile(source.url, { sourceCode: source.code })
149+
/** add a new page module by given path and source code. */
150+
async addPageModule(pathname: string, code: string): Promise<void> {
151+
const url = path.join('/pages/', util.cleanPath(pathname) + '.tsx')
152+
const mod = await this.compile(url, { sourceCode: code })
153+
this.#pageRouting.update(this.getRouteModule(mod))
154+
}
155+
156+
/** add a new page module by given path and source code. */
157+
async removePageModule(pathname: string): Promise<void> {
158+
const url = path.join('/pages/', util.cleanPath(pathname) + '.tsx')
159+
if (this.#modules.has(url)) {
160+
await cleanupCompilation(this.#modules.get(url)!.jsFile)
161+
this.#modules.delete(url)
162+
this.#pageRouting.removeRoute(url)
163+
}
151164
}
152165

153166
createFSWatcher(): EventEmitter {
@@ -368,7 +381,7 @@ export class Appliaction {
368381
for (const name of Array.from(['ts', 'js', 'json']).map(ext => 'aleph.config.' + ext)) {
369382
const p = path.join(this.workingDir, name)
370383
if (existsFileSync(p)) {
371-
log.info('config loaded from', name)
384+
log.info('Aleph server config loaded from', name)
372385
if (name.endsWith('.json')) {
373386
const conf = JSON.parse(await Deno.readTextFile(p))
374387
if (util.isPlainObject(conf)) {
@@ -442,10 +455,9 @@ export class Appliaction {
442455
if (util.isPlainObject(postcss) && util.isArray(postcss.plugins)) {
443456
Object.assign(this.config, { postcss })
444457
} else {
445-
for (const name of Array.from(['ts', 'js', 'mjs', 'json']).map(ext => `postcss.config.${ext}`)) {
458+
for (const name of Array.from(['ts', 'js', 'json']).map(ext => `postcss.config.${ext}`)) {
446459
const p = path.join(this.workingDir, name)
447460
if (existsFileSync(p)) {
448-
log.info(' ✓', name)
449461
if (name.endsWith('.json')) {
450462
const postcss = JSON.parse(await Deno.readTextFile(p))
451463
if (util.isPlainObject(postcss) && util.isArray(postcss.plugins)) {
@@ -582,7 +594,7 @@ export class Appliaction {
582594
const { render } = await import('file://' + this.#modules.get(rendererUrl)!.jsFile)
583595
this.#renderer = { render }
584596

585-
// load plugins
597+
// apply plugins
586598
for (const plugin of this.config.plugins) {
587599
if (plugin.type === 'server') {
588600
await plugin.onInit(this)
@@ -791,7 +803,9 @@ export class Appliaction {
791803

792804
/** preprocess css with postcss plugins */
793805
private async preprocessCSS(sourceCode: string) {
794-
if (this.#postcssReady === null) {
806+
let t: number | null = null
807+
if (this.#postcssReady === false) {
808+
t = performance.now()
795809
this.#postcssReady = Promise.all(this.config.postcss.plugins.map(async p => {
796810
let name: string | null = null
797811
if (util.isNEString(p)) {
@@ -805,7 +819,13 @@ export class Appliaction {
805819
}
806820
}))
807821
}
808-
await this.#postcssReady
822+
if (this.#postcssReady instanceof Promise) {
823+
await this.#postcssReady
824+
this.#postcssReady = true
825+
}
826+
if (t !== null) {
827+
log.debug(`load postcss plugins(${this.config.postcss.plugins.length}) in ${Math.round(performance.now() - t)}ms`)
828+
}
809829
const pcss = (await postcss(this.config.postcss.plugins.map(p => {
810830
if (typeof p === 'string') {
811831
return this.#postcssPlugins[p]
@@ -836,13 +856,16 @@ export class Appliaction {
836856
/** transpile code without types checking. */
837857
private async transpile(sourceCode: string, options: TransformOptions) {
838858
let t: number | null = null
839-
if (this.#swcReady === null) {
859+
if (this.#compilerReady === false) {
840860
t = performance.now()
841-
this.#swcReady = initWasm(this.#denoCacheDir)
861+
this.#compilerReady = initWasm(this.#denoCacheDir)
862+
}
863+
if (this.#compilerReady instanceof Promise) {
864+
await this.#compilerReady
865+
this.#compilerReady = true
842866
}
843-
await this.#swcReady
844-
if (t) {
845-
log.debug('init compiler wasm in ' + Math.round(performance.now() - t) + 'ms')
867+
if (t !== null) {
868+
log.debug(`init compiler wasm in ${Math.round(performance.now() - t)}ms`)
846869
}
847870

848871
return transpileSync(sourceCode, options)

types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ export type Config = {
8484
* An interface that aligns to the parts of the aleph server's `Application`.
8585
*/
8686
interface ServerApplication {
87-
getModule(url: string): Module | null
88-
addModule(source: { url: string, code: string }): Module | Promise<Module>
87+
readonly workingDir: string
88+
readonly mode: 'development' | 'production'
89+
addPageModule(pathname: string, code: string): Promise<void>
90+
removePageModule(pathname: string): Promise<void>
8991
}
9092

9193
/**

0 commit comments

Comments
 (0)