@jk2908/cxx 0.3.7 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -1,8 +1,38 @@
1
- export type Result<ClassName extends string = string> = readonly [
2
- css: string,
3
- classes: Readonly<Record<ClassName, string>>,
4
- href: string,
5
- ]
1
+ import type { ComponentPropsWithRef } from 'react'
2
+
3
+ export type StyleMode = 'embed' | 'extract'
4
+
5
+ export type EmbedStyleDescriptor = {
6
+ mode: 'embed'
7
+ href: string
8
+ css: string
9
+ precedence: string
10
+ }
11
+
12
+ export type EmbedResult<ClassName extends string = string> = {
13
+ mode: 'embed'
14
+ styles: Readonly<Record<ClassName, string>>
15
+ styleProps: EmbedStyleDescriptor
16
+ }
17
+
18
+ export type ExtractResult<ClassName extends string = string> = {
19
+ mode: 'extract'
20
+ styles: Readonly<Record<ClassName, string>>
21
+ }
22
+
23
+ export type Result<ClassName extends string = string> =
24
+ | EmbedResult<ClassName>
25
+ | ExtractResult<ClassName>
26
+
27
+ type StyleElementProps = Omit<
28
+ ComponentPropsWithRef<'style'>,
29
+ 'href' | 'precedence' | 'children'
30
+ > & {
31
+ href: string
32
+ precedence: string
33
+ }
34
+
35
+ export type StyleProps = EmbedStyleDescriptor & StyleElementProps
6
36
 
7
37
  export type Tags = Map<string, string[]>
8
38
 
@@ -14,10 +44,23 @@ export type PluginConfig = {
14
44
  }
15
45
  }
16
46
 
17
- export type Tag = <ClassName extends string = string>(
47
+ export type EmbedTag = <ClassName extends string = string>(
48
+ name: string,
49
+ ) => (_: TemplateStringsArray) => EmbedResult<ClassName>
50
+
51
+ export type ExtractTag = <ClassName extends string = string>(
18
52
  name: string,
19
- ) => (_: TemplateStringsArray) => Result<ClassName>
53
+ ) => (_: TemplateStringsArray) => ExtractResult<ClassName>
54
+
55
+ export type EmbedStyleTag = ((_: TemplateStringsArray) => EmbedResult) & {
56
+ tag: EmbedTag
57
+ }
58
+
59
+ export type ExtractStyleTag = ((_: TemplateStringsArray) => ExtractResult) & {
60
+ tag: ExtractTag
61
+ }
20
62
 
21
- export type Cxx = ((_: TemplateStringsArray) => Result) & {
22
- tag: Tag
63
+ export type Cxx = {
64
+ embed: EmbedStyleTag
65
+ extract: ExtractStyleTag
23
66
  }
package/src/vite/index.ts CHANGED
@@ -7,9 +7,11 @@ import type { PluginConfig } from '../types.js'
7
7
  import {
8
8
  collect,
9
9
  flushTypes,
10
+ injectStyleImports,
10
11
  refreshFile,
11
12
  seedTypes,
12
13
  SOURCE_EXTENSIONS,
14
+ writeStyleSheets,
13
15
  writeTypesForFile,
14
16
  } from '../build.js'
15
17
  import { GENERATED_DIR } from '../config.js'
@@ -121,15 +123,19 @@ export default function cxx(pluginConfig: PluginConfig = {}) {
121
123
 
122
124
  if (!SOURCE_EXTENSIONS.has(path.extname(cleanId))) return null
123
125
 
124
- const { template, tags, map } = collect(code, cleanId, pluginConfig)
126
+ const result = collect(code, cleanId, pluginConfig)
125
127
 
126
- if (template === code) return null
128
+ if (result.template === code) return null
127
129
 
128
- void writeTypesForFile(resolveWatchFile(cleanId), tags)
130
+ const file = resolveWatchFile(cleanId)
131
+ const imports = writeStyleSheets(file, result.sheets)
132
+ const template = injectStyleImports(result.template, imports)
133
+
134
+ void writeTypesForFile(file, result.tags)
129
135
 
130
136
  return {
131
137
  code: template,
132
- map,
138
+ map: result.map,
133
139
  }
134
140
  },
135
141
  configureServer(server: ViteDevServer) {