histoire 0.4.2 → 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.
@@ -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 && changedFile) {
58
- queuedFiles.push(changedFile)
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
- if (changedFile) {
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
- // Queued updates
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,9 +45,15 @@ export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
45
45
  config () {
46
46
  return {
47
47
  optimizeDeps: {
48
- // force include vue to avoid duplicated copies when linked + optimized
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',
56
+ dirname(require.resolve('@vue/runtime-core/package.json')),
51
57
  dirname(require.resolve('shiki/package.json')),
52
58
  dirname(require.resolve('vscode-textmate/package.json')),
53
59
  dirname(require.resolve('vscode-oniguruma/package.json')),
@@ -170,6 +176,10 @@ if (import.meta.hot) {
170
176
  },
171
177
 
172
178
  configureServer (server) {
179
+ server.ws.on('histoire:mount', () => {
180
+ notifyStoryChange()
181
+ })
182
+
173
183
  server.middlewares.use(async (req, res, next) => {
174
184
  if (req.url!.startsWith(`${server.config.base}__sandbox`)) {
175
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: {
@@ -16,6 +41,7 @@ export default defineConfig({
16
41
  rollupOptions: {
17
42
  external: [
18
43
  /\$histoire/,
44
+ /@vue\/*/,
19
45
  // eslint-disable-next-line @typescript-eslint/no-var-requires
20
46
  ...Object.keys(require('./package.json').dependencies),
21
47
  ],