Skip to content

Commit a5259dc

Browse files
fix(v9/svelte): Do not insert preprocess code in script module in Svelte 5 (#17124)
Backport of #17114 --------- Co-authored-by: Richard Jelínek <[email protected]>
1 parent 0329555 commit a5259dc

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
Work in this release was contributed by @richardjelinek-fastest. Thank you for your contribution!
8+
79
## 9.40.0
810

911
### Important Changes

packages/svelte/src/preprocessors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function shouldInjectFunction(
9797
// because the code inside is not executed when the component is instantiated but
9898
// when the module is first imported.
9999
// see: https://svelte.dev/docs#component-format-script-context-module
100-
if (attributes.context === 'module') {
100+
if (attributes.module || attributes.context === 'module') {
101101
return false;
102102
}
103103

packages/svelte/test/preprocessors.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ describe('componentTrackingPreprocessor', () => {
170170

171171
expect(processedComponent.newCode).toEqual(processedComponent.originalCode);
172172
});
173+
174+
it('doesnt inject the function call to a module context script block with Svelte 5 module attribute', () => {
175+
const preProc = componentTrackingPreprocessor();
176+
const component = {
177+
originalCode: 'console.log(cmp2)',
178+
filename: 'lib/Cmp2.svelte',
179+
name: 'Cmp2',
180+
};
181+
182+
const res: any = preProc.script?.({
183+
content: component.originalCode,
184+
filename: component.filename,
185+
attributes: { module: true },
186+
markup: '',
187+
});
188+
189+
const processedComponent = { ...component, newCode: res.code, map: res.map };
190+
191+
expect(processedComponent.newCode).toEqual(processedComponent.originalCode);
192+
});
173193
});
174194

175195
describe('markup hook', () => {

0 commit comments

Comments
 (0)