histoire 0.17.4 → 0.17.5

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.
@@ -120,21 +120,32 @@ export async function createMarkdownFilesWatcher(ctx) {
120
120
  cwd: ctx.root,
121
121
  ignored: ctx.config.storyIgnored,
122
122
  });
123
- async function addFile(relativePath) {
123
+ /**
124
+ * Initial scan is complete.
125
+ */
126
+ let watcherIsReady = false;
127
+ function addFile(relativePath) {
124
128
  const absolutePath = path.resolve(ctx.root, relativePath);
125
- const dirFiles = await fs.readdir(path.dirname(absolutePath));
129
+ const dirFiles = fs.readdirSync(path.dirname(absolutePath));
126
130
  const truncatedName = path.basename(absolutePath, '.md');
127
131
  const isRelatedToStory = dirFiles.some((file) => !file.endsWith('.md') && file.startsWith(truncatedName));
128
- const { data: frontmatter, content } = matter(await fs.readFile(absolutePath, 'utf8'));
129
- const html = md.render(content, {
130
- file: absolutePath,
131
- });
132
+ const { data: frontmatter, content } = matter(fs.readFileSync(absolutePath, 'utf8'));
133
+ let html;
134
+ // We don't immediately render markdown during initial scanning in case
135
+ // markdown references other files in links (otherwise they might not
136
+ // be scanned yet and will throw 'not found' errors).
137
+ if (watcherIsReady) {
138
+ html = md.render(content, {
139
+ file: absolutePath,
140
+ });
141
+ }
132
142
  const file = {
133
143
  id: paramCase(relativePath.toLowerCase()),
134
144
  relativePath,
135
145
  absolutePath,
136
146
  isRelatedToStory,
137
147
  frontmatter,
148
+ content,
138
149
  html,
139
150
  };
140
151
  ctx.markdownFiles.push(file);
@@ -190,7 +201,20 @@ export async function createMarkdownFilesWatcher(ctx) {
190
201
  await new Promise(resolve => {
191
202
  watcher.once('ready', resolve);
192
203
  });
193
- return {
194
- stop,
195
- };
204
+ try {
205
+ // Render markdown after initial scan is complete.
206
+ for (const mdFile of ctx.markdownFiles) {
207
+ mdFile.html = md.render(mdFile.content, {
208
+ file: mdFile.absolutePath,
209
+ });
210
+ }
211
+ watcherIsReady = true;
212
+ return {
213
+ stop,
214
+ };
215
+ }
216
+ catch (e) {
217
+ stop();
218
+ throw e;
219
+ }
196
220
  }
@@ -22,8 +22,9 @@ export async function createServer(ctx, options = {}) {
22
22
  viteConfigFile,
23
23
  };
24
24
  };
25
+ // Should be run sequentially to get a fresh vite.config.js each time
26
+ const { server: nodeServer } = await getViteServer(true); // Run before normal vite to prevent breaking HMR in Nuxt
25
27
  const { server, viteConfigFile } = await getViteServer(false);
26
- const { server: nodeServer } = await getViteServer(true); // Should be run after the first one to get a fresh vite.config.js
27
28
  await watchStories(ctx);
28
29
  const { stop: stopMdFileWatcher } = await createMarkdownFilesWatcher(ctx);
29
30
  const moduleLoader = useModuleLoader({
package/dist/node/vite.js CHANGED
@@ -59,7 +59,7 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
59
59
  }
60
60
  plugins.push({
61
61
  name: 'histoire-vite-plugin',
62
- config() {
62
+ config(_, { command }) {
63
63
  return {
64
64
  resolve: {
65
65
  dedupe: [
@@ -112,7 +112,7 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
112
112
  watch: {
113
113
  ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
114
114
  },
115
- hmr: !isServer,
115
+ hmr: command === 'build' ? false : !isServer,
116
116
  },
117
117
  define: {
118
118
  // We need to force this to be able to use `devtoolsRawSetupState`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.17.4",
3
+ "version": "0.17.5",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -64,9 +64,9 @@
64
64
  "shiki-es": "^0.2.0",
65
65
  "sirv": "^2.0.3",
66
66
  "vite-node": "^0.34.1",
67
+ "@histoire/app": "^0.17.5",
67
68
  "@histoire/controls": "^0.17.4",
68
- "@histoire/app": "^0.17.4",
69
- "@histoire/shared": "^0.17.4",
69
+ "@histoire/shared": "^0.17.5",
70
70
  "@histoire/vendors": "^0.17.4"
71
71
  },
72
72
  "peerDependencies": {
@@ -0,0 +1,7 @@
1
+ <!-- File should link to test2 file. -->
2
+ <!-- File should return test2 successfully, even though test1 loads before test2. -->
3
+
4
+ # Test1
5
+
6
+ Link to test 2
7
+ [Test2](./test2.story.md)
@@ -0,0 +1,5 @@
1
+ <!-- File should link to test1 file. -->
2
+ # Test2
3
+
4
+ Link to test 1
5
+ [Test1](./test1.story.md)
@@ -0,0 +1,59 @@
1
+ import { createWriteStream, unlinkSync } from 'node:fs'
2
+ import path from 'node:path'
3
+ import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest'
4
+ import { createMarkdownFilesWatcher } from '../markdown.js'
5
+ import { watchStories } from '../stories.js'
6
+ import { Context, createContext } from '../context.js'
7
+
8
+ describe('markdown', async () => {
9
+ vi.spyOn(process, 'cwd').mockReturnValue(path.resolve(__dirname, './markdown'))
10
+
11
+ let ctx: Context
12
+ let storyWatcher: Awaited<ReturnType<typeof watchStories>>
13
+
14
+ beforeEach(async () => {
15
+ ctx = await createContext({
16
+ mode: 'dev',
17
+ })
18
+
19
+ // create watch stories to set context root etc.
20
+ storyWatcher = await watchStories(ctx)
21
+ })
22
+
23
+ afterEach(() => {
24
+ storyWatcher.close()
25
+ })
26
+
27
+ test('should not throw error or depend on - resolve order for linking', async () => {
28
+ // FileWatcher should pickup the test markdown files (test1 and test2)
29
+ // test1 links to test2 (issue previously as test1 resolved first)
30
+ // test 2 links to test1
31
+ const { stop } = await createMarkdownFilesWatcher(ctx)
32
+ expect(ctx.markdownFiles.length).toEqual(2)
33
+ stop()
34
+ })
35
+
36
+ test('should render html from md', async () => {
37
+ const { stop } = await createMarkdownFilesWatcher(ctx)
38
+ expect(ctx.markdownFiles[0].html).toContain('<p>')
39
+ stop()
40
+ })
41
+
42
+ test('should throw error on missing [md] story file.', async () => {
43
+ const testFile3 = '/markdown/test3.story.md'
44
+ const writer = createWriteStream(__dirname.concat(testFile3))
45
+ // link to missing file.
46
+ writer.write(
47
+ '<!-- File should link to test1 file. -->\n' +
48
+ '# Test3\n\n' +
49
+ 'Link to test 4\n' +
50
+ '[TEST](./test4.story.md)\n')
51
+ writer.end()
52
+ await new Promise(resolve => writer.on('finish', resolve))
53
+
54
+ // create markdownWatcher and check for error
55
+ await expect(async () => createMarkdownFilesWatcher(ctx)).rejects.toThrowError()
56
+ // delete test file, so failures are removed as well.
57
+ unlinkSync(__dirname.concat(testFile3))
58
+ })
59
+ })
@@ -1,7 +1,7 @@
1
1
  import { describe, test, expect } from 'vitest'
2
- import { makeTree } from './tree'
3
- import { getDefaultConfig } from './config'
4
- import type { StoryFile } from './types'
2
+ import type { ServerStoryFile } from '@histoire/shared'
3
+ import { makeTree } from '../tree.js'
4
+ import { getDefaultConfig } from '../config.js'
5
5
 
6
6
  let id = 0
7
7
 
@@ -9,13 +9,15 @@ interface StoryFileFactoryOptions {
9
9
  treePath: string[]
10
10
  }
11
11
 
12
- function storyFileFactory (options: StoryFileFactoryOptions): StoryFile {
12
+ function storyFileFactory (options: StoryFileFactoryOptions): ServerStoryFile {
13
13
  return {
14
14
  id: `id_${id++}`,
15
15
  path: options.treePath.join('/'),
16
16
  treePath: options.treePath,
17
17
  fileName: 'fileName',
18
18
  moduleId: 'moduleId',
19
+ relativePath: options.treePath.join('/'),
20
+ supportPluginId: 'supportPluginId',
19
21
  }
20
22
  }
21
23
 
@@ -140,16 +140,29 @@ export async function createMarkdownFilesWatcher (ctx: Context) {
140
140
  ignored: ctx.config.storyIgnored,
141
141
  })
142
142
 
143
- async function addFile (relativePath: string) {
143
+ /**
144
+ * Initial scan is complete.
145
+ */
146
+ let watcherIsReady = false
147
+
148
+ function addFile (relativePath: string) {
144
149
  const absolutePath = path.resolve(ctx.root, relativePath)
145
- const dirFiles = await fs.readdir(path.dirname(absolutePath))
150
+ const dirFiles = fs.readdirSync(path.dirname(absolutePath))
146
151
  const truncatedName = path.basename(absolutePath, '.md')
147
152
  const isRelatedToStory = dirFiles.some((file) => !file.endsWith('.md') && file.startsWith(truncatedName))
148
153
 
149
- const { data: frontmatter, content } = matter(await fs.readFile(absolutePath, 'utf8'))
150
- const html = md.render(content, {
151
- file: absolutePath,
152
- })
154
+ const { data: frontmatter, content } = matter(fs.readFileSync(absolutePath, 'utf8'))
155
+
156
+ let html: string | undefined
157
+
158
+ // We don't immediately render markdown during initial scanning in case
159
+ // markdown references other files in links (otherwise they might not
160
+ // be scanned yet and will throw 'not found' errors).
161
+ if (watcherIsReady) {
162
+ html = md.render(content, {
163
+ file: absolutePath,
164
+ })
165
+ }
153
166
 
154
167
  const file: ServerMarkdownFile = {
155
168
  id: paramCase(relativePath.toLowerCase()),
@@ -157,6 +170,7 @@ export async function createMarkdownFilesWatcher (ctx: Context) {
157
170
  absolutePath,
158
171
  isRelatedToStory,
159
172
  frontmatter,
173
+ content,
160
174
  html,
161
175
  }
162
176
  ctx.markdownFiles.push(file)
@@ -219,8 +233,21 @@ export async function createMarkdownFilesWatcher (ctx: Context) {
219
233
  watcher.once('ready', resolve)
220
234
  })
221
235
 
222
- return {
223
- stop,
236
+ try {
237
+ // Render markdown after initial scan is complete.
238
+ for (const mdFile of ctx.markdownFiles) {
239
+ mdFile.html = md.render(mdFile.content, {
240
+ file: mdFile.absolutePath,
241
+ })
242
+ }
243
+ watcherIsReady = true
244
+
245
+ return {
246
+ stop,
247
+ }
248
+ } catch (e) {
249
+ stop()
250
+ throw e
224
251
  }
225
252
  }
226
253
 
@@ -33,8 +33,9 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
33
33
  }
34
34
  }
35
35
 
36
+ // Should be run sequentially to get a fresh vite.config.js each time
37
+ const { server: nodeServer } = await getViteServer(true) // Run before normal vite to prevent breaking HMR in Nuxt
36
38
  const { server, viteConfigFile } = await getViteServer(false)
37
- const { server: nodeServer } = await getViteServer(true) // Should be run after the first one to get a fresh vite.config.js
38
39
  await watchStories(ctx)
39
40
  const { stop: stopMdFileWatcher } = await createMarkdownFilesWatcher(ctx)
40
41
 
package/src/node/vite.ts CHANGED
@@ -80,7 +80,7 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
80
80
  plugins.push({
81
81
  name: 'histoire-vite-plugin',
82
82
 
83
- config () {
83
+ config (_, { command }) {
84
84
  return {
85
85
  resolve: {
86
86
  dedupe: [
@@ -134,7 +134,7 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
134
134
  watch: {
135
135
  ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
136
136
  },
137
- hmr: !isServer,
137
+ hmr: command === 'build' ? false : !isServer,
138
138
  },
139
139
  define: {
140
140
  // We need to force this to be able to use `devtoolsRawSetupState`