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
@@ -0,0 +1,118 @@
1
+ import { resolve } from 'pathe';
2
+ import * as VirtualFiles from './index.js';
3
+ import { ID_SEPARATOR } from './util.js';
4
+ import { generateDocSearchData, generateTitleSearchData, getSearchDataJS } from '../search.js';
5
+ export const createVirtualFilesPlugin = (ctx, isServer) => ({
6
+ name: 'histoire-virtual-files',
7
+ async resolveId(id, importer) {
8
+ if (id.startsWith(VirtualFiles.STORIES_ID)) {
9
+ return VirtualFiles.RESOLVED_STORIES_ID;
10
+ }
11
+ if (id.startsWith(VirtualFiles.SETUP_ID)) {
12
+ const setupFileConfig = ctx.config.setupFile;
13
+ if (setupFileConfig) {
14
+ let file;
15
+ if (typeof setupFileConfig === 'string') {
16
+ file = setupFileConfig;
17
+ }
18
+ else if (isServer && 'server' in setupFileConfig) {
19
+ file = setupFileConfig.server;
20
+ }
21
+ else if (!isServer && 'browser' in setupFileConfig) {
22
+ file = setupFileConfig.browser;
23
+ }
24
+ if (file) {
25
+ return this.resolve(resolve(ctx.root, file), importer, {
26
+ skipSelf: true,
27
+ });
28
+ }
29
+ }
30
+ return VirtualFiles.NOOP_ID;
31
+ }
32
+ if (id.startsWith(VirtualFiles.CONFIG_ID)) {
33
+ return VirtualFiles.RESOLVED_CONFIG_ID;
34
+ }
35
+ if (id.startsWith(VirtualFiles.THEME_ID)) {
36
+ return VirtualFiles.RESOLVED_THEME_ID;
37
+ }
38
+ if (id.startsWith(VirtualFiles.SEARCH_TITLE_DATA_ID)) {
39
+ return VirtualFiles.RESOLVED_SEARCH_TITLE_DATA_ID;
40
+ }
41
+ if (id.startsWith(VirtualFiles.SEARCH_DOCS_DATA_ID)) {
42
+ return VirtualFiles.RESOLVED_SEARCH_DOCS_DATA_ID;
43
+ }
44
+ if (id.startsWith(VirtualFiles.GENERATED_GLOBAL_SETUP)) {
45
+ return VirtualFiles.RESOLVED_GENERATED_GLOBAL_SETUP;
46
+ }
47
+ if (id.startsWith(VirtualFiles.GENERATED_SETUP_CODE)) {
48
+ const [, index] = id.split(ID_SEPARATOR);
49
+ return `${VirtualFiles.RESOLVED_GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}`;
50
+ }
51
+ if (id.startsWith(VirtualFiles.SUPPORT_PLUGINS_CLIENT)) {
52
+ return VirtualFiles.RESOLVED_SUPPORT_PLUGINS_CLIENT;
53
+ }
54
+ if (id.startsWith(VirtualFiles.SUPPORT_PLUGINS_COLLECT)) {
55
+ return VirtualFiles.RESOLVED_SUPPORT_PLUGINS_COLLECT;
56
+ }
57
+ if (id.startsWith(VirtualFiles.MARKDOWN_FILES)) {
58
+ return VirtualFiles.RESOLVED_MARKDOWN_FILES;
59
+ }
60
+ if (id.startsWith('virtual:story:')) {
61
+ return `\0${id}`;
62
+ }
63
+ if (id.startsWith('virtual:story-source:')) {
64
+ return `/__resolved__${id}`;
65
+ // @TODO
66
+ // return `\0${id}`
67
+ }
68
+ if (id.startsWith('virtual:md:')) {
69
+ return `/__resolved__${id}`;
70
+ // @TODO
71
+ // return `\0${id}`
72
+ }
73
+ },
74
+ async load(id) {
75
+ if (id === VirtualFiles.RESOLVED_STORIES_ID) {
76
+ return VirtualFiles.resolvedStories(ctx);
77
+ }
78
+ if (id === VirtualFiles.NOOP_ID) {
79
+ return VirtualFiles.noop();
80
+ }
81
+ if (id === VirtualFiles.RESOLVED_CONFIG_ID) {
82
+ return VirtualFiles.resolvedConfig(ctx);
83
+ }
84
+ if (id === VirtualFiles.RESOLVED_THEME_ID) {
85
+ return VirtualFiles.resolvedTheme(ctx);
86
+ }
87
+ if (id === VirtualFiles.RESOLVED_SEARCH_TITLE_DATA_ID) {
88
+ return getSearchDataJS(await generateTitleSearchData(ctx));
89
+ }
90
+ if (id === VirtualFiles.RESOLVED_SEARCH_DOCS_DATA_ID) {
91
+ return getSearchDataJS(await generateDocSearchData(ctx));
92
+ }
93
+ if (id === VirtualFiles.RESOLVED_GENERATED_GLOBAL_SETUP) {
94
+ return VirtualFiles.resolvedGeneratedGlobalSetup(ctx);
95
+ }
96
+ if (id.startsWith(VirtualFiles.RESOLVED_GENERATED_SETUP_CODE)) {
97
+ return VirtualFiles.resolvedGeneratedSetupCode(ctx, id);
98
+ }
99
+ if (id === VirtualFiles.RESOLVED_SUPPORT_PLUGINS_CLIENT) {
100
+ return VirtualFiles.resolvedSupportPluginsClient(ctx);
101
+ }
102
+ if (id === VirtualFiles.RESOLVED_SUPPORT_PLUGINS_COLLECT) {
103
+ return VirtualFiles.resolvedSupportPluginsCollect(ctx);
104
+ }
105
+ if (id === VirtualFiles.RESOLVED_MARKDOWN_FILES) {
106
+ return VirtualFiles.resolvedMarkdownFiles(ctx);
107
+ }
108
+ if (id.startsWith('\0virtual:story:')) {
109
+ return VirtualFiles.story(ctx, id);
110
+ }
111
+ if (id.startsWith('/__resolved__virtual:story-source:')) {
112
+ return VirtualFiles.storySource(ctx, id);
113
+ }
114
+ if (id.startsWith('/__resolved__virtual:md:')) {
115
+ return VirtualFiles.markdown(ctx, id);
116
+ }
117
+ },
118
+ });
@@ -1,27 +1,5 @@
1
1
  import { InlineConfig } from 'vite';
2
2
  import { Context } from './context.js';
3
- export declare const STORIES_ID = "virtual:$histoire-stories";
4
- export declare const RESOLVED_STORIES_ID: string;
5
- export declare const SETUP_ID = "virtual:$histoire-setup";
6
- export declare const NOOP_ID = "virtual:$histoire-noop";
7
- export declare const CONFIG_ID = "virtual:$histoire-config";
8
- export declare const RESOLVED_CONFIG_ID: string;
9
- export declare const THEME_ID = "virtual:$histoire-theme";
10
- export declare const RESOLVED_THEME_ID: string;
11
- export declare const SEARCH_TITLE_DATA_ID = "virtual:$histoire-search-title-data";
12
- export declare const RESOLVED_SEARCH_TITLE_DATA_ID: string;
13
- export declare const SEARCH_DOCS_DATA_ID = "virtual:$histoire-search-docs-data";
14
- export declare const RESOLVED_SEARCH_DOCS_DATA_ID: string;
15
- export declare const GENERATED_GLOBAL_SETUP = "virtual:$histoire-generated-global-setup";
16
- export declare const RESOLVED_GENERATED_GLOBAL_SETUP: string;
17
- export declare const GENERATED_SETUP_CODE = "virtual:$histoire-generated-setup-code";
18
- export declare const RESOLVED_GENERATED_SETUP_CODE: string;
19
- export declare const SUPPORT_PLUGINS_CLIENT = "virtual:$histoire-support-plugins-client";
20
- export declare const RESOLVED_SUPPORT_PLUGINS_CLIENT: string;
21
- export declare const SUPPORT_PLUGINS_COLLECT = "virtual:$histoire-support-plugins-collect";
22
- export declare const RESOLVED_SUPPORT_PLUGINS_COLLECT: string;
23
- export declare const MARKDOWN_FILES = "virtual:$histoire-markdown-files";
24
- export declare const RESOLVED_MARKDOWN_FILES: string;
25
3
  export declare function mergeHistoireViteConfig(viteConfig: InlineConfig, ctx: Context): Promise<InlineConfig>;
26
4
  export interface ViteConfigWithPlugins {
27
5
  viteConfig: InlineConfig;
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.2",
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.2",
37
+ "@histoire/controls": "^0.13.2",
38
+ "@histoire/shared": "^0.13.2",
39
+ "@histoire/vendors": "^0.13.2",
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/bin.ts CHANGED
@@ -16,6 +16,7 @@ program.version(version)
16
16
  program.command('dev')
17
17
  .describe('open the stories in your browser for development')
18
18
  .option('-p, --port <port>', 'Listening port of the server')
19
+ .option('--open', 'Open in your default browser')
19
20
  .action(async (options) => {
20
21
  const { devCommand } = await import('./commands/dev.js')
21
22
  return devCommand(options)
@@ -0,0 +1,62 @@
1
+ import type { Context } from './context.js'
2
+ import type { ServerStory } from '@histoire/shared'
3
+
4
+ interface SerializedStory extends Omit<ServerStory, 'docsText'> {
5
+ relativePath: string
6
+ supportPluginId: string
7
+ treePath?: string[]
8
+ virtual?: boolean
9
+ markdownFile?: SerializedMarkdownFile
10
+ }
11
+
12
+ interface SerializedMarkdownFile {
13
+ id: string
14
+ relativePath: string
15
+ isRelatedToStory: boolean
16
+ frontmatter?: any
17
+ }
18
+
19
+ interface SerializedStoryData {
20
+ stories: SerializedStory[]
21
+ markdownFiles: SerializedMarkdownFile[]
22
+ }
23
+
24
+ export function getSerializedStoryData (ctx: Context): SerializedStoryData {
25
+ const data: SerializedStoryData = {
26
+ stories: [],
27
+ markdownFiles: [],
28
+ }
29
+
30
+ for (const storyFile of ctx.storyFiles) {
31
+ if (storyFile.story) {
32
+ data.stories.push({
33
+ ...Object.assign({}, storyFile.story, {
34
+ docsText: undefined,
35
+ }),
36
+ relativePath: storyFile.relativePath,
37
+ supportPluginId: storyFile.supportPluginId,
38
+ treePath: storyFile.treePath,
39
+ virtual: storyFile.virtual,
40
+ markdownFile: storyFile.markdownFile
41
+ ? {
42
+ id: storyFile.markdownFile.id,
43
+ relativePath: storyFile.markdownFile.relativePath,
44
+ isRelatedToStory: storyFile.markdownFile.isRelatedToStory,
45
+ frontmatter: storyFile.markdownFile.frontmatter,
46
+ }
47
+ : null,
48
+ })
49
+ }
50
+ }
51
+
52
+ for (const markdownFile of ctx.markdownFiles) {
53
+ data.markdownFiles.push({
54
+ id: markdownFile.id,
55
+ relativePath: markdownFile.relativePath,
56
+ isRelatedToStory: markdownFile.isRelatedToStory,
57
+ frontmatter: markdownFile.frontmatter,
58
+ })
59
+ }
60
+
61
+ return data
62
+ }
package/src/node/build.ts CHANGED
@@ -24,6 +24,7 @@ import { BuildPluginApi } from './plugin.js'
24
24
  import { useModuleLoader } from './load.js'
25
25
  import { startPreview } from './preview.js'
26
26
  import { createMarkdownFilesWatcher } from './markdown.js'
27
+ import { getSerializedStoryData } from './build-serialize.js'
27
28
 
28
29
  const PRELOAD_MODULES = [
29
30
  'vendor',
@@ -86,13 +87,6 @@ export async function build (ctx: Context) {
86
87
  join(APP_PATH, 'bundle-main.js'),
87
88
  join(APP_PATH, 'bundle-sandbox.js'),
88
89
  ],
89
- output: {
90
- manualChunks (id) {
91
- if (!id.includes('@histoire/app') && id.includes('node_modules')) {
92
- return 'vendor'
93
- }
94
- },
95
- },
96
90
  plugins: [
97
91
  {
98
92
  name: 'histoire-build-rollup-options-override',
@@ -104,10 +98,6 @@ export async function build (ctx: Context) {
104
98
  },
105
99
  ],
106
100
  },
107
- outDir: ctx.config.outDir,
108
- emptyOutDir: true,
109
- cssCodeSplit: false,
110
- minify: false,
111
101
  },
112
102
  })
113
103
 
@@ -145,6 +135,25 @@ export async function build (ctx: Context) {
145
135
  config (config) {
146
136
  // Don't externalize
147
137
  config.build.rollupOptions.external = []
138
+
139
+ // Force chunk strategy
140
+ config.build.rollupOptions.output = {
141
+ manualChunks (id) {
142
+ if (!id.includes('@histoire/app') && id.includes('node_modules')) {
143
+ return 'vendor'
144
+ }
145
+ },
146
+ }
147
+
148
+ // Force vite build options
149
+ Object.assign(config.build, {
150
+ outDir: ctx.config.outDir,
151
+ emptyOutDir: true,
152
+ cssCodeSplit: false,
153
+ minify: false,
154
+ // Don't build in SSR mode
155
+ ssr: false,
156
+ })
148
157
  },
149
158
  })
150
159
 
@@ -173,6 +182,8 @@ export async function build (ctx: Context) {
173
182
  const sandboxHtml = generateEntryHtml(sandboxOutput.fileName, styleOutput.fileName, {}, ctx)
174
183
  await writeFile('__sandbox.html', sandboxHtml, ctx)
175
184
 
185
+ await writeFile('histoire.json', JSON.stringify(getSerializedStoryData(ctx), null, 2), ctx)
186
+
176
187
  const duration = performance.now() - startTime
177
188
  if (emptyStoryCount) {
178
189
  console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`))
@@ -6,6 +6,7 @@ import { createServer } from '../server.js'
6
6
 
7
7
  export interface DevOptions {
8
8
  port: number
9
+ open?: boolean
9
10
  }
10
11
 
11
12
  export async function devCommand (options: DevOptions) {
@@ -15,7 +16,10 @@ export async function devCommand (options: DevOptions) {
15
16
  const ctx = await createContext({
16
17
  mode: 'dev',
17
18
  })
18
- const { server, viteConfigFile, close } = await createServer(ctx, { port: options.port })
19
+ const { server, viteConfigFile, close } = await createServer(ctx, {
20
+ port: options.port,
21
+ open: options.open,
22
+ })
19
23
  server.printUrls()
20
24
 
21
25
  // Histoire config watcher
@@ -48,6 +48,8 @@ export function getDefaultConfig (): HistoireConfig {
48
48
  primary: defaultColors.emerald,
49
49
  gray: defaultColors.zinc,
50
50
  },
51
+ defaultColorScheme: 'auto',
52
+ storeColorScheme: true,
51
53
  },
52
54
  responsivePresets: [
53
55
  {