histoire 0.0.0 → 0.0.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.
Files changed (208) hide show
  1. package/LICENSE +21 -0
  2. package/client.d.ts +2 -0
  3. package/dist/client/app/App.vue +84 -21
  4. package/dist/client/app/assets/histoire-text-dark.svg +89 -0
  5. package/dist/client/app/assets/histoire-text.svg +89 -0
  6. package/dist/client/app/assets/histoire.svg +51 -0
  7. package/dist/client/app/codegen/serialize-js.d.ts +1 -0
  8. package/dist/client/app/codegen/serialize-js.js +114 -0
  9. package/dist/client/app/codegen/serialize-js.js.map +1 -0
  10. package/dist/client/app/codegen/serialize-js.ts +119 -0
  11. package/dist/client/app/codegen/util.d.ts +10 -0
  12. package/dist/client/app/codegen/util.js +71 -0
  13. package/dist/client/app/codegen/util.js.map +1 -0
  14. package/dist/client/app/codegen/util.ts +79 -0
  15. package/dist/client/app/codegen/vue3.d.ts +2 -0
  16. package/dist/client/app/codegen/vue3.js +239 -0
  17. package/dist/client/app/codegen/vue3.js.map +1 -0
  18. package/dist/client/app/codegen/vue3.ts +240 -0
  19. package/dist/client/app/components/HomeView.vue +15 -1
  20. package/dist/client/app/components/StoryControls.vue +7 -2
  21. package/dist/client/app/components/StoryList.vue +20 -13
  22. package/dist/client/app/components/StoryListFolder.vue +80 -0
  23. package/dist/client/app/components/StoryListItem.vue +38 -12
  24. package/dist/client/app/components/StorySourceCode.vue +121 -0
  25. package/dist/client/app/components/StoryVariantGridItem.vue +32 -17
  26. package/dist/client/app/components/StoryVariantListItem.vue +46 -0
  27. package/dist/client/app/components/StoryVariantSingleView.vue +117 -15
  28. package/dist/client/app/components/StoryView.vue +88 -54
  29. package/dist/client/app/components/app/AppHeader.vue +56 -0
  30. package/dist/client/app/components/app/AppLogo.vue +24 -0
  31. package/dist/client/app/components/base/BaseEmpty.vue +1 -1
  32. package/dist/client/app/components/base/BaseListItemLink.vue +55 -0
  33. package/dist/client/app/components/base/BaseSplitPane.vue +57 -21
  34. package/dist/client/app/components/exposed/Story.vue +23 -58
  35. package/dist/client/app/components/exposed/Variant.vue +24 -47
  36. package/dist/client/app/components/misc/HatchedPattern.vue +25 -0
  37. package/dist/client/app/components/{SandboxVue.vue → sandbox/SandboxVue3.vue} +31 -12
  38. package/dist/client/app/components/search/SearchItem.vue +110 -0
  39. package/dist/client/app/components/search/SearchModal.vue +179 -0
  40. package/dist/client/app/composable/variant.ts +20 -0
  41. package/dist/client/app/global-components.ts +10 -0
  42. package/dist/client/app/index.d.ts +1 -0
  43. package/dist/client/app/index.js +4 -1
  44. package/dist/client/app/index.js.map +1 -1
  45. package/dist/client/app/index.ts +18 -0
  46. package/dist/client/app/router.ts +17 -0
  47. package/dist/client/app/sandbox.d.ts +1 -0
  48. package/dist/client/app/sandbox.js +54 -0
  49. package/dist/client/app/sandbox.js.map +1 -0
  50. package/dist/client/app/sandbox.ts +60 -0
  51. package/dist/client/app/stores/story.d.ts +161 -50
  52. package/dist/client/app/stores/story.js +5 -16
  53. package/dist/client/app/stores/story.js.map +1 -1
  54. package/dist/client/app/stores/story.ts +22 -0
  55. package/dist/client/app/style/main.css +16 -2
  56. package/dist/client/app/style/sandbox.css +14 -0
  57. package/dist/client/app/types.d.ts +36 -4
  58. package/dist/client/app/types.ts +45 -0
  59. package/dist/client/app/util/config.d.ts +3 -0
  60. package/dist/client/app/util/config.js +5 -0
  61. package/dist/client/app/util/config.js.map +1 -0
  62. package/dist/client/app/util/config.ts +6 -0
  63. package/dist/client/app/util/const.d.ts +1 -0
  64. package/dist/client/app/util/const.js +2 -0
  65. package/dist/client/app/util/const.js.map +1 -0
  66. package/dist/client/app/util/const.ts +1 -0
  67. package/dist/client/app/util/dark.d.ts +2 -0
  68. package/dist/client/app/util/dark.js +4 -0
  69. package/dist/client/app/util/dark.js.map +1 -0
  70. package/dist/client/app/util/dark.ts +4 -0
  71. package/dist/client/app/util/env.d.ts +1 -0
  72. package/dist/client/app/util/env.js +2 -0
  73. package/dist/client/app/util/env.js.map +1 -0
  74. package/dist/client/app/util/keyboard.d.ts +8 -0
  75. package/dist/client/app/util/keyboard.js +83 -0
  76. package/dist/client/app/util/keyboard.js.map +1 -0
  77. package/dist/client/app/util/mapping.d.ts +3 -0
  78. package/dist/client/app/util/mapping.js +37 -0
  79. package/dist/client/app/util/mapping.js.map +1 -0
  80. package/dist/client/app/util/mapping.ts +38 -0
  81. package/dist/client/app/util/tooltip.d.ts +6 -0
  82. package/dist/client/app/util/tooltip.js +12 -0
  83. package/dist/client/app/util/tooltip.js.map +1 -0
  84. package/dist/client/index.d.ts +8 -0
  85. package/dist/client/index.js +4 -0
  86. package/dist/client/index.js.map +1 -0
  87. package/dist/client/index.ts +12 -0
  88. package/dist/client/server/Story.server.vue +65 -0
  89. package/dist/client/server/Variant.server.vue +50 -0
  90. package/dist/client/server/index.d.ts +2 -0
  91. package/dist/client/server/index.js +26 -0
  92. package/dist/client/server/index.js.map +1 -0
  93. package/dist/client/server/index.ts +29 -0
  94. package/dist/client/shim.d.ts +5 -0
  95. package/dist/node/bin.js +8 -2
  96. package/dist/node/bin.js.map +1 -1
  97. package/dist/node/build.d.ts +2 -0
  98. package/dist/node/build.js +72 -0
  99. package/dist/node/build.js.map +1 -0
  100. package/dist/node/collect.d.ts +12 -0
  101. package/dist/node/collect.js +66 -0
  102. package/dist/node/collect.js.map +1 -0
  103. package/dist/node/colors.d.ts +274 -0
  104. package/dist/node/colors.js +313 -0
  105. package/dist/node/colors.js.map +1 -0
  106. package/dist/node/commands/build.d.ts +1 -0
  107. package/dist/node/commands/build.js +11 -0
  108. package/dist/node/commands/build.js.map +1 -0
  109. package/dist/node/commands/dev.d.ts +1 -1
  110. package/dist/node/commands/dev.js +6 -11
  111. package/dist/node/commands/dev.js.map +1 -1
  112. package/dist/node/config.d.ts +99 -1
  113. package/dist/node/config.js +90 -1
  114. package/dist/node/config.js.map +1 -1
  115. package/dist/node/context.d.ts +13 -0
  116. package/dist/node/context.js +12 -1
  117. package/dist/node/context.js.map +1 -1
  118. package/dist/node/dom/dom-keys.d.ts +4 -0
  119. package/dist/node/dom/dom-keys.js +224 -0
  120. package/dist/node/dom/dom-keys.js.map +1 -0
  121. package/dist/node/dom/env.d.ts +5 -0
  122. package/dist/node/dom/env.js +23 -0
  123. package/dist/node/dom/env.js.map +1 -0
  124. package/dist/node/index.d.ts +2 -0
  125. package/dist/node/index.js +2 -1
  126. package/dist/node/index.js.map +1 -1
  127. package/dist/node/plugin.d.ts +8 -2
  128. package/dist/node/plugin.js +142 -21
  129. package/dist/node/plugin.js.map +1 -1
  130. package/dist/node/server.d.ts +4 -1
  131. package/dist/node/server.js +80 -20
  132. package/dist/node/server.js.map +1 -1
  133. package/dist/node/stories.d.ts +8 -9
  134. package/dist/node/stories.js +42 -22
  135. package/dist/node/stories.js.map +1 -1
  136. package/dist/node/tree.d.ts +19 -0
  137. package/dist/node/tree.js +55 -0
  138. package/dist/node/tree.js.map +1 -0
  139. package/dist/node/types.d.ts +38 -0
  140. package/dist/node/types.js +2 -0
  141. package/dist/node/types.js.map +1 -0
  142. package/dist/style.css +558 -52
  143. package/package.json +42 -25
  144. package/scripts/copy.mjs +11 -0
  145. package/src/client/app/App.vue +84 -21
  146. package/src/client/app/assets/histoire-text-dark.svg +89 -0
  147. package/src/client/app/assets/histoire-text.svg +89 -0
  148. package/src/client/app/assets/histoire.svg +51 -0
  149. package/src/client/app/codegen/serialize-js.ts +119 -0
  150. package/src/client/app/codegen/util.ts +79 -0
  151. package/src/client/app/codegen/vue3.ts +240 -0
  152. package/src/client/app/components/HomeView.vue +15 -1
  153. package/src/client/app/components/StoryControls.vue +7 -2
  154. package/src/client/app/components/StoryList.vue +20 -13
  155. package/src/client/app/components/StoryListFolder.vue +80 -0
  156. package/src/client/app/components/StoryListItem.vue +38 -12
  157. package/src/client/app/components/StorySourceCode.vue +121 -0
  158. package/src/client/app/components/StoryVariantGridItem.vue +32 -17
  159. package/src/client/app/components/StoryVariantListItem.vue +46 -0
  160. package/src/client/app/components/StoryVariantSingleView.vue +117 -15
  161. package/src/client/app/components/StoryView.vue +88 -54
  162. package/src/client/app/components/app/AppHeader.vue +56 -0
  163. package/src/client/app/components/app/AppLogo.vue +24 -0
  164. package/src/client/app/components/base/BaseEmpty.vue +1 -1
  165. package/src/client/app/components/base/BaseListItemLink.vue +55 -0
  166. package/src/client/app/components/base/BaseSplitPane.vue +57 -21
  167. package/src/client/app/components/exposed/Story.vue +23 -58
  168. package/src/client/app/components/exposed/Variant.vue +24 -47
  169. package/src/client/app/components/misc/HatchedPattern.vue +25 -0
  170. package/src/client/app/components/{SandboxVue.vue → sandbox/SandboxVue3.vue} +31 -12
  171. package/src/client/app/components/search/SearchItem.vue +110 -0
  172. package/src/client/app/components/search/SearchModal.vue +179 -0
  173. package/src/client/app/index.ts +4 -1
  174. package/src/client/app/sandbox.ts +60 -0
  175. package/src/client/app/stores/story.ts +6 -18
  176. package/src/client/app/style/main.css +16 -2
  177. package/src/client/app/style/sandbox.css +14 -0
  178. package/src/client/app/types.ts +42 -4
  179. package/src/client/app/util/config.ts +6 -0
  180. package/src/client/app/util/const.ts +1 -0
  181. package/src/client/app/util/dark.ts +4 -0
  182. package/src/client/app/util/env.ts +1 -0
  183. package/src/client/app/util/keyboard.ts +99 -0
  184. package/src/client/app/util/mapping.ts +38 -0
  185. package/src/client/app/util/tooltip.ts +13 -0
  186. package/src/client/index.ts +12 -0
  187. package/src/client/server/Story.server.vue +65 -0
  188. package/src/client/server/Variant.server.vue +50 -0
  189. package/src/client/server/index.ts +29 -0
  190. package/src/node/bin.ts +9 -2
  191. package/src/node/build.ts +80 -0
  192. package/src/node/collect.ts +78 -0
  193. package/src/node/colors.ts +328 -0
  194. package/src/node/commands/build.ts +12 -0
  195. package/src/node/commands/dev.ts +6 -13
  196. package/src/node/config.ts +193 -1
  197. package/src/node/context.ts +21 -1
  198. package/src/node/dom/dom-keys.ts +227 -0
  199. package/src/node/dom/env.ts +26 -0
  200. package/src/node/index.ts +2 -0
  201. package/src/node/plugin.ts +146 -22
  202. package/src/node/server.ts +95 -20
  203. package/src/node/stories.ts +53 -31
  204. package/src/node/tree.ts +88 -0
  205. package/src/node/types.ts +40 -0
  206. package/tailwind.config.cjs +41 -1
  207. package/dist/client/app/components/StoryVariantItem.vue +0 -29
  208. package/src/client/app/components/StoryVariantItem.vue +0 -29
@@ -1,47 +1,88 @@
1
- import { defineConfig, mergeConfig, Plugin } from 'vite'
1
+ import { relative } from 'pathe'
2
+ import { resolveConfig as resolveViteConfig, Plugin as VitePlugin } from 'vite'
3
+ import { lookup as lookupMime } from 'mrmime'
2
4
  import { APP_PATH, DIST_CLIENT_PATH } from './alias.js'
3
5
  import { Context } from './context.js'
4
- import { stories } from './stories.js'
6
+ import { notifyStoryChange } from './stories.js'
7
+ import { makeTree } from './tree.js'
8
+ import { parseColor } from './colors.js'
5
9
 
6
10
  export const STORIES_ID = '$histoire-stories'
7
11
  export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`
12
+ export const SETUP_ID = '$histoire-setup'
13
+ export const NOOP_ID = '/$histoire-noop'
14
+ export const CONFIG_ID = '$histoire-config'
15
+ export const RESOLVED_CONFIG_ID = `/${CONFIG_ID}-resolved`
16
+ export const THEME_ID = '$histoire-theme'
17
+ export const RESOLVED_THEME_ID = `/${THEME_ID}-resolved.css`
8
18
 
9
- export async function createVitePlugins (ctx: Context): Promise<Plugin[]> {
10
- const userViteConfig = {} // @TODO
19
+ export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
20
+ const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build')
21
+ const hasVuePlugin = viteConfig.plugins.find(p => p.name === 'vite:vue')
11
22
 
12
- const vuePlugin = (await import('@vitejs/plugin-vue')).default()
23
+ const plugins: VitePlugin[] = []
13
24
 
14
- const vitePlugin: Plugin = {
25
+ if (!hasVuePlugin) {
26
+ plugins.push((await import('@vitejs/plugin-vue')).default())
27
+ }
28
+
29
+ plugins.push({
15
30
  name: 'histoire-vite-plugin',
16
31
 
17
32
  config () {
18
- const baseConfig = defineConfig({
19
-
33
+ return {
20
34
  optimizeDeps: {
21
35
  // force include vue to avoid duplicated copies when linked + optimized
22
- include: ['vue'],
36
+ include: [
37
+ 'vue',
38
+ '@vue/runtime-core',
39
+ ],
23
40
  },
24
41
  server: {
25
42
  fs: {
26
- allow: [DIST_CLIENT_PATH, ctx.config.sourceDir, process.cwd()],
43
+ allow: [DIST_CLIENT_PATH, viteConfig.root, process.cwd()],
27
44
  },
28
45
  },
29
- })
30
- return userViteConfig
31
- ? mergeConfig(userViteConfig, baseConfig)
32
- : baseConfig
46
+ }
33
47
  },
34
48
 
35
- resolveId (id) {
49
+ async resolveId (id, importer) {
36
50
  if (id.startsWith(STORIES_ID)) {
37
51
  return RESOLVED_STORIES_ID
38
52
  }
53
+ if (id.startsWith(SETUP_ID)) {
54
+ if (ctx.config.setupFile) {
55
+ return this.resolve(ctx.config.setupFile, importer, {
56
+ skipSelf: true,
57
+ })
58
+ } else {
59
+ return NOOP_ID
60
+ }
61
+ }
62
+ if (id.startsWith(CONFIG_ID)) {
63
+ return RESOLVED_CONFIG_ID
64
+ }
65
+ if (id.startsWith(THEME_ID)) {
66
+ return RESOLVED_THEME_ID
67
+ }
39
68
  },
40
69
 
41
70
  load (id) {
42
71
  if (id === RESOLVED_STORIES_ID) {
43
- return `${stories.map((story, index) => `import Comp${index} from '/${story.file}'`).join('\n')}
44
- export let files = [${stories.map((story, index) => `{ id: '${story.id}', file: '/${story.file}', component: Comp${index} }`).join(',\n')}]
72
+ const resolvedStories = ctx.storyFiles.filter(s => !!s.story)
73
+ const files = resolvedStories.map((file, index) => {
74
+ return {
75
+ id: file.id,
76
+ path: file.treePath,
77
+ story: file.story,
78
+ framework: 'vue3',
79
+ index,
80
+ }
81
+ })
82
+ return `import { defineAsyncComponent } from 'vue'
83
+ ${resolvedStories.map((file, index) => `const Comp${index} = defineAsyncComponent(() => import('${file.path}'))`).join('\n')}
84
+ export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}}`).join(',\n')}]
85
+ export let tree = ${JSON.stringify(makeTree(ctx.config, files))}
45
86
  const handlers = []
46
87
  export function onUpdate (cb) {
47
88
  handlers.push(cb)
@@ -49,16 +90,79 @@ export function onUpdate (cb) {
49
90
  if (import.meta.hot) {
50
91
  import.meta.hot.accept(newModule => {
51
92
  files = newModule.files
93
+ tree = newModule.tree
52
94
  handlers.forEach(h => {
53
- h(newModule.files)
95
+ h(newModule.files, newModule.tree)
54
96
  newModule.onUpdate(h)
55
97
  })
56
98
  })
57
99
  }`
58
100
  }
101
+ if (id === NOOP_ID) {
102
+ return `export default () => {}`
103
+ }
104
+ if (id === RESOLVED_CONFIG_ID) {
105
+ let js = `export const config = ${JSON.stringify(ctx.config)}\n`
106
+ if (ctx.config.theme?.logo) {
107
+ for (const key in ctx.config.theme.logo) {
108
+ js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`
109
+ }
110
+ }
111
+ js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`
112
+ return js
113
+ }
114
+ if (id === RESOLVED_THEME_ID) {
115
+ let css = '*, ::before, ::after {'
116
+ // Colors
117
+ for (const color in ctx.config.theme?.colors ?? {}) {
118
+ for (const key in ctx.config.theme.colors[color]) {
119
+ css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`
120
+ }
121
+ }
122
+ css += '}'
123
+ return css
124
+ }
125
+ },
126
+
127
+ handleHotUpdate (updateContext) {
128
+ const story = ctx.storyFiles.find(file => file.path === updateContext.file)
129
+ if (story) {
130
+ notifyStoryChange(story)
131
+ }
59
132
  },
60
133
 
61
134
  configureServer (server) {
135
+ server.middlewares.use((req, res, next) => {
136
+ if (req.url!.startsWith('/__sandbox')) {
137
+ res.statusCode = 200
138
+ res.end(`
139
+ <!DOCTYPE html>
140
+ <html>
141
+ <head>
142
+ <title></title>
143
+ <meta charset="utf-8">
144
+ <meta name="viewport" content="width=device-width,initial-scale=1">
145
+ <meta name="description" content="">
146
+ </head>
147
+ <body>
148
+ <div id="app"></div>
149
+ <script>
150
+ // Hide spammy vite messages
151
+ const origConsoleLog = console.log
152
+ console.log = (...args) => {
153
+ if (typeof args[0] !== 'string' || !args[0].startsWith('[vite] connect')) {
154
+ origConsoleLog(...args)
155
+ }
156
+ }
157
+ </script>
158
+ <script type="module" src="/@fs/${APP_PATH}/sandbox.js"></script>
159
+ </body>
160
+ </html>`)
161
+ return
162
+ }
163
+ next()
164
+ })
165
+
62
166
  // serve our index.html after vite history fallback
63
167
  return () => {
64
168
  server.middlewares.use((req, res, next) => {
@@ -69,9 +173,11 @@ if (import.meta.hot) {
69
173
  <html>
70
174
  <head>
71
175
  <title></title>
176
+ <link rel="icon" href=""/>
72
177
  <meta charset="utf-8">
73
178
  <meta name="viewport" content="width=device-width,initial-scale=1">
74
179
  <meta name="description" content="">
180
+ ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
75
181
  </head>
76
182
  <body>
77
183
  <div id="app"></div>
@@ -84,9 +190,27 @@ if (import.meta.hot) {
84
190
  })
85
191
  }
86
192
  },
193
+ })
194
+
195
+ if (ctx.mode === 'build') {
196
+ // Add file name in build mode to have components names instead of <Anonymous>
197
+ const include = [/\.vue$/]
198
+ const exclude = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/]
199
+ plugins.push({
200
+ name: 'histoire-file-name-plugin',
201
+ enforce: 'post',
202
+
203
+ transform (code, id) {
204
+ if (exclude.some(r => r.test(id))) return
205
+ if (include.some(r => r.test(id))) {
206
+ const file = relative(viteConfig.root, id)
207
+ const index = code.indexOf('export default')
208
+ const result = `${code.substring(0, index)}_sfc_main.__file = '${file}'\n${code.substring(index)}`
209
+ return result
210
+ }
211
+ },
212
+ })
87
213
  }
88
- return [
89
- vitePlugin,
90
- vuePlugin,
91
- ]
214
+
215
+ return plugins
92
216
  }
@@ -1,12 +1,14 @@
1
1
  import { createServer as createViteServer } from 'vite'
2
2
  import { Context } from './context.js'
3
3
  import { createVitePlugins, RESOLVED_STORIES_ID } from './plugin.js'
4
- import { onStoryChange, watchStories } from './stories.js'
4
+ import { notifyStoryChange, onStoryChange, watchStories } from './stories.js'
5
+ import { useCollectStories } from './collect.js'
6
+ import type { StoryFile } from './types.js'
5
7
 
6
- export async function createServer (ctx: Context) {
8
+ export async function createServer (ctx: Context, port: number) {
7
9
  await watchStories(ctx)
10
+
8
11
  const server = await createViteServer({
9
- root: ctx.config.sourceDir,
10
12
  plugins: await createVitePlugins(ctx),
11
13
  server: {
12
14
  watch: {
@@ -14,24 +16,97 @@ export async function createServer (ctx: Context) {
14
16
  },
15
17
  },
16
18
  })
17
- onStoryChange(() => {
19
+ await server.pluginContainer.buildStart({})
20
+ // Wait for pre-bundling (in `listen()`)
21
+ await server.listen(port)
22
+
23
+ const {
24
+ clearCache,
25
+ executeStoryFile,
26
+ destroy: destroyCollectStories,
27
+ } = useCollectStories({
28
+ server,
29
+ }, ctx)
30
+
31
+ // onStoryChange debouncing
32
+ let queue: Promise<void>
33
+ let queueResolve: () => void
34
+ let queued = false
35
+ let queuedFiles: StoryFile[] = []
36
+
37
+ onStoryChange(async (changedFile) => {
38
+ if (queue) {
39
+ if (queued && changedFile) {
40
+ queuedFiles.push(changedFile)
41
+ return
42
+ } else {
43
+ queued = true
44
+ // Next call
45
+ await queue
46
+ }
47
+ }
48
+ queued = false
49
+ queue = new Promise(resolve => {
50
+ queueResolve = resolve
51
+ })
52
+
53
+ clearCache()
54
+
55
+ if (changedFile) {
56
+ // Granular update
57
+ await executeStoryFile(changedFile)
58
+ } else {
59
+ // Full update
60
+ for (const storyFile of ctx.storyFiles) {
61
+ await executeStoryFile(storyFile)
62
+ }
63
+ }
64
+ // Queued updates
65
+ for (const storyFile of queuedFiles) {
66
+ await executeStoryFile(storyFile)
67
+ }
68
+
69
+ queuedFiles = []
70
+ queueResolve()
71
+
72
+ // Invalidate stories data module
18
73
  const mod = server.moduleGraph.getModuleById(RESOLVED_STORIES_ID)
19
- if (mod) {
20
- server.moduleGraph.invalidateModule(mod)
21
- const timestamp = Date.now()
22
- mod.lastHMRTimestamp = timestamp
23
- server.ws.send({
24
- type: 'update',
25
- updates: [
26
- {
27
- type: 'js-update',
28
- acceptedPath: mod.url,
29
- path: mod.url,
30
- timestamp: timestamp,
31
- },
32
- ],
33
- })
74
+ if (!mod) {
75
+ return
76
+ }
77
+ server.moduleGraph.invalidateModule(mod)
78
+
79
+ // Send stories data HMR update
80
+ const timestamp = Date.now()
81
+ mod.lastHMRTimestamp = timestamp
82
+ server.ws.send({
83
+ type: 'update',
84
+ updates: [
85
+ {
86
+ type: 'js-update',
87
+ acceptedPath: mod.url,
88
+ path: mod.url,
89
+ timestamp: timestamp,
90
+ },
91
+ ],
92
+ })
93
+ })
94
+
95
+ async function close () {
96
+ await server.close()
97
+ await destroyCollectStories()
98
+ }
99
+
100
+ // On page refresh, refresh stories
101
+ // Useful when vite optimizes new dependencies
102
+ server.ws.on('connection', (socket) => {
103
+ if (socket.protocol === 'vite-hmr') {
104
+ notifyStoryChange()
34
105
  }
35
106
  })
36
- return server
107
+
108
+ return {
109
+ server,
110
+ close,
111
+ }
37
112
  }
@@ -1,54 +1,76 @@
1
- import { Context } from './context.js'
2
1
  import chokidar from 'chokidar'
3
2
  import { globby } from 'globby'
4
3
  import Case from 'case'
4
+ import { join } from 'pathe'
5
+ import { Context } from './context.js'
6
+ import type { StoryFile } from './types.js'
5
7
 
6
- export interface Story {
7
- id: string
8
- file: string
9
- }
10
-
11
- export const stories: Story[] = []
8
+ type StoryChangeHandler = (file?: StoryFile) => unknown
9
+ const handlers: StoryChangeHandler[] = []
12
10
 
13
- const handlers: (() => unknown)[] = []
11
+ let context: Context
14
12
 
15
- export function onStoryChange (handler: () => unknown) {
13
+ export function onStoryChange (handler: StoryChangeHandler) {
16
14
  handlers.push(handler)
17
15
  }
18
16
 
19
- export async function watchStories (ctx: Context) {
20
- const watcher = chokidar.watch(ctx.config.storyMatch, {
21
- cwd: ctx.config.sourceDir,
22
- })
23
- watcher.on('add', (file) => {
24
- addStory(file)
25
- notifyChange()
26
- })
27
- watcher.on('unlink', (file) => {
28
- stories.splice(stories.findIndex((story) => story.file === file), 1)
29
- notifyChange()
17
+ export async function watchStories (newContext: Context) {
18
+ context = newContext
19
+ const watcher = chokidar.watch(context.config.storyMatch, {
20
+ cwd: context.root,
21
+ ignored: context.config.storyIgnored,
30
22
  })
23
+
24
+ watcher
25
+ .on('add', (file) => {
26
+ const storyFile = addStory(file)
27
+ setTimeout(() => notifyStoryChange(storyFile), 100) // Delay in case file renaming fired Add event before Unlink event
28
+ })
29
+ .on('unlink', (file) => {
30
+ removeStory(file)
31
+ notifyStoryChange()
32
+ })
33
+
31
34
  return watcher
32
35
  }
33
36
 
34
- function notifyChange () {
37
+ export function notifyStoryChange (file?: StoryFile) {
35
38
  for (const handler of handlers) {
36
- handler()
39
+ handler(file)
37
40
  }
38
41
  }
39
42
 
40
- function addStory (file: string) {
41
- stories.push({
42
- id: Case.kebab(file),
43
- file,
44
- })
43
+ function getAbsoluteFilePath (relativeFilePath: string) {
44
+ return join(context.root, relativeFilePath)
45
+ }
46
+
47
+ function addStory (relativeFilePath: string) {
48
+ const absoluteFilePath = getAbsoluteFilePath(relativeFilePath)
49
+ const fileId = Case.kebab(relativeFilePath)
50
+
51
+ const file: StoryFile = {
52
+ id: fileId,
53
+ path: absoluteFilePath,
54
+ moduleId: `/${relativeFilePath}`,
55
+ }
56
+ context.storyFiles.push(file)
57
+ return file
58
+ }
59
+
60
+ function removeStory (relativeFilePath: string) {
61
+ const fileId = Case.kebab(relativeFilePath)
62
+ const index = context.storyFiles.findIndex((file) => file.id === fileId)
63
+ if (index !== -1) context.storyFiles.splice(index, 1)
45
64
  }
46
65
 
47
- export async function findStories (ctx: Context) {
48
- const files = await globby(ctx.config.storyMatch, {
49
- cwd: ctx.config.sourceDir,
66
+ export async function findAllStories (newContext: Context) {
67
+ context = newContext
68
+
69
+ const files = await globby(context.config.storyMatch, {
70
+ cwd: context.root,
71
+ ignore: context.config.storyIgnored,
50
72
  })
51
- stories.splice(0, stories.length)
73
+ context.storyFiles.length = 0
52
74
  for (const file of files) {
53
75
  addStory(file)
54
76
  }
@@ -0,0 +1,88 @@
1
+ import { HistoireConfig } from './config.js'
2
+
3
+ export type TreeFile = {
4
+ title: string
5
+ path: string
6
+ }
7
+
8
+ export type TreeLeaf = {
9
+ title: string
10
+ index: number
11
+ }
12
+
13
+ export type TreeFolder = {
14
+ title: string
15
+ children: (TreeFolder | TreeLeaf)[]
16
+ }
17
+
18
+ export type Tree = (TreeFolder | TreeLeaf)[]
19
+
20
+ export function createPath (config: HistoireConfig, file: TreeFile) {
21
+ if (config.tree.file === 'title') {
22
+ return file.title.split('/')
23
+ }
24
+
25
+ if (config.tree.file === 'path') {
26
+ const path = file.path.split('/')
27
+ path.pop()
28
+ path.push(file.title)
29
+ return path
30
+ }
31
+
32
+ return config.tree.file(file)
33
+ }
34
+
35
+ export function makeTree (config: HistoireConfig, files: { path: string[], index: number }[]) {
36
+ interface ITreeObject {
37
+ [index: string]: number | ITreeObject
38
+ }
39
+
40
+ const treeObject: ITreeObject = {}
41
+
42
+ for (const file of files) {
43
+ setPath(file.path, file.index, treeObject)
44
+ }
45
+
46
+ let sortingFunction = (a: string, b: string) => a.localeCompare(b)
47
+
48
+ if (config.tree.order !== 'asc') {
49
+ sortingFunction = config.tree.order
50
+ }
51
+
52
+ return buildTree(treeObject)
53
+
54
+ // Undefined behavior for folder and file that share the same name under the same directory
55
+ function setPath (path: string[], value: unknown, tree: unknown) {
56
+ path.reduce((subtree, key, i) => {
57
+ if (!subtree[key]) {
58
+ subtree[key] = {}
59
+ }
60
+ if (i === path.length - 1) {
61
+ subtree[key] = value
62
+ }
63
+ return subtree[key]
64
+ }, tree)
65
+ }
66
+
67
+ function buildTree (treeObject: ITreeObject): Tree {
68
+ const tree: Tree = []
69
+
70
+ for (const [key, element] of Object.entries(treeObject)) {
71
+ if (Number.isInteger(element)) {
72
+ tree.push({
73
+ title: key,
74
+ index: element as number,
75
+ })
76
+ } else {
77
+ tree.push({
78
+ title: key,
79
+ children: buildTree(element as ITreeObject),
80
+ })
81
+ }
82
+ }
83
+
84
+ tree.sort((a, b) => sortingFunction(a.title, b.title))
85
+
86
+ return tree
87
+ }
88
+ }
@@ -0,0 +1,40 @@
1
+ export interface StoryFile {
2
+ id: string
3
+ /**
4
+ * Absolute path
5
+ */
6
+ path: string
7
+ /**
8
+ * Generated path for tree UI
9
+ */
10
+ treePath?: string[]
11
+ /**
12
+ * Use the module id in imports to allow HMR
13
+ */
14
+ moduleId: string
15
+ /**
16
+ * Resolved story data from story file execution
17
+ */
18
+ story?: Story
19
+ }
20
+
21
+ export interface Story {
22
+ id: string
23
+ title: string
24
+ variants: Variant[]
25
+ layout?: {
26
+ type: 'single'
27
+ } | {
28
+ type: 'grid'
29
+ width: number
30
+ }
31
+ icon?: string
32
+ iconColor?: string
33
+ }
34
+
35
+ export interface Variant {
36
+ id: string
37
+ title: string
38
+ icon?: string
39
+ iconColor?: string
40
+ }
@@ -1,5 +1,41 @@
1
+
2
+ const inheritedConfig = require('../../tailwind.config.cjs')
3
+ const defaultColors = require('tailwindcss/colors')
4
+
5
+ // Colors
6
+
7
+ function withOpacityValue (variable) {
8
+ return ({ opacityValue }) => {
9
+ if (opacityValue === undefined) {
10
+ return `rgb(var(${variable}))`
11
+ }
12
+ return `rgb(var(${variable}) / ${opacityValue})`
13
+ }
14
+ }
15
+
16
+ const colorKeys = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900]
17
+ const grayKeys = [...colorKeys, 750, 850, 950]
18
+ const themedColors = ['primary', 'gray'].reduce((acc, color) => {
19
+ const keys = (color === 'gray' ? grayKeys : colorKeys)
20
+ for (const key of keys) {
21
+ acc[`${color}-${key}`] = withOpacityValue(`--_histoire-color-${color}-${key}`)
22
+ }
23
+ return acc
24
+ }, {})
25
+
26
+ const colors = {
27
+ ...{
28
+ ...defaultColors,
29
+ gray: undefined,
30
+ },
31
+ white: '#fff',
32
+ black: '#000',
33
+ transparent: 'transparent',
34
+ ...themedColors,
35
+ }
36
+
1
37
  module.exports = {
2
- ...require('../../tailwind.config.cjs'),
38
+ ...inheritedConfig,
3
39
  content: [
4
40
  './src/**/*.{vue,js,ts,jsx,tsx,md}',
5
41
  ],
@@ -7,3 +43,7 @@ module.exports = {
7
43
  preflight: false,
8
44
  },
9
45
  }
46
+
47
+ delete module.exports.theme.extend.colors.primary
48
+ delete module.exports.theme.extend.colors.gray
49
+ module.exports.theme.colors = colors
@@ -1,29 +0,0 @@
1
- <script lang="ts" setup>
2
- import { PropType, toRefs } from 'vue'
3
- import { useCurrentVariantRoute } from '../composable/variant'
4
- import { Variant } from '../types'
5
-
6
- const props = defineProps({
7
- variant: {
8
- type: Object as PropType<Variant>,
9
- required: true,
10
- },
11
- })
12
-
13
- const { variant } = toRefs(props)
14
- const { isActive, targetRoute } = useCurrentVariantRoute(variant)
15
- </script>
16
-
17
- <template>
18
- <div>
19
- <RouterLink
20
- :to="targetRoute"
21
- class="htw-p-2 htw-flex htw-space-x-2 hover:htw-bg-primary-100 dark:htw-bg-primary-900"
22
- :class="{
23
- 'htw-bg-primary-200 hover:htw-bg-primary-300 htw-text-primary-800': isActive,
24
- }"
25
- >
26
- {{ variant.title }}
27
- </RouterLink>
28
- </div>
29
- </template>