Skip to content

Commit 7cdc1df

Browse files
committed
wip: first steps
1 parent 8d7d51d commit 7cdc1df

File tree

11 files changed

+184
-23
lines changed

11 files changed

+184
-23
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { config } from './plugins/config.js';
2+
import { preprocess } from './plugins/preprocess.js';
3+
import { compile } from './plugins/compile.js';
4+
import { externalCss } from './plugins/external-css.js';
5+
import { optimize } from './plugins/optimize.js';
6+
import { optimizeModule } from './plugins/optimize-module.js';
7+
import { compileModule } from './plugins/compile-module.js';
8+
import { svelteInspector } from '@sveltejs/vite-plugin-svelte-inspector';
9+
/**
10+
* returns a list of plugins to handle svelte files
11+
* plugins are named `vite-plugin-svelte:<task>`
12+
*
13+
* @param {Partial<import('./public.d.ts').Options>} [inlineOptions]
14+
* @returns {import('vite').Plugin[]}
15+
*/
16+
export function svelte(inlineOptions) {
17+
return [
18+
config(inlineOptions), // parse config and put it on api.__internal for the other plugins to use
19+
optimize(), // create optimize plugin
20+
preprocess(), // preprocess .svelte files
21+
compile(), // compile .svelte files
22+
externalCss(), // return vitrual css modules created by compile
23+
optimizeModule(), // create optimize module plugin
24+
compileModule(),// compile module
25+
svelteInspector()
26+
];
27+
}
28+
29+
export { vitePreprocess } from './preprocess.js';
30+
export { loadSvelteConfig } from './utils/load-svelte-config.js';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @returns {import('vite').Plugin}
3+
*/
4+
export function compileModule() {
5+
return {
6+
name: 'vite-plugin-svelte:compile-module'
7+
};
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @returns {import('vite').Plugin}
3+
*/
4+
export function compile() {
5+
return {
6+
name: 'vite-plugin-svelte:compile'
7+
};
8+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @returns {import('vite').Plugin}
3+
*/
4+
export function externalCss() {
5+
return {
6+
name: 'vite-plugin-svelte:externalCss'
7+
};
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @returns {import('vite').Plugin}
3+
*/
4+
export function optimizeModule() {
5+
return {
6+
name: 'vite-plugin-svelte:optimize-module'
7+
};
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @returns {import('vite').Plugin}
3+
*/
4+
export function optimize() {
5+
return {
6+
name: 'vite-plugin-svelte:optimize'
7+
};
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @returns {import('vite').Plugin}
3+
*/
4+
export function preprocess() {
5+
return {
6+
name: 'vite-plugin-svelte:preprocess'
7+
};
8+
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type { ResolvedOptions } from './options.d.ts';
2+
import { VitePluginSvelteCache } from '../utils/vite-plugin-svelte-cache.js';
3+
import { VitePluginSvelteStats } from '../utils/vite-plugin-svelte-stats.js';
24

35
export interface PluginAPI {
46
/**
5-
* must not be modified, should not be used outside of vite-plugin-svelte repo
7+
* must not be used by plugins outside of the vite-plugin-svelte monorepo
8+
* this is not part of our public semver contract, breaking changes to it can and will happen in patch releases
69
* @internal
7-
* @experimental
810
*/
9-
options?: ResolvedOptions;
10-
// TODO expose compile cache here so other utility plugins can use it
11+
__internal: {
12+
options: ResolvedOptions;
13+
cache?: VitePluginSvelteCache;
14+
stats?: VitePluginSvelteStats;
15+
};
1116
}

packages/vite-plugin-svelte/src/utils/options.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,9 @@ function mergeConfigs(...configs) {
198198
*
199199
* @param {import('../types/options.d.ts').PreResolvedOptions} preResolveOptions
200200
* @param {import('vite').ResolvedConfig} viteConfig
201-
* @param {import('./vite-plugin-svelte-cache.js').VitePluginSvelteCache} cache
202201
* @returns {import('../types/options.d.ts').ResolvedOptions}
203202
*/
204-
export function resolveOptions(preResolveOptions, viteConfig, cache) {
203+
export function resolveOptions(preResolveOptions, viteConfig) {
205204
const css = preResolveOptions.emitCss ? 'external' : 'injected';
206205
/** @type {Partial<import('../public.d.ts').Options>} */
207206
const defaultOptions = {
@@ -230,10 +229,7 @@ export function resolveOptions(preResolveOptions, viteConfig, cache) {
230229
addExtraPreprocessors(merged, viteConfig);
231230
enforceOptionsForHmr(merged, viteConfig);
232231
enforceOptionsForProduction(merged);
233-
// mergeConfigs would mangle functions on the stats class, so do this afterwards
234-
if (log.debug.enabled && isDebugNamespaceEnabled('stats')) {
235-
merged.stats = new VitePluginSvelteStats(cache);
236-
}
232+
237233
return merged;
238234
}
239235

0 commit comments

Comments
 (0)