Skip to content

Commit bca41f2

Browse files
committed
fix: don't destructure api
1 parent f02a174 commit bca41f2

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

packages/vite-plugin-svelte/src/plugins/load-compiled-css.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const filter = { id: SVELTE_VIRTUAL_STYLE_ID_REGEX };
77
* @param {import('../types/plugin-api.d.ts').PluginAPI} api
88
* @returns {import('vite').Plugin}
99
*/
10-
export function loadCompiledCss({getEnvironmentState}) {
10+
export function loadCompiledCss(api) {
1111
return {
1212
name: 'vite-plugin-svelte:load-compiled-css',
13+
applyToEnvironment(env) {
14+
return env.config.consumer === 'client'; // ssr compile does not emit css
15+
},
1316
resolveId: {
1417
filter, // same filter in load to ensure minimal work
1518
handler(id) {
@@ -20,15 +23,16 @@ export function loadCompiledCss({getEnvironmentState}) {
2023
load: {
2124
filter,
2225
async handler(id) {
23-
const {cache} = getEnvironmentState(this);
26+
const { cache } = api.getEnvironmentState(this);
2427
const cachedCss = cache.getCSS(id);
2528
if (cachedCss) {
2629
const { hasGlobal, ...css } = cachedCss;
2730
if (hasGlobal === false) {
2831
// hasGlobal was added in svelte 5.26.0, so make sure it is boolean false
2932
css.meta ??= {};
3033
css.meta.vite ??= {};
31-
css.meta.vite.cssScopeTo = [id.slice(0,id.lastIndexOf('?')), 'default'];
34+
// TODO is that slice the best way to get the filename without parsing the id?
35+
css.meta.vite.cssScopeTo = [id.slice(0, id.lastIndexOf('?')), 'default'];
3236
}
3337
css.moduleType = 'css';
3438
return css;

packages/vite-plugin-svelte/src/plugins/load-custom.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@ import fs from 'node:fs';
22
import { toRollupError } from '../utils/error.js';
33
import { log } from '../utils/log.js';
44

5-
65
/**
76
* @param {import('../types/plugin-api.d.ts').PluginAPI} api
87
* @returns {import('vite').Plugin}
98
*/
10-
export function loadCustom({idFilter,idParser, options, compileSvelte}) {
11-
9+
export function loadCustom(api) {
1210
/** @type {import('vite').Plugin} */
1311
const plugin = {
1412
name: 'vite-plugin-svelte:load-custom',
1513
configResolved() {
1614
//@ts-expect-error load defined below but filter not in type
17-
plugin.load.filter = idFilter;
15+
plugin.load.filter = api.idFilter;
1816
},
1917

2018
load: {
21-
filter: {id:/^$/}, // set in configResolved
19+
//filter: is set in configResolved
2220
async handler(id) {
2321
const config = this.environment.config;
2422
const ssr = config.consumer === 'server';
25-
const svelteRequest = idParser(id, ssr);
23+
const svelteRequest = api.idParser(id, ssr);
2624
if (svelteRequest) {
2725
const { filename, raw } = svelteRequest;
2826
if (raw) {
29-
const code = await compileRaw(svelteRequest, compileSvelte, options);
27+
const code = await compileRaw(svelteRequest, api.compileSvelte, api.options);
3028
// prevent vite from injecting sourcemaps in the results.
3129
return {
3230
code,
@@ -43,7 +41,7 @@ export function loadCustom({idFilter,idParser, options, compileSvelte}) {
4341
}
4442
}
4543
}
46-
},
44+
}
4745
};
4846
return plugin;
4947
}

packages/vite-plugin-svelte/src/plugins/setup-optimizer.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { safeBase64Hash } from '../utils/hash.js';
88
import { normalize } from '../utils/id.js';
99
import * as vite from 'vite';
1010
// @ts-expect-error not typed on vite
11-
const {rolldownVersion} = vite;
11+
const { rolldownVersion } = vite;
1212

1313
/**
1414
* @typedef {NonNullable<import('vite').DepOptimizationOptions['esbuildOptions']>} EsbuildOptions
@@ -25,14 +25,14 @@ const optimizeSvelteModulePluginName = 'vite-plugin-svelte:optimize-module';
2525
* @param {import('../types/plugin-api.d.ts').PluginAPI} api
2626
* @returns {import('vite').Plugin}
2727
*/
28-
export function setupOptimizer({options}) {
28+
export function setupOptimizer(api) {
2929
/** @type {import('vite').ResolvedConfig} */
3030
let viteConfig;
3131

32-
return {
32+
return {
3333
name: 'vite-plugin-svelte:setup-optimizer',
3434
apply: 'serve',
35-
config(){
35+
config() {
3636
/** @type {import('vite').UserConfig['optimizeDeps']} */
3737
const optimizeDeps = {};
3838
// Add optimizer plugins to prebundle Svelte files.
@@ -54,7 +54,7 @@ export function setupOptimizer({options}) {
5454
]
5555
};
5656
}
57-
return {optimizeDeps};
57+
return { optimizeDeps };
5858
},
5959
configResolved(c) {
6060
viteConfig = c;
@@ -66,30 +66,30 @@ export function setupOptimizer({options}) {
6666
[optimizeSveltePluginName, optimizeSvelteModulePluginName].includes(p.name)
6767
) ?? [];
6868
for (const plugin of plugins) {
69-
patchRolldownOptimizerPlugin(plugin, options);
69+
patchRolldownOptimizerPlugin(plugin, api.options);
7070
}
7171
} else {
72-
const plugins = optimizeDeps.esbuildOptions?.plugins?.filter((p) =>
73-
[optimizeSveltePluginName, optimizeSvelteModulePluginName].includes(p.name)
74-
) ?? [];
72+
const plugins =
73+
optimizeDeps.esbuildOptions?.plugins?.filter((p) =>
74+
[optimizeSveltePluginName, optimizeSvelteModulePluginName].includes(p.name)
75+
) ?? [];
7576
for (const plugin of plugins) {
76-
patchESBuildOptimizerPlugin(plugin, options);
77+
patchESBuildOptimizerPlugin(plugin, api.options);
7778
}
7879
}
7980
},
8081
async buildStart() {
81-
if (!options.prebundleSvelteLibraries) return;
82-
const changed = await svelteMetadataChanged(viteConfig.cacheDir, options);
82+
if (!api.options.prebundleSvelteLibraries) return;
83+
const changed = await svelteMetadataChanged(viteConfig.cacheDir, api.options);
8384
if (changed) {
8485
// Force Vite to optimize again. Although we mutate the config here, it works because
8586
// Vite's optimizer runs after `buildStart()`.
8687
viteConfig.optimizeDeps.force = true;
8788
}
88-
},
89+
}
8990
};
9091
}
9192

92-
9393
/**
9494
* @param {EsbuildPlugin} plugin
9595
* @param {import('../types/options.d.ts').ResolvedOptions} options
@@ -231,9 +231,9 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
231231

232232
const finalCompileOptions = dynamicCompileOptions
233233
? {
234-
...compileOptions,
235-
...dynamicCompileOptions
236-
}
234+
...compileOptions,
235+
...dynamicCompileOptions
236+
}
237237
: compileOptions;
238238
const endStat = statsCollection?.start(filename);
239239
const compiled = svelte.compile(finalCode, finalCompileOptions);
@@ -268,7 +268,6 @@ async function compileSvelteModule(options, { filename, code }, statsCollection)
268268
};
269269
}
270270

271-
272271
// List of options that changes the prebundling result
273272
/** @type {(keyof import('../types/options.d.ts').ResolvedOptions)[]} */
274273
const PREBUNDLE_SENSITIVE_OPTIONS = [
@@ -314,14 +313,14 @@ async function svelteMetadataChanged(cacheDir, options) {
314313
* @param {string} name
315314
* @returns {import('vite').Rollup.Plugin}
316315
*/
317-
function placeholderRolldownOptimizerPlugin(name){
316+
function placeholderRolldownOptimizerPlugin(name) {
318317
return {
319318
name,
320319
options() {},
321320
buildStart() {},
322321
buildEnd() {},
323322
transform: { filter: { id: /^$/ }, handler() {} }
324-
}
323+
};
325324
}
326325

327326
/**
@@ -336,4 +335,3 @@ function generateSvelteMetadata(options) {
336335
}
337336
return metadata;
338337
}
339-

0 commit comments

Comments
 (0)