histoire 0.13.1 → 0.14.0
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/client.d.ts +3 -0
- package/dist/node/bin.js +1 -0
- package/dist/node/build-serialize.d.ts +21 -0
- package/dist/node/build-serialize.js +36 -0
- package/dist/node/build.js +2 -0
- package/dist/node/commands/dev.d.ts +1 -0
- package/dist/node/commands/dev.js +4 -1
- package/dist/node/config.js +2 -0
- package/dist/node/context.d.ts +2 -1
- package/dist/node/context.js +7 -0
- package/dist/node/plugin.d.ts +8 -1
- package/dist/node/plugin.js +15 -0
- package/dist/node/server.d.ts +1 -0
- package/dist/node/server.js +17 -1
- package/dist/node/virtual/index.d.ts +3 -0
- package/dist/node/virtual/index.js +4 -0
- package/dist/node/virtual/resolved-commands.d.ts +2 -0
- package/dist/node/virtual/resolved-commands.js +32 -0
- package/dist/node/virtual/resolved-stories.js +1 -0
- package/dist/node/virtual/vite-plugin.js +6 -0
- package/dist/node/vite.js +34 -0
- package/package.json +9 -5
- package/plugin.d.ts +3 -0
- package/plugin.js +11 -0
- package/src/node/bin.ts +1 -0
- package/src/node/build-serialize.ts +62 -0
- package/src/node/build.ts +3 -0
- package/src/node/commands/dev.ts +5 -1
- package/src/node/config.ts +2 -0
- package/src/node/context.ts +10 -0
- package/src/node/plugin.ts +23 -0
- package/src/node/server.ts +21 -1
- package/src/node/virtual/index.ts +4 -0
- package/src/node/virtual/resolved-commands.ts +41 -0
- package/src/node/virtual/resolved-stories.ts +1 -0
- package/src/node/virtual/vite-plugin.ts +7 -0
- package/src/node/vite.ts +34 -0
package/client.d.ts
CHANGED
|
@@ -14,3 +14,6 @@ export function logEvent (name: string, argument): void
|
|
|
14
14
|
* Returns `true` when in the NodeJS server while collecting stories.
|
|
15
15
|
*/
|
|
16
16
|
export function isCollecting (): boolean
|
|
17
|
+
|
|
18
|
+
export function toggleDark (value?: boolean): boolean
|
|
19
|
+
export function isDark (): boolean
|
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('--open', 'Open in your default browser')
|
|
14
15
|
.action(async (options) => {
|
|
15
16
|
const { devCommand } = await import('./commands/dev.js');
|
|
16
17
|
return devCommand(options);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context } from './context.js';
|
|
2
|
+
import type { ServerStory } from '@histoire/shared';
|
|
3
|
+
interface SerializedStory extends Omit<ServerStory, 'docsText'> {
|
|
4
|
+
relativePath: string;
|
|
5
|
+
supportPluginId: string;
|
|
6
|
+
treePath?: string[];
|
|
7
|
+
virtual?: boolean;
|
|
8
|
+
markdownFile?: SerializedMarkdownFile;
|
|
9
|
+
}
|
|
10
|
+
interface SerializedMarkdownFile {
|
|
11
|
+
id: string;
|
|
12
|
+
relativePath: string;
|
|
13
|
+
isRelatedToStory: boolean;
|
|
14
|
+
frontmatter?: any;
|
|
15
|
+
}
|
|
16
|
+
interface SerializedStoryData {
|
|
17
|
+
stories: SerializedStory[];
|
|
18
|
+
markdownFiles: SerializedMarkdownFile[];
|
|
19
|
+
}
|
|
20
|
+
export declare function getSerializedStoryData(ctx: Context): SerializedStoryData;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function getSerializedStoryData(ctx) {
|
|
2
|
+
const data = {
|
|
3
|
+
stories: [],
|
|
4
|
+
markdownFiles: [],
|
|
5
|
+
};
|
|
6
|
+
for (const storyFile of ctx.storyFiles) {
|
|
7
|
+
if (storyFile.story) {
|
|
8
|
+
data.stories.push({
|
|
9
|
+
...Object.assign({}, storyFile.story, {
|
|
10
|
+
docsText: undefined,
|
|
11
|
+
}),
|
|
12
|
+
relativePath: storyFile.relativePath,
|
|
13
|
+
supportPluginId: storyFile.supportPluginId,
|
|
14
|
+
treePath: storyFile.treePath,
|
|
15
|
+
virtual: storyFile.virtual,
|
|
16
|
+
markdownFile: storyFile.markdownFile
|
|
17
|
+
? {
|
|
18
|
+
id: storyFile.markdownFile.id,
|
|
19
|
+
relativePath: storyFile.markdownFile.relativePath,
|
|
20
|
+
isRelatedToStory: storyFile.markdownFile.isRelatedToStory,
|
|
21
|
+
frontmatter: storyFile.markdownFile.frontmatter,
|
|
22
|
+
}
|
|
23
|
+
: null,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
for (const markdownFile of ctx.markdownFiles) {
|
|
28
|
+
data.markdownFiles.push({
|
|
29
|
+
id: markdownFile.id,
|
|
30
|
+
relativePath: markdownFile.relativePath,
|
|
31
|
+
isRelatedToStory: markdownFile.isRelatedToStory,
|
|
32
|
+
frontmatter: markdownFile.frontmatter,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return data;
|
|
36
|
+
}
|
package/dist/node/build.js
CHANGED
|
@@ -12,6 +12,7 @@ import { BuildPluginApi } from './plugin.js';
|
|
|
12
12
|
import { useModuleLoader } from './load.js';
|
|
13
13
|
import { startPreview } from './preview.js';
|
|
14
14
|
import { createMarkdownFilesWatcher } from './markdown.js';
|
|
15
|
+
import { getSerializedStoryData } from './build-serialize.js';
|
|
15
16
|
const PRELOAD_MODULES = [
|
|
16
17
|
'vendor',
|
|
17
18
|
];
|
|
@@ -149,6 +150,7 @@ export async function build(ctx) {
|
|
|
149
150
|
const sandboxOutput = result.output.find(o => o.name === 'bundle-sandbox' && o.type === 'chunk');
|
|
150
151
|
const sandboxHtml = generateEntryHtml(sandboxOutput.fileName, styleOutput.fileName, {}, ctx);
|
|
151
152
|
await writeFile('__sandbox.html', sandboxHtml, ctx);
|
|
153
|
+
await writeFile('histoire.json', JSON.stringify(getSerializedStoryData(ctx), null, 2), ctx);
|
|
152
154
|
const duration = performance.now() - startTime;
|
|
153
155
|
if (emptyStoryCount) {
|
|
154
156
|
console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`));
|
|
@@ -9,7 +9,10 @@ export async function devCommand(options) {
|
|
|
9
9
|
const ctx = await createContext({
|
|
10
10
|
mode: 'dev',
|
|
11
11
|
});
|
|
12
|
-
const { server, viteConfigFile, close } = await createServer(ctx, {
|
|
12
|
+
const { server, viteConfigFile, close } = await createServer(ctx, {
|
|
13
|
+
port: options.port,
|
|
14
|
+
open: options.open,
|
|
15
|
+
});
|
|
13
16
|
server.printUrls();
|
|
14
17
|
// Histoire config watcher
|
|
15
18
|
let watcher;
|
package/dist/node/config.js
CHANGED
package/dist/node/context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ResolvedConfig } from 'vite';
|
|
2
|
-
import type { ServerStoryFile, FinalSupportPlugin, ServerMarkdownFile, HistoireConfig, ConfigMode } from '@histoire/shared';
|
|
2
|
+
import type { ServerStoryFile, FinalSupportPlugin, ServerMarkdownFile, HistoireConfig, ConfigMode, PluginCommand } from '@histoire/shared';
|
|
3
3
|
export interface Context {
|
|
4
4
|
root: string;
|
|
5
5
|
config: HistoireConfig;
|
|
@@ -8,6 +8,7 @@ export interface Context {
|
|
|
8
8
|
storyFiles: ServerStoryFile[];
|
|
9
9
|
supportPlugins: FinalSupportPlugin[];
|
|
10
10
|
markdownFiles: ServerMarkdownFile[];
|
|
11
|
+
registeredCommands?: PluginCommand[];
|
|
11
12
|
}
|
|
12
13
|
export interface CreateContextOptions {
|
|
13
14
|
mode: Context['mode'];
|
package/dist/node/context.js
CHANGED
|
@@ -14,8 +14,15 @@ export async function createContext(options) {
|
|
|
14
14
|
storyFiles: [],
|
|
15
15
|
supportPlugins,
|
|
16
16
|
markdownFiles: [],
|
|
17
|
+
registeredCommands: [],
|
|
17
18
|
};
|
|
18
19
|
ctx.resolvedViteConfig = await mergeHistoireViteConfig(viteConfig, ctx);
|
|
19
20
|
await processConfig(ctx);
|
|
21
|
+
// List commands
|
|
22
|
+
for (const plugin of ctx.config.plugins) {
|
|
23
|
+
if (plugin.commands?.length) {
|
|
24
|
+
ctx.registeredCommands.push(...plugin.commands);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
20
27
|
return ctx;
|
|
21
28
|
}
|
package/dist/node/plugin.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import chokidar from 'chokidar';
|
|
3
3
|
import fs from 'fs-extra';
|
|
4
|
-
import type { Plugin, PluginApiBase, PluginApiDev, PluginApiBuild, BuildEndCallback, PreviewStoryCallback, ModuleLoader } from '@histoire/shared';
|
|
4
|
+
import type { Plugin, PluginApiBase, PluginApiDev, PluginApiBuild, BuildEndCallback, PreviewStoryCallback, ModuleLoader, ServerStory, PluginApiDevEvent, HistoireConfig } from '@histoire/shared';
|
|
5
5
|
import type { Context } from './context.js';
|
|
6
6
|
export declare class BasePluginApi implements PluginApiBase {
|
|
7
7
|
protected ctx: Context;
|
|
@@ -18,6 +18,8 @@ export declare class BasePluginApi implements PluginApiBase {
|
|
|
18
18
|
warn(...msg: any[]): void;
|
|
19
19
|
error(...msg: any[]): void;
|
|
20
20
|
addStoryFile(file: string): void;
|
|
21
|
+
getStories(): ServerStory[];
|
|
22
|
+
getConfig(): HistoireConfig;
|
|
21
23
|
}
|
|
22
24
|
export declare class DevPluginApi extends BasePluginApi implements PluginApiDev {
|
|
23
25
|
watcher: typeof chokidar;
|
|
@@ -28,3 +30,8 @@ export declare class BuildPluginApi extends BasePluginApi implements PluginApiBu
|
|
|
28
30
|
onBuildEnd(cb: BuildEndCallback): void;
|
|
29
31
|
onPreviewStory(cb: PreviewStoryCallback): void;
|
|
30
32
|
}
|
|
33
|
+
export declare class DevEventPluginApi extends BasePluginApi implements PluginApiDevEvent {
|
|
34
|
+
event: string;
|
|
35
|
+
payload: any;
|
|
36
|
+
constructor(ctx: Context, plugin: Plugin, moduleLoader: ModuleLoader, event: string, payload: any);
|
|
37
|
+
}
|
package/dist/node/plugin.js
CHANGED
|
@@ -33,6 +33,12 @@ export class BasePluginApi {
|
|
|
33
33
|
removeStory(file);
|
|
34
34
|
addStory(file);
|
|
35
35
|
}
|
|
36
|
+
getStories() {
|
|
37
|
+
return this.ctx.storyFiles.map(f => f.story).filter(Boolean);
|
|
38
|
+
}
|
|
39
|
+
getConfig() {
|
|
40
|
+
return this.ctx.config;
|
|
41
|
+
}
|
|
36
42
|
}
|
|
37
43
|
export class DevPluginApi extends BasePluginApi {
|
|
38
44
|
watcher = chokidar;
|
|
@@ -47,3 +53,12 @@ export class BuildPluginApi extends BasePluginApi {
|
|
|
47
53
|
this.previewStoryCallbacks.push(cb);
|
|
48
54
|
}
|
|
49
55
|
}
|
|
56
|
+
export class DevEventPluginApi extends BasePluginApi {
|
|
57
|
+
event;
|
|
58
|
+
payload;
|
|
59
|
+
constructor(ctx, plugin, moduleLoader, event, payload) {
|
|
60
|
+
super(ctx, plugin, moduleLoader);
|
|
61
|
+
this.event = event;
|
|
62
|
+
this.payload = payload;
|
|
63
|
+
}
|
|
64
|
+
}
|
package/dist/node/server.d.ts
CHANGED
package/dist/node/server.js
CHANGED
|
@@ -5,13 +5,16 @@ import { getViteConfigWithPlugins } from './vite.js';
|
|
|
5
5
|
import * as VirtualFiles from './virtual/index.js';
|
|
6
6
|
import { onStoryChange, onStoryListChange, watchStories } from './stories.js';
|
|
7
7
|
import { useCollectStories } from './collect/index.js';
|
|
8
|
-
import { DevPluginApi } from './plugin.js';
|
|
8
|
+
import { DevPluginApi, DevEventPluginApi } from './plugin.js';
|
|
9
9
|
import { useModuleLoader } from './load.js';
|
|
10
10
|
import { wrapLogError } from './util/log.js';
|
|
11
11
|
import { createMarkdownFilesWatcher, onMarkdownListChange } from './markdown.js';
|
|
12
12
|
export async function createServer(ctx, options = {}) {
|
|
13
13
|
const getViteServer = async (collecting) => {
|
|
14
14
|
const { viteConfig, viteConfigFile } = await getViteConfigWithPlugins(collecting, ctx);
|
|
15
|
+
if (!collecting && options.open) {
|
|
16
|
+
viteConfig.server.open = true;
|
|
17
|
+
}
|
|
15
18
|
const server = await createViteServer(viteConfig);
|
|
16
19
|
await server.pluginContainer.buildStart({});
|
|
17
20
|
return {
|
|
@@ -38,6 +41,19 @@ export async function createServer(ctx, options = {}) {
|
|
|
38
41
|
await plugin.onDev(api, onCleanup);
|
|
39
42
|
}
|
|
40
43
|
}
|
|
44
|
+
// Custom dev events
|
|
45
|
+
server.ws.on(`histoire:dev-event`, async ({ event, payload }) => {
|
|
46
|
+
for (const plugin of ctx.config.plugins) {
|
|
47
|
+
if (plugin.onDevEvent) {
|
|
48
|
+
const api = new DevEventPluginApi(ctx, plugin, moduleLoader, event, payload);
|
|
49
|
+
const result = await plugin.onDevEvent(api);
|
|
50
|
+
if (!event.startsWith('on') && result !== undefined) {
|
|
51
|
+
server.ws.send(`histoire:dev-event-result`, { event, result });
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
41
57
|
// Wait for pre-bundling (in `listen()`)
|
|
42
58
|
await server.listen(options.port ?? server.config.server?.port);
|
|
43
59
|
const { clearCache, executeStoryFile, destroy: destroyCollectStories, clearInvalidates, } = useCollectStories({
|
|
@@ -20,8 +20,11 @@ export declare const SUPPORT_PLUGINS_COLLECT = "virtual:$histoire-support-plugin
|
|
|
20
20
|
export declare const RESOLVED_SUPPORT_PLUGINS_COLLECT: string;
|
|
21
21
|
export declare const MARKDOWN_FILES = "virtual:$histoire-markdown-files";
|
|
22
22
|
export declare const RESOLVED_MARKDOWN_FILES: string;
|
|
23
|
+
export declare const COMMANDS = "virtual:$histoire-commands";
|
|
24
|
+
export declare const RESOLVED_COMMANDS: string;
|
|
23
25
|
export * from './markdown.js';
|
|
24
26
|
export * from './noop.js';
|
|
27
|
+
export * from './resolved-commands.js';
|
|
25
28
|
export * from './resolved-config.js';
|
|
26
29
|
export * from './resolved-generated-global-setup.js';
|
|
27
30
|
export * from './resolved-generated-setup-code.js';
|
|
@@ -32,8 +32,12 @@ export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_
|
|
|
32
32
|
export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files';
|
|
33
33
|
export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`;
|
|
34
34
|
// export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
|
|
35
|
+
// @TODO virtual module id
|
|
36
|
+
export const COMMANDS = 'virtual:$histoire-commands';
|
|
37
|
+
export const RESOLVED_COMMANDS = `/__resolved__${COMMANDS}`;
|
|
35
38
|
export * from './markdown.js';
|
|
36
39
|
export * from './noop.js';
|
|
40
|
+
export * from './resolved-commands.js';
|
|
37
41
|
export * from './resolved-config.js';
|
|
38
42
|
export * from './resolved-generated-global-setup.js';
|
|
39
43
|
export * from './resolved-generated-setup-code.js';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const serializedFields = ['id', 'label', 'prompts', 'icon', 'searchText'];
|
|
2
|
+
export const resolvedCommands = (ctx) => {
|
|
3
|
+
const imports = [];
|
|
4
|
+
const commands = [];
|
|
5
|
+
for (const command of ctx.registeredCommands) {
|
|
6
|
+
const fields = getCommandFields(command, imports);
|
|
7
|
+
commands.push(`{ ${fields.join(', ')} }`);
|
|
8
|
+
}
|
|
9
|
+
return `${imports.join('\n')}
|
|
10
|
+
export const registeredCommands = [${commands.join(', ')}]
|
|
11
|
+
`;
|
|
12
|
+
};
|
|
13
|
+
function getCommandFields(command, imports) {
|
|
14
|
+
const fields = [];
|
|
15
|
+
for (const field of serializedFields) {
|
|
16
|
+
if (command[field] != null) {
|
|
17
|
+
fields.push(`${field}: ${JSON.stringify(command[field])}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Add client-side data
|
|
21
|
+
if (command.clientSetupFile) {
|
|
22
|
+
const importedVar = `__setup${imports.length}__`;
|
|
23
|
+
if (typeof command.clientSetupFile === 'string') {
|
|
24
|
+
imports.push(`import ${importedVar} from '${command.clientSetupFile}'`);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
imports.push(`import { ${command.clientSetupFile.importName} as ${importedVar} } from '${command.clientSetupFile.file}'`);
|
|
28
|
+
}
|
|
29
|
+
fields.push(`...${importedVar}`);
|
|
30
|
+
}
|
|
31
|
+
return fields;
|
|
32
|
+
}
|
|
@@ -57,6 +57,9 @@ export const createVirtualFilesPlugin = (ctx, isServer) => ({
|
|
|
57
57
|
if (id.startsWith(VirtualFiles.MARKDOWN_FILES)) {
|
|
58
58
|
return VirtualFiles.RESOLVED_MARKDOWN_FILES;
|
|
59
59
|
}
|
|
60
|
+
if (id.startsWith(VirtualFiles.COMMANDS)) {
|
|
61
|
+
return VirtualFiles.RESOLVED_COMMANDS;
|
|
62
|
+
}
|
|
60
63
|
if (id.startsWith('virtual:story:')) {
|
|
61
64
|
return `\0${id}`;
|
|
62
65
|
}
|
|
@@ -105,6 +108,9 @@ export const createVirtualFilesPlugin = (ctx, isServer) => ({
|
|
|
105
108
|
if (id === VirtualFiles.RESOLVED_MARKDOWN_FILES) {
|
|
106
109
|
return VirtualFiles.resolvedMarkdownFiles(ctx);
|
|
107
110
|
}
|
|
111
|
+
if (id === VirtualFiles.RESOLVED_COMMANDS) {
|
|
112
|
+
return VirtualFiles.resolvedCommands(ctx);
|
|
113
|
+
}
|
|
108
114
|
if (id.startsWith('\0virtual:story:')) {
|
|
109
115
|
return VirtualFiles.story(ctx, id);
|
|
110
116
|
}
|
package/dist/node/vite.js
CHANGED
|
@@ -221,6 +221,40 @@ export async function getViteConfigWithPlugins(isServer, ctx) {
|
|
|
221
221
|
},
|
|
222
222
|
});
|
|
223
223
|
plugins.push(createVirtualFilesPlugin(ctx, isServer));
|
|
224
|
+
// Replace dev flag
|
|
225
|
+
const flags = {
|
|
226
|
+
'_ctx.__HISTOIRE_DEV__': JSON.stringify(ctx.mode === 'dev'),
|
|
227
|
+
__HISTOIRE_DEV__: JSON.stringify(ctx.mode === 'dev'),
|
|
228
|
+
};
|
|
229
|
+
plugins.push({
|
|
230
|
+
name: 'histoire:flags',
|
|
231
|
+
enforce: 'pre',
|
|
232
|
+
transform(code, id) {
|
|
233
|
+
if (id.match(/\.(vue|js)($|\?)/)) {
|
|
234
|
+
const original = code;
|
|
235
|
+
for (const flag in flags) {
|
|
236
|
+
code = code.replace(new RegExp(flag, 'g'), flags[flag]);
|
|
237
|
+
}
|
|
238
|
+
if (original !== code) {
|
|
239
|
+
return code;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
if (ctx.mode === 'dev') {
|
|
245
|
+
// Dev commands
|
|
246
|
+
plugins.push({
|
|
247
|
+
name: 'histoire:dev-commands',
|
|
248
|
+
configureServer(server) {
|
|
249
|
+
server.ws.on('histoire:dev-command', ({ id, params }) => {
|
|
250
|
+
const command = ctx.registeredCommands.find(c => c.id === id);
|
|
251
|
+
if (command?.serverAction) {
|
|
252
|
+
command.serverAction(params);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
}
|
|
224
258
|
// Markdown
|
|
225
259
|
plugins.push(...await createMarkdownPlugins(ctx));
|
|
226
260
|
if (ctx.mode === 'build') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "histoire",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "A fast story book powered by Vite",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
"types": "./client.d.ts",
|
|
25
25
|
"default": "./client.js"
|
|
26
26
|
},
|
|
27
|
+
"./plugin": {
|
|
28
|
+
"types": "./plugin.d.ts",
|
|
29
|
+
"default": "./plugin.js"
|
|
30
|
+
},
|
|
27
31
|
"./*": "./*"
|
|
28
32
|
},
|
|
29
33
|
"main": "./dist/node/index.js",
|
|
@@ -33,10 +37,10 @@
|
|
|
33
37
|
"histoire": "./bin.mjs"
|
|
34
38
|
},
|
|
35
39
|
"dependencies": {
|
|
36
|
-
"@histoire/app": "^0.
|
|
37
|
-
"@histoire/controls": "^0.
|
|
38
|
-
"@histoire/shared": "^0.
|
|
39
|
-
"@histoire/vendors": "^0.
|
|
40
|
+
"@histoire/app": "^0.14.0",
|
|
41
|
+
"@histoire/controls": "^0.14.0",
|
|
42
|
+
"@histoire/shared": "^0.14.0",
|
|
43
|
+
"@histoire/vendors": "^0.14.0",
|
|
40
44
|
"@types/flexsearch": "^0.7.3",
|
|
41
45
|
"@types/markdown-it": "^12.2.3",
|
|
42
46
|
"birpc": "^0.1.1",
|
package/plugin.d.ts
ADDED
package/plugin.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function sendEvent (event, payload) {
|
|
2
|
+
if (window.__HST_PLUGIN_API__) {
|
|
3
|
+
return window.__HST_PLUGIN_API__.sendEvent(event, payload)
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function openStory (storyId) {
|
|
8
|
+
if (window.__HST_PLUGIN_API__) {
|
|
9
|
+
return window.__HST_PLUGIN_API__.openStory(storyId)
|
|
10
|
+
}
|
|
11
|
+
}
|
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',
|
|
@@ -181,6 +182,8 @@ export async function build (ctx: Context) {
|
|
|
181
182
|
const sandboxHtml = generateEntryHtml(sandboxOutput.fileName, styleOutput.fileName, {}, ctx)
|
|
182
183
|
await writeFile('__sandbox.html', sandboxHtml, ctx)
|
|
183
184
|
|
|
185
|
+
await writeFile('histoire.json', JSON.stringify(getSerializedStoryData(ctx), null, 2), ctx)
|
|
186
|
+
|
|
184
187
|
const duration = performance.now() - startTime
|
|
185
188
|
if (emptyStoryCount) {
|
|
186
189
|
console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`))
|
package/src/node/commands/dev.ts
CHANGED
|
@@ -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, {
|
|
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
|
package/src/node/config.ts
CHANGED
package/src/node/context.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
ServerMarkdownFile,
|
|
6
6
|
HistoireConfig,
|
|
7
7
|
ConfigMode,
|
|
8
|
+
PluginCommand,
|
|
8
9
|
} from '@histoire/shared'
|
|
9
10
|
import { processConfig, resolveConfig } from './config.js'
|
|
10
11
|
import { mergeHistoireViteConfig } from './vite.js'
|
|
@@ -17,6 +18,7 @@ export interface Context {
|
|
|
17
18
|
storyFiles: ServerStoryFile[]
|
|
18
19
|
supportPlugins: FinalSupportPlugin[]
|
|
19
20
|
markdownFiles: ServerMarkdownFile[]
|
|
21
|
+
registeredCommands?: PluginCommand[]
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export interface CreateContextOptions {
|
|
@@ -38,11 +40,19 @@ export async function createContext (options: CreateContextOptions): Promise<Con
|
|
|
38
40
|
storyFiles: [],
|
|
39
41
|
supportPlugins,
|
|
40
42
|
markdownFiles: [],
|
|
43
|
+
registeredCommands: [],
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
ctx.resolvedViteConfig = await mergeHistoireViteConfig(viteConfig as unknown, ctx)
|
|
44
47
|
|
|
45
48
|
await processConfig(ctx)
|
|
46
49
|
|
|
50
|
+
// List commands
|
|
51
|
+
for (const plugin of ctx.config.plugins) {
|
|
52
|
+
if (plugin.commands?.length) {
|
|
53
|
+
ctx.registeredCommands.push(...plugin.commands)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
47
57
|
return ctx
|
|
48
58
|
}
|
package/src/node/plugin.ts
CHANGED
|
@@ -10,6 +10,9 @@ import type {
|
|
|
10
10
|
BuildEndCallback,
|
|
11
11
|
PreviewStoryCallback,
|
|
12
12
|
ModuleLoader,
|
|
13
|
+
ServerStory,
|
|
14
|
+
PluginApiDevEvent,
|
|
15
|
+
HistoireConfig,
|
|
13
16
|
} from '@histoire/shared'
|
|
14
17
|
import { TEMP_PATH } from './alias.js'
|
|
15
18
|
import type { Context } from './context.js'
|
|
@@ -47,6 +50,14 @@ export class BasePluginApi implements PluginApiBase {
|
|
|
47
50
|
removeStory(file)
|
|
48
51
|
addStory(file)
|
|
49
52
|
}
|
|
53
|
+
|
|
54
|
+
getStories (): ServerStory[] {
|
|
55
|
+
return this.ctx.storyFiles.map(f => f.story).filter(Boolean)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getConfig (): HistoireConfig {
|
|
59
|
+
return this.ctx.config
|
|
60
|
+
}
|
|
50
61
|
}
|
|
51
62
|
|
|
52
63
|
export class DevPluginApi extends BasePluginApi implements PluginApiDev {
|
|
@@ -65,3 +76,15 @@ export class BuildPluginApi extends BasePluginApi implements PluginApiBuild {
|
|
|
65
76
|
this.previewStoryCallbacks.push(cb)
|
|
66
77
|
}
|
|
67
78
|
}
|
|
79
|
+
|
|
80
|
+
export class DevEventPluginApi extends BasePluginApi implements PluginApiDevEvent {
|
|
81
|
+
constructor (
|
|
82
|
+
ctx: Context,
|
|
83
|
+
plugin: Plugin,
|
|
84
|
+
moduleLoader: ModuleLoader,
|
|
85
|
+
public event: string,
|
|
86
|
+
public payload: any,
|
|
87
|
+
) {
|
|
88
|
+
super(ctx, plugin, moduleLoader)
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/node/server.ts
CHANGED
|
@@ -7,18 +7,24 @@ import { getViteConfigWithPlugins } from './vite.js'
|
|
|
7
7
|
import * as VirtualFiles from './virtual/index.js'
|
|
8
8
|
import { onStoryChange, onStoryListChange, watchStories } from './stories.js'
|
|
9
9
|
import { useCollectStories } from './collect/index.js'
|
|
10
|
-
import { DevPluginApi } from './plugin.js'
|
|
10
|
+
import { DevPluginApi, DevEventPluginApi } from './plugin.js'
|
|
11
11
|
import { useModuleLoader } from './load.js'
|
|
12
12
|
import { wrapLogError } from './util/log.js'
|
|
13
13
|
import { createMarkdownFilesWatcher, onMarkdownListChange } from './markdown.js'
|
|
14
14
|
|
|
15
15
|
export interface CreateServerOptions {
|
|
16
16
|
port?: number
|
|
17
|
+
open?: boolean
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export async function createServer (ctx: Context, options: CreateServerOptions = {}) {
|
|
20
21
|
const getViteServer = async (collecting: boolean) => {
|
|
21
22
|
const { viteConfig, viteConfigFile } = await getViteConfigWithPlugins(collecting, ctx)
|
|
23
|
+
|
|
24
|
+
if (!collecting && options.open) {
|
|
25
|
+
viteConfig.server.open = true
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
const server = await createViteServer(viteConfig)
|
|
23
29
|
await server.pluginContainer.buildStart({})
|
|
24
30
|
return {
|
|
@@ -54,6 +60,20 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
|
|
|
54
60
|
}
|
|
55
61
|
}
|
|
56
62
|
|
|
63
|
+
// Custom dev events
|
|
64
|
+
server.ws.on(`histoire:dev-event`, async ({ event, payload }) => {
|
|
65
|
+
for (const plugin of ctx.config.plugins) {
|
|
66
|
+
if (plugin.onDevEvent) {
|
|
67
|
+
const api = new DevEventPluginApi(ctx, plugin, moduleLoader, event, payload)
|
|
68
|
+
const result = await plugin.onDevEvent(api)
|
|
69
|
+
if (!event.startsWith('on') && result !== undefined) {
|
|
70
|
+
server.ws.send(`histoire:dev-event-result`, { event, result })
|
|
71
|
+
break
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
57
77
|
// Wait for pre-bundling (in `listen()`)
|
|
58
78
|
await server.listen(options.port ?? server.config.server?.port)
|
|
59
79
|
|
|
@@ -32,9 +32,13 @@ export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_
|
|
|
32
32
|
export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files'
|
|
33
33
|
export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`
|
|
34
34
|
// export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
|
|
35
|
+
// @TODO virtual module id
|
|
36
|
+
export const COMMANDS = 'virtual:$histoire-commands'
|
|
37
|
+
export const RESOLVED_COMMANDS = `/__resolved__${COMMANDS}`
|
|
35
38
|
|
|
36
39
|
export * from './markdown.js'
|
|
37
40
|
export * from './noop.js'
|
|
41
|
+
export * from './resolved-commands.js'
|
|
38
42
|
export * from './resolved-config.js'
|
|
39
43
|
export * from './resolved-generated-global-setup.js'
|
|
40
44
|
export * from './resolved-generated-setup-code.js'
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { PluginCommand } from '@histoire/shared'
|
|
2
|
+
import type { Context } from '../context.js'
|
|
3
|
+
|
|
4
|
+
const serializedFields: Readonly<(keyof PluginCommand)[]> = ['id', 'label', 'prompts', 'icon', 'searchText'] as const
|
|
5
|
+
|
|
6
|
+
export const resolvedCommands = (ctx: Context) => {
|
|
7
|
+
const imports: string[] = []
|
|
8
|
+
const commands: string[] = []
|
|
9
|
+
|
|
10
|
+
for (const command of ctx.registeredCommands) {
|
|
11
|
+
const fields = getCommandFields(command, imports)
|
|
12
|
+
commands.push(`{ ${fields.join(', ')} }`)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return `${imports.join('\n')}
|
|
16
|
+
export const registeredCommands = [${commands.join(', ')}]
|
|
17
|
+
`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getCommandFields (command: PluginCommand, imports: string[]) {
|
|
21
|
+
const fields: string[] = []
|
|
22
|
+
|
|
23
|
+
for (const field of serializedFields) {
|
|
24
|
+
if (command[field] != null) {
|
|
25
|
+
fields.push(`${field}: ${JSON.stringify(command[field])}`)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Add client-side data
|
|
30
|
+
if (command.clientSetupFile) {
|
|
31
|
+
const importedVar = `__setup${imports.length}__`
|
|
32
|
+
if (typeof command.clientSetupFile === 'string') {
|
|
33
|
+
imports.push(`import ${importedVar} from '${command.clientSetupFile}'`)
|
|
34
|
+
} else {
|
|
35
|
+
imports.push(`import { ${command.clientSetupFile.importName} as ${importedVar} } from '${command.clientSetupFile.file}'`)
|
|
36
|
+
}
|
|
37
|
+
fields.push(`...${importedVar}`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return fields
|
|
41
|
+
}
|
|
@@ -59,6 +59,9 @@ export const createVirtualFilesPlugin = (ctx: Context, isServer: boolean) => ({
|
|
|
59
59
|
if (id.startsWith(VirtualFiles.MARKDOWN_FILES)) {
|
|
60
60
|
return VirtualFiles.RESOLVED_MARKDOWN_FILES
|
|
61
61
|
}
|
|
62
|
+
if (id.startsWith(VirtualFiles.COMMANDS)) {
|
|
63
|
+
return VirtualFiles.RESOLVED_COMMANDS
|
|
64
|
+
}
|
|
62
65
|
if (id.startsWith('virtual:story:')) {
|
|
63
66
|
return `\0${id}`
|
|
64
67
|
}
|
|
@@ -120,6 +123,10 @@ export const createVirtualFilesPlugin = (ctx: Context, isServer: boolean) => ({
|
|
|
120
123
|
return VirtualFiles.resolvedMarkdownFiles(ctx)
|
|
121
124
|
}
|
|
122
125
|
|
|
126
|
+
if (id === VirtualFiles.RESOLVED_COMMANDS) {
|
|
127
|
+
return VirtualFiles.resolvedCommands(ctx)
|
|
128
|
+
}
|
|
129
|
+
|
|
123
130
|
if (id.startsWith('\0virtual:story:')) {
|
|
124
131
|
return VirtualFiles.story(ctx, id)
|
|
125
132
|
}
|
package/src/node/vite.ts
CHANGED
|
@@ -251,6 +251,40 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
|
|
|
251
251
|
|
|
252
252
|
plugins.push(createVirtualFilesPlugin(ctx, isServer))
|
|
253
253
|
|
|
254
|
+
// Replace dev flag
|
|
255
|
+
const flags = {
|
|
256
|
+
'_ctx.__HISTOIRE_DEV__': JSON.stringify(ctx.mode === 'dev'),
|
|
257
|
+
__HISTOIRE_DEV__: JSON.stringify(ctx.mode === 'dev'),
|
|
258
|
+
}
|
|
259
|
+
plugins.push({
|
|
260
|
+
name: 'histoire:flags',
|
|
261
|
+
enforce: 'pre',
|
|
262
|
+
transform (code, id) {
|
|
263
|
+
if (id.match(/\.(vue|js)($|\?)/)) {
|
|
264
|
+
const original = code
|
|
265
|
+
for (const flag in flags) {
|
|
266
|
+
code = code.replace(new RegExp(flag, 'g'), flags[flag])
|
|
267
|
+
}
|
|
268
|
+
if (original !== code) { return code }
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
if (ctx.mode === 'dev') {
|
|
274
|
+
// Dev commands
|
|
275
|
+
plugins.push({
|
|
276
|
+
name: 'histoire:dev-commands',
|
|
277
|
+
configureServer (server) {
|
|
278
|
+
server.ws.on('histoire:dev-command', ({ id, params }) => {
|
|
279
|
+
const command = ctx.registeredCommands.find(c => c.id === id)
|
|
280
|
+
if (command?.serverAction) {
|
|
281
|
+
command.serverAction(params)
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
},
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
|
|
254
288
|
// Markdown
|
|
255
289
|
plugins.push(...await createMarkdownPlugins(ctx))
|
|
256
290
|
|