-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: main
Are you sure you want to change the base?
Changes from 13 commits
7cdc1df
f02a174
bca41f2
94ac814
01061c9
dc0019c
fd8c291
04904c2
04531a9
7150c4c
210ff7c
40b8dab
d700ff3
dfbc8e4
a4553e1
e0fe69b
e8089d3
25ce39c
941db5b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 |
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 }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
console.log('default svelte config loaded'); | ||
export default {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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`. | ||
|
@@ -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) { | ||
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')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')` }; | ||
} | ||
} | ||
|
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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