-
-
Notifications
You must be signed in to change notification settings - Fork 425
/
transform.ts
34 lines (31 loc) · 992 Bytes
/
transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { expandState } from './state'
import { loadConfig } from './config'
import { resolvePlugin, getPlugins } from './plugins'
import type { Config } from './config'
import type { State } from './state'
const run = (code: string, config: Config, state: Partial<State>): string => {
const expandedState = expandState(state)
const plugins = getPlugins(config, state).map(resolvePlugin)
let nextCode = String(code).replace('\0', '')
// eslint-disable-next-line no-restricted-syntax
for (const plugin of plugins) {
nextCode = plugin(nextCode, config, expandedState)
}
return nextCode
}
export const transform = async (
code: string,
config: Config = {},
state: Partial<State> = {},
): Promise<string> => {
config = await loadConfig(config, state)
return run(code, config, state)
}
transform.sync = (
code: string,
config: Config = {},
state: Partial<State> = {},
): string => {
config = loadConfig.sync(config, state)
return run(code, config, state)
}