histoire 0.13.0 → 0.13.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 (65) hide show
  1. package/client.d.ts +3 -0
  2. package/dist/node/bin.js +1 -0
  3. package/dist/node/build-serialize.d.ts +21 -0
  4. package/dist/node/build-serialize.js +36 -0
  5. package/dist/node/build.js +19 -11
  6. package/dist/node/commands/dev.d.ts +1 -0
  7. package/dist/node/commands/dev.js +4 -1
  8. package/dist/node/config.js +2 -0
  9. package/dist/node/server.d.ts +1 -0
  10. package/dist/node/server.js +10 -6
  11. package/dist/node/virtual/index.d.ts +34 -0
  12. package/dist/node/virtual/index.js +46 -0
  13. package/dist/node/virtual/markdown.d.ts +2 -0
  14. package/dist/node/virtual/markdown.js +19 -0
  15. package/dist/node/virtual/noop.d.ts +1 -0
  16. package/dist/node/virtual/noop.js +1 -0
  17. package/dist/node/virtual/resolved-config.d.ts +2 -0
  18. package/dist/node/virtual/resolved-config.js +10 -0
  19. package/dist/node/virtual/resolved-generated-global-setup.d.ts +2 -0
  20. package/dist/node/virtual/resolved-generated-global-setup.js +23 -0
  21. package/dist/node/virtual/resolved-generated-setup-code.d.ts +2 -0
  22. package/dist/node/virtual/resolved-generated-setup-code.js +5 -0
  23. package/dist/node/virtual/resolved-markdown-files.d.ts +2 -0
  24. package/dist/node/virtual/resolved-markdown-files.js +17 -0
  25. package/dist/node/virtual/resolved-stories.d.ts +2 -0
  26. package/dist/node/virtual/resolved-stories.js +41 -0
  27. package/dist/node/virtual/resolved-support-plugins-client.d.ts +2 -0
  28. package/dist/node/virtual/resolved-support-plugins-client.js +11 -0
  29. package/dist/node/virtual/resolved-support-plugins-collect.d.ts +2 -0
  30. package/dist/node/virtual/resolved-support-plugins-collect.js +11 -0
  31. package/dist/node/virtual/resolved-theme.d.ts +2 -0
  32. package/dist/node/virtual/resolved-theme.js +12 -0
  33. package/dist/node/virtual/story-source.d.ts +2 -0
  34. package/dist/node/virtual/story-source.js +16 -0
  35. package/dist/node/virtual/story.d.ts +2 -0
  36. package/dist/node/virtual/story.js +7 -0
  37. package/dist/node/virtual/util.d.ts +2 -0
  38. package/dist/node/virtual/util.js +4 -0
  39. package/dist/node/virtual/vite-plugin.d.ts +3 -0
  40. package/dist/node/virtual/vite-plugin.js +118 -0
  41. package/dist/node/vite.d.ts +0 -22
  42. package/dist/node/vite.js +8 -280
  43. package/package.json +6 -6
  44. package/src/node/bin.ts +1 -0
  45. package/src/node/build-serialize.ts +62 -0
  46. package/src/node/build.ts +22 -11
  47. package/src/node/commands/dev.ts +5 -1
  48. package/src/node/config.ts +2 -0
  49. package/src/node/server.ts +13 -6
  50. package/src/node/virtual/index.ts +47 -0
  51. package/src/node/virtual/markdown.ts +21 -0
  52. package/src/node/virtual/noop.ts +1 -0
  53. package/src/node/virtual/resolved-config.ts +12 -0
  54. package/src/node/virtual/resolved-generated-global-setup.ts +24 -0
  55. package/src/node/virtual/resolved-generated-setup-code.ts +7 -0
  56. package/src/node/virtual/resolved-markdown-files.ts +19 -0
  57. package/src/node/virtual/resolved-stories.ts +43 -0
  58. package/src/node/virtual/resolved-support-plugins-client.ts +14 -0
  59. package/src/node/virtual/resolved-support-plugins-collect.ts +14 -0
  60. package/src/node/virtual/resolved-theme.ts +14 -0
  61. package/src/node/virtual/story-source.ts +17 -0
  62. package/src/node/virtual/story.ts +9 -0
  63. package/src/node/virtual/util.ts +5 -0
  64. package/src/node/virtual/vite-plugin.ts +135 -0
  65. package/src/node/vite.ts +9 -295
package/src/node/vite.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createRequire } from 'node:module'
2
- import { relative, resolve, join, dirname } from 'pathe'
2
+ import { relative, join, dirname } from 'pathe'
3
3
  import {
4
4
  Plugin as VitePlugin,
5
5
  UserConfig as ViteConfig,
@@ -8,59 +8,14 @@ import {
8
8
  loadConfigFromFile as loadViteConfigFromFile,
9
9
  } from 'vite'
10
10
  import { lookup as lookupMime } from 'mrmime'
11
- import fs from 'fs-extra'
12
11
  import { APP_PATH, TEMP_PATH } from './alias.js'
13
12
  import { Context } from './context.js'
14
13
  import { notifyStoryChange } from './stories.js'
15
- import { makeTree } from './tree.js'
16
- import { parseColor } from './colors.js'
17
14
  import { createMarkdownPlugins } from './markdown.js'
18
- import { getSearchDataJS, generateDocSearchData, generateTitleSearchData } from './search.js'
19
- import { getInjectedImport } from './util/vendors.js'
15
+ import { createVirtualFilesPlugin } from './virtual/vite-plugin.js'
20
16
 
21
17
  const require = createRequire(import.meta.url)
22
18
 
23
- // @TODO wait for virtual modules HMR to work in vite to use `virtual:` and `\0` prefix
24
- // https://github.com/vitejs/vite/pull/10313
25
- export const STORIES_ID = 'virtual:$histoire-stories'
26
- export const RESOLVED_STORIES_ID = `/__resolved__${STORIES_ID}`
27
- // export const RESOLVED_STORIES_ID = `\0${STORIES_ID}`
28
- export const SETUP_ID = 'virtual:$histoire-setup'
29
- export const NOOP_ID = 'virtual:$histoire-noop'
30
- export const CONFIG_ID = 'virtual:$histoire-config'
31
- export const RESOLVED_CONFIG_ID = `/__resolved__${CONFIG_ID}`
32
- // export const RESOLVED_CONFIG_ID = `\0${CONFIG_ID}`
33
- export const THEME_ID = 'virtual:$histoire-theme'
34
- export const RESOLVED_THEME_ID = `/__resolved__${THEME_ID}.css`
35
- // export const RESOLVED_THEME_ID = `\0${THEME_ID}.css`
36
- export const SEARCH_TITLE_DATA_ID = 'virtual:$histoire-search-title-data'
37
- export const RESOLVED_SEARCH_TITLE_DATA_ID = `/__resolved__${SEARCH_TITLE_DATA_ID}`
38
- // export const RESOLVED_SEARCH_TITLE_DATA_ID = `\0${SEARCH_TITLE_DATA_ID}`
39
- export const SEARCH_DOCS_DATA_ID = 'virtual:$histoire-search-docs-data'
40
- export const RESOLVED_SEARCH_DOCS_DATA_ID = `/__resolved__${SEARCH_DOCS_DATA_ID}`
41
- // export const RESOLVED_SEARCH_DOCS_DATA_ID = `\0${SEARCH_DOCS_DATA_ID}`
42
- export const GENERATED_GLOBAL_SETUP = 'virtual:$histoire-generated-global-setup'
43
- export const RESOLVED_GENERATED_GLOBAL_SETUP = `/__resolved__${GENERATED_GLOBAL_SETUP}`
44
- // export const RESOLVED_GENERATED_GLOBAL_SETUP = `\0${GENERATED_GLOBAL_SETUP}`
45
- export const GENERATED_SETUP_CODE = 'virtual:$histoire-generated-setup-code'
46
- export const RESOLVED_GENERATED_SETUP_CODE = `/__resolved__${GENERATED_SETUP_CODE}`
47
- // export const RESOLVED_GENERATED_SETUP_CODE = `\0${GENERATED_SETUP_CODE}`
48
- export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client'
49
- export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `/__resolved__${SUPPORT_PLUGINS_CLIENT}`
50
- // export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
51
- export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect'
52
- export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_COLLECT}`
53
- // export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
54
- export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files'
55
- export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`
56
- // export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
57
-
58
- const ID_SEPARATOR = '__-__'
59
-
60
- const PLUGINS_HAVE_DEV = [
61
- '@histoire/plugin-vue',
62
- ]
63
-
64
19
  export async function mergeHistoireViteConfig (viteConfig: InlineConfig, ctx: Context) {
65
20
  if (ctx.config.vite) {
66
21
  const command = ctx.mode === 'dev' ? 'serve' : 'build'
@@ -179,6 +134,11 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
179
134
  watch: {
180
135
  ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
181
136
  },
137
+ ...isServer
138
+ ? {
139
+ hmr: false,
140
+ }
141
+ : {},
182
142
  },
183
143
  define: {
184
144
  // We need to force this to be able to use `devtoolsRawSetupState`
@@ -202,254 +162,6 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
202
162
  }
203
163
  },
204
164
 
205
- async resolveId (id, importer) {
206
- if (id.startsWith(STORIES_ID)) {
207
- return RESOLVED_STORIES_ID
208
- }
209
- if (id.startsWith(SETUP_ID)) {
210
- const setupFileConfig = ctx.config.setupFile
211
- if (setupFileConfig) {
212
- let file: string
213
- if (typeof setupFileConfig === 'string') {
214
- file = setupFileConfig
215
- } else if (isServer && 'server' in setupFileConfig) {
216
- file = setupFileConfig.server
217
- } else if (!isServer && 'browser' in setupFileConfig) {
218
- file = setupFileConfig.browser
219
- }
220
- if (file) {
221
- return this.resolve(resolve(ctx.root, file), importer, {
222
- skipSelf: true,
223
- })
224
- }
225
- }
226
- return NOOP_ID
227
- }
228
- if (id.startsWith(CONFIG_ID)) {
229
- return RESOLVED_CONFIG_ID
230
- }
231
- if (id.startsWith(THEME_ID)) {
232
- return RESOLVED_THEME_ID
233
- }
234
- if (id.startsWith(SEARCH_TITLE_DATA_ID)) {
235
- return RESOLVED_SEARCH_TITLE_DATA_ID
236
- }
237
- if (id.startsWith(SEARCH_DOCS_DATA_ID)) {
238
- return RESOLVED_SEARCH_DOCS_DATA_ID
239
- }
240
- if (id.startsWith(GENERATED_GLOBAL_SETUP)) {
241
- return RESOLVED_GENERATED_GLOBAL_SETUP
242
- }
243
- if (id.startsWith(GENERATED_SETUP_CODE)) {
244
- const [, index] = id.split(ID_SEPARATOR)
245
- return `${RESOLVED_GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}`
246
- }
247
- if (id.startsWith(SUPPORT_PLUGINS_CLIENT)) {
248
- return RESOLVED_SUPPORT_PLUGINS_CLIENT
249
- }
250
- if (id.startsWith(SUPPORT_PLUGINS_COLLECT)) {
251
- return RESOLVED_SUPPORT_PLUGINS_COLLECT
252
- }
253
- if (id.startsWith(MARKDOWN_FILES)) {
254
- return RESOLVED_MARKDOWN_FILES
255
- }
256
- if (id.startsWith('virtual:story:')) {
257
- return `\0${id}`
258
- }
259
- if (id.startsWith('virtual:story-source:')) {
260
- return `/__resolved__${id}`
261
- // @TODO
262
- // return `\0${id}`
263
- }
264
-
265
- if (id.startsWith('virtual:md:')) {
266
- return `/__resolved__${id}`
267
- // @TODO
268
- // return `\0${id}`
269
- }
270
- },
271
-
272
- async load (id) {
273
- if (id === RESOLVED_STORIES_ID) {
274
- const resolvedStories = ctx.storyFiles.filter(s => !!s.story)
275
- const files = resolvedStories.map((file, index) => {
276
- return {
277
- id: file.id,
278
- path: file.treePath,
279
- filePath: file.relativePath,
280
- story: {
281
- ...file.story,
282
- docsText: undefined,
283
- },
284
- supportPluginId: file.supportPluginId,
285
- index,
286
- }
287
- })
288
- return `${ctx.supportPlugins.map(p => p.importStoriesPrepend).filter(Boolean).join('\n')}
289
- ${resolvedStories.map((file, index) => {
290
- const supportPlugin = ctx.supportPlugins.find(p => p.id === file.supportPluginId)
291
- if (!supportPlugin) {
292
- throw new Error(`Could not find support plugin for story ${file.path}: ${file.supportPluginId}`)
293
- }
294
- return supportPlugin.importStoryComponent(file, index)
295
- }).filter(Boolean).join('\n')}
296
- 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')}]
297
- export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
298
- const handlers = []
299
- export function onUpdate (cb) {
300
- handlers.push(cb)
301
- }
302
- if (import.meta.hot) {
303
- import.meta.hot.accept(newModule => {
304
- files = newModule.files
305
- tree = newModule.tree
306
- handlers.forEach(h => {
307
- h(newModule.files, newModule.tree)
308
- newModule.onUpdate(h)
309
- })
310
- })
311
- }`
312
- }
313
-
314
- if (id === NOOP_ID) {
315
- return `export default () => {}`
316
- }
317
-
318
- if (id === RESOLVED_CONFIG_ID) {
319
- let js = `export const config = ${JSON.stringify(ctx.config)}\n`
320
- if (ctx.config.theme?.logo) {
321
- for (const key in ctx.config.theme.logo) {
322
- js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`
323
- }
324
- }
325
- js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`
326
- return js
327
- }
328
-
329
- if (id === RESOLVED_THEME_ID) {
330
- let css = '*, ::before, ::after {'
331
- // Colors
332
- for (const color in ctx.config.theme?.colors ?? {}) {
333
- for (const key in ctx.config.theme.colors[color]) {
334
- css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`
335
- }
336
- }
337
- css += '}'
338
- return css
339
- }
340
-
341
- if (id === RESOLVED_SEARCH_TITLE_DATA_ID) {
342
- return getSearchDataJS(await generateTitleSearchData(ctx))
343
- }
344
-
345
- if (id === RESOLVED_SEARCH_DOCS_DATA_ID) {
346
- return getSearchDataJS(await generateDocSearchData(ctx))
347
- }
348
-
349
- if (id === RESOLVED_GENERATED_GLOBAL_SETUP) {
350
- if (ctx.config.setupCode) {
351
- return [
352
- // Import
353
- `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
354
- // List
355
- `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
356
- // Setups
357
- ...ctx.supportPlugins.map(p => p.setupFn).map(fnName => `export async function ${fnName} (payload) {
358
- for (const setup of setupList) {
359
- if (setup?.${fnName}) {
360
- await setup.${fnName}(payload)
361
- }
362
- }
363
- }`),
364
- ].join('\n')
365
- } else {
366
- return ''
367
- }
368
- }
369
-
370
- if (id.startsWith(RESOLVED_GENERATED_SETUP_CODE)) {
371
- const [, index] = id.split(ID_SEPARATOR)
372
- return ctx.config.setupCode?.[index] ?? ''
373
- }
374
-
375
- if (id === RESOLVED_SUPPORT_PLUGINS_CLIENT) {
376
- const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/client${process.env.HISTOIRE_DEV && PLUGINS_HAVE_DEV.includes(p.moduleName) ? '-dev' : ''}`, {
377
- paths: [ctx.root, import.meta.url],
378
- }))})`)
379
- return `export const clientSupportPlugins = {
380
- ${plugins.join(',\n ')}
381
- }`
382
- }
383
-
384
- if (id === RESOLVED_SUPPORT_PLUGINS_COLLECT) {
385
- const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/collect${process.env.HISTOIRE_DEV && PLUGINS_HAVE_DEV.includes(p.moduleName) ? '-dev' : ''}`, {
386
- paths: [ctx.root, import.meta.url],
387
- }))})`)
388
- return `export const collectSupportPlugins = {
389
- ${plugins.join(',\n ')}
390
- }`
391
- }
392
-
393
- if (id === RESOLVED_MARKDOWN_FILES) {
394
- const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`virtual:md:${f.id}`)})`).join(',')
395
- return `import { reactive } from ${process.env.HISTOIRE_DEV ? `'vue'` : getInjectedImport('@histoire/vendors/vue')}
396
- export const markdownFiles = reactive({${filesJs}})
397
- if (import.meta.hot) {
398
- if (!window.__hst_md_hmr) {
399
- window.__hst_md_hmr = (newModule) => {
400
- markdownFiles[newModule.relativePath] = () => newModule
401
- }
402
- }
403
-
404
- import.meta.hot.accept(newModule => {
405
- Object.assign(markdownFiles, newModule.markdownFiles)
406
- })
407
- }`
408
- }
409
-
410
- if (id.startsWith('\0virtual:story:')) {
411
- const moduleId = id.replace('\0', '')
412
- const storyFile = ctx.storyFiles.find(f => f.moduleId === moduleId && f.virtual)
413
- if (storyFile) {
414
- return storyFile.moduleCode
415
- }
416
- }
417
-
418
- if (id.startsWith('/__resolved__virtual:story-source:')) {
419
- const storyId = id.slice('/__resolved__virtual:story-source:'.length)
420
- const storyFile = ctx.storyFiles.find(f => f.story?.id === storyId)
421
- if (storyFile) {
422
- let source: string
423
- if (storyFile.virtual) {
424
- source = storyFile.moduleCode
425
- } else {
426
- source = await fs.readFile(resolve(ctx.root, storyFile.relativePath), 'utf-8')
427
- }
428
- return `export default ${JSON.stringify(source)}`
429
- }
430
- }
431
-
432
- if (id.startsWith('/__resolved__virtual:md:')) {
433
- const fileId = id.slice('/__resolved__virtual:md:'.length)
434
- const file = ctx.markdownFiles.find(f => f.id === fileId)
435
- if (!file) {
436
- throw new Error(`Markdown file not found: ${fileId}`)
437
- }
438
- const { html, frontmatter, relativePath } = file
439
- return `export const html = ${JSON.stringify(html)}
440
- export const frontmatter = ${JSON.stringify(frontmatter)}
441
- export const relativePath = ${JSON.stringify(relativePath)}
442
-
443
- if (import.meta.hot) {
444
- import.meta.hot.accept(newModule => {
445
- if (newModule) {
446
- window.__hst_md_hmr(newModule)
447
- }
448
- })
449
- }`
450
- }
451
- },
452
-
453
165
  handleHotUpdate (updateContext) {
454
166
  const story = ctx.storyFiles.find(file => file.path === updateContext.file)
455
167
  if (story) {
@@ -537,6 +249,8 @@ if (import.meta.hot) {
537
249
  },
538
250
  })
539
251
 
252
+ plugins.push(createVirtualFilesPlugin(ctx, isServer))
253
+
540
254
  // Markdown
541
255
  plugins.push(...await createMarkdownPlugins(ctx))
542
256