Skip to content

refactor: modular plugins #1145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/shiny-hats-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/vite-plugin-svelte-inspector': patch
'@sveltejs/vite-plugin-svelte': patch
---

use vite environment api internally
5 changes: 5 additions & 0 deletions .changeset/thirty-roses-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': major
---

split preprocess and compile into separate plugins
8 changes: 2 additions & 6 deletions packages/e2e-tests/autoprefixer-browerslist/src/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import App from './App.svelte';

if (App.toString().startsWith('class ')) {
new App({ target: document.body });
} else {
import('svelte').then(({ mount }) => mount(App, { target: document.body }));
}
import { mount } from 'svelte';
mount(App, { target: document.body });
2 changes: 2 additions & 0 deletions packages/e2e-tests/configfile-custom/svelte.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('default svelte config loaded');
export default {};
79 changes: 47 additions & 32 deletions packages/vite-plugin-svelte-inspector/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function svelteInspector(options) {
apply: 'serve',
enforce: 'pre',

applyToEnvironment(env) {
return env.config.consumer === 'client';
},

configResolved(config) {
viteConfig = config;
const environmentOptions = parseEnvironmentOptions(config);
Expand All @@ -43,7 +47,7 @@ export function svelteInspector(options) {
}

// Handle config from svelte.config.js through vite-plugin-svelte
const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte');
const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte:config');
const configFileOptions = vps?.api?.options?.inspector;

// vite-plugin-svelte can only pass options through it's `api` instead of `options`.
Expand All @@ -69,42 +73,53 @@ export function svelteInspector(options) {
base: config.base?.replace(/\/$/, '') || ''
};
},

async resolveId(importee, _, options) {
if (options?.ssr || disabled) {
return;
}
if (importee.startsWith('virtual:svelte-inspector-options')) {
return importee;
} else if (importee.startsWith('virtual:svelte-inspector-path:')) {
return importee.replace('virtual:svelte-inspector-path:', inspectorPath);
resolveId: {
filter: {
id: /^virtual:svelte-inspector-/
},
async handler(importee, _, options) {
if (options?.ssr || disabled) {
Copy link
Member

@eltigerchino eltigerchino Jun 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the options?.ssr check still needed if this plugin only applies to the client environment? (my understanding of line 37)

Suggested change
if (options?.ssr || disabled) {
if (disabled) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, the 2 unused arguments can be removed as well then

return;
}
if (importee.startsWith('virtual:svelte-inspector-options')) {
return importee;
} else if (importee.startsWith('virtual:svelte-inspector-path:')) {
return importee.replace('virtual:svelte-inspector-path:', inspectorPath);
}
}
},

async load(id, options) {
if (options?.ssr || disabled) {
return;
}
if (id === 'virtual:svelte-inspector-options') {
return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
} else if (id.startsWith(inspectorPath)) {
// read file ourselves to avoid getting shut out by vites fs.allow check
const file = cleanUrl(id);
if (fs.existsSync(id)) {
return await fs.promises.readFile(file, 'utf-8');
} else {
viteConfig.logger.error(
`[vite-plugin-svelte-inspector] failed to find svelte-inspector: ${id}`
);
load: {
filter: {
id: {
include: [`${inspectorPath}/**`, /^virtual:svelte-inspector-options$/],
exclude: [/style&lang\.css$/]
}
},
async handler(id) {
if (disabled) {
return;
}
if (id === 'virtual:svelte-inspector-options') {
return `export default ${JSON.stringify(inspectorOptions ?? {})}`;
} else if (id.startsWith(inspectorPath)) {
// read file ourselves to avoid getting shut out by vites fs.allow check
const file = cleanUrl(id);
if (fs.existsSync(id)) {
return await fs.promises.readFile(file, 'utf-8');
} else {
viteConfig.logger.error(
`[vite-plugin-svelte-inspector] failed to find svelte-inspector: ${id}`
);
}
}
}
},

transform(code, id, options) {
if (options?.ssr || disabled) {
return;
}
if (id.includes('vite/dist/client/client.mjs')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to keep this check for backwards compatibility in the case where the filter key isn't used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the peer dependency range only includes vite versions that support filter

transform: {
filter: { id: /vite\/dist\/client\/client\.mjs(?:\?|$)/ },
handler(code) {
if (disabled) {
return;
}
return { code: `${code}\nimport('virtual:svelte-inspector-path:load-inspector.js')` };
}
}
Expand Down
145 changes: 0 additions & 145 deletions packages/vite-plugin-svelte/src/handle-hot-update.js

This file was deleted.

Loading