-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: load plugins via config file
- Loading branch information
Showing
11 changed files
with
155 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,30 @@ | ||
// @ts-check | ||
|
||
import type { Plugin, PluginContext, PluginEntry } from '../dist/index.d.ts' | ||
import type { Plugin, PluginContext } from '../dist/index.d.ts' | ||
|
||
export function demoPlugin(): PluginEntry<Data> { | ||
return { | ||
name: 'demo', | ||
entry: import.meta.url, | ||
data: { count: 10 }, | ||
} | ||
} | ||
let context: PluginContext | ||
|
||
export interface Data { | ||
count: number | ||
} | ||
export function demoPlugin(): Plugin { | ||
return { | ||
name: 'demo-plugin', | ||
buildStart(_context) { | ||
context = _context | ||
context.log('hello world') | ||
}, | ||
async resolveId(source, importer, options) { | ||
if (source.startsWith('node:')) return | ||
|
||
let context: PluginContext<Data> | ||
if (source === 'virtual-mod') { | ||
return 'file:///virtual-mod.ts' | ||
} | ||
|
||
const plugin: Plugin<Data> = { | ||
buildStart(_context) { | ||
context = _context | ||
context.log(`count is ${context.data.count}`) | ||
}, | ||
async resolveId(source, importer, options) { | ||
if (source === 'virtual-mod') { | ||
return 'file:///virtual-mod.ts' | ||
} | ||
const result = await this.resolve(`${source}.ts`, importer, options) | ||
if (result) return result | ||
}, | ||
load(id) { | ||
if (id === 'file:///virtual-mod.ts') { | ||
return { code: 'export const count = 42' } | ||
} | ||
}, | ||
const result = await this.resolve(`${source}.ts`, importer, options) | ||
if (result) return result | ||
}, | ||
load(id) { | ||
if (id === 'file:///virtual-mod.ts') { | ||
return { code: 'export const count = 42' } | ||
} | ||
}, | ||
} | ||
} | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export default plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,9 @@ | ||
import module from 'node:module' | ||
import process from 'node:process' | ||
import type { Data } from './loader/index.ts' | ||
import type { MessageLog } from './loader/rpc.ts' | ||
import type { PluginEntry } from './plugin.ts' | ||
import type { UnloaderConfig } from './loader/config.ts' | ||
|
||
export * from './plugin.ts' | ||
export * from './register.ts' | ||
export * from './loader/config.ts' | ||
|
||
export function register(plugins: PluginEntry[] = []): void { | ||
if (!module.register) { | ||
throw new Error( | ||
`This version of Node.js (${process.version}) does not support module.register(). Please upgrade to Node v18.19 or v20.6 and above.`, | ||
) | ||
} | ||
|
||
const { port1, port2 } = new MessageChannel() | ||
const data: Data = { | ||
port: port2, | ||
plugins: Object.create(null), | ||
} | ||
const transferList = [port2] | ||
|
||
for (const plugin of plugins) { | ||
data.plugins[plugin.name] = { | ||
entry: plugin.entry, | ||
data: plugin.data, | ||
} | ||
transferList.push(...(plugin.transferList || [])) | ||
} | ||
|
||
module.register('./loader/index.js', { | ||
parentURL: import.meta.url, | ||
data, | ||
transferList, | ||
}) | ||
|
||
port1.on('message', (message: MessageLog) => { | ||
switch (message.type) { | ||
case 'log': | ||
console.info('[port log]', message.message) | ||
} | ||
}) | ||
port1.unref() | ||
export function defineConfig(config: UnloaderConfig): UnloaderConfig { | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import process from 'node:process' | ||
import { loadConfig as unconfig } from 'unconfig' | ||
import type { Plugin } from '../plugin.ts' | ||
|
||
export interface UnloaderConfig { | ||
plugins: Plugin[] | ||
} | ||
|
||
export async function loadConfig(): Promise<UnloaderConfig> { | ||
const { config } = await unconfig<UnloaderConfig>({ | ||
sources: [ | ||
{ | ||
files: 'unloader.config', | ||
extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''], | ||
}, | ||
], | ||
cwd: process.cwd(), | ||
defaults: { plugins: [] }, | ||
}) | ||
|
||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.