histoire 0.10.6 → 0.11.0

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
package/dist/node/vite.js CHANGED
@@ -1,7 +1,8 @@
1
- import { createRequire } from 'module';
1
+ import { createRequire } from 'node:module';
2
2
  import { relative, resolve, join, dirname } from 'pathe';
3
3
  import { resolveConfig as resolveViteConfigInternal, mergeConfig as mergeViteConfig, loadConfigFromFile as loadViteConfigFromFile, } from 'vite';
4
4
  import { lookup as lookupMime } from 'mrmime';
5
+ import fs from 'fs-extra';
5
6
  import { APP_PATH, TEMP_PATH } from './alias.js';
6
7
  import { notifyStoryChange } from './stories.js';
7
8
  import { makeTree } from './tree.js';
@@ -9,29 +10,43 @@ import { parseColor } from './colors.js';
9
10
  import { createMarkdownPlugins } from './markdown.js';
10
11
  import { getSearchDataJS, generateDocSearchData, generateTitleSearchData } from './search.js';
11
12
  import { getInjectedImport } from './util/vendors.js';
13
+ import { findUp } from './util/find-up.js';
12
14
  const require = createRequire(import.meta.url);
13
- export const STORIES_ID = '$histoire-stories';
14
- export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`;
15
- export const SETUP_ID = '$histoire-setup';
16
- export const NOOP_ID = '/$histoire-noop';
17
- export const CONFIG_ID = '$histoire-config';
18
- export const RESOLVED_CONFIG_ID = `/${CONFIG_ID}-resolved`;
19
- export const THEME_ID = '$histoire-theme';
20
- export const RESOLVED_THEME_ID = `/${THEME_ID}-resolved.css`;
21
- export const SEARCH_TITLE_DATA_ID = '$histoire-search-title-data';
22
- export const RESOLVED_SEARCH_TITLE_DATA_ID = `/${SEARCH_TITLE_DATA_ID}-resolved`;
23
- export const SEARCH_DOCS_DATA_ID = '$histoire-search-docs-data';
24
- export const RESOLVED_SEARCH_DOCS_DATA_ID = `/${SEARCH_DOCS_DATA_ID}-resolved`;
25
- export const GENERATED_GLOBAL_SETUP = '$histoire-generated-global-setup';
26
- export const RESOLVED_GENERATED_GLOBAL_SETUP = `/${GENERATED_GLOBAL_SETUP}-resolved`;
27
- export const GENERATED_SETUP_CODE = '$histoire-generated-setup-code';
28
- export const RESOLVED_GENERATED_SETUP_CODE = `/${GENERATED_SETUP_CODE}-resolved`;
15
+ // @TODO wait for virtual modules HMR to work in vite to use `virtual:` and `\0` prefix
16
+ // https://github.com/vitejs/vite/pull/10313
17
+ export const STORIES_ID = 'virtual:$histoire-stories';
18
+ export const RESOLVED_STORIES_ID = `/__resolved__${STORIES_ID}`;
19
+ // export const RESOLVED_STORIES_ID = `\0${STORIES_ID}`
20
+ export const SETUP_ID = 'virtual:$histoire-setup';
21
+ export const NOOP_ID = 'virtual:$histoire-noop';
22
+ export const CONFIG_ID = 'virtual:$histoire-config';
23
+ export const RESOLVED_CONFIG_ID = `/__resolved__${CONFIG_ID}`;
24
+ // export const RESOLVED_CONFIG_ID = `\0${CONFIG_ID}`
25
+ export const THEME_ID = 'virtual:$histoire-theme';
26
+ export const RESOLVED_THEME_ID = `/__resolved__${THEME_ID}.css`;
27
+ // export const RESOLVED_THEME_ID = `\0${THEME_ID}.css`
28
+ export const SEARCH_TITLE_DATA_ID = 'virtual:$histoire-search-title-data';
29
+ export const RESOLVED_SEARCH_TITLE_DATA_ID = `/__resolved__${SEARCH_TITLE_DATA_ID}`;
30
+ // export const RESOLVED_SEARCH_TITLE_DATA_ID = `\0${SEARCH_TITLE_DATA_ID}`
31
+ export const SEARCH_DOCS_DATA_ID = 'virtual:$histoire-search-docs-data';
32
+ export const RESOLVED_SEARCH_DOCS_DATA_ID = `/__resolved__${SEARCH_DOCS_DATA_ID}`;
33
+ // export const RESOLVED_SEARCH_DOCS_DATA_ID = `\0${SEARCH_DOCS_DATA_ID}`
34
+ export const GENERATED_GLOBAL_SETUP = 'virtual:$histoire-generated-global-setup';
35
+ export const RESOLVED_GENERATED_GLOBAL_SETUP = `/__resolved__${GENERATED_GLOBAL_SETUP}`;
36
+ // export const RESOLVED_GENERATED_GLOBAL_SETUP = `\0${GENERATED_GLOBAL_SETUP}`
37
+ export const GENERATED_SETUP_CODE = 'virtual:$histoire-generated-setup-code';
38
+ export const RESOLVED_GENERATED_SETUP_CODE = `/__resolved__${GENERATED_SETUP_CODE}`;
39
+ // export const RESOLVED_GENERATED_SETUP_CODE = `\0${GENERATED_SETUP_CODE}`
29
40
  export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client';
30
- export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`;
41
+ export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `/__resolved__${SUPPORT_PLUGINS_CLIENT}`;
42
+ // export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
31
43
  export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect';
32
- export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`;
44
+ export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_COLLECT}`;
45
+ // export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
33
46
  export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files';
34
- export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`;
47
+ export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`;
48
+ // export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
49
+ const ID_SEPARATOR = '__-__';
35
50
  export async function resolveViteConfig(ctx) {
36
51
  const command = ctx.mode === 'dev' ? 'serve' : 'build';
37
52
  let viteConfig = (await resolveViteConfigInternal({}, command));
@@ -74,9 +89,13 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
74
89
  const userViteConfig = await loadViteConfigFromFile({ command: ctx.mode === 'dev' ? 'serve' : 'build', mode: ctx.mode });
75
90
  const inlineConfig = await mergeHistoireViteConfig(userViteConfig?.config ?? {}, ctx);
76
91
  const plugins = [];
92
+ const hasPnpm = !!(await findUp(ctx.root, ['pnpm-lock.yaml']));
77
93
  function optimizeDeps(deps) {
78
94
  const result = [];
79
95
  for (const dep of deps) {
96
+ if (!hasPnpm) {
97
+ result.push(dep);
98
+ }
80
99
  try {
81
100
  result.push(dirname(require.resolve(`${dep}/package.json`)));
82
101
  }
@@ -104,7 +123,6 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
104
123
  `${APP_PATH}/bundle-sandbox.js`,
105
124
  ],
106
125
  include: optimizeDeps([
107
- 'case',
108
126
  'flexsearch',
109
127
  'shiki',
110
128
  // Shiki dependencies
@@ -134,6 +152,8 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
134
152
  __VUE_PROD_DEVTOOLS__: 'true',
135
153
  // Disable warnings
136
154
  'process.env.NODE_ENV': JSON.stringify(isServer ? 'production' : process.env.NODE_ENV ?? 'development'),
155
+ // Collect flag
156
+ 'process.env.HST_COLLECT': 'false',
137
157
  },
138
158
  cacheDir: isServer ? 'node_modules/.hst-vite-server' : 'node_modules/.hst-vite',
139
159
  };
@@ -143,8 +163,19 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
143
163
  return RESOLVED_STORIES_ID;
144
164
  }
145
165
  if (id.startsWith(SETUP_ID)) {
146
- if (ctx.config.setupFile) {
147
- return this.resolve(resolve(ctx.root, ctx.config.setupFile), importer, {
166
+ const setupFileConfig = ctx.config.setupFile;
167
+ if (setupFileConfig) {
168
+ let file;
169
+ if (typeof setupFileConfig === 'string') {
170
+ file = setupFileConfig;
171
+ }
172
+ else if (isServer) {
173
+ file = setupFileConfig.server;
174
+ }
175
+ else {
176
+ file = setupFileConfig.browser;
177
+ }
178
+ return this.resolve(resolve(ctx.root, file), importer, {
148
179
  skipSelf: true,
149
180
  });
150
181
  }
@@ -168,8 +199,8 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
168
199
  return RESOLVED_GENERATED_GLOBAL_SETUP;
169
200
  }
170
201
  if (id.startsWith(GENERATED_SETUP_CODE)) {
171
- const [, index] = id.split('__');
172
- return `${RESOLVED_GENERATED_SETUP_CODE}__${index}`;
202
+ const [, index] = id.split(ID_SEPARATOR);
203
+ return `${RESOLVED_GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}`;
173
204
  }
174
205
  if (id.startsWith(SUPPORT_PLUGINS_CLIENT)) {
175
206
  return RESOLVED_SUPPORT_PLUGINS_CLIENT;
@@ -183,6 +214,11 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
183
214
  if (id.startsWith('virtual:story:')) {
184
215
  return `\0${id}`;
185
216
  }
217
+ if (id.startsWith('virtual:story-source:')) {
218
+ return `/__resolved__${id}`;
219
+ // @TODO
220
+ // return `\0${id}`
221
+ }
186
222
  },
187
223
  async load(id) {
188
224
  if (id === RESOLVED_STORIES_ID) {
@@ -208,7 +244,7 @@ ${resolvedStories.map((file, index) => {
208
244
  }
209
245
  return supportPlugin.importStoryComponent(file, index);
210
246
  }).filter(Boolean).join('\n')}
211
- export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}}`).join(',\n')}]
247
+ 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')}]
212
248
  export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
213
249
  const handlers = []
214
250
  export function onUpdate (cb) {
@@ -259,7 +295,7 @@ if (import.meta.hot) {
259
295
  if (ctx.config.setupCode) {
260
296
  return [
261
297
  // Import
262
- `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}__${index}'`).join('\n')}`,
298
+ `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
263
299
  // List
264
300
  `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
265
301
  // Setups
@@ -277,7 +313,7 @@ if (import.meta.hot) {
277
313
  }
278
314
  }
279
315
  if (id.startsWith(RESOLVED_GENERATED_SETUP_CODE)) {
280
- const [, index] = id.split('__');
316
+ const [, index] = id.split(ID_SEPARATOR);
281
317
  return ctx.config.setupCode?.[index] ?? '';
282
318
  }
283
319
  if (id === RESOLVED_SUPPORT_PLUGINS_CLIENT) {
@@ -297,13 +333,19 @@ if (import.meta.hot) {
297
333
  }`;
298
334
  }
299
335
  if (id === RESOLVED_MARKDOWN_FILES) {
300
- const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`./${f.relativePath}`)})`).join(',');
336
+ const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`/${f.relativePath}`)})`).join(',');
301
337
  return `import { reactive } from ${getInjectedImport('@histoire/vendors/vue')}
302
338
  export const markdownFiles = reactive({${filesJs}})
303
339
  if (import.meta.hot) {
304
- window.__hst_md_hmr = (newModule) => {
305
- markdownFiles[newModule.relativePath] = () => newModule
340
+ if (!window.__hst_md_hmr) {
341
+ window.__hst_md_hmr = (newModule) => {
342
+ markdownFiles[newModule.relativePath] = () => newModule
343
+ }
306
344
  }
345
+
346
+ import.meta.hot.accept(newModule => {
347
+ Object.assign(markdownFiles, newModule.markdownFiles)
348
+ })
307
349
  }`;
308
350
  }
309
351
  if (id.startsWith('\0virtual:story:')) {
@@ -313,6 +355,20 @@ if (import.meta.hot) {
313
355
  return storyFile.moduleCode;
314
356
  }
315
357
  }
358
+ if (id.startsWith('/__resolved__virtual:story-source:')) {
359
+ const storyId = id.slice('/__resolved__virtual:story-source:'.length);
360
+ const storyFile = ctx.storyFiles.find(f => f.story?.id === storyId);
361
+ if (storyFile) {
362
+ let source;
363
+ if (storyFile.virtual) {
364
+ source = storyFile.moduleCode;
365
+ }
366
+ else {
367
+ source = await fs.readFile(resolve(ctx.root, storyFile.relativePath), 'utf-8');
368
+ }
369
+ return `export default ${JSON.stringify(source)}`;
370
+ }
371
+ }
316
372
  },
317
373
  handleHotUpdate(updateContext) {
318
374
  const story = ctx.storyFiles.find(file => file.path === updateContext.file);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.10.6",
3
+ "version": "0.11.0",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -17,8 +17,12 @@
17
17
  "type": "module",
18
18
  "exports": {
19
19
  ".": {
20
- "import": "./dist/node/index.js",
21
- "types": "./dist/node/index.d.ts"
20
+ "types": "./dist/node/index.d.ts",
21
+ "default": "./dist/node/index.js"
22
+ },
23
+ "./client": {
24
+ "types": "./client.d.ts",
25
+ "default": "./client.js"
22
26
  },
23
27
  "./*": "./*"
24
28
  },
@@ -29,14 +33,14 @@
29
33
  "histoire": "./bin.mjs"
30
34
  },
31
35
  "dependencies": {
32
- "@histoire/app": "^0.10.6",
33
- "@histoire/controls": "^0.10.6",
34
- "@histoire/shared": "^0.10.6",
35
- "@histoire/vendors": "^0.10.6",
36
+ "@histoire/app": "^0.11.0",
37
+ "@histoire/controls": "^0.11.0",
38
+ "@histoire/shared": "^0.11.0",
39
+ "@histoire/vendors": "^0.11.0",
36
40
  "@types/flexsearch": "^0.7.3",
37
41
  "@types/markdown-it": "^12.2.3",
38
42
  "birpc": "^0.1.1",
39
- "case": "^1.6.3",
43
+ "change-case": "^4.1.2",
40
44
  "chokidar": "^3.5.3",
41
45
  "connect": "^3.7.0",
42
46
  "defu": "^6.0.0",
@@ -46,6 +50,7 @@
46
50
  "globby": "^13.1.1",
47
51
  "gray-matter": "^4.0.3",
48
52
  "happy-dom": "^2.55.0",
53
+ "jiti": "^1.16.0",
49
54
  "markdown-it": "^12.3.2",
50
55
  "markdown-it-anchor": "^8.6.2",
51
56
  "markdown-it-attrs": "^4.1.3",
@@ -58,13 +63,14 @@
58
63
  "shiki": "0.10.1",
59
64
  "sirv": "^2.0.2",
60
65
  "tinypool": "^0.1.2",
61
- "vite-node": "0.22.1"
66
+ "vite-node": "0.23.4"
62
67
  },
63
68
  "peerDependencies": {
64
69
  "vite": "^2.9.0 || ^3.0.0"
65
70
  },
66
71
  "devDependencies": {
67
- "@peeky/test": "^0.13.5",
72
+ "@peeky/server": "^0.14.0",
73
+ "@peeky/test": "^0.14.1",
68
74
  "@types/fs-extra": "^9.0.13",
69
75
  "@types/markdown-it": "^12.2.3",
70
76
  "@types/micromatch": "^4.0.2",
package/src/node/alias.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import path from 'pathe'
2
- import { createRequire } from 'module'
2
+ import { createRequire } from 'node:module'
3
3
 
4
4
  const require = createRequire(import.meta.url)
5
5
 
package/src/node/bin.ts CHANGED
@@ -1,6 +1,6 @@
1
- import fs from 'fs'
1
+ import fs from 'node:fs'
2
2
  import { dirname, resolve } from 'pathe'
3
- import { fileURLToPath } from 'url'
3
+ import { fileURLToPath } from 'node:url'
4
4
  import sade from 'sade'
5
5
 
6
6
  const __dirname = dirname(fileURLToPath(import.meta.url))
package/src/node/build.ts CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  import fs from 'fs-extra'
11
11
  import { lookup as lookupMime } from 'mrmime'
12
12
  import pc from 'picocolors'
13
- import { performance } from 'perf_hooks'
13
+ import { performance } from 'node:perf_hooks'
14
14
  import type {
15
15
  BuildEndCallback,
16
16
  PreviewStoryCallback,
@@ -121,6 +121,7 @@ export async function build (ctx: Context) {
121
121
  config (config) {
122
122
  const vuePlugin = config.plugins.find((p: any) => p.name === 'vite:vue') as VitePlugin
123
123
  if (vuePlugin) {
124
+ // @ts-expect-error vue plugin use function form
124
125
  const original = vuePlugin.configureServer.bind(vuePlugin)
125
126
  vuePlugin.configureServer = () => {
126
127
  original({
@@ -11,9 +11,9 @@ import {
11
11
  } from '@histoire/vendors/vue'
12
12
  import type { Story, Variant } from '@histoire/shared'
13
13
  // @ts-expect-error virtual module id
14
- import * as setup from '$histoire-setup'
14
+ import * as setup from 'virtual:$histoire-setup'
15
15
  // @ts-expect-error virtual module id
16
- import * as generatedSetup from '$histoire-generated-global-setup'
16
+ import * as generatedSetup from 'virtual:$histoire-generated-global-setup'
17
17
  import type { App, MountApi, VanillaApi } from './types'
18
18
 
19
19
  export default _defineComponent({
@@ -1,5 +1,5 @@
1
1
  import { dirname } from 'pathe'
2
- import { fileURLToPath } from 'url'
2
+ import { fileURLToPath } from 'node:url'
3
3
  import type { Plugin } from '@histoire/shared'
4
4
 
5
5
  const __dirname = dirname(fileURLToPath(import.meta.url))
@@ -1,4 +1,4 @@
1
- import { MessageChannel } from 'worker_threads'
1
+ import { MessageChannel } from 'node:worker_threads'
2
2
  import type { ViteDevServer } from 'vite'
3
3
  import { ViteNodeServer } from 'vite-node/server'
4
4
  import type { FetchFunction, ResolveIdFunction } from 'vite-node'
@@ -6,7 +6,7 @@ import { relative } from 'pathe'
6
6
  import pc from 'picocolors'
7
7
  import Tinypool from 'tinypool'
8
8
  import { createBirpc } from 'birpc'
9
- import { cpus } from 'os'
9
+ import { cpus } from 'node:os'
10
10
  import type { ServerStoryFile } from '@histoire/shared'
11
11
  import { createPath } from '../tree.js'
12
12
  import type { Context } from '../context.js'
@@ -92,10 +92,26 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
92
92
  } else if (storyData.length > 1) {
93
93
  console.warn(pc.yellow(`⚠️ Multiple stories not supported: ${storyFile.path}`))
94
94
  }
95
- storyFile.id = storyData[0].id
96
- storyFile.story = storyData[0]
95
+
96
+ const finalData = storyData[0]
97
+
98
+ // Default props
99
+ if (ctx.config.defaultStoryProps) {
100
+ for (const key in ctx.config.defaultStoryProps) {
101
+ if (finalData[key] == null) {
102
+ finalData[key] = ctx.config.defaultStoryProps[key]
103
+ }
104
+ }
105
+ }
106
+
107
+ if (!finalData.layout) {
108
+ finalData.layout = { type: 'single', iframe: true }
109
+ }
110
+
111
+ storyFile.id = finalData.id
112
+ storyFile.story = finalData
97
113
  storyFile.treeFile = {
98
- title: storyData[0].title,
114
+ title: finalData.title,
99
115
  path: relative(server.config.root, storyFile.path),
100
116
  }
101
117
  storyFile.treePath = createPath(ctx.config, storyFile.treeFile)
@@ -1,5 +1,5 @@
1
- import { fileURLToPath } from 'url'
2
- import type { MessagePort } from 'worker_threads'
1
+ import { fileURLToPath } from 'node:url'
2
+ import type { MessagePort } from 'node:worker_threads'
3
3
  import { ViteNodeRunner } from 'vite-node/client'
4
4
  import { createBirpc } from 'birpc'
5
5
  import type { FetchFunction, ResolveIdFunction } from 'vite-node'
@@ -21,6 +21,8 @@ export interface ReturnData {
21
21
  }
22
22
 
23
23
  export default async (payload: Payload): Promise<ReturnData> => {
24
+ process.env.HST_COLLECT = 'true'
25
+
24
26
  const rpc = createBirpc<{
25
27
  fetchModule: FetchFunction
26
28
  resolveId: ResolveIdFunction
@@ -1,12 +1,11 @@
1
1
  import path from 'pathe'
2
2
  import { createDefu } from 'defu'
3
3
  import {
4
- createServer,
5
4
  resolveConfig as resolveViteConfig,
6
5
  mergeConfig as mergeViteConfig,
7
6
  } from 'vite'
8
- import { ViteNodeServer } from 'vite-node/server'
9
- import { ViteNodeRunner } from 'vite-node/client'
7
+ import jiti from 'jiti'
8
+ import { fileURLToPath } from 'node:url'
10
9
  import pc from 'picocolors'
11
10
  import type {
12
11
  HistoireConfig,
@@ -19,6 +18,8 @@ import { findUp } from './util/find-up.js'
19
18
  import { tailwindTokens } from './builtin-plugins/tailwind-tokens.js'
20
19
  import { vanillaSupport } from './builtin-plugins/vanilla-support/plugin.js'
21
20
 
21
+ const __filename = fileURLToPath(import.meta.url)
22
+
22
23
  export function getDefaultConfig (): HistoireConfig {
23
24
  return {
24
25
  plugins: [
@@ -146,26 +147,9 @@ export function resolveConfigFile (cwd: string = process.cwd()): string {
146
147
 
147
148
  export async function loadConfigFile (configFile: string): Promise<Partial<HistoireConfig>> {
148
149
  try {
149
- const server = await createServer()
150
- await server.pluginContainer.buildStart({})
151
- const node = new ViteNodeServer(server, {
152
- deps: {
153
- inline: [
154
- /histoire\/dist/,
155
- ],
156
- },
157
- })
158
- const runner = new ViteNodeRunner({
159
- root: path.dirname(configFile),
160
- fetchModule (id) {
161
- return node.fetchModule(id)
162
- },
163
- resolveId (id, importer) {
164
- return node.resolveId(id, importer)
165
- },
166
- })
167
- const result: { default: Partial<HistoireConfig> } = await runner.executeFile(configFile)
168
- await server.close()
150
+ const result = jiti(__filename, {
151
+ esmResolve: true,
152
+ })(configFile)
169
153
  if (!result.default) {
170
154
  throw new Error(`Expected default export in ${configFile}`)
171
155
  }
@@ -240,7 +224,7 @@ export async function resolveConfig (cwd: string = process.cwd(), mode: ConfigMo
240
224
  const viteHistoireConfig = (viteConfig.histoire ?? {}) as HistoireConfig
241
225
 
242
226
  const preUserConfig = mergeConfig(result, viteHistoireConfig)
243
- const processedDefaultConfig = await processDefaultConfig(getDefaultConfig(), preUserConfig, mode)
227
+ const processedDefaultConfig = await processDefaultConfig(getDefaultConfig(), preUserConfig, mode, cwd)
244
228
 
245
229
  return processConfig(mergeConfig(preUserConfig, processedDefaultConfig), mode)
246
230
  }
@@ -258,8 +242,19 @@ export async function processConfig (config: HistoireConfig, mode: ConfigMode):
258
242
  return config
259
243
  }
260
244
 
261
- export async function processDefaultConfig (defaultConfig: HistoireConfig, preUserConfig: HistoireConfig, mode: ConfigMode): Promise<HistoireConfig> {
262
- for (const plugin of [...defaultConfig.plugins, ...preUserConfig.plugins]) {
245
+ export async function processDefaultConfig (defaultConfig: HistoireConfig, preUserConfig: HistoireConfig, mode: ConfigMode, cwd: string): Promise<HistoireConfig> {
246
+ // Automatically inline dependencies in vite-node
247
+ const pkgFile = await findUp(cwd, ['package.json'])
248
+ const fs = (await import('fs-extra')).default
249
+ if (pkgFile) {
250
+ const pkg = await fs.readJSON(pkgFile)
251
+ if (pkg.dependencies) {
252
+ defaultConfig.viteNodeInlineDeps = Object.keys(pkg.dependencies).map(d => new RegExp(d))
253
+ }
254
+ }
255
+
256
+ // Apply plugins
257
+ for (const plugin of [...defaultConfig.plugins, ...preUserConfig.plugins ?? []]) {
263
258
  if (plugin.defaultConfig) {
264
259
  const result = await plugin.defaultConfig(defaultConfig, mode)
265
260
  if (result) {
@@ -13,6 +13,18 @@ import { slugify } from './util/slugify.js'
13
13
  import type { Context } from './context.js'
14
14
  import { addStory, notifyStoryChange, removeStory } from './stories.js'
15
15
 
16
+ const onMarkdownListChangeHandlers: (() => unknown)[] = []
17
+
18
+ export function onMarkdownListChange (handler: () => unknown) {
19
+ onMarkdownListChangeHandlers.push(handler)
20
+ }
21
+
22
+ function notifyMarkdownListChange () {
23
+ for (const handler of onMarkdownListChangeHandlers) {
24
+ handler()
25
+ }
26
+ }
27
+
16
28
  export async function createMarkdownRenderer () {
17
29
  const highlighter = await shiki.getHighlighter({
18
30
  theme: 'github-dark',
@@ -104,7 +116,9 @@ export const relativePath = ${JSON.stringify(relativePath)}
104
116
 
105
117
  if (import.meta.hot) {
106
118
  import.meta.hot.accept(newModule => {
107
- window.__hst_md_hmr(newModule)
119
+ if (newModule) {
120
+ window.__hst_md_hmr(newModule)
121
+ }
108
122
  })
109
123
  }`
110
124
  }
@@ -145,7 +159,7 @@ export async function createMarkdownFilesWatcher (ctx: Context) {
145
159
  const storyFile = addStory(storyRelativePath, `export default ${JSON.stringify({
146
160
  id: frontmatter.id,
147
161
  title: frontmatter.title,
148
- icon: frontmatter.icon,
162
+ icon: frontmatter.icon ?? 'carbon:document-blank',
149
163
  iconColor: frontmatter.iconColor,
150
164
  group: frontmatter.group,
151
165
  docsOnly: true,
@@ -164,6 +178,8 @@ export async function createMarkdownFilesWatcher (ctx: Context) {
164
178
  }
165
179
  }
166
180
 
181
+ notifyMarkdownListChange()
182
+
167
183
  return file
168
184
  }
169
185
 
@@ -176,6 +192,7 @@ export async function createMarkdownFilesWatcher (ctx: Context) {
176
192
  notifyStoryChange()
177
193
  }
178
194
  ctx.markdownFiles.splice(index, 1)
195
+ notifyMarkdownListChange()
179
196
  }
180
197
  }
181
198
 
@@ -1,4 +1,4 @@
1
- import http from 'http'
1
+ import http from 'node:http'
2
2
  import connect from 'connect'
3
3
  import sirv from 'sirv'
4
4
  import type { ResolvedConfig as ViteConfig } from 'vite'
@@ -1,7 +1,7 @@
1
- import { createRequire } from 'module'
1
+ import { createRequire } from 'node:module'
2
2
  import flexsearch from 'flexsearch'
3
3
  import path from 'pathe'
4
- import Case from 'case'
4
+ import { noCase } from 'change-case'
5
5
  import { Context } from './context.js'
6
6
  import { loadModule } from './load.js'
7
7
 
@@ -108,7 +108,7 @@ async function exportSearchIndex (index) {
108
108
  }
109
109
 
110
110
  function convertTitleToSentence (text: string) {
111
- return text.split(' ').map(str => Case.lower(str)).join(' ')
111
+ return text.split(' ').map(str => noCase(str)).join(' ')
112
112
  }
113
113
 
114
114
  // @TODO clear handlers when SearchPane unmounts
@@ -1,13 +1,13 @@
1
1
  import { createServer as createViteServer } from 'vite'
2
2
  import type { ServerStoryFile } from '@histoire/shared'
3
3
  import { Context } from './context.js'
4
- import { getViteConfigWithPlugins, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js'
5
- import { onStoryChange, watchStories } from './stories.js'
4
+ import { getViteConfigWithPlugins, RESOLVED_MARKDOWN_FILES, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js'
5
+ import { onStoryChange, onStoryListChange, watchStories } from './stories.js'
6
6
  import { useCollectStories } from './collect/index.js'
7
7
  import { DevPluginApi } from './plugin.js'
8
8
  import { useModuleLoader } from './load.js'
9
9
  import { wrapLogError } from './util/log.js'
10
- import { createMarkdownFilesWatcher } from './markdown.js'
10
+ import { createMarkdownFilesWatcher, onMarkdownListChange } from './markdown.js'
11
11
 
12
12
  export async function createServer (ctx: Context, port: number) {
13
13
  const server = await createViteServer(await getViteConfigWithPlugins(false, ctx))
@@ -85,17 +85,22 @@ export async function createServer (ctx: Context, port: number) {
85
85
  if (queue) {
86
86
  if (queued) {
87
87
  if (changedFile) {
88
- queuedFiles.push(changedFile)
88
+ if (!queuedFiles.includes(changedFile)) {
89
+ queuedFiles.push(changedFile)
90
+ }
89
91
  } else {
90
92
  queuedAll = true
91
93
  }
92
94
  return
95
+ } else if (queuedFiles.includes(changedFile)) {
96
+ return
93
97
  } else {
94
98
  queued = true
95
99
  // Next call
96
100
  await queue
97
101
  }
98
102
  }
103
+ queuedFiles.unshift(changedFile)
99
104
  queued = false
100
105
  queue = new Promise(resolve => {
101
106
  queueResolve = resolve
@@ -106,11 +111,12 @@ export async function createServer (ctx: Context, port: number) {
106
111
  console.log('Collect stories start', changedFile?.path ?? 'all')
107
112
  const time = Date.now()
108
113
  if (changedFile && !queuedAll) {
109
- // Granular update
110
- await executeStoryFile(changedFile)
111
-
112
- // Queued updates
113
- await Promise.all(queuedFiles.map(storyFile => executeStoryFile(storyFile)))
114
+ await Promise.all(queuedFiles.map(async storyFile => {
115
+ await executeStoryFile(storyFile)
116
+ if (storyFile.story) {
117
+ await invalidateModule(`/__resolved__virtual:story-source:${storyFile.story.id}`)
118
+ }
119
+ }))
114
120
  } else {
115
121
  // Full update
116
122
 
@@ -145,6 +151,15 @@ export async function createServer (ctx: Context, port: number) {
145
151
  invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID)
146
152
  })
147
153
 
154
+ onStoryListChange(() => {
155
+ invalidateModule(RESOLVED_STORIES_ID)
156
+ invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID)
157
+ })
158
+
159
+ onMarkdownListChange(() => {
160
+ invalidateModule(RESOLVED_MARKDOWN_FILES)
161
+ })
162
+
148
163
  async function close () {
149
164
  for (const cb of pluginOnCleanups) {
150
165
  await wrapLogError('plugin.onDev.onCleanup', () => cb())