|
| 1 | +import process from 'node:process'; |
| 2 | +import { log } from '../utils/log.js'; |
| 3 | + |
| 4 | +import { |
| 5 | + buildExtraViteConfig, |
| 6 | + validateInlineOptions, |
| 7 | + resolveOptions, |
| 8 | + preResolveOptions, |
| 9 | + ensureConfigEnvironmentMainFields, |
| 10 | + ensureConfigEnvironmentConditions |
| 11 | +} from '../utils/options.js'; |
| 12 | +import { setupWatchers } from '../utils/watch.js'; |
| 13 | + |
| 14 | +import * as vite from 'vite'; |
| 15 | +// @ts-expect-error rolldownVersion |
| 16 | +const { version: viteVersion, rolldownVersion } = vite; |
| 17 | + |
| 18 | +/** @typedef {import('../types/plugin-api.d.ts').PluginAPI} PluginAPI */ |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {Partial<import('../public.d.ts').Options>} [inlineOptions] |
| 22 | + * @returns {import('vite').Plugin<PluginAPI>} |
| 23 | + */ |
| 24 | +export function config(inlineOptions) { |
| 25 | + if (process.env.DEBUG != null) { |
| 26 | + log.setLevel('debug'); |
| 27 | + } |
| 28 | + if (rolldownVersion) { |
| 29 | + log.warn.once( |
| 30 | + `!!! Support for rolldown-vite in vite-plugin-svelte is experimental (rolldown: ${rolldownVersion}, vite: ${viteVersion}) !!!` |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + validateInlineOptions(inlineOptions); |
| 35 | + |
| 36 | + /** @type {PluginAPI} */ |
| 37 | + const api = { |
| 38 | + __internal: { |
| 39 | + // @ts-expect-error set in config hook |
| 40 | + options: {} |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + /** @type {import('vite').Plugin<PluginAPI>} */ |
| 45 | + return { |
| 46 | + name: 'vite-plugin-svelte:config', |
| 47 | + // make sure it runs first |
| 48 | + enforce: 'pre', |
| 49 | + api, |
| 50 | + async config(config, configEnv) { |
| 51 | + // setup logger |
| 52 | + if (process.env.DEBUG) { |
| 53 | + log.setLevel('debug'); |
| 54 | + } else if (config.logLevel) { |
| 55 | + log.setLevel(config.logLevel); |
| 56 | + } |
| 57 | + |
| 58 | + const options = await preResolveOptions(inlineOptions, config, configEnv); |
| 59 | + // @ts-expect-error temporarily lend the options variable until fixed in configResolved |
| 60 | + api.__internal.options = options; |
| 61 | + // extra vite config |
| 62 | + const extraViteConfig = await buildExtraViteConfig(options, config); |
| 63 | + log.debug('additional vite config', extraViteConfig, 'config'); |
| 64 | + return extraViteConfig; |
| 65 | + }, |
| 66 | + |
| 67 | + configResolved: { |
| 68 | + order: 'pre', // we assign internal api here, make sure it really is first before our other plugins |
| 69 | + handler(config) { |
| 70 | + const options = resolveOptions(api.__internal.options, config); |
| 71 | + api.__internal.options = options; |
| 72 | + log.debug('resolved options', options, 'config'); |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + configEnvironment(name, config, opts) { |
| 77 | + ensureConfigEnvironmentMainFields(name, config, opts); |
| 78 | + // @ts-expect-error the function above should make `resolve.mainFields` non-nullable |
| 79 | + config.resolve.mainFields.unshift('svelte'); |
| 80 | + |
| 81 | + ensureConfigEnvironmentConditions(name, config, opts); |
| 82 | + // @ts-expect-error the function above should make `resolve.conditions` non-nullable |
| 83 | + config.resolve.conditions.push('svelte'); |
| 84 | + }, |
| 85 | + |
| 86 | + configureServer(server) { |
| 87 | + const { options, cache } = api.__internal; |
| 88 | + options.server = server; |
| 89 | + setupWatchers(options, cache, requestParser); |
| 90 | + } |
| 91 | + }; |
| 92 | +} |
0 commit comments