histoire 0.7.5 → 0.7.8

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.
@@ -596,7 +596,7 @@ function getSandboxUrl(story, variant) {
596
596
  const url = new URLSearchParams();
597
597
  url.append("storyId", story.id);
598
598
  url.append("variantId", variant.id);
599
- return `${base}__sandbox?${url.toString()}`;
599
+ return `${base}__sandbox.html?${url.toString()}`;
600
600
  }
601
601
  const _hoisted_1$l = ["href"];
602
602
  const _sfc_main$n = /* @__PURE__ */ defineComponent({
@@ -5,13 +5,13 @@ function toRawDeep(val, seen = /* @__PURE__ */ new Set()) {
5
5
  if (typeof unwrappedValue === "symbol") {
6
6
  return unwrappedValue.toString();
7
7
  }
8
+ if (!isObject(unwrappedValue)) {
9
+ return unwrappedValue;
10
+ }
8
11
  if (seen.has(unwrappedValue)) {
9
12
  return Array.isArray(unwrappedValue) ? [] : {};
10
13
  }
11
14
  seen.add(unwrappedValue);
12
- if (!isObject(unwrappedValue)) {
13
- return unwrappedValue;
14
- }
15
15
  if (Array.isArray(unwrappedValue)) {
16
16
  return unwrappedValue.map((value) => toRawDeep(value, seen));
17
17
  }
@@ -1,9 +1,19 @@
1
1
  import { defineStore } from "pinia";
2
2
  import { ref, computed } from "vue";
3
- import { createRouter, createWebHistory } from "vue-router";
3
+ import { createWebHistory, createWebHashHistory, createRouter } from "vue-router";
4
+ import { h as histoireConfig } from "./config.js";
4
5
  const base = import.meta.env.BASE_URL;
6
+ function createRouterHistory() {
7
+ switch (histoireConfig.routerMode) {
8
+ case "hash":
9
+ return createWebHashHistory(base);
10
+ case "history":
11
+ default:
12
+ return createWebHistory(base);
13
+ }
14
+ }
5
15
  const router = createRouter({
6
- history: createWebHistory(base),
16
+ history: createRouterHistory(),
7
17
  routes: [
8
18
  {
9
19
  path: "/",
@@ -1,7 +1,16 @@
1
- import { createRouter, createWebHistory } from 'vue-router';
1
+ import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
2
+ import { histoireConfig } from './util/config';
2
3
  export const base = import.meta.env.BASE_URL;
4
+ function createRouterHistory() {
5
+ switch (histoireConfig.routerMode) {
6
+ case 'hash': return createWebHashHistory(base);
7
+ case 'history':
8
+ default:
9
+ return createWebHistory(base);
10
+ }
11
+ }
3
12
  export const router = createRouter({
4
- history: createWebHistory(base),
13
+ history: createRouterHistory(),
5
14
  routes: [
6
15
  {
7
16
  path: '/',
@@ -3,5 +3,5 @@ export function getSandboxUrl(story, variant) {
3
3
  const url = new URLSearchParams();
4
4
  url.append('storyId', story.id);
5
5
  url.append('variantId', variant.id);
6
- return `${base}__sandbox?${url.toString()}`;
6
+ return `${base}__sandbox.html?${url.toString()}`;
7
7
  }
@@ -5,13 +5,13 @@ export function toRawDeep(val, seen = new Set()) {
5
5
  if (typeof unwrappedValue === 'symbol') {
6
6
  return unwrappedValue.toString();
7
7
  }
8
+ if (!isObject(unwrappedValue)) {
9
+ return unwrappedValue;
10
+ }
8
11
  if (seen.has(unwrappedValue)) {
9
12
  return Array.isArray(unwrappedValue) ? [] : {};
10
13
  }
11
14
  seen.add(unwrappedValue);
12
- if (!isObject(unwrappedValue)) {
13
- return unwrappedValue;
14
- }
15
15
  if (Array.isArray(unwrappedValue)) {
16
16
  return unwrappedValue.map(value => toRawDeep(value, seen));
17
17
  }
@@ -81,7 +81,7 @@ export async function build(ctx) {
81
81
  const vuePlugin = config.plugins.find((p) => p.name === 'vite:vue');
82
82
  if (vuePlugin) {
83
83
  const original = vuePlugin.configureServer.bind(vuePlugin);
84
- vuePlugin.configureServer = () => {
84
+ vuePlugin.configureServer = (s) => {
85
85
  original({
86
86
  ...server,
87
87
  config: {
@@ -93,10 +93,17 @@ export async function build(ctx) {
93
93
  },
94
94
  });
95
95
  };
96
- vuePlugin.configureServer();
96
+ vuePlugin.configureServer(server);
97
97
  }
98
98
  },
99
99
  });
100
+ buildViteConfig.plugins.push({
101
+ name: 'histoire-build-config-override',
102
+ config(config) {
103
+ // Don't externalize
104
+ config.build.rollupOptions.external = [];
105
+ },
106
+ });
100
107
  const results = await viteBuild(buildViteConfig);
101
108
  const result = Array.isArray(results) ? results[0] : results;
102
109
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset');
@@ -130,7 +137,7 @@ export async function build(ctx) {
130
137
  const query = new URLSearchParams();
131
138
  query.append('storyId', story.id);
132
139
  query.append('variantId', variant.id);
133
- const url = `${baseUrl}__sandbox?${query.toString()}`;
140
+ const url = `${baseUrl}__sandbox.html?${query.toString()}`;
134
141
  for (const fn of previewStoryCallbacks) {
135
142
  await fn({
136
143
  file: storyFile.path,
@@ -29,7 +29,7 @@ export async function devCommand(options) {
29
29
  async function restart(source) {
30
30
  if (stop) {
31
31
  console.log(pc.blue(`${source} config changed, restarting...`));
32
- stop();
32
+ await stop();
33
33
  stop = null; // Don't call stop again until new start() is done
34
34
  stop = await start();
35
35
  }
@@ -126,6 +126,12 @@ export interface HistoireConfig {
126
126
  * Customize the markdown-it renderer
127
127
  */
128
128
  markdown?: (md: MarkdownIt) => MarkdownIt | Promise<MarkdownIt>;
129
+ /**
130
+ * Change the router mode.
131
+ * - history: use HTML history with cleaner URLs
132
+ * - hash: use hashtag hack in the URL to support more hosting services
133
+ */
134
+ routerMode?: 'history' | 'hash';
129
135
  /**
130
136
  * Vite config override
131
137
  */
@@ -94,6 +94,12 @@ export function getDefaultConfig() {
94
94
  },
95
95
  ],
96
96
  sandboxDarkClass: 'dark',
97
+ routerMode: 'history',
98
+ vite: {
99
+ build: {
100
+ lib: false,
101
+ },
102
+ },
97
103
  };
98
104
  }
99
105
  export const configFileNames = [
@@ -4,6 +4,7 @@ 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
+ import { wrapLogError } from './util/log.js';
7
8
  export async function createServer(ctx, port) {
8
9
  const server = await createViteServer(await getViteConfigWithPlugins(false, ctx));
9
10
  await server.pluginContainer.buildStart({});
@@ -119,11 +120,11 @@ export async function createServer(ctx, port) {
119
120
  });
120
121
  async function close() {
121
122
  for (const cb of pluginOnCleanups) {
122
- await cb();
123
+ await wrapLogError('plugin.onDev.onCleanup', () => cb());
123
124
  }
124
- await server.close();
125
- await nodeServer.close();
126
- await destroyCollectStories();
125
+ await wrapLogError('server.close', () => server.close());
126
+ await wrapLogError('nodeServer', () => nodeServer.close());
127
+ await wrapLogError('destroyCollectStories', () => destroyCollectStories());
127
128
  }
128
129
  return {
129
130
  server,
@@ -0,0 +1 @@
1
+ export declare function wrapLogError(id: string, cb: () => unknown): Promise<void>;
@@ -0,0 +1,9 @@
1
+ import pc from 'picocolors';
2
+ export async function wrapLogError(id, cb) {
3
+ try {
4
+ await cb();
5
+ }
6
+ catch (e) {
7
+ console.error(pc.red(`[Error] ${id}: ${e}`));
8
+ }
9
+ }
package/dist/node/vite.js CHANGED
@@ -54,6 +54,11 @@ export async function getViteConfigWithPlugins(server, ctx) {
54
54
  name: 'histoire-vite-plugin',
55
55
  config() {
56
56
  return {
57
+ resolve: {
58
+ dedupe: [
59
+ 'vue',
60
+ ],
61
+ },
57
62
  optimizeDeps: {
58
63
  disabled: server,
59
64
  entries: [
@@ -79,7 +84,7 @@ export async function getViteConfigWithPlugins(server, ctx) {
79
84
  allow: [DIST_PATH, TEMP_PATH, resolvedViteConfig.root, process.cwd()],
80
85
  },
81
86
  watch: {
82
- ignored: [`!**/node_modules/.histoire/**`],
87
+ ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
83
88
  },
84
89
  },
85
90
  define: {
package/index.d.ts CHANGED
@@ -1,3 +1 @@
1
- import './src/components'
2
-
3
1
  export * from './dist/node/index'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.7.5",
3
+ "version": "0.7.8",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -33,9 +33,9 @@
33
33
  "histoire": "./bin.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@histoire/controls": "^0.7.5",
37
- "@histoire/plugin-vue": "^0.7.5",
38
- "@histoire/shared": "^0.7.5",
36
+ "@histoire/controls": "^0.7.8",
37
+ "@histoire/plugin-vue": "^0.7.8",
38
+ "@histoire/shared": "^0.7.8",
39
39
  "@iconify/vue": "^3.2.1",
40
40
  "@types/markdown-it": "^12.2.3",
41
41
  "@vueuse/core": "^8.2.5",
@@ -1,9 +1,19 @@
1
- import { createRouter, createWebHistory } from 'vue-router'
1
+ import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
2
+ import { histoireConfig } from './util/config'
2
3
 
3
4
  export const base = import.meta.env.BASE_URL as string
4
5
 
6
+ function createRouterHistory () {
7
+ switch (histoireConfig.routerMode) {
8
+ case 'hash': return createWebHashHistory(base)
9
+ case 'history':
10
+ default:
11
+ return createWebHistory(base)
12
+ }
13
+ }
14
+
5
15
  export const router = createRouter({
6
- history: createWebHistory(base),
16
+ history: createRouterHistory(),
7
17
  routes: [
8
18
  {
9
19
  path: '/',
@@ -6,5 +6,5 @@ export function getSandboxUrl (story: Story, variant: Variant) {
6
6
  const url = new URLSearchParams()
7
7
  url.append('storyId', story.id)
8
8
  url.append('variantId', variant.id)
9
- return `${base}__sandbox?${url.toString()}`
9
+ return `${base}__sandbox.html?${url.toString()}`
10
10
  }
@@ -9,16 +9,16 @@ export function toRawDeep (val, seen = new Set()) {
9
9
  return unwrappedValue.toString()
10
10
  }
11
11
 
12
+ if (!isObject(unwrappedValue)) {
13
+ return unwrappedValue
14
+ }
15
+
12
16
  if (seen.has(unwrappedValue)) {
13
17
  return Array.isArray(unwrappedValue) ? [] : {}
14
18
  }
15
19
 
16
20
  seen.add(unwrappedValue)
17
21
 
18
- if (!isObject(unwrappedValue)) {
19
- return unwrappedValue
20
- }
21
-
22
22
  if (Array.isArray(unwrappedValue)) {
23
23
  return unwrappedValue.map(value => toRawDeep(value, seen))
24
24
  }
package/src/node/build.ts CHANGED
@@ -2,8 +2,10 @@ import { join } from 'pathe'
2
2
  import {
3
3
  build as viteBuild,
4
4
  createServer as createViteServer,
5
+ InlineConfig as ViteInlineConfig,
5
6
  ResolvedConfig as ViteConfig,
6
7
  mergeConfig as mergeViteConfig,
8
+ Plugin as VitePlugin,
7
9
  } from 'vite'
8
10
  import fs from 'fs-extra'
9
11
  import { lookup as lookupMime } from 'mrmime'
@@ -65,7 +67,7 @@ export async function build (ctx: Context) {
65
67
  const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0)
66
68
  const emptyStoryCount = ctx.storyFiles.length - storyCount
67
69
 
68
- const buildViteConfig = mergeViteConfig(await getViteConfigWithPlugins(false, ctx), {
70
+ const buildViteConfig: ViteInlineConfig = mergeViteConfig(await getViteConfigWithPlugins(false, ctx), {
69
71
  mode: 'development',
70
72
  build: {
71
73
  rollupOptions: {
@@ -95,10 +97,10 @@ export async function build (ctx: Context) {
95
97
  buildViteConfig.plugins.push({
96
98
  name: 'histoire-vue-plugin-override',
97
99
  config (config) {
98
- const vuePlugin = config.plugins.find((p: any) => p.name === 'vite:vue')
100
+ const vuePlugin = config.plugins.find((p: any) => p.name === 'vite:vue') as VitePlugin
99
101
  if (vuePlugin) {
100
102
  const original = vuePlugin.configureServer.bind(vuePlugin)
101
- vuePlugin.configureServer = () => {
103
+ vuePlugin.configureServer = (s) => {
102
104
  original({
103
105
  ...server,
104
106
  config: {
@@ -110,11 +112,19 @@ export async function build (ctx: Context) {
110
112
  },
111
113
  })
112
114
  }
113
- vuePlugin.configureServer()
115
+ vuePlugin.configureServer(server)
114
116
  }
115
117
  },
116
118
  })
117
119
 
120
+ buildViteConfig.plugins.push({
121
+ name: 'histoire-build-config-override',
122
+ config (config) {
123
+ // Don't externalize
124
+ config.build.rollupOptions.external = []
125
+ },
126
+ })
127
+
118
128
  const results = await viteBuild(buildViteConfig)
119
129
  const result = Array.isArray(results) ? results[0] : results as RollupOutput
120
130
 
@@ -155,7 +165,7 @@ export async function build (ctx: Context) {
155
165
  const query = new URLSearchParams()
156
166
  query.append('storyId', story.id)
157
167
  query.append('variantId', variant.id)
158
- const url = `${baseUrl}__sandbox?${query.toString()}`
168
+ const url = `${baseUrl}__sandbox.html?${query.toString()}`
159
169
  for (const fn of previewStoryCallbacks) {
160
170
  await fn({
161
171
  file: storyFile.path,
@@ -38,7 +38,7 @@ export async function devCommand (options: DevOptions) {
38
38
  async function restart (source: string) {
39
39
  if (stop) {
40
40
  console.log(pc.blue(`${source} config changed, restarting...`))
41
- stop()
41
+ await stop()
42
42
  stop = null // Don't call stop again until new start() is done
43
43
  stop = await start()
44
44
  }
@@ -145,6 +145,12 @@ export interface HistoireConfig {
145
145
  * Customize the markdown-it renderer
146
146
  */
147
147
  markdown?: (md: MarkdownIt) => MarkdownIt | Promise<MarkdownIt>
148
+ /**
149
+ * Change the router mode.
150
+ * - history: use HTML history with cleaner URLs
151
+ * - hash: use hashtag hack in the URL to support more hosting services
152
+ */
153
+ routerMode?: 'history' | 'hash'
148
154
  /**
149
155
  * Vite config override
150
156
  */
@@ -264,6 +270,12 @@ export function getDefaultConfig (): HistoireConfig {
264
270
  },
265
271
  ],
266
272
  sandboxDarkClass: 'dark',
273
+ routerMode: 'history',
274
+ vite: {
275
+ build: {
276
+ lib: false,
277
+ },
278
+ },
267
279
  }
268
280
  }
269
281
 
@@ -6,6 +6,7 @@ import { onStoryChange, watchStories } from './stories.js'
6
6
  import { useCollectStories } from './collect/index.js'
7
7
  import { DevPluginApi } from './plugin.js'
8
8
  import { useModuleLoader } from './load.js'
9
+ import { wrapLogError } from './util/log.js'
9
10
 
10
11
  export async function createServer (ctx: Context, port: number) {
11
12
  const server = await createViteServer(await getViteConfigWithPlugins(false, ctx))
@@ -143,11 +144,11 @@ export async function createServer (ctx: Context, port: number) {
143
144
 
144
145
  async function close () {
145
146
  for (const cb of pluginOnCleanups) {
146
- await cb()
147
+ await wrapLogError('plugin.onDev.onCleanup', () => cb())
147
148
  }
148
- await server.close()
149
- await nodeServer.close()
150
- await destroyCollectStories()
149
+ await wrapLogError('server.close', () => server.close())
150
+ await wrapLogError('nodeServer', () => nodeServer.close())
151
+ await wrapLogError('destroyCollectStories', () => destroyCollectStories())
151
152
  }
152
153
 
153
154
  return {
@@ -0,0 +1,9 @@
1
+ import pc from 'picocolors'
2
+
3
+ export async function wrapLogError (id: string, cb: () => unknown) {
4
+ try {
5
+ await cb()
6
+ } catch (e) {
7
+ console.error(pc.red(`[Error] ${id}: ${e}`))
8
+ }
9
+ }
package/src/node/vite.ts CHANGED
@@ -70,6 +70,11 @@ export async function getViteConfigWithPlugins (server: boolean, ctx: Context):
70
70
 
71
71
  config () {
72
72
  return {
73
+ resolve: {
74
+ dedupe: [
75
+ 'vue',
76
+ ],
77
+ },
73
78
  optimizeDeps: {
74
79
  disabled: server,
75
80
  entries: [
@@ -95,7 +100,7 @@ export async function getViteConfigWithPlugins (server: boolean, ctx: Context):
95
100
  allow: [DIST_PATH, TEMP_PATH, resolvedViteConfig.root, process.cwd()],
96
101
  },
97
102
  watch: {
98
- ignored: [`!**/node_modules/.histoire/**`],
103
+ ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
99
104
  },
100
105
  },
101
106
  define: {
package/vue.d.ts ADDED
@@ -0,0 +1 @@
1
+ import '@histoire/plugin-vue/components'