Skip to content

Commit

Permalink
feat(sedge): initial plugin prototype (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
arexon committed Aug 16, 2022
1 parent 5f0f9bd commit 3a94d04
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 231 deletions.
1 change: 1 addition & 0 deletions packages/sedge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"esbuild": "^0.14.53",
"fast-glob": "^3.2.11",
"fs-extra": "^10.1.0",
"hookable": "^5.1.1",
"jiti": "^1.14.0",
"pathe": "^0.3.3",
"ws": "^8.8.1"
Expand Down
1 change: 1 addition & 0 deletions packages/sedge/src/compiler/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Config {
ignorePaths?: string[]
initialCleanUp: boolean
scriptEntryName: string
plugins?: string[]
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/sedge/src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './config'
export { definePlugin } from './plugin'
export * from './sedge'
30 changes: 30 additions & 0 deletions packages/sedge/src/compiler/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createHooks } from 'hookable'

interface PluginHooks {
/**
* Runs once before the compiler starts.
*/
buildStart?(): Promise<void> | void

/**
* Transforms a compiled file.
* @param path The path of the original file.
* @param content The content of the compiled file.
*/
transformFile?(path: string, content: any): Promise<any> | any
}

export const pluginHooks = createHooks<Required<PluginHooks>>()

export function definePlugin(hooks: PluginHooks) {
if (Object.keys(hooks).length === 0) {
throw new Error('At least one hook is required in `definePlugin`')
}

if (hooks.buildStart) {
pluginHooks.hookOnce('buildStart', hooks.buildStart)
}
if (hooks.transformFile) {
pluginHooks.hook('transformFile', hooks.transformFile)
}
}
32 changes: 29 additions & 3 deletions packages/sedge/src/compiler/sedge.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { hasOwnProperty } from '@antfu/utils'
import { blackBright, blue, magenta, yellow } from 'colorette'
import { join } from 'pathe'
import { blackBright, blue, green, magenta, yellow } from 'colorette'
import { join, resolve } from 'pathe'
import type { TSConfig } from 'pkg-types'
import { logger } from '../logger'
import { loadConfig } from './config'
import { comMojangDir, tempDir } from './constants'
import { prepareDir, writeJsonFile } from './fs'
import { build, dev } from './modes'
import { evalModule } from './module'
import { getComMojangPathByPack } from './path'
import { pluginHooks } from './plugin'

export type SedgeModes = 'build' | 'dev' | 'dev+websocket'

Expand Down Expand Up @@ -80,8 +82,10 @@ async function runWithMode(): Promise<void> {
)}`
)

await prepare()
generateTypes()
await prepare()
await loadPlugins()
await pluginHooks.callHook('buildStart')

switch (sedge.mode) {
case 'build':
Expand All @@ -106,6 +110,28 @@ async function prepare(): Promise<void> {
}
}

async function loadPlugins(): Promise<void> {
const plugins = sedge.config.sedge.plugins

if (!plugins) return

for (const plugin of plugins) {
await evalModule(resolve('plugins', plugin))
}

const results = await Promise.allSettled(
plugins.map(async (plugin) => {
await evalModule(resolve('plugins', plugin))
})
)
const amount = results.filter(
(result) => result.status === 'fulfilled'
).length

if (!amount) return
logger.success(green(`Loaded ${amount} plugin${amount === 1 ? '' : 's'}`))
}

function generateTypes(): void {
const tsConfig: TSConfig = {
compilerOptions: {
Expand Down
Loading

0 comments on commit 3a94d04

Please sign in to comment.