histoire 0.2.5 → 0.3.2

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.
Files changed (219) hide show
  1. package/dist/client/app/App.vue +8 -3
  2. package/dist/client/app/codegen/const.d.ts +1 -0
  3. package/dist/client/app/codegen/const.js +3 -0
  4. package/dist/client/app/codegen/vue3.js +83 -21
  5. package/dist/client/app/codegen/vue3.js.map +1 -0
  6. package/dist/client/app/components/base/BaseEmpty.vue +17 -1
  7. package/dist/client/app/components/base/BaseOverflowMenu.vue +118 -0
  8. package/dist/client/app/components/base/BaseOverflowTab.vue +46 -0
  9. package/dist/client/app/components/exposed/Story.vue +33 -1
  10. package/dist/client/app/components/exposed/Variant.vue +19 -1
  11. package/dist/client/app/components/panel/PaneTabs.vue +64 -0
  12. package/dist/client/app/components/{story → panel}/StoryControls.vue +6 -1
  13. package/dist/client/app/components/{story → panel}/StoryDocs.vue +18 -11
  14. package/dist/client/app/components/panel/StoryEvent.vue +53 -0
  15. package/dist/client/app/components/panel/StoryEvents.vue +45 -0
  16. package/{src/client/app/components/story → dist/client/app/components/panel}/StorySidePanel.vue +17 -20
  17. package/{src/client/app/components/story → dist/client/app/components/panel}/StorySourceCode.vue +24 -14
  18. package/dist/client/app/components/sandbox/SandboxVue3.vue +16 -4
  19. package/dist/client/app/components/sandbox/lib.js +2 -1
  20. package/dist/client/app/components/search/SearchLoading.vue +16 -0
  21. package/dist/client/app/components/search/SearchModal.vue +12 -149
  22. package/dist/client/app/components/search/SearchPane.vue +210 -0
  23. package/dist/client/app/components/search/search-data.d.ts +2 -0
  24. package/dist/client/app/components/search/search-data.js +8 -0
  25. package/dist/client/app/components/search/search-data.js.map +1 -0
  26. package/dist/client/app/components/story/StoryResponsivePreview.vue +9 -98
  27. package/dist/client/app/components/story/StoryVariantSingle.vue +109 -0
  28. package/dist/client/app/components/story/StoryVariantSinglePreviewNative.vue +39 -0
  29. package/dist/client/app/components/story/StoryVariantSinglePreviewRemote.vue +153 -0
  30. package/dist/client/app/components/story/StoryVariantSingleView.vue +13 -6
  31. package/dist/client/app/components/story/StoryView.vue +17 -3
  32. package/dist/client/app/components/story/StoryViewer.vue +2 -92
  33. package/dist/client/app/components/{story/toolbar → toolbar}/StoryVariantBackground.vue +3 -3
  34. package/{src/client/app/components/story → dist/client/app/components}/toolbar/StoryVariantNewTab.vue +2 -2
  35. package/dist/client/app/components/{story/toolbar → toolbar}/StoryVariantResponsiveSize.vue +3 -3
  36. package/{src/client/app/components/story → dist/client/app/components}/toolbar/StoryVariantTitle.vue +1 -1
  37. package/dist/client/app/components/tree/StoryGroup.vue +4 -4
  38. package/dist/client/app/components/tree/StoryListFolder.vue +4 -4
  39. package/dist/client/app/components/tree/StoryListItem.vue +4 -1
  40. package/dist/client/app/global-components.js +1 -0
  41. package/dist/client/app/global-components.js.map +1 -0
  42. package/dist/client/app/index.js +1 -0
  43. package/dist/client/app/index.js.map +1 -0
  44. package/dist/client/app/router.d.ts +1 -0
  45. package/dist/client/app/router.js +3 -1
  46. package/dist/client/app/sandbox.js +12 -1
  47. package/dist/client/app/sandbox.js.map +1 -0
  48. package/dist/client/app/stores/events.d.ts +29 -0
  49. package/dist/client/app/stores/events.js +26 -0
  50. package/dist/client/app/stores/events.js.map +1 -0
  51. package/dist/client/app/stores/folder.d.ts +10 -0
  52. package/dist/client/app/stores/folder.js +48 -0
  53. package/dist/client/app/stores/folder.js.map +1 -0
  54. package/dist/client/app/stores/story.d.ts +41 -8
  55. package/dist/client/app/stores/story.js +21 -33
  56. package/dist/client/app/stores/story.js.map +1 -0
  57. package/dist/client/app/style/main.pcss +5 -0
  58. package/dist/client/app/style/sandbox.css +2 -0
  59. package/dist/client/app/style/transitions.pcss +13 -0
  60. package/dist/client/app/types.d.ts +4 -0
  61. package/dist/client/app/types.js +1 -0
  62. package/dist/client/app/types.js.map +1 -0
  63. package/dist/client/app/util/const.d.ts +1 -0
  64. package/dist/client/app/util/const.js +1 -0
  65. package/dist/client/app/util/events.d.ts +1 -0
  66. package/dist/client/app/util/events.js +32 -0
  67. package/dist/client/app/util/events.js.map +1 -0
  68. package/dist/client/app/util/keyboard.js +1 -0
  69. package/dist/client/app/util/keyboard.js.map +1 -0
  70. package/dist/client/app/util/mapping.js +5 -2
  71. package/dist/client/app/util/mapping.js.map +1 -0
  72. package/dist/client/app/util/preview-settings.js +1 -0
  73. package/dist/client/app/util/preview-settings.js.map +1 -0
  74. package/dist/client/app/util/reactivity.js +1 -0
  75. package/dist/client/app/util/reactivity.js.map +1 -0
  76. package/dist/client/app/util/scroll.js +1 -0
  77. package/dist/client/app/util/scroll.js.map +1 -0
  78. package/dist/client/app/util/variant.js +1 -0
  79. package/dist/client/app/util/variant.js.map +1 -0
  80. package/dist/client/index.d.ts +4 -3
  81. package/dist/client/index.js +3 -1
  82. package/dist/client/index.js.map +1 -0
  83. package/dist/client/server/index.d.ts +7 -2
  84. package/dist/client/server/index.js +8 -26
  85. package/{src/client/server/Story.server.vue → dist/client/server/vue3/Story.vue} +14 -3
  86. package/dist/client/server/{Variant.server.vue → vue3/Variant.vue} +0 -0
  87. package/dist/client/server/vue3/run.d.ts +2 -0
  88. package/dist/client/server/vue3/run.js +33 -0
  89. package/dist/client/server/vue3/run.js.map +1 -0
  90. package/dist/controls.js +1 -0
  91. package/dist/controls.js.map +1 -0
  92. package/dist/node/alias.d.ts +2 -1
  93. package/dist/node/alias.js +3 -2
  94. package/dist/node/build.js +74 -21
  95. package/dist/node/builtin-plugins/tailwind-tokens.d.ts +5 -0
  96. package/dist/node/builtin-plugins/tailwind-tokens.js +510 -0
  97. package/dist/node/collect/index.js +3 -1
  98. package/dist/node/collect/worker.js +6 -2
  99. package/dist/node/commands/preview.js +4 -25
  100. package/dist/node/config.d.ts +103 -3
  101. package/dist/node/config.js +40 -23
  102. package/dist/node/context.d.ts +3 -3
  103. package/dist/node/context.js +1 -1
  104. package/dist/node/index.d.ts +1 -0
  105. package/dist/node/index.js +1 -0
  106. package/dist/node/load.d.ts +12 -0
  107. package/dist/node/load.js +42 -0
  108. package/dist/node/plugin.d.ts +67 -11
  109. package/dist/node/plugin.js +42 -245
  110. package/dist/node/preview.d.ts +8 -0
  111. package/dist/node/preview.js +41 -0
  112. package/dist/node/search.d.ts +2 -0
  113. package/dist/node/search.js +68 -0
  114. package/dist/node/server.js +43 -21
  115. package/dist/node/stories.d.ts +2 -0
  116. package/dist/node/stories.js +4 -4
  117. package/dist/node/types.d.ts +1 -0
  118. package/dist/node/util/find-up.d.ts +1 -0
  119. package/dist/node/util/find-up.js +20 -0
  120. package/dist/node/vite.d.ts +13 -0
  121. package/dist/node/vite.js +278 -0
  122. package/dist/style.css +174 -86
  123. package/package.json +7 -6
  124. package/scripts/copy.mjs +2 -1
  125. package/src/client/app/App.vue +8 -3
  126. package/src/client/app/codegen/const.ts +3 -0
  127. package/src/client/app/codegen/vue3.ts +80 -22
  128. package/src/client/app/components/base/BaseEmpty.vue +17 -1
  129. package/src/client/app/components/base/BaseOverflowMenu.vue +118 -0
  130. package/src/client/app/components/base/BaseOverflowTab.vue +46 -0
  131. package/src/client/app/components/exposed/Story.vue +33 -1
  132. package/src/client/app/components/exposed/Variant.vue +19 -1
  133. package/src/client/app/components/panel/PaneTabs.vue +64 -0
  134. package/src/client/app/components/{story → panel}/StoryControls.vue +6 -1
  135. package/src/client/app/components/{story → panel}/StoryDocs.vue +18 -11
  136. package/src/client/app/components/panel/StoryEvent.vue +53 -0
  137. package/src/client/app/components/panel/StoryEvents.vue +45 -0
  138. package/{dist/client/app/components/story → src/client/app/components/panel}/StorySidePanel.vue +17 -20
  139. package/{dist/client/app/components/story → src/client/app/components/panel}/StorySourceCode.vue +24 -14
  140. package/src/client/app/components/sandbox/SandboxVue3.vue +16 -4
  141. package/src/client/app/components/sandbox/lib.ts +3 -1
  142. package/src/client/app/components/search/SearchLoading.vue +16 -0
  143. package/src/client/app/components/search/SearchModal.vue +12 -149
  144. package/src/client/app/components/search/SearchPane.vue +210 -0
  145. package/src/client/app/components/search/search-data.ts +9 -0
  146. package/src/client/app/components/story/StoryResponsivePreview.vue +9 -98
  147. package/src/client/app/components/story/StoryVariantSingle.vue +109 -0
  148. package/src/client/app/components/story/StoryVariantSinglePreviewNative.vue +39 -0
  149. package/src/client/app/components/story/StoryVariantSinglePreviewRemote.vue +153 -0
  150. package/src/client/app/components/story/StoryVariantSingleView.vue +13 -6
  151. package/src/client/app/components/story/StoryView.vue +17 -3
  152. package/src/client/app/components/story/StoryViewer.vue +2 -92
  153. package/src/client/app/components/{story/toolbar → toolbar}/StoryVariantBackground.vue +3 -3
  154. package/{dist/client/app/components/story → src/client/app/components}/toolbar/StoryVariantNewTab.vue +2 -2
  155. package/src/client/app/components/{story/toolbar → toolbar}/StoryVariantResponsiveSize.vue +3 -3
  156. package/{dist/client/app/components/story → src/client/app/components}/toolbar/StoryVariantTitle.vue +1 -1
  157. package/src/client/app/components/tree/StoryGroup.vue +4 -4
  158. package/src/client/app/components/tree/StoryListFolder.vue +4 -4
  159. package/src/client/app/components/tree/StoryListItem.vue +4 -1
  160. package/src/client/app/router.ts +4 -1
  161. package/src/client/app/sandbox.ts +11 -1
  162. package/src/client/app/stores/events.ts +36 -0
  163. package/{dist/client/app/stores/story.ts → src/client/app/stores/folder.ts} +16 -23
  164. package/src/client/app/stores/story.ts +21 -40
  165. package/src/client/app/style/main.pcss +5 -0
  166. package/src/client/app/style/sandbox.css +2 -0
  167. package/src/client/app/style/transitions.pcss +13 -0
  168. package/src/client/app/types.ts +5 -0
  169. package/src/client/app/util/const.ts +1 -0
  170. package/src/client/app/util/events.ts +30 -0
  171. package/src/client/app/util/mapping.ts +4 -2
  172. package/src/client/index.ts +4 -3
  173. package/src/client/server/index.ts +14 -27
  174. package/{dist/client/server/Story.server.vue → src/client/server/vue3/Story.vue} +14 -3
  175. package/src/client/server/{Variant.server.vue → vue3/Variant.vue} +0 -0
  176. package/{dist/client/server/index.ts → src/client/server/vue3/run.ts} +10 -4
  177. package/src/node/alias.ts +4 -2
  178. package/src/node/build.ts +86 -21
  179. package/src/node/builtin-plugins/tailwind-tokens.ts +521 -0
  180. package/src/node/collect/index.ts +3 -1
  181. package/src/node/collect/worker.ts +7 -2
  182. package/src/node/commands/preview.ts +4 -33
  183. package/src/node/config.ts +53 -27
  184. package/src/node/context.ts +3 -3
  185. package/src/node/index.ts +1 -0
  186. package/src/node/load.ts +63 -0
  187. package/src/node/plugin.ts +75 -252
  188. package/src/node/preview.ts +56 -0
  189. package/src/node/search.ts +75 -0
  190. package/src/node/server.ts +49 -23
  191. package/src/node/stories.ts +4 -4
  192. package/src/node/types.ts +1 -0
  193. package/src/node/util/find-up.ts +24 -0
  194. package/src/node/vite.ts +296 -0
  195. package/tailwind.config.cjs +1 -0
  196. package/dist/client/app/codegen/serialize-js.ts +0 -119
  197. package/dist/client/app/codegen/util.ts +0 -79
  198. package/dist/client/app/codegen/vue3.ts +0 -276
  199. package/dist/client/app/components/sandbox/lib.ts +0 -8
  200. package/dist/client/app/global-components.ts +0 -13
  201. package/dist/client/app/index.ts +0 -28
  202. package/dist/client/app/router.ts +0 -17
  203. package/dist/client/app/sandbox.ts +0 -94
  204. package/dist/client/app/stores/preview-settings.ts +0 -17
  205. package/dist/client/app/types.ts +0 -78
  206. package/dist/client/app/util/config.ts +0 -6
  207. package/dist/client/app/util/const.ts +0 -3
  208. package/dist/client/app/util/dark.ts +0 -4
  209. package/dist/client/app/util/env.ts +0 -1
  210. package/dist/client/app/util/keyboard.ts +0 -99
  211. package/dist/client/app/util/mapping.ts +0 -48
  212. package/dist/client/app/util/preview-settings.ts +0 -8
  213. package/dist/client/app/util/reactivity.ts +0 -22
  214. package/dist/client/app/util/responsive.ts +0 -3
  215. package/dist/client/app/util/scroll.ts +0 -31
  216. package/dist/client/app/util/tooltip.ts +0 -13
  217. package/dist/client/app/util/variant.ts +0 -20
  218. package/dist/client/index.ts +0 -12
  219. package/dist/client/shim.d.ts +0 -5
package/src/node/build.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import { join } from 'pathe'
2
- import { build as viteBuild, createServer as createViteServer } from 'vite'
2
+ import { build as viteBuild, createServer as createViteServer, ResolvedConfig as ViteConfig, resolveConfig as resolveViteConfig } from 'vite'
3
3
  import fs from 'fs-extra'
4
4
  import { lookup as lookupMime } from 'mrmime'
5
5
  import pc from 'picocolors'
6
6
  import { performance } from 'perf_hooks'
7
7
  import { APP_PATH } from './alias.js'
8
8
  import { Context } from './context.js'
9
- import { createVitePlugins } from './plugin.js'
9
+ import { createVitePlugins } from './vite.js'
10
10
  import { findAllStories } from './stories.js'
11
11
  import type { RollupOutput } from 'rollup'
12
12
  import { useCollectStories } from './collect/index.js'
13
+ import { BuildEndCallback, BuildPluginApi, PreviewStoryCallback } from './plugin.js'
14
+ import { useModuleLoader } from './load.js'
15
+ import { startPreview } from './preview.js'
13
16
 
14
17
  const PRELOAD_MODULES = [
15
18
  'vendor',
@@ -25,11 +28,27 @@ export async function build (ctx: Context) {
25
28
  const startTime = performance.now()
26
29
  await findAllStories(ctx)
27
30
 
28
- // Collect story data
29
31
  const server = await createViteServer({
30
32
  plugins: await createVitePlugins(ctx),
31
33
  })
32
34
  await server.pluginContainer.buildStart({})
35
+
36
+ const moduleLoader = useModuleLoader({
37
+ server,
38
+ throws: true,
39
+ })
40
+ const buildEndCallbacks: BuildEndCallback[] = []
41
+ const previewStoryCallbacks: PreviewStoryCallback[] = []
42
+ for (const plugin of ctx.config.plugins) {
43
+ if (plugin.onBuild) {
44
+ const api = new BuildPluginApi(ctx, plugin, moduleLoader)
45
+ await plugin.onBuild(api)
46
+ buildEndCallbacks.push(...api.buildEndCallbacks)
47
+ previewStoryCallbacks.push(...api.previewStoryCallbacks)
48
+ }
49
+ }
50
+
51
+ // Collect story data
33
52
  const { executeStoryFile, destroy: destroyCollectStories } = useCollectStories({
34
53
  server,
35
54
  throws: true,
@@ -37,7 +56,6 @@ export async function build (ctx: Context) {
37
56
  for (const storyFile of ctx.storyFiles) {
38
57
  await executeStoryFile(storyFile)
39
58
  }
40
- await server.close()
41
59
  await destroyCollectStories()
42
60
 
43
61
  const storyCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ? 1 : 0), 0)
@@ -49,8 +67,8 @@ export async function build (ctx: Context) {
49
67
  build: {
50
68
  rollupOptions: {
51
69
  input: [
52
- join(APP_PATH, 'index.ts'),
53
- join(APP_PATH, 'sandbox.ts'),
70
+ join(APP_PATH, 'index.js'),
71
+ join(APP_PATH, 'sandbox.js'),
54
72
  ],
55
73
  output: {
56
74
  manualChunks (id) {
@@ -63,57 +81,104 @@ export async function build (ctx: Context) {
63
81
  outDir: ctx.config.outDir,
64
82
  emptyOutDir: true,
65
83
  cssCodeSplit: false,
84
+ minify: false,
66
85
  },
67
86
  })
68
87
  const result = Array.isArray(results) ? results[0] : results as RollupOutput
69
88
 
89
+ const resolvedViteConfig = await resolveViteConfig({
90
+ plugins: await createVitePlugins(ctx),
91
+ }, 'build')
92
+
70
93
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset')
71
94
 
72
95
  // Preload
73
96
  const preloadOutputs = result.output.filter(o => PRELOAD_MODULES.includes(o.name) && o.type === 'chunk')
74
- const preloadHtml = generateScriptLinks(preloadOutputs.map(o => o.fileName), 'preload')
97
+ const preloadHtml = generateScriptLinks(preloadOutputs.map(o => o.fileName), 'preload', ctx, resolvedViteConfig)
75
98
 
76
99
  // Prefetch
77
100
  const prefetchOutputs = result.output.filter(o => PREFETCHED_MODULES.includes(o.name) && o.type === 'chunk')
78
- const prefetchHtml = generateScriptLinks(prefetchOutputs.map(o => o.fileName), 'prefetch')
101
+ const prefetchHtml = generateScriptLinks(prefetchOutputs.map(o => o.fileName), 'prefetch', ctx, resolvedViteConfig)
79
102
 
80
103
  // Index
81
104
  const indexOutput = result.output.find(o => o.name === 'index' && o.type === 'chunk')
82
- await writeHtml(indexOutput.fileName, styleOutput.fileName, 'index.html', {
105
+ const indexHtml = generateEntryHtml(indexOutput.fileName, styleOutput.fileName, {
83
106
  HEAD: `${preloadHtml}${prefetchHtml}`,
84
- }, ctx)
107
+ }, ctx, resolvedViteConfig)
108
+ await writeFile('index.html', indexHtml, ctx)
85
109
 
86
110
  // Sandbox
87
111
  const sandboxOutput = result.output.find(o => o.name === 'sandbox' && o.type === 'chunk')
88
- await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', {}, ctx)
112
+ const sandboxHtml = generateEntryHtml(sandboxOutput.fileName, styleOutput.fileName, {}, ctx, resolvedViteConfig)
113
+ await writeFile('__sandbox.html', sandboxHtml, ctx)
89
114
 
90
115
  const duration = performance.now() - startTime
91
116
  if (emptyStoryCount) {
92
117
  console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`))
93
118
  }
94
119
  console.log(pc.green(`✅ Built ${storyCount} stories (${variantCount} variants) in ${Math.round(duration / 1000 * 100) / 100}s`))
120
+
121
+ // Render
122
+ if (previewStoryCallbacks.length) {
123
+ const { baseUrl, close } = await startPreview(resolvedViteConfig, null, ctx)
124
+ for (const storyFile of ctx.storyFiles) {
125
+ const story = storyFile.story
126
+ for (const variant of story.variants) {
127
+ const query = new URLSearchParams()
128
+ query.append('storyId', story.id)
129
+ query.append('variantId', variant.id)
130
+ const url = `${baseUrl}__sandbox?${query.toString()}`
131
+ for (const fn of previewStoryCallbacks) {
132
+ await fn({
133
+ file: storyFile.path,
134
+ story,
135
+ variant,
136
+ url,
137
+ })
138
+ }
139
+ }
140
+ }
141
+ await close()
142
+ }
143
+
144
+ await server.close()
145
+
146
+ for (const fn of buildEndCallbacks) {
147
+ await fn()
148
+ }
95
149
  }
96
150
 
97
- async function writeHtml (jsEntryFile: string, cssEntryFile: string, htmlFileName: string, variables: { HEAD?: string }, ctx: Context) {
98
- const code = `<!DOCTYPE html>
151
+ function generateBaseHtml (head: string, body: string, ctx: Context) {
152
+ return `<!DOCTYPE html>
99
153
  <html>
100
154
  <head>
101
155
  <title>${ctx.config.theme.title}</title>
102
156
  <meta charset="utf-8">
103
157
  <meta name="viewport" content="width=device-width,initial-scale=1">
104
158
  <meta name="description" content="">
105
- <link rel="stylesheet" href="/${cssEntryFile}">
106
- ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
107
- ${variables.HEAD ?? ''}
159
+ ${head}
108
160
  </head>
109
161
  <body>
110
- <div id="app"></div>
111
- <script type="module" src="/${jsEntryFile}"></script>
162
+ ${body}
112
163
  </body>
113
164
  </html>`
114
- await fs.writeFile(join(ctx.config.outDir, htmlFileName), code, 'utf8')
115
165
  }
116
166
 
117
- function generateScriptLinks (prefetchScripts: string[], rel: string) {
118
- return prefetchScripts.map(s => `<link rel="${rel}" href="${s}" as="script" crossOrigin="anonymous">`).join('')
167
+ function generateEntryHtml (jsEntryFile: string, cssEntryFile: string, variables: { HEAD?: string }, ctx: Context, resolvedViteConfig: ViteConfig) {
168
+ return generateBaseHtml(
169
+ `<link rel="stylesheet" href="${resolvedViteConfig.base}${cssEntryFile}">
170
+ ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
171
+ ${variables.HEAD ?? ''}`,
172
+ `<div id="app"></div>
173
+ <script type="module" src="${resolvedViteConfig.base}${jsEntryFile}"></script>`,
174
+ ctx,
175
+ )
176
+ }
177
+
178
+ async function writeFile (fileName: string, content: string, ctx: Context) {
179
+ await fs.writeFile(join(ctx.config.outDir, fileName), content, 'utf8')
180
+ }
181
+
182
+ function generateScriptLinks (prefetchScripts: string[], rel: string, ctx: Context, resolvedViteConfig: ViteConfig) {
183
+ return prefetchScripts.map(s => `<link rel="${rel}" href="${resolvedViteConfig.base}${s}" as="script" crossOrigin="anonymous">`).join('')
119
184
  }
@@ -0,0 +1,521 @@
1
+ import { BasePluginApi, Plugin } from '../plugin.js'
2
+ import { findUp } from '../util/find-up.js'
3
+
4
+ export interface TailwindTokensOptions {
5
+ configFile?: string
6
+ }
7
+
8
+ export function tailwindTokens (options: TailwindTokensOptions = {}): Plugin {
9
+ const tailwindConfigFile = options.configFile ?? findUp(process.cwd(), [
10
+ 'tailwind.config.js',
11
+ 'tailwind.config.cjs',
12
+ 'tailwind.config.mjs',
13
+ 'tailwind.config.ts',
14
+ 'tailwind-config.js',
15
+ 'tailwind-config.cjs',
16
+ 'tailwind-config.mjs',
17
+ 'tailwind-config.ts',
18
+ ])
19
+
20
+ async function generate (api: BasePluginApi) {
21
+ try {
22
+ await api.fs.ensureDir(api.pluginTempDir)
23
+ await api.fs.emptyDir(api.pluginTempDir)
24
+ api.moduleLoader.clearCache()
25
+ const tailwindConfig = await api.moduleLoader.loadModule(tailwindConfigFile)
26
+ const { default: resolveConfig } = await import('tailwindcss/resolveConfig.js')
27
+ const resolvedTailwindConfig = resolveConfig(tailwindConfig)
28
+ const storyFile = api.path.resolve(api.pluginTempDir, 'Tailwind.story.vue')
29
+ await api.fs.writeFile(storyFile, storyTemplate(resolvedTailwindConfig))
30
+ api.addStoryFile(storyFile)
31
+ } catch (e) {
32
+ api.error(e.stack ?? e.message)
33
+ }
34
+ }
35
+
36
+ return {
37
+ name: 'tailwind-tokens',
38
+
39
+ config (config) {
40
+ if (tailwindConfigFile) {
41
+ // Add 'design-system' group
42
+ if (!config.tree) {
43
+ config.tree = {}
44
+ }
45
+ if (!config.tree.groups) {
46
+ config.tree.groups = []
47
+ }
48
+ if (!config.tree.groups.some(g => g.id === 'design-system')) {
49
+ let index = 0
50
+ // After 'top' group
51
+ const topIndex = config.tree.groups.findIndex(g => g.id === 'top')
52
+ if (topIndex > -1) {
53
+ index = topIndex + 1
54
+ }
55
+ // Insert group
56
+ config.tree.groups.splice(index, 0, {
57
+ id: 'design-system',
58
+ title: 'Design System',
59
+ })
60
+ }
61
+ }
62
+ },
63
+
64
+ onDev (api, onCleanup) {
65
+ if (tailwindConfigFile) {
66
+ const watcher = api.watcher.watch(tailwindConfigFile)
67
+ .on('change', () => generate(api))
68
+ .on('add', () => generate(api))
69
+ onCleanup(() => {
70
+ watcher.close()
71
+ })
72
+ }
73
+ },
74
+
75
+ async onBuild (api) {
76
+ if (tailwindConfigFile) {
77
+ await generate(api)
78
+ }
79
+ },
80
+ }
81
+ }
82
+
83
+ const storyTemplate = (tailwindConfig: any) => `<script setup>
84
+ import 'histoire/dist/style.css'
85
+ import { ref } from 'vue'
86
+ const config = ${JSON.stringify(tailwindConfig, null, 2)}
87
+ const search = ref('')
88
+ const sampleText = ref('Cat sit like bread eat prawns daintily with a claw then lick paws clean wash down prawns with a lap of carnation milk then retire to the warmest spot on the couch to claw at the fabric before taking a catnap mrow cat cat moo moo lick ears lick paws')
89
+ const fontSize = ref(16)
90
+ defineExpose({
91
+ search,
92
+ sampleText,
93
+ fontSize,
94
+ })
95
+ </script>
96
+ <template>
97
+ <Story id="tailwind" title="Tailwind" group="design-system" icon="mdi:tailwind" responsiveDisabled>
98
+ <Variant id="background-color" title="Background Color" icon="carbon:color-palette">
99
+ <HstColorShades
100
+ v-for="(shades, key) of config.theme.backgroundColor"
101
+ :key="key"
102
+ :shades="typeof shades === 'object' ? shades : { DEFAULT: shades }"
103
+ :get-name="shade => (config.prefix ?? '') + (shade === 'DEFAULT' ? \`bg-\${key}\` : \`bg-\${key}-\${shade}\`)"
104
+ :search="search"
105
+ >
106
+ <template #default="{ color }">
107
+ <div
108
+ class="shade"
109
+ :style="{
110
+ backgroundColor: color,
111
+ }"
112
+ />
113
+ </template>
114
+ </HstColorShades>
115
+ <template #controls>
116
+ <HstText
117
+ v-model="search"
118
+ title="Filter..."
119
+ />
120
+ </template>
121
+ </Variant>
122
+ <Variant id="text-color" title="Text Color" icon="carbon:text-color">
123
+ <HstColorShades
124
+ v-for="(shades, key) of config.theme.textColor"
125
+ :key="key"
126
+ :shades="typeof shades === 'object' ? shades : { DEFAULT: shades }"
127
+ :get-name="shade => (config.prefix ?? '') + (shade === 'DEFAULT' ? \`text-\${key}\` : \`text-\${key}-\${shade}\`)"
128
+ :search="search"
129
+ >
130
+ <template #default="{ color }">
131
+ <div
132
+ class="shade text"
133
+ :style="{
134
+ color: color,
135
+ }"
136
+ >
137
+ Aa
138
+ </div>
139
+ </template>
140
+ </HstColorShades>
141
+ <template #controls>
142
+ <HstText
143
+ v-model="search"
144
+ title="Filter..."
145
+ />
146
+ </template>
147
+ </Variant>
148
+ <Variant id="border-color" title="Border Color" icon="carbon:color-palette">
149
+ <HstColorShades
150
+ v-for="(shades, key) of config.theme.borderColor"
151
+ :key="key"
152
+ :shades="typeof shades === 'object' ? shades : { DEFAULT: shades }"
153
+ :get-name="shade => (config.prefix ?? '') + (shade === 'DEFAULT' ? \`border-\${key}\` : \`border-\${key}-\${shade}\`)"
154
+ :search="search"
155
+ >
156
+ <template #default="{ color }">
157
+ <div
158
+ class="shade border"
159
+ :style="{
160
+ borderColor: color,
161
+ }"
162
+ />
163
+ </template>
164
+ </HstColorShades>
165
+ <template #controls>
166
+ <HstText
167
+ v-model="search"
168
+ title="Filter..."
169
+ />
170
+ </template>
171
+ </Variant>
172
+ <Variant id="padding" title="Padding" icon="carbon:area">
173
+ <HstTokenList
174
+ :tokens="config.theme.padding"
175
+ :get-name="key => \`\${config.prefix ?? ''}p-\${key}\`"
176
+ >
177
+ <template #default="{ token }">
178
+ <div class="padding"
179
+ :style="{
180
+ padding: token.value,
181
+ }"
182
+ >
183
+ <div
184
+ class="padding-box"
185
+ />
186
+ </div>
187
+ </template>
188
+ </HstTokenList>
189
+ </Variant>
190
+ <Variant id="margin" title="Margin" icon="carbon:area">
191
+ <HstTokenList
192
+ :tokens="config.theme.margin"
193
+ :get-name="key => \`\${config.prefix ?? ''}m-\${key}\`"
194
+ >
195
+ <template #default="{ token }">
196
+ <div class="margin"
197
+ :style="{
198
+ padding: token.value,
199
+ }"
200
+ >
201
+ <div
202
+ class="margin-box"
203
+ />
204
+ </div>
205
+ </template>
206
+ </HstTokenList>
207
+ </Variant>
208
+ <Variant id="font-size" title="Font Size" icon="carbon:text-font">
209
+ <HstTokenList
210
+ :tokens="config.theme.fontSize"
211
+ :get-name="key => \`\${config.prefix ?? ''}text-\${key}\`"
212
+ >
213
+ <template #default="{ token }">
214
+ <div
215
+ class="truncate"
216
+ :style="{
217
+ fontSize: token.value[0],
218
+ ...token.value[1],
219
+ }"
220
+ >
221
+ {{ sampleText }}
222
+ </div>
223
+ </template>
224
+ </HstTokenList>
225
+ <template #controls>
226
+ <HstTextarea
227
+ v-model="sampleText"
228
+ title="Sample text"
229
+ rows="5"
230
+ />
231
+ </template>
232
+ </Variant>
233
+ <Variant id="font-weight" title="Font Weight" icon="carbon:text-font">
234
+ <HstTokenList
235
+ :tokens="config.theme.fontWeight"
236
+ :get-name="key => \`\${config.prefix ?? ''}font-\${key}\`"
237
+ >
238
+ <template #default="{ token }">
239
+ <div
240
+ class="truncate"
241
+ :style="{
242
+ fontWeight: token.value,
243
+ fontSize: \`\${fontSize}px\`,
244
+ }"
245
+ >
246
+ {{ sampleText }}
247
+ </div>
248
+ </template>
249
+ </HstTokenList>
250
+ <template #controls>
251
+ <HstTextarea
252
+ v-model="sampleText"
253
+ title="Sample text"
254
+ rows="5"
255
+ />
256
+ <HstNumber
257
+ v-model="fontSize"
258
+ title="Font size"
259
+ min="1"
260
+ />
261
+ </template>
262
+ </Variant>
263
+ <Variant id="font-family" title="Font Family" icon="carbon:text-font">
264
+ <HstTokenList
265
+ :tokens="config.theme.fontFamily"
266
+ :get-name="key => \`\${config.prefix ?? ''}font-\${key}\`"
267
+ >
268
+ <template #default="{ token }">
269
+ <div
270
+ class="truncate"
271
+ :style="{
272
+ fontFamily: token.value,
273
+ fontSize: \`\${fontSize}px\`,
274
+ }"
275
+ >
276
+ {{ sampleText }}
277
+ </div>
278
+ </template>
279
+ </HstTokenList>
280
+ <template #controls>
281
+ <HstTextarea
282
+ v-model="sampleText"
283
+ title="Sample text"
284
+ rows="5"
285
+ />
286
+ <HstNumber
287
+ v-model="fontSize"
288
+ title="Font size"
289
+ min="1"
290
+ />
291
+ </template>
292
+ </Variant>
293
+ <Variant id="letter-spacing" title="Letter Spacing" icon="carbon:text-font">
294
+ <HstTokenList
295
+ :tokens="config.theme.letterSpacing"
296
+ :get-name="key => \`\${config.prefix ?? ''}tracking-\${key}\`"
297
+ >
298
+ <template #default="{ token }">
299
+ <div
300
+ class="truncate"
301
+ :style="{
302
+ letterSpacing: token.value,
303
+ fontSize: \`\${fontSize}px\`,
304
+ }"
305
+ >
306
+ {{ sampleText }}
307
+ </div>
308
+ </template>
309
+ </HstTokenList>
310
+ <template #controls>
311
+ <HstTextarea
312
+ v-model="sampleText"
313
+ title="Sample text"
314
+ rows="5"
315
+ />
316
+ <HstNumber
317
+ v-model="fontSize"
318
+ title="Font size"
319
+ min="1"
320
+ />
321
+ </template>
322
+ </Variant>
323
+ <Variant id="line-height" title="Line Height" icon="carbon:text-font">
324
+ <HstTokenList
325
+ :tokens="config.theme.lineHeight"
326
+ :get-name="key => \`\${config.prefix ?? ''}leading-\${key}\`"
327
+ >
328
+ <template #default="{ token }">
329
+ <div
330
+ :style="{
331
+ lineHeight: token.value,
332
+ fontSize: \`\${fontSize}px\`,
333
+ }"
334
+ >
335
+ {{ sampleText }}
336
+ </div>
337
+ </template>
338
+ </HstTokenList>
339
+ <template #controls>
340
+ <HstTextarea
341
+ v-model="sampleText"
342
+ title="Sample text"
343
+ rows="5"
344
+ />
345
+ <HstNumber
346
+ v-model="fontSize"
347
+ title="Font size"
348
+ min="1"
349
+ />
350
+ </template>
351
+ </Variant>
352
+ <Variant id="drop-shadow" title="Drop Shadow" icon="carbon:shape-except">
353
+ <HstTokenGrid
354
+ :tokens="config.theme.dropShadow"
355
+ :get-name="key => (config.prefix ?? '') + (key === 'DEFAULT' ? 'drop-shadow' : \`drop-shadow-\${key}\`)"
356
+ :col-size="180"
357
+ >
358
+ <template #default="{ token }">
359
+ <div
360
+ class="drop-shadow"
361
+ :style="{
362
+ filter: \`\${(Array.isArray(token.value) ? token.value : [token.value]).map(v => \`drop-shadow(\${v})\`).join(' ')}\`,
363
+ }"
364
+ />
365
+ </template>
366
+ </HstTokenGrid>
367
+ </Variant>
368
+ <Variant id="border-radius" title="Border Radius" icon="carbon:condition-wait-point">
369
+ <HstTokenGrid
370
+ :tokens="config.theme.borderRadius"
371
+ :get-name="key => (config.prefix ?? '') + (key === 'DEFAULT' ? 'rounded' : \`rounded-\${key}\`)"
372
+ :col-size="180"
373
+ >
374
+ <template #default="{ token }">
375
+ <div
376
+ class="border-radius"
377
+ :style="{
378
+ borderRadius: token.value,
379
+ }"
380
+ />
381
+ </template>
382
+ </HstTokenGrid>
383
+ </Variant>
384
+ <Variant id="border-width" title="Border Width" icon="carbon:checkbox">
385
+ <HstTokenGrid
386
+ :tokens="config.theme.borderWidth"
387
+ :get-name="key => (config.prefix ?? '') + (key === 'DEFAULT' ? 'border' : \`border-\${key}\`)"
388
+ :col-size="180"
389
+ >
390
+ <template #default="{ token }">
391
+ <div
392
+ class="border-width"
393
+ :style="{
394
+ borderWidth: token.value,
395
+ }"
396
+ />
397
+ </template>
398
+ </HstTokenGrid>
399
+ </Variant>
400
+ <Variant id="width" title="Width" icon="carbon:pan-horizontal">
401
+ <HstTokenList
402
+ :tokens="config.theme.width"
403
+ :get-name="key => (config.prefix ?? '') + (key === 'DEFAULT' ? 'w' : \`w-\${key}\`)"
404
+ >
405
+ <template #default="{ token }">
406
+ <div class="width">
407
+ <div
408
+ class="width-box"
409
+ :style="{
410
+ width: token.value,
411
+ }"
412
+ />
413
+ </div>
414
+ </template>
415
+ </HstTokenList>
416
+ </Variant>
417
+ <Variant id="height" title="Height" icon="carbon:pan-vertical">
418
+ <HstTokenList
419
+ :tokens="config.theme.height"
420
+ :get-name="key => (config.prefix ?? '') + (key === 'DEFAULT' ? 'h' : \`h-\${key}\`)"
421
+ >
422
+ <template #default="{ token }">
423
+ <div
424
+ class="height"
425
+ :style="{
426
+ height: token.value,
427
+ }"
428
+ />
429
+ </template>
430
+ </HstTokenList>
431
+ </Variant>
432
+ <Variant id="full-config" title="Full Config" icon="carbon:code">
433
+ <pre>{{ config }}</pre>
434
+ </Variant>
435
+ </Story>
436
+ </template>
437
+ <style scoped>
438
+ .shade {
439
+ height: 80px;
440
+ border-radius: 4px;
441
+ }
442
+
443
+ .text {
444
+ font-size: 4rem;
445
+ display: flex;
446
+ align-items: flex-end;
447
+ }
448
+
449
+ .border {
450
+ border-style: solid;
451
+ border-width: 2px;
452
+ }
453
+
454
+ .padding {
455
+ background-color: rgb(113 113 122 / 0.1);
456
+ width: min-content;
457
+ }
458
+
459
+ .margin {
460
+ border: dashed 1px rgb(113 113 122 / 0.5);
461
+ width: min-content;
462
+ }
463
+
464
+ .padding-box,
465
+ .margin-box {
466
+ width: 5rem;
467
+ height: 5rem;
468
+ background-color: rgb(113 113 122 / 0.5);
469
+ }
470
+
471
+ .padding,
472
+ .padding-box,
473
+ .margin,
474
+ .margin-box,
475
+ .drop-shadow {
476
+ border-radius: 4px;
477
+ }
478
+
479
+ .truncate {
480
+ overflow: hidden;
481
+ text-overflow: ellipsis;
482
+ white-space: nowrap;
483
+ }
484
+
485
+ .drop-shadow {
486
+ width: 8rem;
487
+ height: 8rem;
488
+ background: white;
489
+ margin-bottom: 0.5rem;
490
+ }
491
+
492
+ .dark .drop-shadow {
493
+ background: #4e4e57;
494
+ }
495
+
496
+ .border-radius {
497
+ width: 8rem;
498
+ height: 8rem;
499
+ background-color: rgb(113 113 122 / 0.5);
500
+ }
501
+
502
+ .border-width {
503
+ width: 8rem;
504
+ height: 8rem;
505
+ border-color: rgb(113 113 122 / 0.5);
506
+ background-color: rgb(113 113 122 / 0.1);
507
+ }
508
+
509
+ .width {
510
+ background-color: rgb(113 113 122 / 0.1);
511
+ }
512
+
513
+ .width-box,
514
+ .height {
515
+ background-color: rgb(113 113 122 / 0.5);
516
+ }
517
+
518
+ .width-box {
519
+ height: 5rem;
520
+ }
521
+ </style>`