histoire 0.13.2 → 0.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,7 @@ export function useCollectStories(options, ctx) {
13
13
  deps: {
14
14
  inline: [
15
15
  /histoire\/dist/,
16
+ /histoire\/client/,
16
17
  /@histoire\/[\w\d-]+\/dist/,
17
18
  /histoire-[\w\d-]+\/dist/,
18
19
  /@vue\/devtools-api/,
@@ -38,6 +38,7 @@ export function getDefaultConfig() {
38
38
  },
39
39
  defaultColorScheme: 'auto',
40
40
  storeColorScheme: true,
41
+ darkClass: 'dark',
41
42
  },
42
43
  responsivePresets: [
43
44
  {
@@ -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'];
@@ -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
  }
@@ -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
+ }
@@ -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
+ }
@@ -5,7 +5,7 @@ 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';
@@ -41,6 +41,19 @@ export async function createServer(ctx, options = {}) {
41
41
  await plugin.onDev(api, onCleanup);
42
42
  }
43
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
+ });
44
57
  // Wait for pre-bundling (in `listen()`)
45
58
  await server.listen(options.port ?? server.config.server?.port);
46
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,11 @@ 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
+ export const COMMANDS = 'virtual:$histoire-commands';
36
+ export const RESOLVED_COMMANDS = `/__resolved__${COMMANDS}`;
35
37
  export * from './markdown.js';
36
38
  export * from './noop.js';
39
+ export * from './resolved-commands.js';
37
40
  export * from './resolved-config.js';
38
41
  export * from './resolved-generated-global-setup.js';
39
42
  export * from './resolved-generated-setup-code.js';
@@ -0,0 +1,2 @@
1
+ import type { Context } from '../context.js';
2
+ export declare const resolvedCommands: (ctx: Context) => string;
@@ -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
+ }
@@ -11,6 +11,7 @@ export const resolvedStories = (ctx) => {
11
11
  docsText: undefined,
12
12
  },
13
13
  supportPluginId: file.supportPluginId,
14
+ docsFilePath: file.markdownFile?.relativePath,
14
15
  index,
15
16
  };
16
17
  });
@@ -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.13.2",
3
+ "version": "0.14.1",
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.13.2",
37
- "@histoire/controls": "^0.13.2",
38
- "@histoire/shared": "^0.13.2",
39
- "@histoire/vendors": "^0.13.2",
40
+ "@histoire/app": "^0.14.1",
41
+ "@histoire/controls": "^0.14.1",
42
+ "@histoire/shared": "^0.14.1",
43
+ "@histoire/vendors": "^0.14.1",
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
@@ -0,0 +1,3 @@
1
+ export function sendEvent (event: string, payload?: any): Promise<any>
2
+
3
+ export function openStory (storyId: string): void
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
+ }
@@ -26,6 +26,7 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
26
26
  deps: {
27
27
  inline: [
28
28
  /histoire\/dist/,
29
+ /histoire\/client/,
29
30
  /@histoire\/[\w\d-]+\/dist/,
30
31
  /histoire-[\w\d-]+\/dist/,
31
32
  /@vue\/devtools-api/,
@@ -50,6 +50,7 @@ export function getDefaultConfig (): HistoireConfig {
50
50
  },
51
51
  defaultColorScheme: 'auto',
52
52
  storeColorScheme: true,
53
+ darkClass: 'dark',
53
54
  },
54
55
  responsivePresets: [
55
56
  {
@@ -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
  }
@@ -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
+ }
@@ -7,7 +7,7 @@ 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'
@@ -60,6 +60,20 @@ export async function createServer (ctx: Context, options: CreateServerOptions =
60
60
  }
61
61
  }
62
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
+
63
77
  // Wait for pre-bundling (in `listen()`)
64
78
  await server.listen(options.port ?? server.config.server?.port)
65
79
 
@@ -32,9 +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
+ export const COMMANDS = 'virtual:$histoire-commands'
36
+ export const RESOLVED_COMMANDS = `/__resolved__${COMMANDS}`
35
37
 
36
38
  export * from './markdown.js'
37
39
  export * from './noop.js'
40
+ export * from './resolved-commands.js'
38
41
  export * from './resolved-config.js'
39
42
  export * from './resolved-generated-global-setup.js'
40
43
  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
+ }
@@ -13,6 +13,7 @@ export const resolvedStories = (ctx: Context) => {
13
13
  docsText: undefined,
14
14
  },
15
15
  supportPluginId: file.supportPluginId,
16
+ docsFilePath: file.markdownFile?.relativePath,
16
17
  index,
17
18
  }
18
19
  })
@@ -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