histoire 0.4.4 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundled/global-components.js +11 -1
- package/dist/bundled/index2.js +3 -0
- package/dist/bundled/story.js +1 -1
- package/dist/client/app/index.js +5 -0
- package/dist/client/app/util/mapping.js +11 -1
- package/dist/node/build.js +3 -3
- package/dist/node/collect/worker.js +0 -4
- package/dist/node/commands/preview.js +1 -1
- package/dist/node/server.js +53 -45
- package/dist/node/vite.d.ts +1 -1
- package/dist/node/vite.js +10 -2
- package/package.json +3 -3
- package/src/client/app/index.ts +6 -0
- package/src/client/app/util/mapping.ts +12 -1
- package/src/node/build.ts +3 -3
- package/src/node/collect/worker.ts +0 -4
- package/src/node/commands/preview.ts +1 -1
- package/src/node/server.ts +55 -47
- package/src/node/vite.ts +11 -2
- package/vite.config.ts +25 -0
|
@@ -19,6 +19,16 @@ var __spreadValues = (a, b) => {
|
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
20
|
import { markRaw, defineComponent, useAttrs, getCurrentInstance, inject, watch, computed, provide, h } from "vue";
|
|
21
21
|
import { registerVueComponents } from "@histoire/controls";
|
|
22
|
+
const copiedFromExistingVariant = [
|
|
23
|
+
"state",
|
|
24
|
+
"slots",
|
|
25
|
+
"initState",
|
|
26
|
+
"source",
|
|
27
|
+
"responsiveDisabled",
|
|
28
|
+
"setupApp",
|
|
29
|
+
"configReady",
|
|
30
|
+
"previewReady"
|
|
31
|
+
];
|
|
22
32
|
function mapFile(file, existingFile) {
|
|
23
33
|
const result = __spreadProps(__spreadValues({}, file), {
|
|
24
34
|
component: markRaw(file.component),
|
|
@@ -34,7 +44,7 @@ function mapFile(file, existingFile) {
|
|
|
34
44
|
const variant = result.story.variants[index];
|
|
35
45
|
const existingVariant = existingFile.story.variants[index];
|
|
36
46
|
if (existingVariant) {
|
|
37
|
-
for (const key
|
|
47
|
+
for (const key of copiedFromExistingVariant) {
|
|
38
48
|
if (typeof existingVariant[key] !== "undefined") {
|
|
39
49
|
if (key === "state") {
|
|
40
50
|
variant.state = existingVariant.state;
|
package/dist/bundled/index2.js
CHANGED
package/dist/bundled/story.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defineStore } from "pinia";
|
|
2
2
|
import { ref, computed } from "vue";
|
|
3
3
|
import { createRouter, createWebHistory } from "vue-router";
|
|
4
|
-
const base =
|
|
4
|
+
const base = import.meta.env.BASE_URL;
|
|
5
5
|
const router = createRouter({
|
|
6
6
|
history: createWebHistory(base),
|
|
7
7
|
routes: [
|
package/dist/client/app/index.js
CHANGED
|
@@ -26,4 +26,9 @@ 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
|
+
if (import.meta.hot) {
|
|
31
|
+
// @ts-expect-error vite hot api
|
|
32
|
+
import.meta.hot.send('histoire:mount', {});
|
|
33
|
+
}
|
|
29
34
|
}
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { markRaw } from 'vue';
|
|
2
|
+
const copiedFromExistingVariant = [
|
|
3
|
+
'state',
|
|
4
|
+
'slots',
|
|
5
|
+
'initState',
|
|
6
|
+
'source',
|
|
7
|
+
'responsiveDisabled',
|
|
8
|
+
'setupApp',
|
|
9
|
+
'configReady',
|
|
10
|
+
'previewReady',
|
|
11
|
+
];
|
|
2
12
|
export function mapFile(file, existingFile) {
|
|
3
13
|
const result = {
|
|
4
14
|
...file,
|
|
@@ -16,7 +26,7 @@ export function mapFile(file, existingFile) {
|
|
|
16
26
|
const variant = result.story.variants[index];
|
|
17
27
|
const existingVariant = existingFile.story.variants[index];
|
|
18
28
|
if (existingVariant) {
|
|
19
|
-
for (const key
|
|
29
|
+
for (const key of copiedFromExistingVariant) {
|
|
20
30
|
if (typeof existingVariant[key] !== 'undefined') {
|
|
21
31
|
if (key === 'state') {
|
|
22
32
|
variant.state = existingVariant.state;
|
package/dist/node/build.js
CHANGED
|
@@ -23,7 +23,7 @@ export async function build(ctx) {
|
|
|
23
23
|
const startTime = performance.now();
|
|
24
24
|
await findAllStories(ctx);
|
|
25
25
|
const server = await createViteServer({
|
|
26
|
-
plugins: await createVitePlugins(ctx),
|
|
26
|
+
plugins: await createVitePlugins(true, ctx),
|
|
27
27
|
});
|
|
28
28
|
await server.pluginContainer.buildStart({});
|
|
29
29
|
const moduleLoader = useModuleLoader({
|
|
@@ -53,7 +53,7 @@ export async function build(ctx) {
|
|
|
53
53
|
const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0);
|
|
54
54
|
const emptyStoryCount = ctx.storyFiles.length - storyCount;
|
|
55
55
|
const results = await viteBuild({
|
|
56
|
-
plugins: await createVitePlugins(ctx),
|
|
56
|
+
plugins: await createVitePlugins(false, ctx),
|
|
57
57
|
build: {
|
|
58
58
|
rollupOptions: {
|
|
59
59
|
input: [
|
|
@@ -76,7 +76,7 @@ export async function build(ctx) {
|
|
|
76
76
|
});
|
|
77
77
|
const result = Array.isArray(results) ? results[0] : results;
|
|
78
78
|
const resolvedViteConfig = await resolveViteConfig({
|
|
79
|
-
plugins: await createVitePlugins(ctx),
|
|
79
|
+
plugins: await createVitePlugins(false, ctx),
|
|
80
80
|
}, 'build');
|
|
81
81
|
const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset');
|
|
82
82
|
// Preload
|
|
@@ -8,7 +8,7 @@ export async function previewCommand(options) {
|
|
|
8
8
|
mode: 'build',
|
|
9
9
|
});
|
|
10
10
|
const viteConfig = await resolveViteConfig({
|
|
11
|
-
plugins: await createVitePlugins(ctx),
|
|
11
|
+
plugins: await createVitePlugins(false, ctx),
|
|
12
12
|
}, 'build');
|
|
13
13
|
const { baseUrl } = await startPreview(viteConfig, options.port, ctx);
|
|
14
14
|
console.log(`Preview server listening on ${pc.cyan(baseUrl)}`);
|
package/dist/node/server.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
import { createServer as createViteServer } from 'vite';
|
|
2
2
|
import { createVitePlugins, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js';
|
|
3
|
-
import {
|
|
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
8
|
const server = await createViteServer({
|
|
9
|
-
plugins: await createVitePlugins(ctx),
|
|
10
|
-
server: {
|
|
11
|
-
watch: {
|
|
12
|
-
usePolling: true,
|
|
13
|
-
},
|
|
14
|
-
},
|
|
9
|
+
plugins: await createVitePlugins(false, ctx),
|
|
15
10
|
});
|
|
16
11
|
await server.pluginContainer.buildStart({});
|
|
12
|
+
const nodeServer = await createViteServer({
|
|
13
|
+
plugins: await createVitePlugins(true, ctx),
|
|
14
|
+
});
|
|
15
|
+
await nodeServer.pluginContainer.buildStart({});
|
|
17
16
|
const moduleLoader = useModuleLoader({
|
|
18
|
-
server,
|
|
17
|
+
server: nodeServer,
|
|
19
18
|
});
|
|
20
19
|
const pluginOnCleanups = [];
|
|
21
20
|
for (const plugin of ctx.config.plugins) {
|
|
@@ -31,17 +30,49 @@ export async function createServer(ctx, port) {
|
|
|
31
30
|
// Wait for pre-bundling (in `listen()`)
|
|
32
31
|
await server.listen(port);
|
|
33
32
|
const { clearCache, executeStoryFile, destroy: destroyCollectStories, } = useCollectStories({
|
|
34
|
-
server,
|
|
33
|
+
server: nodeServer,
|
|
35
34
|
}, ctx);
|
|
36
35
|
// onStoryChange debouncing
|
|
37
36
|
let queue;
|
|
38
37
|
let queueResolve;
|
|
39
38
|
let queued = false;
|
|
40
39
|
let queuedFiles = [];
|
|
40
|
+
let queuedAll = false;
|
|
41
|
+
let didAllStoriesYet = false;
|
|
42
|
+
// Invalidate modules
|
|
43
|
+
const invalidateModule = (id) => {
|
|
44
|
+
const mod = server.moduleGraph.getModuleById(id);
|
|
45
|
+
if (!mod) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
server.moduleGraph.invalidateModule(mod);
|
|
49
|
+
// Send HMR update
|
|
50
|
+
const timestamp = Date.now();
|
|
51
|
+
mod.lastHMRTimestamp = timestamp;
|
|
52
|
+
server.ws.send({
|
|
53
|
+
type: 'update',
|
|
54
|
+
updates: [
|
|
55
|
+
{
|
|
56
|
+
type: 'js-update',
|
|
57
|
+
acceptedPath: mod.url,
|
|
58
|
+
path: mod.url,
|
|
59
|
+
timestamp: timestamp,
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
};
|
|
41
64
|
onStoryChange(async (changedFile) => {
|
|
65
|
+
if (changedFile && !didAllStoriesYet) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
42
68
|
if (queue) {
|
|
43
|
-
if (queued
|
|
44
|
-
|
|
69
|
+
if (queued) {
|
|
70
|
+
if (changedFile) {
|
|
71
|
+
queuedFiles.push(changedFile);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
queuedAll = true;
|
|
75
|
+
}
|
|
45
76
|
return;
|
|
46
77
|
}
|
|
47
78
|
else {
|
|
@@ -55,44 +86,27 @@ export async function createServer(ctx, port) {
|
|
|
55
86
|
queueResolve = resolve;
|
|
56
87
|
});
|
|
57
88
|
clearCache();
|
|
58
|
-
|
|
89
|
+
console.log('Collect stories start', changedFile?.path ?? 'all');
|
|
90
|
+
const time = Date.now();
|
|
91
|
+
if (changedFile && !queuedAll) {
|
|
59
92
|
// Granular update
|
|
60
93
|
await executeStoryFile(changedFile);
|
|
94
|
+
// Queued updates
|
|
95
|
+
for (const storyFile of queuedFiles) {
|
|
96
|
+
await executeStoryFile(storyFile);
|
|
97
|
+
}
|
|
61
98
|
}
|
|
62
99
|
else {
|
|
63
100
|
// Full update
|
|
64
101
|
for (const storyFile of ctx.storyFiles) {
|
|
65
102
|
await executeStoryFile(storyFile);
|
|
66
103
|
}
|
|
104
|
+
didAllStoriesYet = true;
|
|
67
105
|
}
|
|
68
|
-
|
|
69
|
-
for (const storyFile of queuedFiles) {
|
|
70
|
-
await executeStoryFile(storyFile);
|
|
71
|
-
}
|
|
106
|
+
console.log('Collect stories end', Date.now() - time, 'ms');
|
|
72
107
|
queuedFiles = [];
|
|
108
|
+
queuedAll = false;
|
|
73
109
|
queueResolve();
|
|
74
|
-
// Invalidate modules
|
|
75
|
-
function invalidateModule(id) {
|
|
76
|
-
const mod = server.moduleGraph.getModuleById(id);
|
|
77
|
-
if (!mod) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
server.moduleGraph.invalidateModule(mod);
|
|
81
|
-
// Send HMR update
|
|
82
|
-
const timestamp = Date.now();
|
|
83
|
-
mod.lastHMRTimestamp = timestamp;
|
|
84
|
-
server.ws.send({
|
|
85
|
-
type: 'update',
|
|
86
|
-
updates: [
|
|
87
|
-
{
|
|
88
|
-
type: 'js-update',
|
|
89
|
-
acceptedPath: mod.url,
|
|
90
|
-
path: mod.url,
|
|
91
|
-
timestamp: timestamp,
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
110
|
invalidateModule(RESOLVED_STORIES_ID);
|
|
97
111
|
invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID);
|
|
98
112
|
});
|
|
@@ -101,15 +115,9 @@ export async function createServer(ctx, port) {
|
|
|
101
115
|
await cb();
|
|
102
116
|
}
|
|
103
117
|
await server.close();
|
|
118
|
+
await nodeServer.close();
|
|
104
119
|
await destroyCollectStories();
|
|
105
120
|
}
|
|
106
|
-
// On page refresh, refresh stories
|
|
107
|
-
// Useful when vite optimizes new dependencies
|
|
108
|
-
server.ws.on('connection', (socket) => {
|
|
109
|
-
if (socket.protocol === 'vite-hmr') {
|
|
110
|
-
notifyStoryChange();
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
121
|
return {
|
|
114
122
|
server,
|
|
115
123
|
close,
|
package/dist/node/vite.d.ts
CHANGED
|
@@ -12,4 +12,4 @@ 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(ctx: Context): Promise<VitePlugin[]>;
|
|
15
|
+
export declare function createVitePlugins(server: boolean, ctx: Context): Promise<VitePlugin[]>;
|
package/dist/node/vite.js
CHANGED
|
@@ -21,7 +21,7 @@ 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(ctx) {
|
|
24
|
+
export async function createVitePlugins(server, ctx) {
|
|
25
25
|
const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build');
|
|
26
26
|
const plugins = [];
|
|
27
27
|
if (ctx.config.vite) {
|
|
@@ -37,7 +37,12 @@ export async function createVitePlugins(ctx) {
|
|
|
37
37
|
config() {
|
|
38
38
|
return {
|
|
39
39
|
optimizeDeps: {
|
|
40
|
-
|
|
40
|
+
disabled: server,
|
|
41
|
+
entries: [
|
|
42
|
+
`${APP_PATH}/bundle-main.js`,
|
|
43
|
+
`${APP_PATH}/bundle-sandbox.js`,
|
|
44
|
+
`${APP_PATH}/server/index.js`,
|
|
45
|
+
],
|
|
41
46
|
include: [
|
|
42
47
|
'vue',
|
|
43
48
|
dirname(require.resolve('@vue/runtime-core/package.json')),
|
|
@@ -160,6 +165,9 @@ if (import.meta.hot) {
|
|
|
160
165
|
}
|
|
161
166
|
},
|
|
162
167
|
configureServer(server) {
|
|
168
|
+
server.ws.on('histoire:mount', () => {
|
|
169
|
+
notifyStoryChange();
|
|
170
|
+
});
|
|
163
171
|
server.middlewares.use(async (req, res, next) => {
|
|
164
172
|
if (req.url.startsWith(`${server.config.base}__sandbox`)) {
|
|
165
173
|
res.statusCode = 200;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "histoire",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
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.4.
|
|
36
|
+
"@histoire/controls": "^0.4.5",
|
|
37
37
|
"@iconify/vue": "^3.2.1",
|
|
38
38
|
"@types/markdown-it": "^12.2.3",
|
|
39
39
|
"@vueuse/core": "^8.2.5",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"shiki": "^0.10.1",
|
|
62
62
|
"sirv": "^2.0.2",
|
|
63
63
|
"tinypool": "^0.1.2",
|
|
64
|
-
"vite-node": "0.
|
|
64
|
+
"vite-node": "0.12.1",
|
|
65
65
|
"vue-router": "^4.0.14",
|
|
66
66
|
"vue": "^3.2.31"
|
|
67
67
|
},
|
package/src/client/app/index.ts
CHANGED
|
@@ -29,4 +29,10 @@ export function mountMainApp () {
|
|
|
29
29
|
app.use(router)
|
|
30
30
|
registerGlobalComponents(app)
|
|
31
31
|
app.mount('#app')
|
|
32
|
+
|
|
33
|
+
// @ts-expect-error vite hot api
|
|
34
|
+
if (import.meta.hot) {
|
|
35
|
+
// @ts-expect-error vite hot api
|
|
36
|
+
import.meta.hot.send('histoire:mount', {})
|
|
37
|
+
}
|
|
32
38
|
}
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { markRaw } from 'vue'
|
|
2
2
|
import type { StoryFile, Variant } from '../types'
|
|
3
3
|
|
|
4
|
+
const copiedFromExistingVariant = [
|
|
5
|
+
'state',
|
|
6
|
+
'slots',
|
|
7
|
+
'initState',
|
|
8
|
+
'source',
|
|
9
|
+
'responsiveDisabled',
|
|
10
|
+
'setupApp',
|
|
11
|
+
'configReady',
|
|
12
|
+
'previewReady',
|
|
13
|
+
]
|
|
14
|
+
|
|
4
15
|
export function mapFile (file: StoryFile, existingFile?: StoryFile): StoryFile {
|
|
5
16
|
const result: StoryFile = {
|
|
6
17
|
...file,
|
|
@@ -19,7 +30,7 @@ export function mapFile (file: StoryFile, existingFile?: StoryFile): StoryFile {
|
|
|
19
30
|
const variant = result.story.variants[index]
|
|
20
31
|
const existingVariant = existingFile.story.variants[index]
|
|
21
32
|
if (existingVariant) {
|
|
22
|
-
for (const key
|
|
33
|
+
for (const key of copiedFromExistingVariant) {
|
|
23
34
|
if (typeof existingVariant[key] !== 'undefined') {
|
|
24
35
|
if (key === 'state') {
|
|
25
36
|
variant.state = existingVariant.state
|
package/src/node/build.ts
CHANGED
|
@@ -29,7 +29,7 @@ export async function build (ctx: Context) {
|
|
|
29
29
|
await findAllStories(ctx)
|
|
30
30
|
|
|
31
31
|
const server = await createViteServer({
|
|
32
|
-
plugins: await createVitePlugins(ctx),
|
|
32
|
+
plugins: await createVitePlugins(true, ctx),
|
|
33
33
|
})
|
|
34
34
|
await server.pluginContainer.buildStart({})
|
|
35
35
|
|
|
@@ -63,7 +63,7 @@ export async function build (ctx: Context) {
|
|
|
63
63
|
const emptyStoryCount = ctx.storyFiles.length - storyCount
|
|
64
64
|
|
|
65
65
|
const results = await viteBuild({
|
|
66
|
-
plugins: await createVitePlugins(ctx),
|
|
66
|
+
plugins: await createVitePlugins(false, ctx),
|
|
67
67
|
build: {
|
|
68
68
|
rollupOptions: {
|
|
69
69
|
input: [
|
|
@@ -87,7 +87,7 @@ export async function build (ctx: Context) {
|
|
|
87
87
|
const result = Array.isArray(results) ? results[0] : results as RollupOutput
|
|
88
88
|
|
|
89
89
|
const resolvedViteConfig = await resolveViteConfig({
|
|
90
|
-
plugins: await createVitePlugins(ctx),
|
|
90
|
+
plugins: await createVitePlugins(false, ctx),
|
|
91
91
|
}, 'build')
|
|
92
92
|
|
|
93
93
|
const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset')
|
|
@@ -34,10 +34,6 @@ export default async (payload: Payload): Promise<ReturnData> => {
|
|
|
34
34
|
root: payload.root,
|
|
35
35
|
base: payload.base,
|
|
36
36
|
fetchModule (id) {
|
|
37
|
-
// Alias
|
|
38
|
-
if (id.endsWith('vue.runtime.esm-bundler.js')) {
|
|
39
|
-
id = 'vue'
|
|
40
|
-
}
|
|
41
37
|
return rpc.fetchModule(id)
|
|
42
38
|
},
|
|
43
39
|
resolveId (id, importer) {
|
|
@@ -13,7 +13,7 @@ export async function previewCommand (options: PreviewOptions) {
|
|
|
13
13
|
mode: 'build',
|
|
14
14
|
})
|
|
15
15
|
const viteConfig = await resolveViteConfig({
|
|
16
|
-
plugins: await createVitePlugins(ctx),
|
|
16
|
+
plugins: await createVitePlugins(false, ctx),
|
|
17
17
|
}, 'build')
|
|
18
18
|
|
|
19
19
|
const { baseUrl } = await startPreview(viteConfig, options.port, ctx)
|
package/src/node/server.ts
CHANGED
|
@@ -9,17 +9,17 @@ import { useModuleLoader } from './load.js'
|
|
|
9
9
|
|
|
10
10
|
export async function createServer (ctx: Context, port: number) {
|
|
11
11
|
const server = await createViteServer({
|
|
12
|
-
plugins: await createVitePlugins(ctx),
|
|
13
|
-
server: {
|
|
14
|
-
watch: {
|
|
15
|
-
usePolling: true,
|
|
16
|
-
},
|
|
17
|
-
},
|
|
12
|
+
plugins: await createVitePlugins(false, ctx),
|
|
18
13
|
})
|
|
19
14
|
await server.pluginContainer.buildStart({})
|
|
20
15
|
|
|
16
|
+
const nodeServer = await createViteServer({
|
|
17
|
+
plugins: await createVitePlugins(true, ctx),
|
|
18
|
+
})
|
|
19
|
+
await nodeServer.pluginContainer.buildStart({})
|
|
20
|
+
|
|
21
21
|
const moduleLoader = useModuleLoader({
|
|
22
|
-
server,
|
|
22
|
+
server: nodeServer,
|
|
23
23
|
})
|
|
24
24
|
|
|
25
25
|
const pluginOnCleanups: (() => void | Promise<void>)[] = []
|
|
@@ -43,7 +43,7 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
43
43
|
executeStoryFile,
|
|
44
44
|
destroy: destroyCollectStories,
|
|
45
45
|
} = useCollectStories({
|
|
46
|
-
server,
|
|
46
|
+
server: nodeServer,
|
|
47
47
|
}, ctx)
|
|
48
48
|
|
|
49
49
|
// onStoryChange debouncing
|
|
@@ -51,11 +51,44 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
51
51
|
let queueResolve: () => void
|
|
52
52
|
let queued = false
|
|
53
53
|
let queuedFiles: StoryFile[] = []
|
|
54
|
+
let queuedAll = false
|
|
55
|
+
let didAllStoriesYet = false
|
|
56
|
+
|
|
57
|
+
// Invalidate modules
|
|
58
|
+
const invalidateModule = (id: string) => {
|
|
59
|
+
const mod = server.moduleGraph.getModuleById(id)
|
|
60
|
+
if (!mod) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
server.moduleGraph.invalidateModule(mod)
|
|
64
|
+
|
|
65
|
+
// Send HMR update
|
|
66
|
+
const timestamp = Date.now()
|
|
67
|
+
mod.lastHMRTimestamp = timestamp
|
|
68
|
+
server.ws.send({
|
|
69
|
+
type: 'update',
|
|
70
|
+
updates: [
|
|
71
|
+
{
|
|
72
|
+
type: 'js-update',
|
|
73
|
+
acceptedPath: mod.url,
|
|
74
|
+
path: mod.url,
|
|
75
|
+
timestamp: timestamp,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
})
|
|
79
|
+
}
|
|
54
80
|
|
|
55
81
|
onStoryChange(async (changedFile) => {
|
|
82
|
+
if (changedFile && !didAllStoriesYet) {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
56
85
|
if (queue) {
|
|
57
|
-
if (queued
|
|
58
|
-
|
|
86
|
+
if (queued) {
|
|
87
|
+
if (changedFile) {
|
|
88
|
+
queuedFiles.push(changedFile)
|
|
89
|
+
} else {
|
|
90
|
+
queuedAll = true
|
|
91
|
+
}
|
|
59
92
|
return
|
|
60
93
|
} else {
|
|
61
94
|
queued = true
|
|
@@ -70,47 +103,29 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
70
103
|
|
|
71
104
|
clearCache()
|
|
72
105
|
|
|
73
|
-
|
|
106
|
+
console.log('Collect stories start', changedFile?.path ?? 'all')
|
|
107
|
+
const time = Date.now()
|
|
108
|
+
if (changedFile && !queuedAll) {
|
|
74
109
|
// Granular update
|
|
75
110
|
await executeStoryFile(changedFile)
|
|
111
|
+
|
|
112
|
+
// Queued updates
|
|
113
|
+
for (const storyFile of queuedFiles) {
|
|
114
|
+
await executeStoryFile(storyFile)
|
|
115
|
+
}
|
|
76
116
|
} else {
|
|
77
117
|
// Full update
|
|
78
118
|
for (const storyFile of ctx.storyFiles) {
|
|
79
119
|
await executeStoryFile(storyFile)
|
|
80
120
|
}
|
|
121
|
+
didAllStoriesYet = true
|
|
81
122
|
}
|
|
82
|
-
|
|
83
|
-
for (const storyFile of queuedFiles) {
|
|
84
|
-
await executeStoryFile(storyFile)
|
|
85
|
-
}
|
|
123
|
+
console.log('Collect stories end', Date.now() - time, 'ms')
|
|
86
124
|
|
|
87
125
|
queuedFiles = []
|
|
126
|
+
queuedAll = false
|
|
88
127
|
queueResolve()
|
|
89
128
|
|
|
90
|
-
// Invalidate modules
|
|
91
|
-
function invalidateModule (id: string) {
|
|
92
|
-
const mod = server.moduleGraph.getModuleById(id)
|
|
93
|
-
if (!mod) {
|
|
94
|
-
return
|
|
95
|
-
}
|
|
96
|
-
server.moduleGraph.invalidateModule(mod)
|
|
97
|
-
|
|
98
|
-
// Send HMR update
|
|
99
|
-
const timestamp = Date.now()
|
|
100
|
-
mod.lastHMRTimestamp = timestamp
|
|
101
|
-
server.ws.send({
|
|
102
|
-
type: 'update',
|
|
103
|
-
updates: [
|
|
104
|
-
{
|
|
105
|
-
type: 'js-update',
|
|
106
|
-
acceptedPath: mod.url,
|
|
107
|
-
path: mod.url,
|
|
108
|
-
timestamp: timestamp,
|
|
109
|
-
},
|
|
110
|
-
],
|
|
111
|
-
})
|
|
112
|
-
}
|
|
113
|
-
|
|
114
129
|
invalidateModule(RESOLVED_STORIES_ID)
|
|
115
130
|
invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID)
|
|
116
131
|
})
|
|
@@ -120,17 +135,10 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
120
135
|
await cb()
|
|
121
136
|
}
|
|
122
137
|
await server.close()
|
|
138
|
+
await nodeServer.close()
|
|
123
139
|
await destroyCollectStories()
|
|
124
140
|
}
|
|
125
141
|
|
|
126
|
-
// On page refresh, refresh stories
|
|
127
|
-
// Useful when vite optimizes new dependencies
|
|
128
|
-
server.ws.on('connection', (socket) => {
|
|
129
|
-
if (socket.protocol === 'vite-hmr') {
|
|
130
|
-
notifyStoryChange()
|
|
131
|
-
}
|
|
132
|
-
})
|
|
133
|
-
|
|
134
142
|
return {
|
|
135
143
|
server,
|
|
136
144
|
close,
|
package/src/node/vite.ts
CHANGED
|
@@ -25,7 +25,7 @@ export const RESOLVED_SEARCH_TITLE_DATA_ID = `/${SEARCH_TITLE_DATA_ID}-resolved`
|
|
|
25
25
|
export const SEARCH_DOCS_DATA_ID = '$histoire-search-docs-data'
|
|
26
26
|
export const RESOLVED_SEARCH_DOCS_DATA_ID = `/${SEARCH_DOCS_DATA_ID}-resolved`
|
|
27
27
|
|
|
28
|
-
export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
|
|
28
|
+
export async function createVitePlugins (server: boolean, ctx: Context): Promise<VitePlugin[]> {
|
|
29
29
|
const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build')
|
|
30
30
|
|
|
31
31
|
const plugins: VitePlugin[] = []
|
|
@@ -45,7 +45,12 @@ export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
|
|
|
45
45
|
config () {
|
|
46
46
|
return {
|
|
47
47
|
optimizeDeps: {
|
|
48
|
-
|
|
48
|
+
disabled: server,
|
|
49
|
+
entries: [
|
|
50
|
+
`${APP_PATH}/bundle-main.js`,
|
|
51
|
+
`${APP_PATH}/bundle-sandbox.js`,
|
|
52
|
+
`${APP_PATH}/server/index.js`,
|
|
53
|
+
],
|
|
49
54
|
include: [
|
|
50
55
|
'vue',
|
|
51
56
|
dirname(require.resolve('@vue/runtime-core/package.json')),
|
|
@@ -171,6 +176,10 @@ if (import.meta.hot) {
|
|
|
171
176
|
},
|
|
172
177
|
|
|
173
178
|
configureServer (server) {
|
|
179
|
+
server.ws.on('histoire:mount', () => {
|
|
180
|
+
notifyStoryChange()
|
|
181
|
+
})
|
|
182
|
+
|
|
174
183
|
server.middlewares.use(async (req, res, next) => {
|
|
175
184
|
if (req.url!.startsWith(`${server.config.base}__sandbox`)) {
|
|
176
185
|
res.statusCode = 200
|
package/vite.config.ts
CHANGED
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import { defineConfig } from 'vite'
|
|
2
2
|
import vue from '@vitejs/plugin-vue'
|
|
3
|
+
import fs from 'fs-extra'
|
|
4
|
+
import path from 'pathe'
|
|
3
5
|
|
|
4
6
|
export default defineConfig({
|
|
5
7
|
plugins: [
|
|
6
8
|
vue(),
|
|
9
|
+
{
|
|
10
|
+
name: 'histoire:preserve:import.meta',
|
|
11
|
+
enforce: 'pre',
|
|
12
|
+
transform (code) {
|
|
13
|
+
if (code.includes('import.meta')) {
|
|
14
|
+
return {
|
|
15
|
+
code: code.replace(/import\.meta/g, 'import__meta'),
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
closeBundle () {
|
|
20
|
+
const files = fs.readdirSync('./dist/bundled')
|
|
21
|
+
for (const file of files) {
|
|
22
|
+
if (file.endsWith('.js')) {
|
|
23
|
+
const filePath = path.join('./dist/bundled', file)
|
|
24
|
+
const content = fs.readFileSync(filePath, 'utf-8')
|
|
25
|
+
if (content.includes('import__meta')) {
|
|
26
|
+
fs.writeFileSync(filePath, content.replace(/import__meta/g, 'import.meta'), 'utf-8')
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
},
|
|
7
32
|
],
|
|
8
33
|
|
|
9
34
|
build: {
|