-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next): make defineConfig generic (#12243)
Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
166ea96
commit eb41d13
Showing
10 changed files
with
125 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Improves `defineConfig` type safety. TypeScript will now error if a group of related configuration options do not have consistent types. For example, you will now see an error if your language set for `i18n.defaultLocale` is not one of the supported locales specified in `i18n.locales`. |
This file was deleted.
Oops, something went wrong.
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
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,30 @@ | ||
// IMPORTANT: this file is the entrypoint for "astro/config". Keep it as light as possible! | ||
|
||
import type { SharpImageServiceConfig } from '../assets/services/sharp.js'; | ||
import type { ImageServiceConfig } from '../types/public/index.js'; | ||
|
||
export { defineConfig, getViteConfig } from './index.js'; | ||
export { envField } from '../env/config.js'; | ||
|
||
/** | ||
* Return the configuration needed to use the Sharp-based image service | ||
*/ | ||
export function sharpImageService(config: SharpImageServiceConfig = {}): ImageServiceConfig { | ||
return { | ||
entrypoint: 'astro/assets/services/sharp', | ||
config, | ||
}; | ||
} | ||
|
||
/** | ||
* Return the configuration needed to use the passthrough image service. This image services does not perform | ||
* any image transformations, and is mainly useful when your platform does not support other image services, or you are | ||
* not using Astro's built-in image processing. | ||
* See: https://docs.astro.build/en/guides/images/#configure-no-op-passthrough-service | ||
*/ | ||
export function passthroughImageService(): ImageServiceConfig { | ||
return { | ||
entrypoint: 'astro/assets/services/noop', | ||
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
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,41 @@ | ||
import { describe, it } from 'node:test'; | ||
import { defineConfig } from '../../dist/config/index.js'; | ||
import type { AstroUserConfig } from '../../dist/types/public/index.js'; | ||
import { expectTypeOf } from 'expect-type'; | ||
|
||
describe('defineConfig()', () => { | ||
it('Infers generics correctly', () => { | ||
const config_0 = defineConfig({}); | ||
expectTypeOf(config_0).toEqualTypeOf<AstroUserConfig<never>>(); | ||
expectTypeOf(config_0.i18n!.defaultLocale).toEqualTypeOf<string>(); | ||
|
||
const config_1 = defineConfig({ | ||
i18n: { | ||
locales: ['en'], | ||
defaultLocale: 'en', | ||
}, | ||
}); | ||
expectTypeOf(config_1).toEqualTypeOf<AstroUserConfig<['en']>>(); | ||
expectTypeOf(config_1.i18n!.defaultLocale).toEqualTypeOf<'en'>(); | ||
|
||
const config_2 = defineConfig({ | ||
i18n: { | ||
locales: ['en', 'fr'], | ||
defaultLocale: 'fr', | ||
}, | ||
}); | ||
expectTypeOf(config_2).toEqualTypeOf<AstroUserConfig<['en', 'fr']>>(); | ||
expectTypeOf(config_2.i18n!.defaultLocale).toEqualTypeOf<'en' | 'fr'>(); | ||
|
||
const config_3 = defineConfig({ | ||
i18n: { | ||
locales: ['en', { path: 'french', codes: ['fr', 'fr-FR'] }], | ||
defaultLocale: 'en', | ||
}, | ||
}); | ||
expectTypeOf(config_3).toEqualTypeOf< | ||
AstroUserConfig<['en', { readonly path: 'french'; readonly codes: ['fr', 'fr-FR'] }]> | ||
>(); | ||
expectTypeOf(config_3.i18n!.defaultLocale).toEqualTypeOf<'en' | 'fr' | 'fr-FR'>(); | ||
}); | ||
}); |