Skip to content

Commit 80b116f

Browse files
authored
Merge pull request #214 from UW-Macrostrat/hyperstyles
Fix vike hyperstyles plugin
2 parents a8cbc09 + b339b40 commit 80b116f

File tree

8 files changed

+158
-43
lines changed

8 files changed

+158
-43
lines changed

scripts/publish-helpers/update-package-json-files.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const manualPackagesToFix = [
1212
"@macrostrat/style-system",
1313
"@macrostrat/web-components-bundler",
1414
"@macrostrat/hyperstyle-loader",
15+
"@macrostrat/vite-plugin-hyperstyles",
1516
];
1617

1718
const packageJSONKeyOrder = [

toolchain/vite-plugin-hyperstyles/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [2.0.0] - 2026-02-01
4+
5+
- Add `magic-string` to attempt to create source maps for transformed files.
6+
- Improve `@macrostrat/vite-plugin-hyperstyles/client` type file to not create
7+
typescript errors.
8+
39
## [1.1.1] - 2026-01-29
410

511
- Change layout of `package.json`
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,76 @@
11
# Vite-Plugin-Hyperstyles
22

3-
A Vite plugin that allows you to use styled hyperscript easily.
3+
A Vite plugin that allows you to easily style your hyperscript using
4+
module-scoped CSS.
5+
6+
## Usage
7+
8+
```css
9+
/* styles.modules.css */
10+
.container {
11+
display: flex;
12+
flex-direction: column;
13+
align-items: center;
14+
}
15+
16+
.title {
17+
color: blue;
18+
font-size: 20px;
19+
}
20+
21+
.description {
22+
color: gray;
23+
font-size: 16px;
24+
}
25+
```
26+
27+
```typescript
28+
import h from "./styles.modules.css";
29+
30+
// Class name scopes are applied automatically
31+
const element = h("div.container", [
32+
h("h1.title", "Hello, World!"),
33+
h(
34+
"p.description",
35+
{ className: h.description },
36+
"This is a styled hyperscript example.",
37+
),
38+
]);
39+
```
40+
41+
This replaces the slightly more verbose syntax that can be used without the
42+
plugin:
43+
44+
```typescript
45+
import hyper from "@macrostrat/hyper";
46+
import styles from "./styles.modules.css";
47+
48+
const h = hyper.styled(styles);
49+
...
50+
```
51+
52+
## Configuration
53+
54+
In your `vite.config.ts`, add the plugin:
55+
56+
```typescript
57+
import { defineConfig } from "vite";
58+
import hyperStyles from "@macrostrat/vite-plugin-hyperstyles";
59+
60+
export default defineConfig({
61+
plugins: [hyperStyles()],
62+
});
63+
```
64+
65+
If you're using Typescript, you'll want to add a type declaration for the
66+
enhanced hyperscript function to your TSConfig
67+
68+
```json
69+
{
70+
"compilerOptions": {
71+
// ...
72+
"types": ["@macrostrat/vite-plugin-hyperstyles/client", "vite/client"]
73+
// Note: order matters; ensure vite/client comes after this plugin
74+
}
75+
}
76+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/** Replacement for vite/client declaration that handles hyperscript-enhanced stylesheets
2+
* from the vite-plugin-hyperstyles module
3+
*/
4+
type CSSModuleClasses = { readonly [key: string]: string };
5+
6+
declare module "*.module.css" {
7+
import type { Hyper } from "@macrostrat/hyper";
8+
const h: Hyper & CSSModuleClasses;
9+
export default h;
10+
}
11+
declare module "*.module.scss" {
12+
import type { Hyper } from "@macrostrat/hyper";
13+
const h: Hyper & CSSModuleClasses;
14+
export default h;
15+
}
16+
declare module "*.module.sass" {
17+
import type { Hyper } from "@macrostrat/hyper";
18+
const h: Hyper & CSSModuleClasses;
19+
export default h;
20+
}
21+
declare module "*.module.less" {
22+
import type { Hyper } from "@macrostrat/hyper";
23+
const h: Hyper & CSSModuleClasses;
24+
export default h;
25+
}
26+
declare module "*.module.styl" {
27+
import type { Hyper } from "@macrostrat/hyper";
28+
const h: Hyper & CSSModuleClasses;
29+
export default h;
30+
}
31+
declare module "*.module.stylus" {
32+
import type { Hyper } from "@macrostrat/hyper";
33+
const h: Hyper & CSSModuleClasses;
34+
export default h;
35+
}
36+
declare module "*.module.pcss" {
37+
import type { Hyper } from "@macrostrat/hyper";
38+
const h: Hyper & CSSModuleClasses;
39+
export default h;
40+
}
41+
declare module "*.module.sss" {
42+
import type { Hyper } from "@macrostrat/hyper";
43+
const h: Hyper & CSSModuleClasses;
44+
export default h;
45+
}

toolchain/vite-plugin-hyperstyles/package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@macrostrat/vite-plugin-hyperstyles",
3-
"version": "1.1.1",
3+
"version": "2.0.0",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/UW-Macrostrat/web-components.git",
@@ -14,7 +14,9 @@
1414
"types": "dist/index.d.ts",
1515
"files": [
1616
"src",
17-
"dist"
17+
"dist",
18+
"README.md",
19+
"client.d.ts"
1820
],
1921
"sideEffects": false,
2022
"exports": {
@@ -29,13 +31,16 @@
2931
"default": "./dist/index.cjs"
3032
}
3133
},
32-
"./package.json": "./package.json"
34+
"./package.json": "./package.json",
35+
"./client": "./client.d.ts",
36+
"./client.d.ts": "./client.d.ts"
3337
},
3438
"scripts": {
3539
"build": "bundle-library ."
3640
},
3741
"dependencies": {
3842
"@macrostrat/hyper": "^3.0.6",
43+
"magic-string": "^0.30.21",
3944
"vite": "^5||^6.1.6||^7"
4045
},
4146
"devDependencies": {
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
import { Plugin } from "vite";
1+
import type { Plugin } from "vite";
2+
import MagicString from "magic-string";
23

3-
const cssModuleMatcher = /\.module\.(css|scss|sass|styl)$/;
4+
const cssModuleMatcher = /\.module\.(css|scss|sass|less|styl|stylus|pcss|sss)$/;
45

56
export default function hyperStyles(): Plugin {
67
return {
78
name: "hyper-styles",
89
enforce: "post",
910
// Post-process the output to add the hyperStyled import
1011
transform(code, id) {
11-
const code1 = code.replace("export default", "const styles =");
12-
if (cssModuleMatcher.test(id)) {
13-
//const code2 = code1 + "\nexport default styles\n";
14-
return `import hyper from "@macrostrat/hyper";
15-
${code1}
12+
if (!cssModuleMatcher.test(id)) {
13+
return;
14+
}
15+
16+
const code0 = new MagicString(code);
17+
// Just add sourcemaps
18+
///dd https://github.com/Rich-Harris/magic-string/issues/13
19+
code0.replace("export default ", "const styles = ");
20+
code0.prepend(`import hyper from "@macrostrat/hyper";\n`);
21+
code0.append(`
1622
let h = hyper.styled(styles);
1723
// Keep backwards compatibility with the existing default style object.
1824
Object.assign(h, styles);
19-
export default h;`;
20-
}
25+
export default h;`);
26+
27+
// Generate source map
28+
const map = code0.generateMap();
29+
const codeString = code0.toString();
30+
31+
return {
32+
code: codeString,
33+
map,
34+
};
2135
},
2236
};
2337
}

toolchain/vite-plugin-hyperstyles/styles.d.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,7 @@ __metadata:
16291629
"@macrostrat/hyper": "npm:^3.0.6"
16301630
"@macrostrat/web-components-bundler": "workspace:*"
16311631
"@types/node": "npm:^22.14.1"
1632+
magic-string: "npm:^0.30.21"
16321633
vite: "npm:^5||^6.1.6||^7"
16331634
languageName: unknown
16341635
linkType: soft

0 commit comments

Comments
 (0)