histoire 0.5.4 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/bundled/SandboxVue3.js +14 -0
  2. package/dist/bundled/StoryView.js +1 -1
  3. package/dist/client/app/App.vue +0 -2
  4. package/dist/client/app/components/app/InitialLoading.vue +0 -2
  5. package/dist/client/app/components/panel/ControlsComponentPropItem.vue +1 -1
  6. package/dist/client/app/components/sandbox/SandboxVue3.vue +16 -0
  7. package/dist/client/app/index.js +0 -2
  8. package/dist/client/app/router.d.ts +1 -1
  9. package/dist/client/app/router.js +0 -1
  10. package/dist/client/app/style/main.pcss +1 -1
  11. package/dist/client/app/style/sandbox.css +2 -4
  12. package/dist/client/bundle-sandbox.js +0 -1
  13. package/dist/client/server/vue3/run.js +5 -0
  14. package/dist/node/bin.js +1 -0
  15. package/dist/node/build.js +7 -12
  16. package/dist/node/commands/preview.js +2 -5
  17. package/dist/node/config.d.ts +4 -0
  18. package/dist/node/config.js +4 -0
  19. package/dist/node/server.js +3 -7
  20. package/dist/node/vite.d.ts +7 -2
  21. package/dist/node/vite.js +67 -15
  22. package/dist/style.css +1 -1
  23. package/package.json +2 -2
  24. package/src/client/app/App.vue +0 -2
  25. package/src/client/app/components/app/InitialLoading.vue +0 -2
  26. package/src/client/app/components/panel/ControlsComponentPropItem.vue +1 -1
  27. package/src/client/app/components/sandbox/SandboxVue3.vue +16 -0
  28. package/src/client/app/index.ts +0 -2
  29. package/src/client/app/router.ts +1 -2
  30. package/src/client/app/style/main.pcss +1 -1
  31. package/src/client/app/style/sandbox.css +2 -4
  32. package/src/client/bundle-sandbox.js +0 -1
  33. package/src/client/server/vue3/run.ts +6 -0
  34. package/src/client/shim.d.ts +2 -0
  35. package/src/node/bin.ts +2 -0
  36. package/src/node/build.ts +13 -13
  37. package/src/node/commands/preview.ts +2 -5
  38. package/src/node/config.ts +9 -0
  39. package/src/node/server.ts +4 -8
  40. package/src/node/vite.ts +75 -16
@@ -18,6 +18,7 @@ import { Text, vModelText, vModelSelect, vModelRadio, vModelCheckbox, vModelDyna
18
18
  import { pascal } from "case";
19
19
  import { a as applyStateToVariant } from "./state.js";
20
20
  import * as setup from "$histoire-setup";
21
+ import * as generatedSetup from "$histoire-generated-global-setup";
21
22
  function indent(lines, count = 1) {
22
23
  return lines.map((line) => `${" ".repeat(count)}${line}`);
23
24
  }
@@ -581,6 +582,12 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
581
582
  });
582
583
  if ((_c2 = (_b2 = (_a2 = props.variant.state) == null ? void 0 : _a2._hPropState) == null ? void 0 : _b2[index]) == null ? void 0 : _c2[key]) {
583
584
  vnode.props[key] = props.variant.state._hPropState[index][key];
585
+ if (!vnode.dynamicProps) {
586
+ vnode.dynamicProps = [];
587
+ }
588
+ if (!vnode.dynamicProps.includes(key)) {
589
+ vnode.dynamicProps.push(key);
590
+ }
584
591
  }
585
592
  }
586
593
  return {
@@ -605,6 +612,13 @@ const _sfc_main = /* @__PURE__ */ defineComponent({
605
612
  return vnodes;
606
613
  }
607
614
  });
615
+ if (typeof (generatedSetup == null ? void 0 : generatedSetup.setupVue3) === "function") {
616
+ await generatedSetup.setupVue3({
617
+ app,
618
+ story: props.story,
619
+ variant: props.variant
620
+ });
621
+ }
608
622
  if (typeof (setup == null ? void 0 : setup.setupVue3) === "function") {
609
623
  await setup.setupVue3({
610
624
  app,
@@ -1495,7 +1495,7 @@ const _sfc_main$c = /* @__PURE__ */ defineComponent({
1495
1495
  key: 0,
1496
1496
  modelValue: unref(model),
1497
1497
  "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(model) ? model.value = $event : null),
1498
- title: __props.definition.name,
1498
+ title: `${__props.definition.name}${unref(canReset) ? " *" : ""}`,
1499
1499
  placeholder: unref(isJSON) ? "Enter JSON" : null
1500
1500
  }, {
1501
1501
  actions: withCtx(() => [
@@ -76,10 +76,8 @@ onKeyboardShortcut(['ctrl+k', 'meta+k'], (event) => {
76
76
 
77
77
  const loading = ref(false)
78
78
 
79
- // @ts-expect-error vite hot api
80
79
  if (import.meta.hot && !rawFiles.length) {
81
80
  loading.value = true
82
- // @ts-expect-error vite hot api
83
81
  import.meta.hot.on('histoire:all-stories-loaded', () => {
84
82
  loading.value = false
85
83
  })
@@ -8,9 +8,7 @@ const progress = reactive({
8
8
 
9
9
  const maxCols = window.innerWidth / 20
10
10
 
11
- // @ts-expect-error vite hot api
12
11
  if (import.meta.hot) {
13
- // @ts-expect-error vite hot api
14
12
  import.meta.hot.on('histoire:stories-loading-progress', (data) => {
15
13
  progress.loaded = data.loadedFileCount
16
14
  progress.total = data.totalFileCount
@@ -71,7 +71,7 @@ const canReset = computed(() => props.variant.state?._hPropState?.[props.compone
71
71
  :is="comp"
72
72
  v-if="comp"
73
73
  v-model="model"
74
- :title="definition.name"
74
+ :title="`${definition.name}${canReset ? ' *' : ''}`"
75
75
  :placeholder="isJSON ? 'Enter JSON' : null"
76
76
  >
77
77
  <template #actions>
@@ -5,6 +5,8 @@ import { getTagName } from '../../codegen/vue3'
5
5
  import { applyStateToVariant } from '../../util/state'
6
6
  // @ts-expect-error virtual module id
7
7
  import * as setup from '$histoire-setup'
8
+ // @ts-expect-error virtual module id
9
+ import * as generatedSetup from '$histoire-generated-global-setup'
8
10
 
9
11
  const props = defineProps({
10
12
  variant: {
@@ -86,6 +88,12 @@ async function mountVariant () {
86
88
  // Props overrides
87
89
  if (props.variant.state?._hPropState?.[index]?.[key]) {
88
90
  vnode.props[key] = props.variant.state._hPropState[index][key]
91
+ if (!vnode.dynamicProps) {
92
+ vnode.dynamicProps = []
93
+ }
94
+ if (!vnode.dynamicProps.includes(key)) {
95
+ vnode.dynamicProps.push(key)
96
+ }
89
97
  }
90
98
  }
91
99
 
@@ -114,6 +122,14 @@ async function mountVariant () {
114
122
  },
115
123
  })
116
124
 
125
+ if (typeof generatedSetup?.setupVue3 === 'function') {
126
+ await generatedSetup.setupVue3({
127
+ app,
128
+ story: props.story,
129
+ variant: props.variant,
130
+ })
131
+ }
132
+
117
133
  if (typeof setup?.setupVue3 === 'function') {
118
134
  await setup.setupVue3({
119
135
  app,
@@ -26,9 +26,7 @@ export function mountMainApp() {
26
26
  app.use(router);
27
27
  registerGlobalComponents(app);
28
28
  app.mount('#app');
29
- // @ts-expect-error vite hot api
30
29
  if (import.meta.hot) {
31
- // @ts-expect-error vite hot api
32
30
  import.meta.hot.send('histoire:mount', {});
33
31
  }
34
32
  }
@@ -1,2 +1,2 @@
1
- export declare const base: any;
1
+ export declare const base: string;
2
2
  export declare const router: import("vue-router").Router;
@@ -1,5 +1,4 @@
1
1
  import { createRouter, createWebHistory } from 'vue-router';
2
- // @ts-expect-error env not defined
3
2
  export const base = import.meta.env.BASE_URL;
4
3
  export const router = createRouter({
5
4
  history: createWebHistory(base),
@@ -46,10 +46,10 @@ html,
46
46
  body,
47
47
  #app {
48
48
  height: 100%;
49
- font-family: 'Lato', sans-serif;
50
49
  }
51
50
 
52
51
  html {
52
+ font-family: 'Lato', sans-serif;
53
53
  font-size: 16px;
54
54
  }
55
55
 
@@ -9,11 +9,9 @@ body {
9
9
  margin: 0;
10
10
  }
11
11
 
12
- html,
13
- body,
14
- #app {
15
- font-family: 'Lato', sans-serif;
12
+ html {
16
13
  font-size: 14px;
14
+ font-family: 'Lato', sans-serif;
17
15
  }
18
16
 
19
17
  .htw-sandbox-hidden {
@@ -1,3 +1,2 @@
1
- import '../style.css'
2
1
  import './app/style/sandbox.css'
3
2
  import '../bundled/sandbox.js'
@@ -4,6 +4,8 @@ import Story from './Story.vue';
4
4
  import Variant from './Variant.vue';
5
5
  // @ts-expect-error virtual module id
6
6
  import * as setup from '$histoire-setup';
7
+ // @ts-expect-error virtual module id
8
+ import * as generatedSetup from '$histoire-generated-global-setup';
7
9
  export async function run({ file, storyData, el }) {
8
10
  const { default: Comp } = await import(file.moduleId);
9
11
  const app = createApp({
@@ -19,6 +21,9 @@ export async function run({ file, storyData, el }) {
19
21
  });
20
22
  },
21
23
  });
24
+ if (typeof generatedSetup?.setupVue3 === 'function') {
25
+ await generatedSetup.setupVue3({ app });
26
+ }
22
27
  if (typeof setup?.setupVue3 === 'function') {
23
28
  await setup.setupVue3({ app });
24
29
  }
package/dist/node/bin.js CHANGED
@@ -4,6 +4,7 @@ import { fileURLToPath } from 'url';
4
4
  import sade from 'sade';
5
5
  const __dirname = dirname(fileURLToPath(import.meta.url));
6
6
  const { version } = JSON.parse(fs.readFileSync(resolve(__dirname, '../../package.json'), 'utf8'));
7
+ process.env.NODE_ENV = 'development';
7
8
  const program = sade('histoire');
8
9
  program.version(version);
9
10
  program.command('dev')
@@ -1,11 +1,11 @@
1
1
  import { join } from 'pathe';
2
- import { build as viteBuild, createServer as createViteServer, resolveConfig as resolveViteConfig } from 'vite';
2
+ import { build as viteBuild, createServer as createViteServer, mergeConfig as mergeViteConfig, } from 'vite';
3
3
  import fs from 'fs-extra';
4
4
  import { lookup as lookupMime } from 'mrmime';
5
5
  import pc from 'picocolors';
6
6
  import { performance } from 'perf_hooks';
7
7
  import { APP_PATH } from './alias.js';
8
- import { createVitePlugins } from './vite.js';
8
+ import { getViteConfigWithPlugins, resolveViteConfig } from './vite.js';
9
9
  import { findAllStories } from './stories.js';
10
10
  import { useCollectStories } from './collect/index.js';
11
11
  import { BuildPluginApi } from './plugin.js';
@@ -22,9 +22,8 @@ const PREFETCHED_MODULES = [
22
22
  export async function build(ctx) {
23
23
  const startTime = performance.now();
24
24
  await findAllStories(ctx);
25
- const server = await createViteServer({
26
- plugins: await createVitePlugins(true, ctx),
27
- });
25
+ const resolvedViteConfig = await resolveViteConfig(ctx);
26
+ const server = await createViteServer(await getViteConfigWithPlugins(true, ctx));
28
27
  await server.pluginContainer.buildStart({});
29
28
  const moduleLoader = useModuleLoader({
30
29
  server,
@@ -50,9 +49,8 @@ export async function build(ctx) {
50
49
  const storyCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ? 1 : 0), 0);
51
50
  const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0);
52
51
  const emptyStoryCount = ctx.storyFiles.length - storyCount;
53
- const results = await viteBuild({
52
+ const results = await viteBuild(mergeViteConfig(await getViteConfigWithPlugins(false, ctx), {
54
53
  mode: 'development',
55
- plugins: await createVitePlugins(false, ctx),
56
54
  build: {
57
55
  rollupOptions: {
58
56
  input: [
@@ -61,7 +59,7 @@ export async function build(ctx) {
61
59
  ],
62
60
  output: {
63
61
  manualChunks(id) {
64
- if (!id.includes('histoire/dist/client') && id.includes('node_modules')) {
62
+ if (!id.includes('histoire/dist/client') && !id.includes('histoire/dist/bundled') && id.includes('node_modules')) {
65
63
  return 'vendor';
66
64
  }
67
65
  },
@@ -72,11 +70,8 @@ export async function build(ctx) {
72
70
  cssCodeSplit: false,
73
71
  minify: false,
74
72
  },
75
- });
73
+ }));
76
74
  const result = Array.isArray(results) ? results[0] : results;
77
- const resolvedViteConfig = await resolveViteConfig({
78
- plugins: await createVitePlugins(false, ctx),
79
- }, 'build');
80
75
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset');
81
76
  // Preload
82
77
  const preloadOutputs = result.output.filter(o => PRELOAD_MODULES.includes(o.name) && o.type === 'chunk');
@@ -1,15 +1,12 @@
1
- import { resolveConfig as resolveViteConfig } from 'vite';
2
1
  import pc from 'picocolors';
3
2
  import { createContext } from '../context.js';
4
- import { createVitePlugins } from '../vite.js';
3
+ import { resolveViteConfig } from '../vite.js';
5
4
  import { startPreview } from '../preview.js';
6
5
  export async function previewCommand(options) {
7
6
  const ctx = await createContext({
8
7
  mode: 'build',
9
8
  });
10
- const viteConfig = await resolveViteConfig({
11
- plugins: await createVitePlugins(false, ctx),
12
- }, 'build');
9
+ const viteConfig = await resolveViteConfig(ctx);
13
10
  const { baseUrl } = await startPreview(viteConfig, options.port, ctx);
14
11
  console.log(`Preview server listening on ${pc.cyan(baseUrl)}`);
15
12
  }
@@ -106,6 +106,10 @@ export interface HistoireConfig {
106
106
  * Example: `'/src/histoire-setup.ts'`
107
107
  */
108
108
  setupFile?: string;
109
+ /**
110
+ * Setup code created by plugins
111
+ */
112
+ setupCode?: string[];
109
113
  /**
110
114
  * Predefined responsive sizes for story playgrounds.
111
115
  */
@@ -156,6 +156,10 @@ export const mergeConfig = createDefu((obj, key, value) => {
156
156
  }
157
157
  return true;
158
158
  }
159
+ if (obj[key] && key === 'setupCode') {
160
+ obj[key] = [...obj[key], ...value];
161
+ return true;
162
+ }
159
163
  // By default, arrays should be replaced
160
164
  if (obj[key] && Array.isArray(obj[key])) {
161
165
  obj[key] = value;
@@ -1,17 +1,13 @@
1
1
  import { createServer as createViteServer } from 'vite';
2
- import { createVitePlugins, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js';
2
+ import { getViteConfigWithPlugins, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js';
3
3
  import { onStoryChange, watchStories } from './stories.js';
4
4
  import { useCollectStories } from './collect/index.js';
5
5
  import { DevPluginApi } from './plugin.js';
6
6
  import { useModuleLoader } from './load.js';
7
7
  export async function createServer(ctx, port) {
8
- const server = await createViteServer({
9
- plugins: await createVitePlugins(false, ctx),
10
- });
8
+ const server = await createViteServer(await getViteConfigWithPlugins(false, ctx));
11
9
  await server.pluginContainer.buildStart({});
12
- const nodeServer = await createViteServer({
13
- plugins: await createVitePlugins(true, ctx),
14
- });
10
+ const nodeServer = await createViteServer(await getViteConfigWithPlugins(true, ctx));
15
11
  await nodeServer.pluginContainer.buildStart({});
16
12
  const moduleLoader = useModuleLoader({
17
13
  server: nodeServer,
@@ -1,4 +1,4 @@
1
- import { Plugin as VitePlugin } from 'vite';
1
+ import { ResolvedConfig, InlineConfig } from 'vite';
2
2
  import { Context } from './context.js';
3
3
  export declare const STORIES_ID = "$histoire-stories";
4
4
  export declare const RESOLVED_STORIES_ID: string;
@@ -12,4 +12,9 @@ export declare const SEARCH_TITLE_DATA_ID = "$histoire-search-title-data";
12
12
  export declare const RESOLVED_SEARCH_TITLE_DATA_ID: string;
13
13
  export declare const SEARCH_DOCS_DATA_ID = "$histoire-search-docs-data";
14
14
  export declare const RESOLVED_SEARCH_DOCS_DATA_ID: string;
15
- export declare function createVitePlugins(server: boolean, ctx: Context): Promise<VitePlugin[]>;
15
+ export declare const GENERATED_GLOBAL_SETUP = "$histoire-generated-global-setup";
16
+ export declare const RESOLVED_GENERATED_GLOBAL_SETUP: string;
17
+ export declare const GENERATED_SETUP_CODE = "$histoire-generated-setup-code";
18
+ export declare const RESOLVED_GENERATED_SETUP_CODE: string;
19
+ export declare function resolveViteConfig(ctx: Context): Promise<ResolvedConfig>;
20
+ export declare function getViteConfigWithPlugins(server: boolean, ctx: Context): Promise<InlineConfig>;
package/dist/node/vite.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createRequire } from 'module';
2
- import { relative, dirname } from 'pathe';
3
- import { resolveConfig as resolveViteConfig } from 'vite';
2
+ import { relative, dirname, resolve } from 'pathe';
3
+ import { resolveConfig as resolveViteConfigInternal, mergeConfig as mergeViteConfig, } from 'vite';
4
4
  import { lookup as lookupMime } from 'mrmime';
5
5
  import { APP_PATH, DIST_PATH, TEMP_PATH } from './alias.js';
6
6
  import { notifyStoryChange } from './stories.js';
@@ -21,17 +21,35 @@ export const SEARCH_TITLE_DATA_ID = '$histoire-search-title-data';
21
21
  export const RESOLVED_SEARCH_TITLE_DATA_ID = `/${SEARCH_TITLE_DATA_ID}-resolved`;
22
22
  export const SEARCH_DOCS_DATA_ID = '$histoire-search-docs-data';
23
23
  export const RESOLVED_SEARCH_DOCS_DATA_ID = `/${SEARCH_DOCS_DATA_ID}-resolved`;
24
- export async function createVitePlugins(server, ctx) {
25
- const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build');
26
- const plugins = [];
24
+ export const GENERATED_GLOBAL_SETUP = '$histoire-generated-global-setup';
25
+ export const RESOLVED_GENERATED_GLOBAL_SETUP = `/${GENERATED_GLOBAL_SETUP}-resolved`;
26
+ export const GENERATED_SETUP_CODE = '$histoire-generated-setup-code';
27
+ export const RESOLVED_GENERATED_SETUP_CODE = `/${GENERATED_SETUP_CODE}-resolved`;
28
+ export async function resolveViteConfig(ctx) {
29
+ const command = ctx.mode === 'dev' ? 'serve' : 'build';
30
+ let viteConfig = (await resolveViteConfigInternal({}, command));
31
+ viteConfig = mergeHistoireViteConfig(viteConfig, ctx);
32
+ return viteConfig;
33
+ }
34
+ async function mergeHistoireViteConfig(viteConfig, ctx) {
27
35
  if (ctx.config.vite) {
28
- plugins.push({
29
- name: 'histoire-vite-config-override',
30
- config(config, env) {
31
- return typeof ctx.config.vite === 'function' ? ctx.config.vite(config, env) : ctx.config.vite;
32
- },
33
- });
36
+ const command = ctx.mode === 'dev' ? 'serve' : 'build';
37
+ const overrides = typeof ctx.config.vite === 'function'
38
+ ? await ctx.config.vite(viteConfig, {
39
+ mode: ctx.mode,
40
+ command,
41
+ })
42
+ : ctx.config.vite;
43
+ if (overrides) {
44
+ viteConfig = mergeViteConfig(viteConfig, overrides);
45
+ }
34
46
  }
47
+ return viteConfig;
48
+ }
49
+ export async function getViteConfigWithPlugins(server, ctx) {
50
+ const resolvedViteConfig = await resolveViteConfig(ctx);
51
+ const inlineConfig = await mergeHistoireViteConfig({}, ctx);
52
+ const plugins = [];
35
53
  plugins.push({
36
54
  name: 'histoire-vite-plugin',
37
55
  config() {
@@ -57,7 +75,7 @@ export async function createVitePlugins(server, ctx) {
57
75
  },
58
76
  server: {
59
77
  fs: {
60
- allow: [DIST_PATH, TEMP_PATH, viteConfig.root, process.cwd()],
78
+ allow: [DIST_PATH, TEMP_PATH, resolvedViteConfig.root, process.cwd()],
61
79
  },
62
80
  },
63
81
  define: {
@@ -72,7 +90,7 @@ export async function createVitePlugins(server, ctx) {
72
90
  }
73
91
  if (id.startsWith(SETUP_ID)) {
74
92
  if (ctx.config.setupFile) {
75
- return this.resolve(ctx.config.setupFile, importer, {
93
+ return this.resolve(resolve(ctx.root, ctx.config.setupFile), importer, {
76
94
  skipSelf: true,
77
95
  });
78
96
  }
@@ -92,6 +110,13 @@ export async function createVitePlugins(server, ctx) {
92
110
  if (id.startsWith(SEARCH_DOCS_DATA_ID)) {
93
111
  return RESOLVED_SEARCH_DOCS_DATA_ID;
94
112
  }
113
+ if (id.startsWith(GENERATED_GLOBAL_SETUP)) {
114
+ return RESOLVED_GENERATED_GLOBAL_SETUP;
115
+ }
116
+ if (id.startsWith(GENERATED_SETUP_CODE)) {
117
+ const [, index] = id.split('__');
118
+ return `${RESOLVED_GENERATED_SETUP_CODE}__${index}`;
119
+ }
95
120
  },
96
121
  async load(id) {
97
122
  if (id === RESOLVED_STORIES_ID) {
@@ -157,6 +182,31 @@ if (import.meta.hot) {
157
182
  if (id === RESOLVED_SEARCH_DOCS_DATA_ID) {
158
183
  return getSearchDataJS(await generateDocSearchData(ctx));
159
184
  }
185
+ if (id === RESOLVED_GENERATED_GLOBAL_SETUP) {
186
+ if (ctx.config.setupCode) {
187
+ return [
188
+ // Import
189
+ `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}__${index}'`).join('\n')}`,
190
+ // List
191
+ `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
192
+ // Setups
193
+ ['setupVue3'].map(fnName => `export async function ${fnName} (payload) {
194
+ for (const setup of setupList) {
195
+ if (setup?.${fnName}) {
196
+ await setup.${fnName}(payload)
197
+ }
198
+ }
199
+ }`),
200
+ ].join('\n');
201
+ }
202
+ else {
203
+ return '';
204
+ }
205
+ }
206
+ if (id.startsWith(RESOLVED_GENERATED_SETUP_CODE)) {
207
+ const [, index] = id.split('__');
208
+ return ctx.config.setupCode?.[index] ?? '';
209
+ }
160
210
  },
161
211
  handleHotUpdate(updateContext) {
162
212
  const story = ctx.storyFiles.find(file => file.path === updateContext.file);
@@ -247,7 +297,7 @@ if (import.meta.hot) {
247
297
  if (exclude.some(r => r.test(id)))
248
298
  return;
249
299
  if (include.some(r => r.test(id))) {
250
- const file = relative(viteConfig.root, id);
300
+ const file = relative(resolvedViteConfig.root, id);
251
301
  const index = code.indexOf('export default');
252
302
  const result = `${code.substring(0, index)}_sfc_main.__file = '${file}'\n${code.substring(index)}`;
253
303
  return result;
@@ -255,7 +305,9 @@ if (import.meta.hot) {
255
305
  },
256
306
  });
257
307
  }
258
- return plugins;
308
+ return mergeViteConfig(inlineConfig, {
309
+ plugins,
310
+ });
259
311
  }
260
312
  async function createCustomBlocksPlugins(ctx) {
261
313
  const plugins = [];
package/dist/style.css CHANGED
@@ -1913,10 +1913,10 @@ html,
1913
1913
  body,
1914
1914
  #app {
1915
1915
  height: 100%;
1916
- font-family: 'Lato', sans-serif;
1917
1916
  }
1918
1917
 
1919
1918
  html {
1919
+ font-family: 'Lato', sans-serif;
1920
1920
  font-size: 16px;
1921
1921
  }
1922
1922
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.5.4",
3
+ "version": "0.6.2",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -33,7 +33,7 @@
33
33
  "histoire": "./bin.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@histoire/controls": "^0.5.4",
36
+ "@histoire/controls": "^0.6.2",
37
37
  "@iconify/vue": "^3.2.1",
38
38
  "@types/markdown-it": "^12.2.3",
39
39
  "@vueuse/core": "^8.2.5",
@@ -76,10 +76,8 @@ onKeyboardShortcut(['ctrl+k', 'meta+k'], (event) => {
76
76
 
77
77
  const loading = ref(false)
78
78
 
79
- // @ts-expect-error vite hot api
80
79
  if (import.meta.hot && !rawFiles.length) {
81
80
  loading.value = true
82
- // @ts-expect-error vite hot api
83
81
  import.meta.hot.on('histoire:all-stories-loaded', () => {
84
82
  loading.value = false
85
83
  })
@@ -8,9 +8,7 @@ const progress = reactive({
8
8
 
9
9
  const maxCols = window.innerWidth / 20
10
10
 
11
- // @ts-expect-error vite hot api
12
11
  if (import.meta.hot) {
13
- // @ts-expect-error vite hot api
14
12
  import.meta.hot.on('histoire:stories-loading-progress', (data) => {
15
13
  progress.loaded = data.loadedFileCount
16
14
  progress.total = data.totalFileCount
@@ -71,7 +71,7 @@ const canReset = computed(() => props.variant.state?._hPropState?.[props.compone
71
71
  :is="comp"
72
72
  v-if="comp"
73
73
  v-model="model"
74
- :title="definition.name"
74
+ :title="`${definition.name}${canReset ? ' *' : ''}`"
75
75
  :placeholder="isJSON ? 'Enter JSON' : null"
76
76
  >
77
77
  <template #actions>
@@ -5,6 +5,8 @@ import { getTagName } from '../../codegen/vue3'
5
5
  import { applyStateToVariant } from '../../util/state'
6
6
  // @ts-expect-error virtual module id
7
7
  import * as setup from '$histoire-setup'
8
+ // @ts-expect-error virtual module id
9
+ import * as generatedSetup from '$histoire-generated-global-setup'
8
10
 
9
11
  const props = defineProps({
10
12
  variant: {
@@ -86,6 +88,12 @@ async function mountVariant () {
86
88
  // Props overrides
87
89
  if (props.variant.state?._hPropState?.[index]?.[key]) {
88
90
  vnode.props[key] = props.variant.state._hPropState[index][key]
91
+ if (!vnode.dynamicProps) {
92
+ vnode.dynamicProps = []
93
+ }
94
+ if (!vnode.dynamicProps.includes(key)) {
95
+ vnode.dynamicProps.push(key)
96
+ }
89
97
  }
90
98
  }
91
99
 
@@ -114,6 +122,14 @@ async function mountVariant () {
114
122
  },
115
123
  })
116
124
 
125
+ if (typeof generatedSetup?.setupVue3 === 'function') {
126
+ await generatedSetup.setupVue3({
127
+ app,
128
+ story: props.story,
129
+ variant: props.variant,
130
+ })
131
+ }
132
+
117
133
  if (typeof setup?.setupVue3 === 'function') {
118
134
  await setup.setupVue3({
119
135
  app,
@@ -30,9 +30,7 @@ export function mountMainApp () {
30
30
  registerGlobalComponents(app)
31
31
  app.mount('#app')
32
32
 
33
- // @ts-expect-error vite hot api
34
33
  if (import.meta.hot) {
35
- // @ts-expect-error vite hot api
36
34
  import.meta.hot.send('histoire:mount', {})
37
35
  }
38
36
  }
@@ -1,7 +1,6 @@
1
1
  import { createRouter, createWebHistory } from 'vue-router'
2
2
 
3
- // @ts-expect-error env not defined
4
- export const base = import.meta.env.BASE_URL
3
+ export const base = import.meta.env.BASE_URL as string
5
4
 
6
5
  export const router = createRouter({
7
6
  history: createWebHistory(base),
@@ -46,10 +46,10 @@ html,
46
46
  body,
47
47
  #app {
48
48
  height: 100%;
49
- font-family: 'Lato', sans-serif;
50
49
  }
51
50
 
52
51
  html {
52
+ font-family: 'Lato', sans-serif;
53
53
  font-size: 16px;
54
54
  }
55
55
 
@@ -9,11 +9,9 @@ body {
9
9
  margin: 0;
10
10
  }
11
11
 
12
- html,
13
- body,
14
- #app {
15
- font-family: 'Lato', sans-serif;
12
+ html {
16
13
  font-size: 14px;
14
+ font-family: 'Lato', sans-serif;
17
15
  }
18
16
 
19
17
  .htw-sandbox-hidden {
@@ -1,3 +1,2 @@
1
- import '../style.css'
2
1
  import './app/style/sandbox.css'
3
2
  import '../bundled/sandbox.js'
@@ -5,6 +5,8 @@ import Story from './Story.vue'
5
5
  import Variant from './Variant.vue'
6
6
  // @ts-expect-error virtual module id
7
7
  import * as setup from '$histoire-setup'
8
+ // @ts-expect-error virtual module id
9
+ import * as generatedSetup from '$histoire-generated-global-setup'
8
10
 
9
11
  export async function run ({ file, storyData, el }: ServerRunPayload) {
10
12
  const { default: Comp } = await import(file.moduleId)
@@ -23,6 +25,10 @@ export async function run ({ file, storyData, el }: ServerRunPayload) {
23
25
  },
24
26
  })
25
27
 
28
+ if (typeof generatedSetup?.setupVue3 === 'function') {
29
+ await generatedSetup.setupVue3({ app })
30
+ }
31
+
26
32
  if (typeof setup?.setupVue3 === 'function') {
27
33
  await setup.setupVue3({ app })
28
34
  }
@@ -1,3 +1,5 @@
1
+ /// <reference types="vite/client" />
2
+
1
3
  declare module '*.vue' {
2
4
  import { ComponentOptions } from 'vue'
3
5
  const comp: ComponentOptions
package/src/node/bin.ts CHANGED
@@ -7,6 +7,8 @@ const __dirname = dirname(fileURLToPath(import.meta.url))
7
7
 
8
8
  const { version } = JSON.parse(fs.readFileSync(resolve(__dirname, '../../package.json'), 'utf8'))
9
9
 
10
+ process.env.NODE_ENV = 'development'
11
+
10
12
  const program = sade('histoire')
11
13
  program.version(version)
12
14
 
package/src/node/build.ts CHANGED
@@ -1,12 +1,17 @@
1
1
  import { join } from 'pathe'
2
- import { build as viteBuild, createServer as createViteServer, ResolvedConfig as ViteConfig, resolveConfig as resolveViteConfig } from 'vite'
2
+ import {
3
+ build as viteBuild,
4
+ createServer as createViteServer,
5
+ ResolvedConfig as ViteConfig,
6
+ mergeConfig as mergeViteConfig,
7
+ } from 'vite'
3
8
  import fs from 'fs-extra'
4
9
  import { lookup as lookupMime } from 'mrmime'
5
10
  import pc from 'picocolors'
6
11
  import { performance } from 'perf_hooks'
7
12
  import { APP_PATH } from './alias.js'
8
13
  import { Context } from './context.js'
9
- import { createVitePlugins } from './vite.js'
14
+ import { getViteConfigWithPlugins, resolveViteConfig } from './vite.js'
10
15
  import { findAllStories } from './stories.js'
11
16
  import type { RollupOutput } from 'rollup'
12
17
  import { useCollectStories } from './collect/index.js'
@@ -28,9 +33,9 @@ export async function build (ctx: Context) {
28
33
  const startTime = performance.now()
29
34
  await findAllStories(ctx)
30
35
 
31
- const server = await createViteServer({
32
- plugins: await createVitePlugins(true, ctx),
33
- })
36
+ const resolvedViteConfig = await resolveViteConfig(ctx)
37
+
38
+ const server = await createViteServer(await getViteConfigWithPlugins(true, ctx))
34
39
  await server.pluginContainer.buildStart({})
35
40
 
36
41
  const moduleLoader = useModuleLoader({
@@ -60,9 +65,8 @@ export async function build (ctx: Context) {
60
65
  const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0)
61
66
  const emptyStoryCount = ctx.storyFiles.length - storyCount
62
67
 
63
- const results = await viteBuild({
68
+ const results = await viteBuild(mergeViteConfig(await getViteConfigWithPlugins(false, ctx), {
64
69
  mode: 'development',
65
- plugins: await createVitePlugins(false, ctx),
66
70
  build: {
67
71
  rollupOptions: {
68
72
  input: [
@@ -71,7 +75,7 @@ export async function build (ctx: Context) {
71
75
  ],
72
76
  output: {
73
77
  manualChunks (id) {
74
- if (!id.includes('histoire/dist/client') && id.includes('node_modules')) {
78
+ if (!id.includes('histoire/dist/client') && !id.includes('histoire/dist/bundled') && id.includes('node_modules')) {
75
79
  return 'vendor'
76
80
  }
77
81
  },
@@ -82,13 +86,9 @@ export async function build (ctx: Context) {
82
86
  cssCodeSplit: false,
83
87
  minify: false,
84
88
  },
85
- })
89
+ }))
86
90
  const result = Array.isArray(results) ? results[0] : results as RollupOutput
87
91
 
88
- const resolvedViteConfig = await resolveViteConfig({
89
- plugins: await createVitePlugins(false, ctx),
90
- }, 'build')
91
-
92
92
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset')
93
93
 
94
94
  // Preload
@@ -1,7 +1,6 @@
1
- import { resolveConfig as resolveViteConfig } from 'vite'
2
1
  import pc from 'picocolors'
3
2
  import { createContext } from '../context.js'
4
- import { createVitePlugins } from '../vite.js'
3
+ import { resolveViteConfig } from '../vite.js'
5
4
  import { startPreview } from '../preview.js'
6
5
 
7
6
  export interface PreviewOptions {
@@ -12,9 +11,7 @@ export async function previewCommand (options: PreviewOptions) {
12
11
  const ctx = await createContext({
13
12
  mode: 'build',
14
13
  })
15
- const viteConfig = await resolveViteConfig({
16
- plugins: await createVitePlugins(false, ctx),
17
- }, 'build')
14
+ const viteConfig = await resolveViteConfig(ctx)
18
15
 
19
16
  const { baseUrl } = await startPreview(viteConfig, options.port, ctx)
20
17
  console.log(`Preview server listening on ${pc.cyan(baseUrl)}`)
@@ -125,6 +125,10 @@ export interface HistoireConfig {
125
125
  * Example: `'/src/histoire-setup.ts'`
126
126
  */
127
127
  setupFile?: string
128
+ /**
129
+ * Setup code created by plugins
130
+ */
131
+ setupCode?: string[]
128
132
  /**
129
133
  * Predefined responsive sizes for story playgrounds.
130
134
  */
@@ -303,6 +307,11 @@ export const mergeConfig = createDefu((obj: any, key, value) => {
303
307
  return true
304
308
  }
305
309
 
310
+ if (obj[key] && key === 'setupCode') {
311
+ obj[key] = [...obj[key], ...value]
312
+ return true
313
+ }
314
+
306
315
  // By default, arrays should be replaced
307
316
  if (obj[key] && Array.isArray(obj[key])) {
308
317
  obj[key] = value
@@ -1,21 +1,17 @@
1
1
  import { createServer as createViteServer } from 'vite'
2
2
  import { Context } from './context.js'
3
- import { createVitePlugins, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js'
4
- import { notifyStoryChange, onStoryChange, watchStories } from './stories.js'
3
+ import { getViteConfigWithPlugins, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js'
4
+ import { onStoryChange, watchStories } from './stories.js'
5
5
  import { useCollectStories } from './collect/index.js'
6
6
  import type { StoryFile } from './types.js'
7
7
  import { DevPluginApi } from './plugin.js'
8
8
  import { useModuleLoader } from './load.js'
9
9
 
10
10
  export async function createServer (ctx: Context, port: number) {
11
- const server = await createViteServer({
12
- plugins: await createVitePlugins(false, ctx),
13
- })
11
+ const server = await createViteServer(await getViteConfigWithPlugins(false, ctx))
14
12
  await server.pluginContainer.buildStart({})
15
13
 
16
- const nodeServer = await createViteServer({
17
- plugins: await createVitePlugins(true, ctx),
18
- })
14
+ const nodeServer = await createViteServer(await getViteConfigWithPlugins(true, ctx))
19
15
  await nodeServer.pluginContainer.buildStart({})
20
16
 
21
17
  const moduleLoader = useModuleLoader({
package/src/node/vite.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import { createRequire } from 'module'
2
- import { relative, dirname } from 'pathe'
3
- import { resolveConfig as resolveViteConfig, Plugin as VitePlugin } from 'vite'
2
+ import { relative, dirname, resolve } from 'pathe'
3
+ import {
4
+ resolveConfig as resolveViteConfigInternal,
5
+ Plugin as VitePlugin,
6
+ UserConfig as ViteConfig,
7
+ ResolvedConfig,
8
+ InlineConfig,
9
+ mergeConfig as mergeViteConfig,
10
+ } from 'vite'
4
11
  import { lookup as lookupMime } from 'mrmime'
5
12
  import { APP_PATH, DIST_PATH, TEMP_PATH } from './alias.js'
6
13
  import { Context } from './context.js'
@@ -24,20 +31,39 @@ export const SEARCH_TITLE_DATA_ID = '$histoire-search-title-data'
24
31
  export const RESOLVED_SEARCH_TITLE_DATA_ID = `/${SEARCH_TITLE_DATA_ID}-resolved`
25
32
  export const SEARCH_DOCS_DATA_ID = '$histoire-search-docs-data'
26
33
  export const RESOLVED_SEARCH_DOCS_DATA_ID = `/${SEARCH_DOCS_DATA_ID}-resolved`
34
+ export const GENERATED_GLOBAL_SETUP = '$histoire-generated-global-setup'
35
+ export const RESOLVED_GENERATED_GLOBAL_SETUP = `/${GENERATED_GLOBAL_SETUP}-resolved`
36
+ export const GENERATED_SETUP_CODE = '$histoire-generated-setup-code'
37
+ export const RESOLVED_GENERATED_SETUP_CODE = `/${GENERATED_SETUP_CODE}-resolved`
27
38
 
28
- export async function createVitePlugins (server: boolean, ctx: Context): Promise<VitePlugin[]> {
29
- const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build')
30
-
31
- const plugins: VitePlugin[] = []
39
+ export async function resolveViteConfig (ctx: Context): Promise<ResolvedConfig> {
40
+ const command = ctx.mode === 'dev' ? 'serve' : 'build'
41
+ let viteConfig = (await resolveViteConfigInternal({}, command)) as unknown
42
+ viteConfig = mergeHistoireViteConfig(viteConfig, ctx)
43
+ return viteConfig as ResolvedConfig
44
+ }
32
45
 
46
+ async function mergeHistoireViteConfig (viteConfig: InlineConfig, ctx: Context) {
33
47
  if (ctx.config.vite) {
34
- plugins.push({
35
- name: 'histoire-vite-config-override',
36
- config (config, env) {
37
- return typeof ctx.config.vite === 'function' ? ctx.config.vite(config, env) : ctx.config.vite
38
- },
39
- })
48
+ const command = ctx.mode === 'dev' ? 'serve' : 'build'
49
+ const overrides = typeof ctx.config.vite === 'function'
50
+ ? await ctx.config.vite(viteConfig as ViteConfig, {
51
+ mode: ctx.mode,
52
+ command,
53
+ })
54
+ : ctx.config.vite
55
+ if (overrides) {
56
+ viteConfig = mergeViteConfig(viteConfig, overrides)
57
+ }
40
58
  }
59
+ return viteConfig
60
+ }
61
+
62
+ export async function getViteConfigWithPlugins (server: boolean, ctx: Context): Promise<InlineConfig> {
63
+ const resolvedViteConfig = await resolveViteConfig(ctx)
64
+
65
+ const inlineConfig = await mergeHistoireViteConfig({}, ctx)
66
+ const plugins: VitePlugin[] = []
41
67
 
42
68
  plugins.push({
43
69
  name: 'histoire-vite-plugin',
@@ -65,7 +91,7 @@ export async function createVitePlugins (server: boolean, ctx: Context): Promise
65
91
  },
66
92
  server: {
67
93
  fs: {
68
- allow: [DIST_PATH, TEMP_PATH, viteConfig.root, process.cwd()],
94
+ allow: [DIST_PATH, TEMP_PATH, resolvedViteConfig.root, process.cwd()],
69
95
  },
70
96
  },
71
97
  define: {
@@ -81,7 +107,7 @@ export async function createVitePlugins (server: boolean, ctx: Context): Promise
81
107
  }
82
108
  if (id.startsWith(SETUP_ID)) {
83
109
  if (ctx.config.setupFile) {
84
- return this.resolve(ctx.config.setupFile, importer, {
110
+ return this.resolve(resolve(ctx.root, ctx.config.setupFile), importer, {
85
111
  skipSelf: true,
86
112
  })
87
113
  } else {
@@ -100,6 +126,13 @@ export async function createVitePlugins (server: boolean, ctx: Context): Promise
100
126
  if (id.startsWith(SEARCH_DOCS_DATA_ID)) {
101
127
  return RESOLVED_SEARCH_DOCS_DATA_ID
102
128
  }
129
+ if (id.startsWith(GENERATED_GLOBAL_SETUP)) {
130
+ return RESOLVED_GENERATED_GLOBAL_SETUP
131
+ }
132
+ if (id.startsWith(GENERATED_SETUP_CODE)) {
133
+ const [, index] = id.split('__')
134
+ return `${RESOLVED_GENERATED_SETUP_CODE}__${index}`
135
+ }
103
136
  },
104
137
 
105
138
  async load (id) {
@@ -166,6 +199,30 @@ if (import.meta.hot) {
166
199
  if (id === RESOLVED_SEARCH_DOCS_DATA_ID) {
167
200
  return getSearchDataJS(await generateDocSearchData(ctx))
168
201
  }
202
+ if (id === RESOLVED_GENERATED_GLOBAL_SETUP) {
203
+ if (ctx.config.setupCode) {
204
+ return [
205
+ // Import
206
+ `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}__${index}'`).join('\n')}`,
207
+ // List
208
+ `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
209
+ // Setups
210
+ ['setupVue3'].map(fnName => `export async function ${fnName} (payload) {
211
+ for (const setup of setupList) {
212
+ if (setup?.${fnName}) {
213
+ await setup.${fnName}(payload)
214
+ }
215
+ }
216
+ }`),
217
+ ].join('\n')
218
+ } else {
219
+ return ''
220
+ }
221
+ }
222
+ if (id.startsWith(RESOLVED_GENERATED_SETUP_CODE)) {
223
+ const [, index] = id.split('__')
224
+ return ctx.config.setupCode?.[index] ?? ''
225
+ }
169
226
  },
170
227
 
171
228
  handleHotUpdate (updateContext) {
@@ -263,7 +320,7 @@ if (import.meta.hot) {
263
320
  transform (code, id) {
264
321
  if (exclude.some(r => r.test(id))) return
265
322
  if (include.some(r => r.test(id))) {
266
- const file = relative(viteConfig.root, id)
323
+ const file = relative(resolvedViteConfig.root, id)
267
324
  const index = code.indexOf('export default')
268
325
  const result = `${code.substring(0, index)}_sfc_main.__file = '${file}'\n${code.substring(index)}`
269
326
  return result
@@ -272,7 +329,9 @@ if (import.meta.hot) {
272
329
  })
273
330
  }
274
331
 
275
- return plugins
332
+ return mergeViteConfig(inlineConfig, {
333
+ plugins,
334
+ }) as InlineConfig
276
335
  }
277
336
 
278
337
  async function createCustomBlocksPlugins (ctx) {