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 16 commits into
base: main
Choose a base branch
from
Open

refactor: modular plugins #1145

wants to merge 16 commits into from

Conversation

dominikg
Copy link
Member

@dominikg dominikg commented Jun 22, 2025

this grew rather large but sharing for better discussion

goals:

  • split into multiple plugins with clear responsibilities
  • separate plugins for preprocess and compile
  • fully embrace/adopt environment api
  • improve filter use to ensure less work is done in rolldown-vite
  • move plugins out of "pre" phase where possible

Todo:

  • Documentation about the preprocess/compile change and how to use it to transform preprocessor output with a vite plugin before compile
  • deprecate api.sveltePreprocess with link to above docs
  • discuss removing custom query support. lots of code/complication for no usage

@dominikg dominikg marked this pull request as ready for review June 24, 2025 22:50
if (compileData.compiled?.warnings) {
logCompilerWarnings(svelteRequest, compileData.compiled.warnings, options);
}
if (svelteRequest.raw) {
Copy link
Member Author

Choose a reason for hiding this comment

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

this block of code is for an experimental feature returning data for import queries like App.svelte?raw&svelte&type=script, however that feature has remained unused for a long time.

with the split between preprocess & compile it got more complicated to support it so i propose to remove the feature entirely and only keep basic "raw" support from vite that returns the non-transformed file.

const svelteRequest = api.idParser(id, ssr);
if (svelteRequest) {
const { filename, raw, query } = svelteRequest;
if (!query.url && (raw || config.assetsInclude(filename))) {
Copy link
Member Author

Choose a reason for hiding this comment

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

the check for raw here is needed for the custom import query support

map: preprocessed.map,
meta: {
svelte: {
preprocessed
Copy link
Member Author

Choose a reason for hiding this comment

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

attach the preprocess result to module meta so svelte.compile can get the dependencies and sourcemap.

should the dependencies handling be moved into this plugin instead? sourcemap in compile could be inMap then 🤔

if (closeStylePos > -1) {
// inject rule that forces compile to attach scope class to every node in the template
// this reduces the amount of js hot updates when editing css in .svelte files
finalCode = finalCode.slice(0, closeStylePos) + ' *{}' + finalCode.slice(closeStylePos);
Copy link
Member Author

Choose a reason for hiding this comment

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

this used to be a style preprocessor, however that made the preprocess plugin more complicated. Just slicing it in behind the last closing style tag is much less overhead.

It could break in dev if someone were to have a literal close style tag after the actual close style tag. Is that acceptable?

<style>div{color: red}</style>
<div>{'</style>'}</div>

for (let i = 0; i < svelteModules.length; i++) {
const mod = svelteModules[i];
const prev = prevResults[i];
await this.environment.transformRequest(mod.url);
Copy link
Member Author

Choose a reason for hiding this comment

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

transform the module again so we get the output including preprocess plugin and third party plugin changes.

the previous implementation called our compile helper that preprocessed and compiled according to svelte config but that would not account for the new preprocess plugin or third party plugins.

// svelte5 add_location line numbers argument
.replace(/(\$\.add_locations\(.*), \[(\[[.[\], \d]+])]/g, '$1, []')
// vite import analysis timestamp queries
.replace(/[?&]t=\d+/g, '')
Copy link
Member Author

Choose a reason for hiding this comment

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

is \d+ ok or should it be more targeted eg \d{13}[&\b] ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant