(
inlineConfig: InlineConfig = {},
useLegacyBuilder: null | boolean = false,
)
| 1804 | * @experimental |
| 1805 | */ |
| 1806 | export async function createBuilder( |
| 1807 | inlineConfig: InlineConfig = {}, |
| 1808 | useLegacyBuilder: null | boolean = false, |
| 1809 | ): Promise<ViteBuilder> { |
| 1810 | const patchConfig = (resolved: ResolvedConfig) => { |
| 1811 | if (!(useLegacyBuilder ?? !resolved.builder)) return |
| 1812 | |
| 1813 | // Until the ecosystem updates to use `environment.config.build` instead of `config.build`, |
| 1814 | // we need to make override `config.build` for the current environment. |
| 1815 | // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later |
| 1816 | // remove the default values that shouldn't be used at all once the config is resolved |
| 1817 | const environmentName = resolved.build.ssr ? 'ssr' : 'client' |
| 1818 | ;(resolved.build as ResolvedBuildOptions) = { |
| 1819 | ...resolved.environments[environmentName].build, |
| 1820 | } |
| 1821 | } |
| 1822 | const config = await resolveConfigToBuild(inlineConfig, patchConfig) |
| 1823 | useLegacyBuilder ??= !config.builder |
| 1824 | const configBuilder = config.builder ?? resolveBuilderOptions({})! |
| 1825 | |
| 1826 | const environments: Record<string, BuildEnvironment> = {} |
| 1827 | |
| 1828 | const builder: ViteBuilder = { |
| 1829 | environments, |
| 1830 | config, |
| 1831 | async buildApp() { |
| 1832 | const pluginContext = new BasicMinimalPluginContext( |
| 1833 | { ...basePluginContextMeta, watchMode: false }, |
| 1834 | config.logger, |
| 1835 | ) |
| 1836 | |
| 1837 | // order 'pre' and 'normal' hooks are run first, then config.builder.buildApp, then 'post' hooks |
| 1838 | let configBuilderBuildAppCalled = false |
| 1839 | for (const p of config.getSortedPlugins('buildApp')) { |
| 1840 | const hook = p.buildApp |
| 1841 | if ( |
| 1842 | !configBuilderBuildAppCalled && |
| 1843 | typeof hook === 'object' && |
| 1844 | hook.order === 'post' |
| 1845 | ) { |
| 1846 | configBuilderBuildAppCalled = true |
| 1847 | await configBuilder.buildApp(builder) |
| 1848 | } |
| 1849 | const handler = getHookHandler(hook) |
| 1850 | await handler.call(pluginContext, builder) |
| 1851 | } |
| 1852 | if (!configBuilderBuildAppCalled) { |
| 1853 | await configBuilder.buildApp(builder) |
| 1854 | } |
| 1855 | // fallback to building all environments if no environments have been built |
| 1856 | if ( |
| 1857 | Object.values(builder.environments).every( |
| 1858 | (environment) => !environment.isBuilt, |
| 1859 | ) |
| 1860 | ) { |
| 1861 | for (const environment of Object.values(builder.environments)) { |
| 1862 | await builder.build(environment) |
| 1863 | } |
no test coverage detected