histoire 0.13.0 → 0.13.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 (53) hide show
  1. package/dist/node/build.js +17 -11
  2. package/dist/node/server.js +7 -6
  3. package/dist/node/virtual/index.d.ts +34 -0
  4. package/dist/node/virtual/index.js +46 -0
  5. package/dist/node/virtual/markdown.d.ts +2 -0
  6. package/dist/node/virtual/markdown.js +19 -0
  7. package/dist/node/virtual/noop.d.ts +1 -0
  8. package/dist/node/virtual/noop.js +1 -0
  9. package/dist/node/virtual/resolved-config.d.ts +2 -0
  10. package/dist/node/virtual/resolved-config.js +10 -0
  11. package/dist/node/virtual/resolved-generated-global-setup.d.ts +2 -0
  12. package/dist/node/virtual/resolved-generated-global-setup.js +23 -0
  13. package/dist/node/virtual/resolved-generated-setup-code.d.ts +2 -0
  14. package/dist/node/virtual/resolved-generated-setup-code.js +5 -0
  15. package/dist/node/virtual/resolved-markdown-files.d.ts +2 -0
  16. package/dist/node/virtual/resolved-markdown-files.js +17 -0
  17. package/dist/node/virtual/resolved-stories.d.ts +2 -0
  18. package/dist/node/virtual/resolved-stories.js +41 -0
  19. package/dist/node/virtual/resolved-support-plugins-client.d.ts +2 -0
  20. package/dist/node/virtual/resolved-support-plugins-client.js +11 -0
  21. package/dist/node/virtual/resolved-support-plugins-collect.d.ts +2 -0
  22. package/dist/node/virtual/resolved-support-plugins-collect.js +11 -0
  23. package/dist/node/virtual/resolved-theme.d.ts +2 -0
  24. package/dist/node/virtual/resolved-theme.js +12 -0
  25. package/dist/node/virtual/story-source.d.ts +2 -0
  26. package/dist/node/virtual/story-source.js +16 -0
  27. package/dist/node/virtual/story.d.ts +2 -0
  28. package/dist/node/virtual/story.js +7 -0
  29. package/dist/node/virtual/util.d.ts +2 -0
  30. package/dist/node/virtual/util.js +4 -0
  31. package/dist/node/virtual/vite-plugin.d.ts +3 -0
  32. package/dist/node/virtual/vite-plugin.js +118 -0
  33. package/dist/node/vite.d.ts +0 -22
  34. package/dist/node/vite.js +8 -280
  35. package/package.json +6 -6
  36. package/src/node/build.ts +19 -11
  37. package/src/node/server.ts +7 -6
  38. package/src/node/virtual/index.ts +47 -0
  39. package/src/node/virtual/markdown.ts +21 -0
  40. package/src/node/virtual/noop.ts +1 -0
  41. package/src/node/virtual/resolved-config.ts +12 -0
  42. package/src/node/virtual/resolved-generated-global-setup.ts +24 -0
  43. package/src/node/virtual/resolved-generated-setup-code.ts +7 -0
  44. package/src/node/virtual/resolved-markdown-files.ts +19 -0
  45. package/src/node/virtual/resolved-stories.ts +43 -0
  46. package/src/node/virtual/resolved-support-plugins-client.ts +14 -0
  47. package/src/node/virtual/resolved-support-plugins-collect.ts +14 -0
  48. package/src/node/virtual/resolved-theme.ts +14 -0
  49. package/src/node/virtual/story-source.ts +17 -0
  50. package/src/node/virtual/story.ts +9 -0
  51. package/src/node/virtual/util.ts +5 -0
  52. package/src/node/virtual/vite-plugin.ts +135 -0
  53. package/src/node/vite.ts +9 -295
package/dist/node/vite.js CHANGED
@@ -1,54 +1,12 @@
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 { mergeConfig as mergeViteConfig, loadConfigFromFile as loadViteConfigFromFile, } from 'vite';
4
4
  import { lookup as lookupMime } from 'mrmime';
5
- import fs from 'fs-extra';
6
5
  import { APP_PATH, TEMP_PATH } from './alias.js';
7
6
  import { notifyStoryChange } from './stories.js';
8
- import { makeTree } from './tree.js';
9
- import { parseColor } from './colors.js';
10
7
  import { createMarkdownPlugins } from './markdown.js';
11
- import { getSearchDataJS, generateDocSearchData, generateTitleSearchData } from './search.js';
12
- import { getInjectedImport } from './util/vendors.js';
8
+ import { createVirtualFilesPlugin } from './virtual/vite-plugin.js';
13
9
  const require = createRequire(import.meta.url);
14
- // @TODO wait for virtual modules HMR to work in vite to use `virtual:` and `\0` prefix
15
- // https://github.com/vitejs/vite/pull/10313
16
- export const STORIES_ID = 'virtual:$histoire-stories';
17
- export const RESOLVED_STORIES_ID = `/__resolved__${STORIES_ID}`;
18
- // export const RESOLVED_STORIES_ID = `\0${STORIES_ID}`
19
- export const SETUP_ID = 'virtual:$histoire-setup';
20
- export const NOOP_ID = 'virtual:$histoire-noop';
21
- export const CONFIG_ID = 'virtual:$histoire-config';
22
- export const RESOLVED_CONFIG_ID = `/__resolved__${CONFIG_ID}`;
23
- // export const RESOLVED_CONFIG_ID = `\0${CONFIG_ID}`
24
- export const THEME_ID = 'virtual:$histoire-theme';
25
- export const RESOLVED_THEME_ID = `/__resolved__${THEME_ID}.css`;
26
- // export const RESOLVED_THEME_ID = `\0${THEME_ID}.css`
27
- export const SEARCH_TITLE_DATA_ID = 'virtual:$histoire-search-title-data';
28
- export const RESOLVED_SEARCH_TITLE_DATA_ID = `/__resolved__${SEARCH_TITLE_DATA_ID}`;
29
- // export const RESOLVED_SEARCH_TITLE_DATA_ID = `\0${SEARCH_TITLE_DATA_ID}`
30
- export const SEARCH_DOCS_DATA_ID = 'virtual:$histoire-search-docs-data';
31
- export const RESOLVED_SEARCH_DOCS_DATA_ID = `/__resolved__${SEARCH_DOCS_DATA_ID}`;
32
- // export const RESOLVED_SEARCH_DOCS_DATA_ID = `\0${SEARCH_DOCS_DATA_ID}`
33
- export const GENERATED_GLOBAL_SETUP = 'virtual:$histoire-generated-global-setup';
34
- export const RESOLVED_GENERATED_GLOBAL_SETUP = `/__resolved__${GENERATED_GLOBAL_SETUP}`;
35
- // export const RESOLVED_GENERATED_GLOBAL_SETUP = `\0${GENERATED_GLOBAL_SETUP}`
36
- export const GENERATED_SETUP_CODE = 'virtual:$histoire-generated-setup-code';
37
- export const RESOLVED_GENERATED_SETUP_CODE = `/__resolved__${GENERATED_SETUP_CODE}`;
38
- // export const RESOLVED_GENERATED_SETUP_CODE = `\0${GENERATED_SETUP_CODE}`
39
- export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client';
40
- export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `/__resolved__${SUPPORT_PLUGINS_CLIENT}`;
41
- // export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
42
- export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect';
43
- export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_COLLECT}`;
44
- // export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
45
- export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files';
46
- export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`;
47
- // export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
48
- const ID_SEPARATOR = '__-__';
49
- const PLUGINS_HAVE_DEV = [
50
- '@histoire/plugin-vue',
51
- ];
52
10
  export async function mergeHistoireViteConfig(viteConfig, ctx) {
53
11
  if (ctx.config.vite) {
54
12
  const command = ctx.mode === 'dev' ? 'serve' : 'build';
@@ -154,6 +112,11 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
154
112
  watch: {
155
113
  ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
156
114
  },
115
+ ...isServer
116
+ ? {
117
+ hmr: false,
118
+ }
119
+ : {},
157
120
  },
158
121
  define: {
159
122
  // We need to force this to be able to use `devtoolsRawSetupState`
@@ -175,242 +138,6 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
175
138
  isCollecting: isServer,
176
139
  };
177
140
  },
178
- async resolveId(id, importer) {
179
- if (id.startsWith(STORIES_ID)) {
180
- return RESOLVED_STORIES_ID;
181
- }
182
- if (id.startsWith(SETUP_ID)) {
183
- const setupFileConfig = ctx.config.setupFile;
184
- if (setupFileConfig) {
185
- let file;
186
- if (typeof setupFileConfig === 'string') {
187
- file = setupFileConfig;
188
- }
189
- else if (isServer && 'server' in setupFileConfig) {
190
- file = setupFileConfig.server;
191
- }
192
- else if (!isServer && 'browser' in setupFileConfig) {
193
- file = setupFileConfig.browser;
194
- }
195
- if (file) {
196
- return this.resolve(resolve(ctx.root, file), importer, {
197
- skipSelf: true,
198
- });
199
- }
200
- }
201
- return NOOP_ID;
202
- }
203
- if (id.startsWith(CONFIG_ID)) {
204
- return RESOLVED_CONFIG_ID;
205
- }
206
- if (id.startsWith(THEME_ID)) {
207
- return RESOLVED_THEME_ID;
208
- }
209
- if (id.startsWith(SEARCH_TITLE_DATA_ID)) {
210
- return RESOLVED_SEARCH_TITLE_DATA_ID;
211
- }
212
- if (id.startsWith(SEARCH_DOCS_DATA_ID)) {
213
- return RESOLVED_SEARCH_DOCS_DATA_ID;
214
- }
215
- if (id.startsWith(GENERATED_GLOBAL_SETUP)) {
216
- return RESOLVED_GENERATED_GLOBAL_SETUP;
217
- }
218
- if (id.startsWith(GENERATED_SETUP_CODE)) {
219
- const [, index] = id.split(ID_SEPARATOR);
220
- return `${RESOLVED_GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}`;
221
- }
222
- if (id.startsWith(SUPPORT_PLUGINS_CLIENT)) {
223
- return RESOLVED_SUPPORT_PLUGINS_CLIENT;
224
- }
225
- if (id.startsWith(SUPPORT_PLUGINS_COLLECT)) {
226
- return RESOLVED_SUPPORT_PLUGINS_COLLECT;
227
- }
228
- if (id.startsWith(MARKDOWN_FILES)) {
229
- return RESOLVED_MARKDOWN_FILES;
230
- }
231
- if (id.startsWith('virtual:story:')) {
232
- return `\0${id}`;
233
- }
234
- if (id.startsWith('virtual:story-source:')) {
235
- return `/__resolved__${id}`;
236
- // @TODO
237
- // return `\0${id}`
238
- }
239
- if (id.startsWith('virtual:md:')) {
240
- return `/__resolved__${id}`;
241
- // @TODO
242
- // return `\0${id}`
243
- }
244
- },
245
- async load(id) {
246
- if (id === RESOLVED_STORIES_ID) {
247
- const resolvedStories = ctx.storyFiles.filter(s => !!s.story);
248
- const files = resolvedStories.map((file, index) => {
249
- return {
250
- id: file.id,
251
- path: file.treePath,
252
- filePath: file.relativePath,
253
- story: {
254
- ...file.story,
255
- docsText: undefined,
256
- },
257
- supportPluginId: file.supportPluginId,
258
- index,
259
- };
260
- });
261
- return `${ctx.supportPlugins.map(p => p.importStoriesPrepend).filter(Boolean).join('\n')}
262
- ${resolvedStories.map((file, index) => {
263
- const supportPlugin = ctx.supportPlugins.find(p => p.id === file.supportPluginId);
264
- if (!supportPlugin) {
265
- throw new Error(`Could not find support plugin for story ${file.path}: ${file.supportPluginId}`);
266
- }
267
- return supportPlugin.importStoryComponent(file, index);
268
- }).filter(Boolean).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')}]
270
- export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
271
- const handlers = []
272
- export function onUpdate (cb) {
273
- handlers.push(cb)
274
- }
275
- if (import.meta.hot) {
276
- import.meta.hot.accept(newModule => {
277
- files = newModule.files
278
- tree = newModule.tree
279
- handlers.forEach(h => {
280
- h(newModule.files, newModule.tree)
281
- newModule.onUpdate(h)
282
- })
283
- })
284
- }`;
285
- }
286
- if (id === NOOP_ID) {
287
- return `export default () => {}`;
288
- }
289
- if (id === RESOLVED_CONFIG_ID) {
290
- let js = `export const config = ${JSON.stringify(ctx.config)}\n`;
291
- if (ctx.config.theme?.logo) {
292
- for (const key in ctx.config.theme.logo) {
293
- js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`;
294
- }
295
- }
296
- js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`;
297
- return js;
298
- }
299
- if (id === RESOLVED_THEME_ID) {
300
- let css = '*, ::before, ::after {';
301
- // Colors
302
- for (const color in ctx.config.theme?.colors ?? {}) {
303
- for (const key in ctx.config.theme.colors[color]) {
304
- css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`;
305
- }
306
- }
307
- css += '}';
308
- return css;
309
- }
310
- if (id === RESOLVED_SEARCH_TITLE_DATA_ID) {
311
- return getSearchDataJS(await generateTitleSearchData(ctx));
312
- }
313
- if (id === RESOLVED_SEARCH_DOCS_DATA_ID) {
314
- return getSearchDataJS(await generateDocSearchData(ctx));
315
- }
316
- if (id === RESOLVED_GENERATED_GLOBAL_SETUP) {
317
- if (ctx.config.setupCode) {
318
- return [
319
- // Import
320
- `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
321
- // List
322
- `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
323
- // Setups
324
- ...ctx.supportPlugins.map(p => p.setupFn).map(fnName => `export async function ${fnName} (payload) {
325
- for (const setup of setupList) {
326
- if (setup?.${fnName}) {
327
- await setup.${fnName}(payload)
328
- }
329
- }
330
- }`),
331
- ].join('\n');
332
- }
333
- else {
334
- return '';
335
- }
336
- }
337
- if (id.startsWith(RESOLVED_GENERATED_SETUP_CODE)) {
338
- const [, index] = id.split(ID_SEPARATOR);
339
- return ctx.config.setupCode?.[index] ?? '';
340
- }
341
- if (id === RESOLVED_SUPPORT_PLUGINS_CLIENT) {
342
- 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' : ''}`, {
343
- paths: [ctx.root, import.meta.url],
344
- }))})`);
345
- return `export const clientSupportPlugins = {
346
- ${plugins.join(',\n ')}
347
- }`;
348
- }
349
- if (id === RESOLVED_SUPPORT_PLUGINS_COLLECT) {
350
- 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' : ''}`, {
351
- paths: [ctx.root, import.meta.url],
352
- }))})`);
353
- return `export const collectSupportPlugins = {
354
- ${plugins.join(',\n ')}
355
- }`;
356
- }
357
- if (id === RESOLVED_MARKDOWN_FILES) {
358
- const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`virtual:md:${f.id}`)})`).join(',');
359
- return `import { reactive } from ${process.env.HISTOIRE_DEV ? `'vue'` : getInjectedImport('@histoire/vendors/vue')}
360
- export const markdownFiles = reactive({${filesJs}})
361
- if (import.meta.hot) {
362
- if (!window.__hst_md_hmr) {
363
- window.__hst_md_hmr = (newModule) => {
364
- markdownFiles[newModule.relativePath] = () => newModule
365
- }
366
- }
367
-
368
- import.meta.hot.accept(newModule => {
369
- Object.assign(markdownFiles, newModule.markdownFiles)
370
- })
371
- }`;
372
- }
373
- if (id.startsWith('\0virtual:story:')) {
374
- const moduleId = id.replace('\0', '');
375
- const storyFile = ctx.storyFiles.find(f => f.moduleId === moduleId && f.virtual);
376
- if (storyFile) {
377
- return storyFile.moduleCode;
378
- }
379
- }
380
- if (id.startsWith('/__resolved__virtual:story-source:')) {
381
- const storyId = id.slice('/__resolved__virtual:story-source:'.length);
382
- const storyFile = ctx.storyFiles.find(f => f.story?.id === storyId);
383
- if (storyFile) {
384
- let source;
385
- if (storyFile.virtual) {
386
- source = storyFile.moduleCode;
387
- }
388
- else {
389
- source = await fs.readFile(resolve(ctx.root, storyFile.relativePath), 'utf-8');
390
- }
391
- return `export default ${JSON.stringify(source)}`;
392
- }
393
- }
394
- if (id.startsWith('/__resolved__virtual:md:')) {
395
- const fileId = id.slice('/__resolved__virtual:md:'.length);
396
- const file = ctx.markdownFiles.find(f => f.id === fileId);
397
- if (!file) {
398
- throw new Error(`Markdown file not found: ${fileId}`);
399
- }
400
- const { html, frontmatter, relativePath } = file;
401
- return `export const html = ${JSON.stringify(html)}
402
- export const frontmatter = ${JSON.stringify(frontmatter)}
403
- export const relativePath = ${JSON.stringify(relativePath)}
404
-
405
- if (import.meta.hot) {
406
- import.meta.hot.accept(newModule => {
407
- if (newModule) {
408
- window.__hst_md_hmr(newModule)
409
- }
410
- })
411
- }`;
412
- }
413
- },
414
141
  handleHotUpdate(updateContext) {
415
142
  const story = ctx.storyFiles.find(file => file.path === updateContext.file);
416
143
  if (story) {
@@ -493,6 +220,7 @@ if (import.meta.hot) {
493
220
  };
494
221
  },
495
222
  });
223
+ plugins.push(createVirtualFilesPlugin(ctx, isServer));
496
224
  // Markdown
497
225
  plugins.push(...await createMarkdownPlugins(ctx));
498
226
  if (ctx.mode === 'build') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.13.0",
3
+ "version": "0.13.1",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -33,10 +33,10 @@
33
33
  "histoire": "./bin.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@histoire/app": "^0.13.0",
37
- "@histoire/controls": "^0.13.0",
38
- "@histoire/shared": "^0.13.0",
39
- "@histoire/vendors": "^0.13.0",
36
+ "@histoire/app": "^0.13.1",
37
+ "@histoire/controls": "^0.13.1",
38
+ "@histoire/shared": "^0.13.1",
39
+ "@histoire/vendors": "^0.13.1",
40
40
  "@types/flexsearch": "^0.7.3",
41
41
  "@types/markdown-it": "^12.2.3",
42
42
  "birpc": "^0.1.1",
@@ -60,7 +60,7 @@
60
60
  "pathe": "^0.2.0",
61
61
  "picocolors": "^1.0.0",
62
62
  "sade": "^1.8.1",
63
- "shiki-es": "^0.1.2",
63
+ "shiki-es": "^0.2.0",
64
64
  "sirv": "^2.0.2",
65
65
  "tinypool": "^0.1.2",
66
66
  "vite-node": "0.28.4"
package/src/node/build.ts CHANGED
@@ -86,13 +86,6 @@ export async function build (ctx: Context) {
86
86
  join(APP_PATH, 'bundle-main.js'),
87
87
  join(APP_PATH, 'bundle-sandbox.js'),
88
88
  ],
89
- output: {
90
- manualChunks (id) {
91
- if (!id.includes('@histoire/app') && id.includes('node_modules')) {
92
- return 'vendor'
93
- }
94
- },
95
- },
96
89
  plugins: [
97
90
  {
98
91
  name: 'histoire-build-rollup-options-override',
@@ -104,10 +97,6 @@ export async function build (ctx: Context) {
104
97
  },
105
98
  ],
106
99
  },
107
- outDir: ctx.config.outDir,
108
- emptyOutDir: true,
109
- cssCodeSplit: false,
110
- minify: false,
111
100
  },
112
101
  })
113
102
 
@@ -145,6 +134,25 @@ export async function build (ctx: Context) {
145
134
  config (config) {
146
135
  // Don't externalize
147
136
  config.build.rollupOptions.external = []
137
+
138
+ // Force chunk strategy
139
+ config.build.rollupOptions.output = {
140
+ manualChunks (id) {
141
+ if (!id.includes('@histoire/app') && id.includes('node_modules')) {
142
+ return 'vendor'
143
+ }
144
+ },
145
+ }
146
+
147
+ // Force vite build options
148
+ Object.assign(config.build, {
149
+ outDir: ctx.config.outDir,
150
+ emptyOutDir: true,
151
+ cssCodeSplit: false,
152
+ minify: false,
153
+ // Don't build in SSR mode
154
+ ssr: false,
155
+ })
148
156
  },
149
157
  })
150
158
 
@@ -3,7 +3,8 @@ import { createServer as createViteServer } from 'vite'
3
3
  import pc from 'picocolors'
4
4
  import type { ServerStoryFile } from '@histoire/shared'
5
5
  import { Context } from './context.js'
6
- import { getViteConfigWithPlugins, RESOLVED_MARKDOWN_FILES, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js'
6
+ import { getViteConfigWithPlugins } from './vite.js'
7
+ import * as VirtualFiles from './virtual/index.js'
7
8
  import { onStoryChange, onStoryListChange, watchStories } from './stories.js'
8
9
  import { useCollectStories } from './collect/index.js'
9
10
  import { DevPluginApi } from './plugin.js'
@@ -170,8 +171,8 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
170
171
 
171
172
  clearInvalidates()
172
173
 
173
- invalidateModule(RESOLVED_STORIES_ID)
174
- invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID)
174
+ invalidateModule(VirtualFiles.RESOLVED_STORIES_ID)
175
+ invalidateModule(VirtualFiles.RESOLVED_SEARCH_TITLE_DATA_ID)
175
176
 
176
177
  collecting = false
177
178
 
@@ -181,12 +182,12 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
181
182
  }
182
183
 
183
184
  onStoryListChange(() => {
184
- invalidateModule(RESOLVED_STORIES_ID)
185
- invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID)
185
+ invalidateModule(VirtualFiles.RESOLVED_STORIES_ID)
186
+ invalidateModule(VirtualFiles.RESOLVED_SEARCH_TITLE_DATA_ID)
186
187
  })
187
188
 
188
189
  onMarkdownListChange(() => {
189
- invalidateModule(RESOLVED_MARKDOWN_FILES)
190
+ invalidateModule(VirtualFiles.RESOLVED_MARKDOWN_FILES)
190
191
  })
191
192
 
192
193
  async function close () {
@@ -0,0 +1,47 @@
1
+ // @TODO wait for virtual modules HMR to work in vite to use `virtual:` and `\0` prefix
2
+ // https://github.com/vitejs/vite/pull/10313
3
+ export const STORIES_ID = 'virtual:$histoire-stories'
4
+ export const RESOLVED_STORIES_ID = `/__resolved__${STORIES_ID}`
5
+ // export const RESOLVED_STORIES_ID = `\0${STORIES_ID}`
6
+ export const SETUP_ID = 'virtual:$histoire-setup'
7
+ export const NOOP_ID = 'virtual:$histoire-noop'
8
+ export const CONFIG_ID = 'virtual:$histoire-config'
9
+ export const RESOLVED_CONFIG_ID = `/__resolved__${CONFIG_ID}`
10
+ // export const RESOLVED_CONFIG_ID = `\0${CONFIG_ID}`
11
+ export const THEME_ID = 'virtual:$histoire-theme'
12
+ export const RESOLVED_THEME_ID = `/__resolved__${THEME_ID}.css`
13
+ // export const RESOLVED_THEME_ID = `\0${THEME_ID}.css`
14
+ export const SEARCH_TITLE_DATA_ID = 'virtual:$histoire-search-title-data'
15
+ export const RESOLVED_SEARCH_TITLE_DATA_ID = `/__resolved__${SEARCH_TITLE_DATA_ID}`
16
+ // export const RESOLVED_SEARCH_TITLE_DATA_ID = `\0${SEARCH_TITLE_DATA_ID}`
17
+ export const SEARCH_DOCS_DATA_ID = 'virtual:$histoire-search-docs-data'
18
+ export const RESOLVED_SEARCH_DOCS_DATA_ID = `/__resolved__${SEARCH_DOCS_DATA_ID}`
19
+ // export const RESOLVED_SEARCH_DOCS_DATA_ID = `\0${SEARCH_DOCS_DATA_ID}`
20
+ export const GENERATED_GLOBAL_SETUP = 'virtual:$histoire-generated-global-setup'
21
+ export const RESOLVED_GENERATED_GLOBAL_SETUP = `/__resolved__${GENERATED_GLOBAL_SETUP}`
22
+ // export const RESOLVED_GENERATED_GLOBAL_SETUP = `\0${GENERATED_GLOBAL_SETUP}`
23
+ export const GENERATED_SETUP_CODE = 'virtual:$histoire-generated-setup-code'
24
+ export const RESOLVED_GENERATED_SETUP_CODE = `/__resolved__${GENERATED_SETUP_CODE}`
25
+ // export const RESOLVED_GENERATED_SETUP_CODE = `\0${GENERATED_SETUP_CODE}`
26
+ export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client'
27
+ export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `/__resolved__${SUPPORT_PLUGINS_CLIENT}`
28
+ // export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
29
+ export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect'
30
+ export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_COLLECT}`
31
+ // export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
32
+ export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files'
33
+ export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`
34
+ // export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
35
+
36
+ export * from './markdown.js'
37
+ export * from './noop.js'
38
+ export * from './resolved-config.js'
39
+ export * from './resolved-generated-global-setup.js'
40
+ export * from './resolved-generated-setup-code.js'
41
+ export * from './resolved-markdown-files.js'
42
+ export * from './resolved-stories.js'
43
+ export * from './resolved-support-plugins-client.js'
44
+ export * from './resolved-support-plugins-collect.js'
45
+ export * from './resolved-theme.js'
46
+ export * from './story.js'
47
+ export * from './story-source.js'
@@ -0,0 +1,21 @@
1
+ import type { Context } from '../context.js'
2
+
3
+ export const markdown = (ctx: Context, id: string) => {
4
+ const fileId = id.slice('/__resolved__virtual:md:'.length)
5
+ const file = ctx.markdownFiles.find(f => f.id === fileId)
6
+ if (!file) {
7
+ throw new Error(`Markdown file not found: ${fileId}`)
8
+ }
9
+ const { html, frontmatter, relativePath } = file
10
+ return `export const html = ${JSON.stringify(html)}
11
+ export const frontmatter = ${JSON.stringify(frontmatter)}
12
+ export const relativePath = ${JSON.stringify(relativePath)}
13
+
14
+ if (import.meta.hot) {
15
+ import.meta.hot.accept(newModule => {
16
+ if (newModule) {
17
+ window.__hst_md_hmr(newModule)
18
+ }
19
+ })
20
+ }`
21
+ }
@@ -0,0 +1 @@
1
+ export const noop = () => `export default () => {}`
@@ -0,0 +1,12 @@
1
+ import type { Context } from '../context.js'
2
+
3
+ export const resolvedConfig = (ctx: Context) => {
4
+ let js = `export const config = ${JSON.stringify(ctx.config)}\n`
5
+ if (ctx.config.theme?.logo) {
6
+ for (const key in ctx.config.theme.logo) {
7
+ js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`
8
+ }
9
+ }
10
+ js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`
11
+ return js
12
+ }
@@ -0,0 +1,24 @@
1
+ import type { Context } from '../context.js'
2
+ import { GENERATED_SETUP_CODE } from './index.js'
3
+ import { ID_SEPARATOR } from './util.js'
4
+
5
+ export const resolvedGeneratedGlobalSetup = (ctx: Context) => {
6
+ if (ctx.config.setupCode) {
7
+ return [
8
+ // Import
9
+ `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
10
+ // List
11
+ `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
12
+ // Setups
13
+ ...ctx.supportPlugins.map(p => p.setupFn).map(fnName => `export async function ${fnName} (payload) {
14
+ for (const setup of setupList) {
15
+ if (setup?.${fnName}) {
16
+ await setup.${fnName}(payload)
17
+ }
18
+ }
19
+ }`),
20
+ ].join('\n')
21
+ } else {
22
+ return ''
23
+ }
24
+ }
@@ -0,0 +1,7 @@
1
+ import type { Context } from '../context.js'
2
+ import { ID_SEPARATOR } from './util.js'
3
+
4
+ export const resolvedGeneratedSetupCode = (ctx: Context, id: string) => {
5
+ const [, index] = id.split(ID_SEPARATOR)
6
+ return ctx.config.setupCode?.[index] ?? ''
7
+ }
@@ -0,0 +1,19 @@
1
+ import type { Context } from '../context.js'
2
+ import { getInjectedImport } from '../util/vendors.js'
3
+
4
+ export const resolvedMarkdownFiles = (ctx: Context) => {
5
+ const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`virtual:md:${f.id}`)})`).join(',')
6
+ return `import { reactive } from ${process.env.HISTOIRE_DEV ? `'vue'` : getInjectedImport('@histoire/vendors/vue')}
7
+ export const markdownFiles = reactive({${filesJs}})
8
+ if (import.meta.hot) {
9
+ if (!window.__hst_md_hmr) {
10
+ window.__hst_md_hmr = (newModule) => {
11
+ markdownFiles[newModule.relativePath] = () => newModule
12
+ }
13
+ }
14
+
15
+ import.meta.hot.accept(newModule => {
16
+ Object.assign(markdownFiles, newModule.markdownFiles)
17
+ })
18
+ }`
19
+ }
@@ -0,0 +1,43 @@
1
+ import type { Context } from '../context.js'
2
+ import { makeTree } from '../tree.js'
3
+
4
+ export const resolvedStories = (ctx: Context) => {
5
+ const resolvedStories = ctx.storyFiles.filter(s => !!s.story)
6
+ const files = resolvedStories.map((file, index) => {
7
+ return {
8
+ id: file.id,
9
+ path: file.treePath,
10
+ filePath: file.relativePath,
11
+ story: {
12
+ ...file.story,
13
+ docsText: undefined,
14
+ },
15
+ supportPluginId: file.supportPluginId,
16
+ index,
17
+ }
18
+ })
19
+ return `${ctx.supportPlugins.map(p => p.importStoriesPrepend).filter(Boolean).join('\n')}
20
+ ${resolvedStories.map((file, index) => {
21
+ const supportPlugin = ctx.supportPlugins.find(p => p.id === file.supportPluginId)
22
+ if (!supportPlugin) {
23
+ throw new Error(`Could not find support plugin for story ${file.path}: ${file.supportPluginId}`)
24
+ }
25
+ return supportPlugin.importStoryComponent(file, index)
26
+ }).filter(Boolean).join('\n')}
27
+ 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')}]
28
+ export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
29
+ const handlers = []
30
+ export function onUpdate (cb) {
31
+ handlers.push(cb)
32
+ }
33
+ if (import.meta.hot) {
34
+ import.meta.hot.accept(newModule => {
35
+ files = newModule.files
36
+ tree = newModule.tree
37
+ handlers.forEach(h => {
38
+ h(newModule.files, newModule.tree)
39
+ newModule.onUpdate(h)
40
+ })
41
+ })
42
+ }`
43
+ }
@@ -0,0 +1,14 @@
1
+ import { createRequire } from 'node:module'
2
+ import type { Context } from '../context.js'
3
+ import { PLUGINS_HAVE_DEV } from './util.js'
4
+
5
+ const require = createRequire(import.meta.url)
6
+
7
+ export const resolvedSupportPluginsClient = (ctx: Context) => {
8
+ 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' : ''}`, {
9
+ paths: [ctx.root, import.meta.url],
10
+ }))})`)
11
+ return `export const clientSupportPlugins = {
12
+ ${plugins.join(',\n ')}
13
+ }`
14
+ }
@@ -0,0 +1,14 @@
1
+ import { createRequire } from 'node:module'
2
+ import type { Context } from '../context.js'
3
+ import { PLUGINS_HAVE_DEV } from './util.js'
4
+
5
+ const require = createRequire(import.meta.url)
6
+
7
+ export const resolvedSupportPluginsCollect = (ctx: Context) => {
8
+ 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' : ''}`, {
9
+ paths: [ctx.root, import.meta.url],
10
+ }))})`)
11
+ return `export const collectSupportPlugins = {
12
+ ${plugins.join(',\n ')}
13
+ }`
14
+ }
@@ -0,0 +1,14 @@
1
+ import { parseColor } from '../colors.js'
2
+ import type { Context } from '../context.js'
3
+
4
+ export const resolvedTheme = (ctx: Context) => {
5
+ let css = '*, ::before, ::after {'
6
+ // Colors
7
+ for (const color in ctx.config.theme?.colors ?? {}) {
8
+ for (const key in ctx.config.theme.colors[color]) {
9
+ css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`
10
+ }
11
+ }
12
+ css += '}'
13
+ return css
14
+ }