-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
172 additions
and
104 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
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 was deleted.
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
loadConfig as loadC12Config, | ||
watchConfig as watchC12Config, | ||
} from 'c12'; | ||
import { CONFIG_FILE } from '../constants'; | ||
import type { Config } from './types'; | ||
import { ConfigSchema } from './types'; | ||
|
||
interface ConfigOpts { | ||
cwd: string; | ||
defaults?: Partial<Config>; | ||
overrides?: Partial<Config>; | ||
} | ||
|
||
export async function loadConfig(opts: ConfigOpts): Promise<Config> { | ||
const { config: rawConfig } = await loadC12Config(createLoadConfigOpts(opts)); | ||
const result = ConfigSchema.safeParse(rawConfig); | ||
if (!result.success) { | ||
throw new Error(`Invalid configuration: ${result.error.message}`); | ||
} | ||
return result.data; | ||
} | ||
|
||
export function watchConfig(opts: ConfigOpts) { | ||
return watchC12Config({ | ||
...createLoadConfigOpts(opts), | ||
onWatch(event) { | ||
console.log(event); | ||
}, | ||
onUpdate({ oldConfig, newConfig, getDiff }) { | ||
const result = ConfigSchema.safeParse(newConfig); | ||
if (!result.success) { | ||
console.error(`Invalid configuration: ${result.error.message}`); | ||
return; | ||
} | ||
console.log('onUpdate', oldConfig, result.data, getDiff()); | ||
}, | ||
}); | ||
} | ||
|
||
function createLoadConfigOpts({ cwd, defaults, overrides }: ConfigOpts) { | ||
return { | ||
cwd, | ||
configFile: CONFIG_FILE, | ||
rcFile: false as const, | ||
globalRc: false as const, | ||
defaults, | ||
overrides, | ||
}; | ||
} |
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,50 @@ | ||
import { z } from 'zod'; | ||
import { PluginSchema } from '../plugin/types'; | ||
|
||
export const ConfigSchema = z.object({ | ||
externals: z.record(z.string()).optional(), | ||
devServer: z | ||
.object({ | ||
port: z.number().optional(), | ||
https: z | ||
.object({ | ||
hosts: z.array(z.string()).optional(), | ||
}) | ||
.optional(), | ||
ip: z.string().optional(), | ||
host: z.string().optional(), | ||
}) | ||
.optional(), | ||
less: z | ||
.object({ | ||
modifyVars: z.any().optional(), | ||
globalVars: z.any().optional(), | ||
math: z.any().optional(), | ||
sourceMap: z.any(), | ||
plugins: z.array(z.any()).optional(), | ||
}) | ||
.optional(), | ||
plugins: z.array(PluginSchema).optional(), | ||
router: z | ||
.object({ | ||
defaultPreload: z.enum(['intent', 'render', 'viewport']).optional(), | ||
defaultPreloadDelay: z.number().optional(), | ||
devtool: z | ||
.union([ | ||
z.object({ | ||
options: z.object({ | ||
initialIsOpen: z.boolean().optional(), | ||
position: z | ||
.enum(['bottom-left', 'bottom-right', 'top-left', 'top-right']) | ||
.optional(), | ||
}), | ||
}), | ||
z.literal(false), | ||
]) | ||
.optional(), | ||
}) | ||
.optional(), | ||
tailwindcss: z.boolean().optional(), | ||
}); | ||
|
||
export type Config = z.infer<typeof ConfigSchema>; |
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
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,67 @@ | ||
import type { Plugin } from './plugin/types'; | ||
|
||
type PluginKey = keyof Plugin; | ||
|
||
enum HookType { | ||
First = 'first', | ||
Series = 'series', | ||
Parallel = 'parallel', | ||
} | ||
|
||
export class PluginManager { | ||
#plugins: Plugin[] = []; | ||
constructor(rawPlugins: Plugin[]) { | ||
this.#plugins = [ | ||
...rawPlugins.filter((p) => p.enforce === 'pre'), | ||
...rawPlugins.filter((p) => !p.enforce), | ||
...rawPlugins.filter((p) => p.enforce === 'post'), | ||
]; | ||
} | ||
|
||
async apply({ | ||
hook, | ||
args, | ||
type = HookType.Series, | ||
}: { | ||
hook: PluginKey; | ||
args: any; | ||
type: HookType; | ||
}) { | ||
const plugins = this.#plugins.filter((p) => !!p[hook]); | ||
|
||
if (type === HookType.First) { | ||
for (const plugin of plugins) { | ||
const hookFn = plugin[hook]; | ||
if (typeof hookFn === 'function') { | ||
const result = await hookFn(args); | ||
if (result != null) { | ||
return result; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
if (type === HookType.Parallel) { | ||
const results = await Promise.all( | ||
plugins.map((p) => { | ||
const hookFn = p[hook]; | ||
if (typeof hookFn === 'function') { | ||
return hookFn(args); | ||
} | ||
return null; | ||
}), | ||
); | ||
return results.filter((r) => r != null); | ||
} | ||
|
||
let result = args; | ||
for (const plugin of plugins) { | ||
const hookFn = plugin[hook]; | ||
if (typeof hookFn === 'function') { | ||
result = await hookFn(result); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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