Skip to content

fix(v9/svelte): Do not insert preprocess code in script module in Svelte 5 #17124

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

Merged
merged 3 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott

Work in this release was contributed by @richardjelinek-fastest. Thank you for your contribution!

## 9.40.0

### Important Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/preprocessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function shouldInjectFunction(
// because the code inside is not executed when the component is instantiated but
// when the module is first imported.
// see: https://svelte.dev/docs#component-format-script-context-module
if (attributes.context === 'module') {
if (attributes.module || attributes.context === 'module') {
return false;
}

Expand Down
20 changes: 20 additions & 0 deletions packages/svelte/test/preprocessors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,26 @@ describe('componentTrackingPreprocessor', () => {

expect(processedComponent.newCode).toEqual(processedComponent.originalCode);
});

it('doesnt inject the function call to a module context script block with Svelte 5 module attribute', () => {
const preProc = componentTrackingPreprocessor();
const component = {
originalCode: 'console.log(cmp2)',
filename: 'lib/Cmp2.svelte',
name: 'Cmp2',
};

const res: any = preProc.script?.({
content: component.originalCode,
filename: component.filename,
attributes: { module: true },
markup: '',
});

const processedComponent = { ...component, newCode: res.code, map: res.map };

expect(processedComponent.newCode).toEqual(processedComponent.originalCode);
});
});

describe('markup hook', () => {
Expand Down
Loading