-
-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature:
app.html
is now transformed.
- Loading branch information
Showing
14 changed files
with
245 additions
and
108 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 @@ | ||
--- | ||
'@skeletonlabs/skeleton-cli': patch | ||
--- | ||
|
||
Feature: `app.html` is now transformed. |
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
54 changes: 54 additions & 0 deletions
54
...keleton-cli/src/commands/migrate/migrations/skeleton-3/transformers/transform-app.test.ts
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,54 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { transformApp } from './transform-app.js'; | ||
|
||
describe('transformsApp', () => { | ||
it('edits the `data-them` attribute when already present', () => { | ||
expect( | ||
transformApp( | ||
` | ||
<html lang="en"> | ||
<head><title>foo</title></head> | ||
<body data-theme="skeleton"></body> | ||
</html> | ||
`, | ||
'cerberus' | ||
) | ||
.code.trim() | ||
.replace(/\r\n|\r|\n/g, '\n') | ||
).toBe( | ||
` | ||
<html lang="en"> | ||
<head><title>foo</title></head> | ||
<body data-theme="cerberus"></body> | ||
</html> | ||
` | ||
.trim() | ||
.replace(/\r\n|\r|\n/g, '\n') | ||
); | ||
}); | ||
it('adds the `data-theme` attribute ', () => { | ||
expect( | ||
transformApp( | ||
` | ||
<html lang="en"> | ||
<head><title>foo</title></head> | ||
<body></body> | ||
</html> | ||
`, | ||
'cerberus' | ||
) | ||
.code.trim() | ||
.replace(/\r\n|\r|\n/g, '\n') | ||
).toBe( | ||
` | ||
<html lang="en"> | ||
<head><title>foo</title></head> | ||
<body data-theme="cerberus"></body> | ||
</html> | ||
` | ||
.trim() | ||
.replace(/\r\n|\r|\n/g, '\n') | ||
); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
...ges/skeleton-cli/src/commands/migrate/migrations/skeleton-3/transformers/transform-app.ts
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,30 @@ | ||
import { AST, parse } from 'svelte/compiler'; | ||
import { walk, type Node } from 'estree-walker'; | ||
import MagicString from 'magic-string'; | ||
|
||
function transformApp(code: string, theme: string) { | ||
const s = new MagicString(code); | ||
const ast = parse(code, { | ||
modern: true | ||
}); | ||
walk(ast.fragment as unknown as Node, { | ||
enter(node: AST.SvelteNode) { | ||
if (node.type === 'RegularElement' && node.name === 'body') { | ||
const dataThemeAttribute = node.attributes.find((attribute) => { | ||
return attribute.type === 'Attribute' && attribute.name === 'data-theme'; | ||
}); | ||
if (dataThemeAttribute) { | ||
s.update(dataThemeAttribute.start, dataThemeAttribute.end, `data-theme="${theme}"`); | ||
} else { | ||
s.appendRight(node.start + '<body'.length, ` data-theme="${theme}"`); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
code: s.toString() | ||
}; | ||
} | ||
|
||
export { transformApp }; |
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
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
Oops, something went wrong.