Skip to content

Commit f6d7007

Browse files
dominikgrixo
andauthored
fix: update svelte-hmr and enable partial accept (#440)
* fix: update svelte-hmr and enable partial accept to allow context=module updates, see issue #134 * test: harden partialAccept test (#441) * refactor: improve test logic to avoid jest syntax from hell Co-authored-by: rixo <[email protected]>
1 parent 9a7115f commit f6d7007

File tree

12 files changed

+84
-9
lines changed

12 files changed

+84
-9
lines changed

.changeset/little-ligers-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
update svelte-hmr and enable partial hmr accept by default (fixes #134)

packages/e2e-tests/hmr/__tests__/hmr.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getEl,
55
getText,
66
editFileAndWaitForHmrComplete,
7+
hmrCount,
78
untilMatches,
89
sleep,
910
getColor,
@@ -54,6 +55,10 @@ test('should respect transforms', async () => {
5455
if (!isBuild) {
5556
describe('hmr', () => {
5657
const updateHmrTest = editFileAndWaitForHmrComplete.bind(null, 'src/components/HmrTest.svelte');
58+
const updateModuleContext = editFileAndWaitForHmrComplete.bind(
59+
null,
60+
'src/components/partial-hmr/ModuleContext.svelte'
61+
);
5762
const updateApp = editFileAndWaitForHmrComplete.bind(null, 'src/App.svelte');
5863
const updateStore = editFileAndWaitForHmrComplete.bind(null, 'src/stores/hmr-stores.js');
5964

@@ -136,6 +141,18 @@ if (!isBuild) {
136141
expect(await getText(`#hmr-test-3 .counter`)).toBe('0');
137142
});
138143

144+
test('should work when editing script context="module"', async () => {
145+
expect(await getText(`#hmr-with-context`)).toContain('x=0 y=1 slot=1');
146+
expect(await getText(`#hmr-without-context`)).toContain('x=0 y=1 slot=');
147+
expect(hmrCount('UsingNamed.svelte'), 'updates for UsingNamed.svelte').toBe(0);
148+
expect(hmrCount('UsingDefault.svelte'), 'updates for UsingDefault.svelte').toBe(0);
149+
await updateModuleContext((content) => content.replace('y = 1', 'y = 2'));
150+
expect(await getText(`#hmr-with-context`)).toContain('x=0 y=2 slot=2');
151+
expect(await getText(`#hmr-without-context`)).toContain('x=0 y=2 slot=');
152+
expect(hmrCount('UsingNamed.svelte'), 'updates for UsingNamed.svelte').toBe(1);
153+
expect(hmrCount('UsingDefault.svelte'), 'updates for UsingDefault.svelte').toBe(0);
154+
});
155+
139156
test('should work with emitCss: false in vite config', async () => {
140157
await editViteConfig((c) => c.replace('svelte()', 'svelte({emitCss:false})'));
141158
expect(await getText(`#hmr-test-1 .counter`)).toBe('0');

packages/e2e-tests/hmr/src/App.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import StaticImport from './components/StaticImport.svelte';
33
import Dependency from 'e2e-test-dep-svelte-simple';
44
import HmrTest from './components/HmrTest.svelte';
5+
import PartialHmr from './components/partial-hmr/PartialHmr.svelte';
56
const jsTransform = '__JS_TRANSFORM_1__';
67
let dynamicImportComponent;
78
function importDynamic() {
@@ -25,6 +26,9 @@
2526
<HmrTest id="hmr-test-2" />
2627

2728
<!-- HMR-TEMPLATE-INJECT -->
29+
30+
<PartialHmr />
31+
2832
<style>
2933
h1 {
3034
color: #111111;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script context="module">
2+
export let y = 1;
3+
</script>
4+
5+
<script>
6+
export let id;
7+
let x = 0;
8+
</script>
9+
10+
<pre {id}>
11+
x={x} y={y} slot=<slot />
12+
</pre>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import UsingNamed from './UsingNamed.svelte';
3+
import UsingOnlyDefault from './UsingOnlyDefault.svelte';
4+
</script>
5+
6+
<UsingNamed />
7+
<UsingOnlyDefault />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import ModuleContext, { y } from './ModuleContext.svelte';
3+
</script>
4+
5+
<ModuleContext id="hmr-with-context">{y}</ModuleContext>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import ModuleContext from './ModuleContext.svelte';
3+
</script>
4+
5+
<ModuleContext id="hmr-without-context" />

packages/e2e-tests/testUtils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import colors from 'css-color-names';
77
import { ElementHandle } from 'playwright-core';
88
import fetch from 'node-fetch';
99

10-
import { isBuild, isWin, isCI, page, testDir, viteTestUrl } from './vitestSetup';
10+
import { isBuild, isWin, isCI, page, testDir, viteTestUrl, browserLogs } from './vitestSetup';
1111

1212
export * from './vitestSetup';
1313

@@ -177,6 +177,10 @@ export async function editFileAndWaitForHmrComplete(file, replacer, fileUpdateTo
177177
}
178178
}
179179

180+
export function hmrCount(file) {
181+
return browserLogs.filter((line) => line.includes('hot updated') && line.includes(file)).length;
182+
}
183+
180184
export async function saveScreenshot(name: string) {
181185
if (!page) {
182186
return;

packages/playground/kit-demo-app/src/routes/about/+page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { browser, dev } from '$app/env';
1+
import { browser, dev } from '$app/environment';
22

33
// we don't need any JS on this page, though we'll load
44
// it in dev so that we get hot module replacement...

packages/vite-plugin-svelte/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"deepmerge": "^4.2.2",
5252
"kleur": "^4.1.5",
5353
"magic-string": "^0.26.3",
54-
"svelte-hmr": "^0.14.12"
54+
"svelte-hmr": "^0.15.0"
5555
},
5656
"peerDependencies": {
5757
"diff-match-patch": "^1.0.5",

0 commit comments

Comments
 (0)