histoire 0.17.2 → 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.
- package/dist/node/bin.js +4 -2
- package/dist/node/commands/build.d.ts +4 -1
- package/dist/node/commands/build.js +2 -1
- package/dist/node/commands/dev.d.ts +1 -0
- package/dist/node/commands/dev.js +2 -1
- package/dist/node/config.d.ts +2 -2
- package/dist/node/config.js +12 -6
- package/dist/node/context.d.ts +1 -0
- package/dist/node/context.js +1 -1
- package/dist/node/markdown.js +33 -9
- package/dist/node/plugin.d.ts +2 -2
- package/dist/node/server.js +2 -1
- package/dist/node/util/find-up.js +1 -5
- package/dist/node/vite.js +2 -2
- package/package.json +8 -8
- package/src/node/__tests__/markdown/test1.story.md +7 -0
- package/src/node/__tests__/markdown/test2.story.md +5 -0
- package/src/node/__tests__/markdown.spec.ts +59 -0
- package/src/node/{tree.spec.ts → __tests__/tree.spec.ts} +6 -4
- package/src/node/bin.ts +4 -2
- package/src/node/commands/build.ts +6 -1
- package/src/node/commands/dev.ts +3 -1
- package/src/node/config.ts +11 -6
- package/src/node/context.ts +2 -1
- package/src/node/markdown.ts +35 -8
- package/src/node/server.ts +2 -1
- package/src/node/util/find-up.ts +1 -6
- package/src/node/vite.ts +2 -2
package/dist/node/bin.js
CHANGED
|
@@ -11,6 +11,7 @@ program.version(version);
|
|
|
11
11
|
program.command('dev')
|
|
12
12
|
.describe('open the stories in your browser for development')
|
|
13
13
|
.option('-p, --port <port>', 'Listening port of the server')
|
|
14
|
+
.option('-c, --config <file>', `[string] use specified config file`)
|
|
14
15
|
.option('--open', 'Open in your default browser')
|
|
15
16
|
.action(async (options) => {
|
|
16
17
|
const { devCommand } = await import('./commands/dev.js');
|
|
@@ -18,9 +19,10 @@ program.command('dev')
|
|
|
18
19
|
});
|
|
19
20
|
program.command('build')
|
|
20
21
|
.describe('build the histoire final app you can deploy')
|
|
21
|
-
.
|
|
22
|
+
.option('-c, --config <file>', `[string] use specified config file`)
|
|
23
|
+
.action(async (options) => {
|
|
22
24
|
const { buildCommand } = await import('./commands/build.js');
|
|
23
|
-
return buildCommand();
|
|
25
|
+
return buildCommand(options);
|
|
24
26
|
});
|
|
25
27
|
program.command('preview')
|
|
26
28
|
.describe('preview the built directory')
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { build } from '../build.js';
|
|
2
2
|
import { createContext } from '../context.js';
|
|
3
|
-
export async function buildCommand() {
|
|
3
|
+
export async function buildCommand(options) {
|
|
4
4
|
const ctx = await createContext({
|
|
5
|
+
configFile: options.config,
|
|
5
6
|
mode: 'build',
|
|
6
7
|
});
|
|
7
8
|
await build(ctx);
|
|
@@ -7,6 +7,7 @@ export async function devCommand(options) {
|
|
|
7
7
|
let stop;
|
|
8
8
|
async function start() {
|
|
9
9
|
const ctx = await createContext({
|
|
10
|
+
configFile: options.config,
|
|
10
11
|
mode: 'dev',
|
|
11
12
|
});
|
|
12
13
|
const { server, viteConfigFile, close } = await createServer(ctx, {
|
|
@@ -38,7 +39,7 @@ export async function devCommand(options) {
|
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
stop = await start();
|
|
41
|
-
const configFile = await resolveConfigFile();
|
|
42
|
+
const configFile = await resolveConfigFile(undefined, options.config);
|
|
42
43
|
if (configFile) {
|
|
43
44
|
const watcher = chokidar.watch(configFile, {
|
|
44
45
|
ignoreInitial: true,
|
package/dist/node/config.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { HistoireConfig, ConfigMode } from '@histoire/shared';
|
|
|
2
2
|
import type { Context } from './context.js';
|
|
3
3
|
export declare function getDefaultConfig(): HistoireConfig;
|
|
4
4
|
export declare const configFileNames: string[];
|
|
5
|
-
export declare function resolveConfigFile(cwd?: string): string;
|
|
5
|
+
export declare function resolveConfigFile(cwd?: string, configFile?: string): string;
|
|
6
6
|
export declare function loadConfigFile(configFile: string): Promise<Partial<HistoireConfig>>;
|
|
7
7
|
export declare const mergeBuildConfig: <Source extends {
|
|
8
8
|
[x: string]: any;
|
|
@@ -22,7 +22,7 @@ export declare const mergeConfig: <Source extends {
|
|
|
22
22
|
[x: number]: any;
|
|
23
23
|
[x: symbol]: any;
|
|
24
24
|
} | (number | boolean | any[] | Record<never, any>))[]>(source: Source, ...defaults: Defaults) => import("defu").Defu<Source, Defaults>;
|
|
25
|
-
export declare function resolveConfig(cwd: string, mode: ConfigMode): Promise<HistoireConfig>;
|
|
25
|
+
export declare function resolveConfig(cwd: string, mode: ConfigMode, configFile: string): Promise<HistoireConfig>;
|
|
26
26
|
export declare function processConfig(ctx: Context): Promise<void>;
|
|
27
27
|
export declare function defineConfig(config: Partial<HistoireConfig>): Partial<HistoireConfig>;
|
|
28
28
|
declare module 'vite' {
|
package/dist/node/config.js
CHANGED
|
@@ -139,8 +139,14 @@ export const configFileNames = [
|
|
|
139
139
|
'.histoire.ts',
|
|
140
140
|
'.histoire.js',
|
|
141
141
|
];
|
|
142
|
-
export function resolveConfigFile(cwd = process.cwd()) {
|
|
143
|
-
|
|
142
|
+
export function resolveConfigFile(cwd = process.cwd(), configFile) {
|
|
143
|
+
if (configFile) {
|
|
144
|
+
// explicit config path is always resolved from cwd
|
|
145
|
+
return path.resolve(configFile);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
return findUp(cwd, configFileNames);
|
|
149
|
+
}
|
|
144
150
|
}
|
|
145
151
|
export async function loadConfigFile(configFile) {
|
|
146
152
|
try {
|
|
@@ -219,11 +225,11 @@ export const mergeConfig = createDefu((obj, key, value) => {
|
|
|
219
225
|
return true;
|
|
220
226
|
}
|
|
221
227
|
});
|
|
222
|
-
export async function resolveConfig(cwd = process.cwd(), mode) {
|
|
228
|
+
export async function resolveConfig(cwd = process.cwd(), mode, configFile) {
|
|
223
229
|
let result;
|
|
224
|
-
const
|
|
225
|
-
if (
|
|
226
|
-
result = await loadConfigFile(
|
|
230
|
+
const resolvedConfigFile = resolveConfigFile(cwd, configFile);
|
|
231
|
+
if (resolvedConfigFile) {
|
|
232
|
+
result = await loadConfigFile(resolvedConfigFile);
|
|
227
233
|
}
|
|
228
234
|
const viteConfig = await resolveViteConfig({}, 'serve');
|
|
229
235
|
const viteHistoireConfig = (viteConfig.histoire ?? {});
|
package/dist/node/context.d.ts
CHANGED
package/dist/node/context.js
CHANGED
|
@@ -2,7 +2,7 @@ import { resolveConfig as resolveViteConfig } from 'vite';
|
|
|
2
2
|
import { processConfig, resolveConfig } from './config.js';
|
|
3
3
|
import { mergeHistoireViteConfig } from './vite.js';
|
|
4
4
|
export async function createContext(options) {
|
|
5
|
-
const config = await resolveConfig(process.cwd(), options.mode);
|
|
5
|
+
const config = await resolveConfig(process.cwd(), options.mode, options.configFile);
|
|
6
6
|
const command = options.mode === 'dev' ? 'serve' : 'build';
|
|
7
7
|
const viteConfig = await resolveViteConfig({}, command);
|
|
8
8
|
const supportPlugins = config.plugins.map(p => p.supportPlugin).filter(Boolean);
|
package/dist/node/markdown.js
CHANGED
|
@@ -120,21 +120,32 @@ export async function createMarkdownFilesWatcher(ctx) {
|
|
|
120
120
|
cwd: ctx.root,
|
|
121
121
|
ignored: ctx.config.storyIgnored,
|
|
122
122
|
});
|
|
123
|
-
|
|
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 =
|
|
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(
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
194
|
-
|
|
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
|
}
|
package/dist/node/plugin.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import chokidar from 'chokidar';
|
|
2
|
+
import path from 'pathe';
|
|
3
3
|
import fs from 'fs-extra';
|
|
4
4
|
import type { Plugin, PluginApiBase, PluginApiDev, PluginApiBuild, ChangeViteConfigCallback, BuildEndCallback, PreviewStoryCallback, ModuleLoader, ServerStory, PluginApiDevEvent, HistoireConfig } from '@histoire/shared';
|
|
5
5
|
import type { Context } from './context.js';
|
|
@@ -10,7 +10,7 @@ export declare class BasePluginApi implements PluginApiBase {
|
|
|
10
10
|
colors: import("picocolors/types.js").Colors & {
|
|
11
11
|
createColors: (enabled?: boolean) => import("picocolors/types.js").Colors;
|
|
12
12
|
};
|
|
13
|
-
path:
|
|
13
|
+
path: typeof path;
|
|
14
14
|
fs: typeof fs;
|
|
15
15
|
constructor(ctx: Context, plugin: Plugin, moduleLoader: ModuleLoader);
|
|
16
16
|
get pluginTempDir(): string;
|
package/dist/node/server.js
CHANGED
|
@@ -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({
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import path from 'pathe';
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
export function findUp(cwd = process.cwd(), fileNames) {
|
|
4
|
-
|
|
4
|
+
const { root } = path.parse(cwd);
|
|
5
5
|
let dir = cwd;
|
|
6
|
-
// Fix for windows, waiting for pathe to fix this: https://github.com/unjs/pathe/issues/5
|
|
7
|
-
if (root === '' && dir[1] === ':') {
|
|
8
|
-
root = dir.substring(0, 2);
|
|
9
|
-
}
|
|
10
6
|
while (dir !== root) {
|
|
11
7
|
for (const fileName of fileNames) {
|
|
12
8
|
const searchPath = path.join(dir, fileName);
|
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.
|
|
3
|
+
"version": "0.17.5",
|
|
4
4
|
"description": "A fast story book powered by Vite",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -58,16 +58,16 @@
|
|
|
58
58
|
"markdown-it-emoji": "^2.0.2",
|
|
59
59
|
"micromatch": "^4.0.5",
|
|
60
60
|
"mrmime": "^1.0.1",
|
|
61
|
-
"pathe": "^
|
|
61
|
+
"pathe": "^1.1.1",
|
|
62
62
|
"picocolors": "^1.0.0",
|
|
63
63
|
"sade": "^1.8.1",
|
|
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.
|
|
68
|
-
"@histoire/controls": "^0.17.
|
|
69
|
-
"@histoire/
|
|
70
|
-
"@histoire/
|
|
67
|
+
"@histoire/app": "^0.17.5",
|
|
68
|
+
"@histoire/controls": "^0.17.4",
|
|
69
|
+
"@histoire/shared": "^0.17.5",
|
|
70
|
+
"@histoire/vendors": "^0.17.4"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
73
|
"vite": "^2.9.0 || ^3.0.0 || ^4.0.0"
|
|
@@ -78,9 +78,9 @@
|
|
|
78
78
|
"@types/micromatch": "^4.0.2",
|
|
79
79
|
"@types/node": "^17.0.45",
|
|
80
80
|
"esbuild": "^0.18.20",
|
|
81
|
-
"rollup": "^3.
|
|
81
|
+
"rollup": "^3.29.4",
|
|
82
82
|
"typescript": "^4.9.5",
|
|
83
|
-
"vite": "^4.
|
|
83
|
+
"vite": "^4.5.0",
|
|
84
84
|
"vitest": "^0.34.1"
|
|
85
85
|
},
|
|
86
86
|
"scripts": {
|
|
@@ -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 {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
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):
|
|
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
|
|
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('-c, --config <file>', `[string] use specified config file`)
|
|
19
20
|
.option('--open', 'Open in your default browser')
|
|
20
21
|
.action(async (options) => {
|
|
21
22
|
const { devCommand } = await import('./commands/dev.js')
|
|
@@ -24,9 +25,10 @@ program.command('dev')
|
|
|
24
25
|
|
|
25
26
|
program.command('build')
|
|
26
27
|
.describe('build the histoire final app you can deploy')
|
|
27
|
-
.
|
|
28
|
+
.option('-c, --config <file>', `[string] use specified config file`)
|
|
29
|
+
.action(async (options) => {
|
|
28
30
|
const { buildCommand } = await import('./commands/build.js')
|
|
29
|
-
return buildCommand()
|
|
31
|
+
return buildCommand(options)
|
|
30
32
|
})
|
|
31
33
|
|
|
32
34
|
program.command('preview')
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { build } from '../build.js'
|
|
2
2
|
import { createContext } from '../context.js'
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export interface BuildOptions {
|
|
5
|
+
config?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function buildCommand (options: BuildOptions) {
|
|
5
9
|
const ctx = await createContext({
|
|
10
|
+
configFile: options.config,
|
|
6
11
|
mode: 'build',
|
|
7
12
|
})
|
|
8
13
|
await build(ctx)
|
package/src/node/commands/dev.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { createServer } from '../server.js'
|
|
|
7
7
|
export interface DevOptions {
|
|
8
8
|
port: number
|
|
9
9
|
open?: boolean
|
|
10
|
+
config?: string
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export async function devCommand (options: DevOptions) {
|
|
@@ -14,6 +15,7 @@ export async function devCommand (options: DevOptions) {
|
|
|
14
15
|
|
|
15
16
|
async function start () {
|
|
16
17
|
const ctx = await createContext({
|
|
18
|
+
configFile: options.config,
|
|
17
19
|
mode: 'dev',
|
|
18
20
|
})
|
|
19
21
|
const { server, viteConfigFile, close } = await createServer(ctx, {
|
|
@@ -50,7 +52,7 @@ export async function devCommand (options: DevOptions) {
|
|
|
50
52
|
|
|
51
53
|
stop = await start()
|
|
52
54
|
|
|
53
|
-
const configFile = await resolveConfigFile()
|
|
55
|
+
const configFile = await resolveConfigFile(undefined, options.config)
|
|
54
56
|
if (configFile) {
|
|
55
57
|
const watcher = chokidar.watch(configFile, {
|
|
56
58
|
ignoreInitial: true,
|
package/src/node/config.ts
CHANGED
|
@@ -154,8 +154,13 @@ export const configFileNames = [
|
|
|
154
154
|
'.histoire.js',
|
|
155
155
|
]
|
|
156
156
|
|
|
157
|
-
export function resolveConfigFile (cwd: string = process.cwd()): string {
|
|
158
|
-
|
|
157
|
+
export function resolveConfigFile (cwd: string = process.cwd(), configFile?: string): string {
|
|
158
|
+
if (configFile) {
|
|
159
|
+
// explicit config path is always resolved from cwd
|
|
160
|
+
return path.resolve(configFile)
|
|
161
|
+
} else {
|
|
162
|
+
return findUp(cwd, configFileNames)
|
|
163
|
+
}
|
|
159
164
|
}
|
|
160
165
|
|
|
161
166
|
export async function loadConfigFile (configFile: string): Promise<Partial<HistoireConfig>> {
|
|
@@ -244,11 +249,11 @@ export const mergeConfig = createDefu((obj: any, key, value) => {
|
|
|
244
249
|
}
|
|
245
250
|
})
|
|
246
251
|
|
|
247
|
-
export async function resolveConfig (cwd: string = process.cwd(), mode: ConfigMode): Promise<HistoireConfig> {
|
|
252
|
+
export async function resolveConfig (cwd: string = process.cwd(), mode: ConfigMode, configFile: string): Promise<HistoireConfig> {
|
|
248
253
|
let result: Partial<HistoireConfig>
|
|
249
|
-
const
|
|
250
|
-
if (
|
|
251
|
-
result = await loadConfigFile(
|
|
254
|
+
const resolvedConfigFile = resolveConfigFile(cwd, configFile)
|
|
255
|
+
if (resolvedConfigFile) {
|
|
256
|
+
result = await loadConfigFile(resolvedConfigFile)
|
|
252
257
|
}
|
|
253
258
|
const viteConfig = await resolveViteConfig({}, 'serve')
|
|
254
259
|
const viteHistoireConfig = (viteConfig.histoire ?? {}) as HistoireConfig
|
package/src/node/context.ts
CHANGED
|
@@ -23,10 +23,11 @@ export interface Context {
|
|
|
23
23
|
|
|
24
24
|
export interface CreateContextOptions {
|
|
25
25
|
mode: Context['mode']
|
|
26
|
+
configFile?: string
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
export async function createContext (options: CreateContextOptions): Promise<Context> {
|
|
29
|
-
const config = await resolveConfig(process.cwd(), options.mode)
|
|
30
|
+
const config = await resolveConfig(process.cwd(), options.mode, options.configFile)
|
|
30
31
|
const command = options.mode === 'dev' ? 'serve' : 'build'
|
|
31
32
|
const viteConfig = await resolveViteConfig({}, command)
|
|
32
33
|
|
package/src/node/markdown.ts
CHANGED
|
@@ -140,16 +140,29 @@ export async function createMarkdownFilesWatcher (ctx: Context) {
|
|
|
140
140
|
ignored: ctx.config.storyIgnored,
|
|
141
141
|
})
|
|
142
142
|
|
|
143
|
-
|
|
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 =
|
|
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(
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
223
|
-
|
|
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
|
|
package/src/node/server.ts
CHANGED
|
@@ -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/util/find-up.ts
CHANGED
|
@@ -2,14 +2,9 @@ import path from 'pathe'
|
|
|
2
2
|
import fs from 'node:fs'
|
|
3
3
|
|
|
4
4
|
export function findUp (cwd: string = process.cwd(), fileNames: string[]): string {
|
|
5
|
-
|
|
5
|
+
const { root } = path.parse(cwd)
|
|
6
6
|
let dir = cwd
|
|
7
7
|
|
|
8
|
-
// Fix for windows, waiting for pathe to fix this: https://github.com/unjs/pathe/issues/5
|
|
9
|
-
if (root === '' && dir[1] === ':') {
|
|
10
|
-
root = dir.substring(0, 2)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
8
|
while (dir !== root) {
|
|
14
9
|
for (const fileName of fileNames) {
|
|
15
10
|
const searchPath = path.join(dir, fileName)
|
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`
|