-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Astro HMR style-only change detection (#10139)
- Loading branch information
Showing
3 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
Fixes style-only change detection for Astro files if both the markup and styles are updated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { describe, it } from 'node:test'; | ||
import * as assert from 'node:assert/strict'; | ||
import { isStyleOnlyChanged } from '../../../dist/vite-plugin-astro/hmr.js'; | ||
|
||
describe('isStyleOnlyChanged', () => { | ||
it('should return false if nothing change', () => { | ||
const oldCode = 'a'; | ||
const newCode = 'a'; | ||
assert.equal(isStyleOnlyChanged(oldCode, newCode), false); | ||
}); | ||
|
||
it('should return false if script has changed', () => { | ||
const oldCode = '<script>console.log("Hello");</script><style>body { color: red; }</style>'; | ||
const newCode = '<script>console.log("Hi");</script><style>body { color: red; }</style>'; | ||
assert.equal(isStyleOnlyChanged(oldCode, newCode), false); | ||
}); | ||
|
||
it('should return true if only style has changed', () => { | ||
const oldCode = '<style>body { color: red; }</style>'; | ||
const newCode = '<style>body { color: blue; }</style>'; | ||
assert.equal(isStyleOnlyChanged(oldCode, newCode), true); | ||
}); | ||
|
||
it('should return false if style tags are added or removed', () => { | ||
const oldCode = '<style>body { color: red; }</style>'; | ||
const newCode = '<style>body { color: red; }</style><style>a { color: blue; }</style>'; | ||
assert.equal(isStyleOnlyChanged(oldCode, newCode), false); | ||
}); | ||
|
||
it('should return false if frontmatter has changed', () => { | ||
const oldCode = ` | ||
--- | ||
title: Hello | ||
--- | ||
<style>body { color: red; }</style>`; | ||
const newCode = ` | ||
--- | ||
title: Hi | ||
--- | ||
<style>body { color: red; }</style>`; | ||
assert.equal(isStyleOnlyChanged(oldCode, newCode), false); | ||
}); | ||
|
||
it('should return false if both frontmatter and style have changed', () => { | ||
const oldCode = ` | ||
--- | ||
title: Hello | ||
--- | ||
<style>body { color: red; }</style>`; | ||
const newCode = ` | ||
--- | ||
title: Hi | ||
--- | ||
<style>body { color: blue; }</style>`; | ||
assert.equal(isStyleOnlyChanged(oldCode, newCode), false); | ||
}); | ||
|
||
it('should return false if both markup and style have changed', () => { | ||
const oldCode = '<h1>Hello</h1><style>body { color: red; }</style>'; | ||
const newCode = '<h1>Hi</h1><style>body { color: blue; }</style>'; | ||
assert.equal(isStyleOnlyChanged(oldCode, newCode), false); | ||
}); | ||
}); |