Skip to content

Commit 8c224e2

Browse files
fix(svelte): Do not insert preprocess code in script module in Svelte 5 (#17113)
1 parent 726c868 commit 8c224e2

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,27 @@ describe('componentTrackingPreprocessor', () => {
172172
});
173173
});
174174

175+
it('doesnt inject the function call to a module context script block with Svelte 5 module attribute', () => {
176+
const preProc = componentTrackingPreprocessor();
177+
const component = {
178+
originalCode: 'console.log(cmp2)',
179+
filename: 'lib/Cmp2.svelte',
180+
name: 'Cmp2',
181+
};
182+
183+
const res: any = preProc.script?.({
184+
content: component.originalCode,
185+
filename: component.filename,
186+
attributes: { module: true },
187+
markup: '',
188+
});
189+
190+
const processedComponent = { ...component, newCode: res.code, map: res.map };
191+
192+
expect(processedComponent.newCode).toEqual(processedComponent.originalCode);
193+
});
194+
});
195+
175196
describe('markup hook', () => {
176197
it("adds a <script> tag to components that don't have one", () => {
177198
const preProc = componentTrackingPreprocessor();

0 commit comments

Comments
 (0)