Skip to content

Commit 8289440

Browse files
fix(svelte): Do not insert preprocess code in script module in Svelte 5 (#17114)
Co-authored-by: Lukas Stracke <[email protected]>
1 parent 726c868 commit 8289440

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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)