histoire 0.10.7 → 0.11.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 (40) hide show
  1. package/client.d.ts +15 -0
  2. package/client.js +14 -0
  3. package/dist/node/alias.js +1 -1
  4. package/dist/node/bin.js +2 -2
  5. package/dist/node/build.js +2 -1
  6. package/dist/node/builtin-plugins/vanilla-support/MountStory.d.ts +1 -1
  7. package/dist/node/builtin-plugins/vanilla-support/RenderStory.js +2 -2
  8. package/dist/node/builtin-plugins/vanilla-support/plugin.js +1 -1
  9. package/dist/node/collect/index.js +17 -5
  10. package/dist/node/collect/worker.d.ts +1 -1
  11. package/dist/node/collect/worker.js +2 -1
  12. package/dist/node/config.d.ts +1 -1
  13. package/dist/node/config.js +20 -26
  14. package/dist/node/markdown.d.ts +1 -0
  15. package/dist/node/markdown.js +15 -2
  16. package/dist/node/preview.js +1 -1
  17. package/dist/node/search.js +3 -3
  18. package/dist/node/server.js +23 -8
  19. package/dist/node/stories.d.ts +12 -1
  20. package/dist/node/stories.js +28 -11
  21. package/dist/node/util/find-up.js +1 -1
  22. package/dist/node/vite.d.ts +9 -9
  23. package/dist/node/vite.js +87 -31
  24. package/package.json +16 -10
  25. package/src/node/alias.ts +1 -1
  26. package/src/node/bin.ts +2 -2
  27. package/src/node/build.ts +2 -1
  28. package/src/node/builtin-plugins/vanilla-support/RenderStory.ts +2 -2
  29. package/src/node/builtin-plugins/vanilla-support/plugin.ts +1 -1
  30. package/src/node/collect/index.ts +21 -5
  31. package/src/node/collect/worker.ts +4 -2
  32. package/src/node/config.ts +21 -26
  33. package/src/node/markdown.ts +19 -2
  34. package/src/node/preview.ts +1 -1
  35. package/src/node/search.ts +3 -3
  36. package/src/node/server.ts +24 -9
  37. package/src/node/stories.ts +34 -13
  38. package/src/node/util/find-up.ts +1 -1
  39. package/src/node/vite.ts +87 -31
  40. package/tsconfig.json +0 -1
@@ -1,20 +1,47 @@
1
1
  import chokidar from 'chokidar'
2
2
  import { globby } from 'globby'
3
- import Case from 'case'
3
+ import { paramCase } from 'change-case'
4
4
  import { resolve, basename } from 'pathe'
5
5
  import micromatch from 'micromatch'
6
6
  import type { ServerStoryFile } from '@histoire/shared'
7
7
  import { Context } from './context.js'
8
8
 
9
9
  type StoryChangeHandler = (file?: ServerStoryFile) => unknown
10
- const handlers: StoryChangeHandler[] = []
11
-
12
- let context: Context
10
+ const storyChangeHandlers: StoryChangeHandler[] = []
13
11
 
12
+ /**
13
+ * Called when a new story is added or modified. Collecting should be done.
14
+ * @param handler
15
+ */
14
16
  export function onStoryChange (handler: StoryChangeHandler) {
15
- handlers.push(handler)
17
+ storyChangeHandlers.push(handler)
18
+ }
19
+
20
+ export function notifyStoryChange (file?: ServerStoryFile) {
21
+ for (const handler of storyChangeHandlers) {
22
+ handler(file)
23
+ }
24
+ }
25
+
26
+ type StoryListChangeHandler = () => unknown
27
+ const storyListChangeHandlers: StoryListChangeHandler[] = []
28
+
29
+ /**
30
+ * Called when the story list has changed (ex: removed a story). No collecting should be needed.
31
+ * @param handler
32
+ */
33
+ export function onStoryListChange (handler: StoryListChangeHandler) {
34
+ storyListChangeHandlers.push(handler)
16
35
  }
17
36
 
37
+ export function notifyStoryListChange () {
38
+ for (const handler of storyListChangeHandlers) {
39
+ handler()
40
+ }
41
+ }
42
+
43
+ let context: Context
44
+
18
45
  export async function watchStories (newContext: Context) {
19
46
  context = newContext
20
47
  const watcher = chokidar.watch(context.config.storyMatch, {
@@ -29,25 +56,19 @@ export async function watchStories (newContext: Context) {
29
56
  })
30
57
  .on('unlink', (file) => {
31
58
  removeStory(file)
32
- notifyStoryChange()
59
+ notifyStoryListChange()
33
60
  })
34
61
 
35
62
  return watcher
36
63
  }
37
64
 
38
- export function notifyStoryChange (file?: ServerStoryFile) {
39
- for (const handler of handlers) {
40
- handler(file)
41
- }
42
- }
43
-
44
65
  function getAbsoluteFilePath (relativeFilePath: string) {
45
66
  return resolve(context.root, relativeFilePath)
46
67
  }
47
68
 
48
69
  export function addStory (relativeFilePath: string, virtualModuleCode?: string) {
49
70
  const absoluteFilePath = getAbsoluteFilePath(relativeFilePath)
50
- const fileId = Case.kebab(relativeFilePath)
71
+ const fileId = paramCase(relativeFilePath.toLowerCase())
51
72
  let fileName = basename(relativeFilePath)
52
73
  if (fileName.includes('.')) {
53
74
  fileName = fileName.substring(0, fileName.indexOf('.'))
@@ -1,5 +1,5 @@
1
1
  import path from 'pathe'
2
- import fs from 'fs'
2
+ import fs from 'node:fs'
3
3
 
4
4
  export function findUp (cwd: string = process.cwd(), fileNames: string[]): string {
5
5
  let { root } = path.parse(cwd)
package/src/node/vite.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { createRequire } from 'module'
1
+ import { createRequire } from 'node:module'
2
2
  import { relative, resolve, join, dirname } from 'pathe'
3
3
  import {
4
4
  resolveConfig as resolveViteConfigInternal,
@@ -10,6 +10,7 @@ import {
10
10
  loadConfigFromFile as loadViteConfigFromFile,
11
11
  } from 'vite'
12
12
  import { lookup as lookupMime } from 'mrmime'
13
+ import fs from 'fs-extra'
13
14
  import { APP_PATH, TEMP_PATH } from './alias.js'
14
15
  import { Context } from './context.js'
15
16
  import { notifyStoryChange } from './stories.js'
@@ -18,31 +19,46 @@ import { parseColor } from './colors.js'
18
19
  import { createMarkdownPlugins } from './markdown.js'
19
20
  import { getSearchDataJS, generateDocSearchData, generateTitleSearchData } from './search.js'
20
21
  import { getInjectedImport } from './util/vendors.js'
22
+ import { findUp } from './util/find-up.js'
21
23
 
22
24
  const require = createRequire(import.meta.url)
23
25
 
24
- export const STORIES_ID = '$histoire-stories'
25
- export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`
26
- export const SETUP_ID = '$histoire-setup'
27
- export const NOOP_ID = '/$histoire-noop'
28
- export const CONFIG_ID = '$histoire-config'
29
- export const RESOLVED_CONFIG_ID = `/${CONFIG_ID}-resolved`
30
- export const THEME_ID = '$histoire-theme'
31
- export const RESOLVED_THEME_ID = `/${THEME_ID}-resolved.css`
32
- export const SEARCH_TITLE_DATA_ID = '$histoire-search-title-data'
33
- export const RESOLVED_SEARCH_TITLE_DATA_ID = `/${SEARCH_TITLE_DATA_ID}-resolved`
34
- export const SEARCH_DOCS_DATA_ID = '$histoire-search-docs-data'
35
- export const RESOLVED_SEARCH_DOCS_DATA_ID = `/${SEARCH_DOCS_DATA_ID}-resolved`
36
- export const GENERATED_GLOBAL_SETUP = '$histoire-generated-global-setup'
37
- export const RESOLVED_GENERATED_GLOBAL_SETUP = `/${GENERATED_GLOBAL_SETUP}-resolved`
38
- export const GENERATED_SETUP_CODE = '$histoire-generated-setup-code'
39
- export const RESOLVED_GENERATED_SETUP_CODE = `/${GENERATED_SETUP_CODE}-resolved`
26
+ // @TODO wait for virtual modules HMR to work in vite to use `virtual:` and `\0` prefix
27
+ // https://github.com/vitejs/vite/pull/10313
28
+ export const STORIES_ID = 'virtual:$histoire-stories'
29
+ export const RESOLVED_STORIES_ID = `/__resolved__${STORIES_ID}`
30
+ // export const RESOLVED_STORIES_ID = `\0${STORIES_ID}`
31
+ export const SETUP_ID = 'virtual:$histoire-setup'
32
+ export const NOOP_ID = 'virtual:$histoire-noop'
33
+ export const CONFIG_ID = 'virtual:$histoire-config'
34
+ export const RESOLVED_CONFIG_ID = `/__resolved__${CONFIG_ID}`
35
+ // export const RESOLVED_CONFIG_ID = `\0${CONFIG_ID}`
36
+ export const THEME_ID = 'virtual:$histoire-theme'
37
+ export const RESOLVED_THEME_ID = `/__resolved__${THEME_ID}.css`
38
+ // export const RESOLVED_THEME_ID = `\0${THEME_ID}.css`
39
+ export const SEARCH_TITLE_DATA_ID = 'virtual:$histoire-search-title-data'
40
+ export const RESOLVED_SEARCH_TITLE_DATA_ID = `/__resolved__${SEARCH_TITLE_DATA_ID}`
41
+ // export const RESOLVED_SEARCH_TITLE_DATA_ID = `\0${SEARCH_TITLE_DATA_ID}`
42
+ export const SEARCH_DOCS_DATA_ID = 'virtual:$histoire-search-docs-data'
43
+ export const RESOLVED_SEARCH_DOCS_DATA_ID = `/__resolved__${SEARCH_DOCS_DATA_ID}`
44
+ // export const RESOLVED_SEARCH_DOCS_DATA_ID = `\0${SEARCH_DOCS_DATA_ID}`
45
+ export const GENERATED_GLOBAL_SETUP = 'virtual:$histoire-generated-global-setup'
46
+ export const RESOLVED_GENERATED_GLOBAL_SETUP = `/__resolved__${GENERATED_GLOBAL_SETUP}`
47
+ // export const RESOLVED_GENERATED_GLOBAL_SETUP = `\0${GENERATED_GLOBAL_SETUP}`
48
+ export const GENERATED_SETUP_CODE = 'virtual:$histoire-generated-setup-code'
49
+ export const RESOLVED_GENERATED_SETUP_CODE = `/__resolved__${GENERATED_SETUP_CODE}`
50
+ // export const RESOLVED_GENERATED_SETUP_CODE = `\0${GENERATED_SETUP_CODE}`
40
51
  export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client'
41
- export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
52
+ export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `/__resolved__${SUPPORT_PLUGINS_CLIENT}`
53
+ // export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
42
54
  export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect'
43
- export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
55
+ export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_COLLECT}`
56
+ // export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
44
57
  export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files'
45
- export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
58
+ export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`
59
+ // export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
60
+
61
+ const ID_SEPARATOR = '__-__'
46
62
 
47
63
  export async function resolveViteConfig (ctx: Context): Promise<ResolvedConfig> {
48
64
  const command = ctx.mode === 'dev' ? 'serve' : 'build'
@@ -94,9 +110,14 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
94
110
  const inlineConfig = await mergeHistoireViteConfig(userViteConfig?.config ?? {}, ctx)
95
111
  const plugins: VitePlugin[] = []
96
112
 
113
+ const hasPnpm = !!(await findUp(ctx.root, ['pnpm-lock.yaml']))
114
+
97
115
  function optimizeDeps (deps: string[]): string[] {
98
116
  const result = []
99
117
  for (const dep of deps) {
118
+ if (!hasPnpm) {
119
+ result.push(dep)
120
+ }
100
121
  try {
101
122
  result.push(dirname(require.resolve(`${dep}/package.json`)))
102
123
  } catch (e) {
@@ -125,7 +146,6 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
125
146
  `${APP_PATH}/bundle-sandbox.js`,
126
147
  ],
127
148
  include: optimizeDeps([
128
- 'case',
129
149
  'flexsearch',
130
150
  'shiki',
131
151
  // Shiki dependencies
@@ -155,6 +175,8 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
155
175
  __VUE_PROD_DEVTOOLS__: 'true',
156
176
  // Disable warnings
157
177
  'process.env.NODE_ENV': JSON.stringify(isServer ? 'production' : process.env.NODE_ENV ?? 'development'),
178
+ // Collect flag
179
+ 'process.env.HST_COLLECT': 'false',
158
180
  },
159
181
  cacheDir: isServer ? 'node_modules/.hst-vite-server' : 'node_modules/.hst-vite',
160
182
  }
@@ -165,8 +187,17 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
165
187
  return RESOLVED_STORIES_ID
166
188
  }
167
189
  if (id.startsWith(SETUP_ID)) {
168
- if (ctx.config.setupFile) {
169
- return this.resolve(resolve(ctx.root, ctx.config.setupFile), importer, {
190
+ const setupFileConfig = ctx.config.setupFile
191
+ if (setupFileConfig) {
192
+ let file: string
193
+ if (typeof setupFileConfig === 'string') {
194
+ file = setupFileConfig
195
+ } else if (isServer) {
196
+ file = setupFileConfig.server
197
+ } else {
198
+ file = setupFileConfig.browser
199
+ }
200
+ return this.resolve(resolve(ctx.root, file), importer, {
170
201
  skipSelf: true,
171
202
  })
172
203
  } else {
@@ -189,8 +220,8 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
189
220
  return RESOLVED_GENERATED_GLOBAL_SETUP
190
221
  }
191
222
  if (id.startsWith(GENERATED_SETUP_CODE)) {
192
- const [, index] = id.split('__')
193
- return `${RESOLVED_GENERATED_SETUP_CODE}__${index}`
223
+ const [, index] = id.split(ID_SEPARATOR)
224
+ return `${RESOLVED_GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}`
194
225
  }
195
226
  if (id.startsWith(SUPPORT_PLUGINS_CLIENT)) {
196
227
  return RESOLVED_SUPPORT_PLUGINS_CLIENT
@@ -204,6 +235,11 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
204
235
  if (id.startsWith('virtual:story:')) {
205
236
  return `\0${id}`
206
237
  }
238
+ if (id.startsWith('virtual:story-source:')) {
239
+ return `/__resolved__${id}`
240
+ // @TODO
241
+ // return `\0${id}`
242
+ }
207
243
  },
208
244
 
209
245
  async load (id) {
@@ -230,7 +266,7 @@ ${resolvedStories.map((file, index) => {
230
266
  }
231
267
  return supportPlugin.importStoryComponent(file, index)
232
268
  }).filter(Boolean).join('\n')}
233
- export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}}`).join(',\n')}]
269
+ export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}, source: () => import('virtual:story-source:${file.story.id}')}`).join(',\n')}]
234
270
  export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
235
271
  const handlers = []
236
272
  export function onUpdate (cb) {
@@ -287,7 +323,7 @@ if (import.meta.hot) {
287
323
  if (ctx.config.setupCode) {
288
324
  return [
289
325
  // Import
290
- `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}__${index}'`).join('\n')}`,
326
+ `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
291
327
  // List
292
328
  `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
293
329
  // Setups
@@ -305,7 +341,7 @@ if (import.meta.hot) {
305
341
  }
306
342
 
307
343
  if (id.startsWith(RESOLVED_GENERATED_SETUP_CODE)) {
308
- const [, index] = id.split('__')
344
+ const [, index] = id.split(ID_SEPARATOR)
309
345
  return ctx.config.setupCode?.[index] ?? ''
310
346
  }
311
347
 
@@ -328,13 +364,19 @@ if (import.meta.hot) {
328
364
  }
329
365
 
330
366
  if (id === RESOLVED_MARKDOWN_FILES) {
331
- const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`./${f.relativePath}`)})`).join(',')
367
+ const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`/${f.relativePath}`)})`).join(',')
332
368
  return `import { reactive } from ${getInjectedImport('@histoire/vendors/vue')}
333
369
  export const markdownFiles = reactive({${filesJs}})
334
370
  if (import.meta.hot) {
335
- window.__hst_md_hmr = (newModule) => {
336
- markdownFiles[newModule.relativePath] = () => newModule
371
+ if (!window.__hst_md_hmr) {
372
+ window.__hst_md_hmr = (newModule) => {
373
+ markdownFiles[newModule.relativePath] = () => newModule
374
+ }
337
375
  }
376
+
377
+ import.meta.hot.accept(newModule => {
378
+ Object.assign(markdownFiles, newModule.markdownFiles)
379
+ })
338
380
  }`
339
381
  }
340
382
 
@@ -345,6 +387,20 @@ if (import.meta.hot) {
345
387
  return storyFile.moduleCode
346
388
  }
347
389
  }
390
+
391
+ if (id.startsWith('/__resolved__virtual:story-source:')) {
392
+ const storyId = id.slice('/__resolved__virtual:story-source:'.length)
393
+ const storyFile = ctx.storyFiles.find(f => f.story?.id === storyId)
394
+ if (storyFile) {
395
+ let source: string
396
+ if (storyFile.virtual) {
397
+ source = storyFile.moduleCode
398
+ } else {
399
+ source = await fs.readFile(resolve(ctx.root, storyFile.relativePath), 'utf-8')
400
+ }
401
+ return `export default ${JSON.stringify(source)}`
402
+ }
403
+ }
348
404
  },
349
405
 
350
406
  handleHotUpdate (updateContext) {
package/tsconfig.json CHANGED
@@ -21,7 +21,6 @@
21
21
  ],
22
22
  "sourceMap": false,
23
23
  "preserveWatchOutput": true,
24
- "preserveSymlinks": true,
25
24
  "declaration": true,
26
25
  // Strict
27
26
  "noImplicitAny": false,