refactor(config): refactor duplicated default config values (#3504)

* refactor(config): refactor duplicated default configs

* refactor(config): constant CONSTANT_CASE, remove export
This commit is contained in:
Maurer Krisztian 2022-06-03 15:53:38 +02:00 committed by GitHub
parent ae14595407
commit 93d0225ade
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,6 +23,38 @@ interface PostCSSConfigResult {
plugins: Postcss.Plugin[]; plugins: Postcss.Plugin[];
} }
const ASTRO_CONFIG_DEFAULTS: AstroUserConfig & any = {
root: '.',
srcDir: './src',
publicDir: './public',
outDir: './dist',
base: '/',
trailingSlash: 'ignore',
build: { format: 'directory' },
server: {
host: false,
port: 3000,
},
style: { postcss: { options: {}, plugins: [] }},
integrations: [],
markdown: {
drafts: false,
syntaxHighlight: 'shiki',
shikiConfig: {
langs: [],
theme: 'github-dark',
wrap: false,
},
remarkPlugins: [],
rehypePlugins: [],
},
vite: {},
experimental: {
ssr: false,
integrations: false,
},
}
async function resolvePostcssConfig(inlineOptions: any, root: URL): Promise<PostCSSConfigResult> { async function resolvePostcssConfig(inlineOptions: any, root: URL): Promise<PostCSSConfigResult> {
if (isObject(inlineOptions)) { if (isObject(inlineOptions)) {
const options = { ...inlineOptions }; const options = { ...inlineOptions };
@ -65,22 +97,22 @@ export const AstroConfigSchema = z.object({
root: z root: z
.string() .string()
.optional() .optional()
.default('.') .default(ASTRO_CONFIG_DEFAULTS.root)
.transform((val) => new URL(val)), .transform((val) => new URL(val)),
srcDir: z srcDir: z
.string() .string()
.optional() .optional()
.default('./src') .default(ASTRO_CONFIG_DEFAULTS.srcDir)
.transform((val) => new URL(val)), .transform((val) => new URL(val)),
publicDir: z publicDir: z
.string() .string()
.optional() .optional()
.default('./public') .default(ASTRO_CONFIG_DEFAULTS.publicDir)
.transform((val) => new URL(val)), .transform((val) => new URL(val)),
outDir: z outDir: z
.string() .string()
.optional() .optional()
.default('./dist') .default(ASTRO_CONFIG_DEFAULTS.outDir)
.transform((val) => new URL(val)), .transform((val) => new URL(val)),
site: z site: z
.string() .string()
@ -94,18 +126,18 @@ export const AstroConfigSchema = z.object({
base: z base: z
.string() .string()
.optional() .optional()
.default('/') .default(ASTRO_CONFIG_DEFAULTS.base)
.transform((val) => prependForwardSlash(appendForwardSlash(trimSlashes(val)))), .transform((val) => prependForwardSlash(appendForwardSlash(trimSlashes(val)))),
trailingSlash: z trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')]) .union([z.literal('always'), z.literal('never'), z.literal('ignore')])
.optional() .optional()
.default('ignore'), .default(ASTRO_CONFIG_DEFAULTS.trailingSlash),
build: z build: z
.object({ .object({
format: z format: z
.union([z.literal('file'), z.literal('directory')]) .union([z.literal('file'), z.literal('directory')])
.optional() .optional()
.default('directory'), .default(ASTRO_CONFIG_DEFAULTS.build.format),
}) })
.optional() .optional()
.default({}), .default({}),
@ -117,8 +149,8 @@ export const AstroConfigSchema = z.object({
// validate // validate
z z
.object({ .object({
host: z.union([z.string(), z.boolean()]).optional().default(false), host: z.union([z.string(), z.boolean()]).optional().default(ASTRO_CONFIG_DEFAULTS.server.host),
port: z.number().optional().default(3000), port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
}) })
.optional() .optional()
.default({}) .default({})
@ -129,7 +161,7 @@ export const AstroConfigSchema = z.object({
// validate // validate
z z
.array(z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) })) .array(z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) }))
.default([]) .default(ASTRO_CONFIG_DEFAULTS.integrations)
), ),
style: z style: z
.object({ .object({
@ -139,7 +171,7 @@ export const AstroConfigSchema = z.object({
plugins: z.array(z.any()), plugins: z.array(z.any()),
}) })
.optional() .optional()
.default({ options: {}, plugins: [] }), .default(ASTRO_CONFIG_DEFAULTS.style.postcss),
}) })
.optional() .optional()
.default({}), .default({}),
@ -151,15 +183,15 @@ export const AstroConfigSchema = z.object({
drafts: z.boolean().default(false), drafts: z.boolean().default(false),
syntaxHighlight: z syntaxHighlight: z
.union([z.literal('shiki'), z.literal('prism'), z.literal(false)]) .union([z.literal('shiki'), z.literal('prism'), z.literal(false)])
.default('shiki'), .default(ASTRO_CONFIG_DEFAULTS.markdown.syntaxHighlight),
shikiConfig: z shikiConfig: z
.object({ .object({
langs: z.custom<ILanguageRegistration>().array().default([]), langs: z.custom<ILanguageRegistration>().array().default([]),
theme: z theme: z
.enum(BUNDLED_THEMES as [Theme, ...Theme[]]) .enum(BUNDLED_THEMES as [Theme, ...Theme[]])
.or(z.custom<IThemeRegistration>()) .or(z.custom<IThemeRegistration>())
.default('github-dark'), .default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.theme),
wrap: z.boolean().or(z.null()).default(false), wrap: z.boolean().or(z.null()).default(ASTRO_CONFIG_DEFAULTS.markdown.shikiConfig.wrap),
}) })
.default({}), .default({}),
remarkPlugins: z remarkPlugins: z
@ -170,7 +202,7 @@ export const AstroConfigSchema = z.object({
z.tuple([z.custom<RemarkPlugin>((data) => typeof data === 'function'), z.any()]), z.tuple([z.custom<RemarkPlugin>((data) => typeof data === 'function'), z.any()]),
]) ])
.array() .array()
.default([]), .default(ASTRO_CONFIG_DEFAULTS.markdown.remarkPlugins),
rehypePlugins: z rehypePlugins: z
.union([ .union([
z.string(), z.string(),
@ -179,16 +211,16 @@ export const AstroConfigSchema = z.object({
z.tuple([z.custom<RehypePlugin>((data) => typeof data === 'function'), z.any()]), z.tuple([z.custom<RehypePlugin>((data) => typeof data === 'function'), z.any()]),
]) ])
.array() .array()
.default([]), .default(ASTRO_CONFIG_DEFAULTS.markdown.rehypePlugins),
}) })
.default({}), .default({}),
vite: z vite: z
.custom<ViteUserConfig>((data) => data instanceof Object && !Array.isArray(data)) .custom<ViteUserConfig>((data) => data instanceof Object && !Array.isArray(data))
.default({}), .default(ASTRO_CONFIG_DEFAULTS.vite),
experimental: z experimental: z
.object({ .object({
ssr: z.boolean().optional().default(false), ssr: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.ssr),
integrations: z.boolean().optional().default(false), integrations: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.integrations),
}) })
.optional() .optional()
.default({}), .default({}),
@ -254,19 +286,19 @@ export async function validateConfig(
const AstroConfigRelativeSchema = AstroConfigSchema.extend({ const AstroConfigRelativeSchema = AstroConfigSchema.extend({
root: z root: z
.string() .string()
.default('.') .default(ASTRO_CONFIG_DEFAULTS.root)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)), .transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
srcDir: z srcDir: z
.string() .string()
.default('./src') .default(ASTRO_CONFIG_DEFAULTS.srcDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)), .transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
publicDir: z publicDir: z
.string() .string()
.default('./public') .default(ASTRO_CONFIG_DEFAULTS.publicDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)), .transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
outDir: z outDir: z
.string() .string()
.default('./dist') .default(ASTRO_CONFIG_DEFAULTS.outDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)), .transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
server: z.preprocess( server: z.preprocess(
// preprocess // preprocess
@ -275,8 +307,8 @@ export async function validateConfig(
// validate // validate
z z
.object({ .object({
host: z.union([z.string(), z.boolean()]).optional().default(false), host: z.union([z.string(), z.boolean()]).optional().default(ASTRO_CONFIG_DEFAULTS.server.host),
port: z.number().optional().default(3000), port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
}) })
.optional() .optional()
.default({}) .default({})
@ -291,7 +323,7 @@ export async function validateConfig(
plugins: z.array(z.any()), plugins: z.array(z.any()),
}) })
.optional() .optional()
.default({ options: {}, plugins: [] }) .default(ASTRO_CONFIG_DEFAULTS.style.postcss)
), ),
}) })
.optional() .optional()