poveste 0.5.3 → 0.6.0

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 (50) hide show
  1. package/dist/node/bin.js +1 -1
  2. package/dist/node/build.d.ts.map +1 -1
  3. package/dist/node/build.js +31 -0
  4. package/dist/node/builtin-plugins/pinceau-tokens.js +2 -2
  5. package/dist/node/commands/port.d.ts +8 -0
  6. package/dist/node/commands/port.d.ts.map +1 -1
  7. package/dist/node/commands/port.js +11 -0
  8. package/dist/node/config.d.ts.map +1 -1
  9. package/dist/node/config.js +9 -2
  10. package/dist/node/index.d.ts +1 -0
  11. package/dist/node/index.d.ts.map +1 -1
  12. package/dist/node/index.js +3 -0
  13. package/dist/node/markdown.d.ts.map +1 -1
  14. package/dist/node/markdown.js +2 -10
  15. package/dist/node/server.d.ts.map +1 -1
  16. package/dist/node/server.js +16 -1
  17. package/dist/node/stories.d.ts.map +1 -1
  18. package/dist/node/stories.js +2 -10
  19. package/dist/node/util/watch-ignore.d.ts +3 -0
  20. package/dist/node/util/watch-ignore.d.ts.map +1 -0
  21. package/dist/node/util/watch-ignore.js +12 -0
  22. package/dist/node/virtual/resolved-generated-global-setup.d.ts.map +1 -1
  23. package/dist/node/virtual/resolved-generated-global-setup.js +13 -5
  24. package/dist/node/virtual/util.d.ts +11 -0
  25. package/dist/node/virtual/util.d.ts.map +1 -1
  26. package/dist/node/virtual/util.js +13 -0
  27. package/dist/node/vite.d.ts.map +1 -1
  28. package/dist/node/vite.js +12 -2
  29. package/package.json +5 -5
  30. package/src/node/__tests__/config.spec.ts +25 -0
  31. package/src/node/__tests__/port.spec.ts +33 -1
  32. package/src/node/__tests__/virtual-setup.spec.ts +13 -3
  33. package/src/node/__tests__/watch-ignore.spec.ts +33 -0
  34. package/src/node/bin.ts +1 -1
  35. package/src/node/build.ts +34 -0
  36. package/src/node/builtin-plugins/pinceau-tokens.ts +2 -2
  37. package/src/node/commands/port.ts +12 -0
  38. package/src/node/config.ts +10 -2
  39. package/src/node/index.ts +3 -0
  40. package/src/node/markdown.ts +2 -11
  41. package/src/node/server.ts +17 -1
  42. package/src/node/stories.ts +2 -11
  43. package/src/node/util/watch-ignore.ts +13 -0
  44. package/src/node/virtual/resolved-generated-global-setup.ts +13 -5
  45. package/src/node/virtual/util.ts +14 -0
  46. package/src/node/vite.ts +12 -1
  47. package/dist/node/builtin-plugins/tailwind-tokens.d.ts +0 -11
  48. package/dist/node/builtin-plugins/tailwind-tokens.d.ts.map +0 -1
  49. package/dist/node/builtin-plugins/tailwind-tokens.js +0 -660
  50. package/src/node/builtin-plugins/tailwind-tokens.ts +0 -694
package/src/node/build.ts CHANGED
@@ -39,6 +39,24 @@ const PRELOAD_MODULES = [
39
39
  'vendor',
40
40
  ]
41
41
 
42
+ // Kept out of the shared `vendor` chunk because only the app UI imports them,
43
+ // while `vendor` is imported by the sandbox too.
44
+ //
45
+ // Shiki is the source panel's syntax highlighter: 386 grammars and 60 themes,
46
+ // and it was the bulk of an 11.5 MB `vendor`. The sandbox entry pulls seven Vue
47
+ // exports from that chunk and nothing else, but a grid renders one sandbox
48
+ // iframe per cell and each iframe is its own realm — so every cell parsed and
49
+ // executed all 11.5 MB again, with the HTTP cache saving only the download.
50
+ // That was 34% of a scroll profile spent in top-level vendor evaluation (#103).
51
+ //
52
+ // They go to a chunk of their own rather than being left to Rollup: leaving
53
+ // placement automatic emitted one chunk per grammar — 402 assets — and tripled
54
+ // the blocking time it was meant to cut.
55
+ const APP_ONLY_VENDORS = [
56
+ /[\\/]node_modules[\\/]shiki[\\/]/,
57
+ /[\\/]node_modules[\\/]@shikijs[\\/]/,
58
+ ]
59
+
42
60
  const PREFETCHED_MODULES = [
43
61
  'StoryView',
44
62
  'reactivity',
@@ -155,7 +173,23 @@ export async function build(ctx: Context) {
155
173
  // Force chunk strategy
156
174
  config.build.rollupOptions.output = {
157
175
  manualChunks(id) {
176
+ // Vite's runtime helpers (`\0vite/preload-helper.js` and friends) are
177
+ // virtual modules, so the node_modules routing below never sees them
178
+ // and their placement is left to the bundler — which parked
179
+ // `__vitePreload` inside the 10 MB `highlighter` chunk. Every chunk
180
+ // importing the helper then statically imported the whole highlighter,
181
+ // and the sandbox was back to evaluating it once per grid cell (#197),
182
+ // undoing exactly what `APP_ONLY_VENDORS` is for. Pin them somewhere
183
+ // harmless instead of leaving the choice to chunking heuristics.
184
+ if (id.replace(/^\0/, '').startsWith('vite/')) {
185
+ return 'app-runtime'
186
+ }
187
+
158
188
  if (!id.includes('@poveste/app') && id.includes('node_modules')) {
189
+ if (APP_ONLY_VENDORS.some(test => test.test(id))) {
190
+ return 'highlighter'
191
+ }
192
+
159
193
  for (const test of ctx.config.build?.excludeFromVendorsChunk ?? []) {
160
194
  if ((
161
195
  typeof test === 'string' && id.includes(test)
@@ -73,8 +73,8 @@ export function pinceauTokens(_options: PinceauTokensOptions = {}): Plugin {
73
73
  function storyTemplate(pinceauConfig: any) {
74
74
  pinceauConfig = pinceauConfig?.theme || pinceauConfig?.default?.theme || {}
75
75
 
76
- return `import 'poveste-style'
77
- import './style.css'
76
+ // No `import 'poveste-style'` — see the tailwind plugin's template (#219).
77
+ return `import './style.css'
78
78
  import { createApp, h, markRaw, ref } from ${getInjectedImport('@poveste/vendors/vue')}
79
79
  import {
80
80
  HstColorShades,
@@ -27,3 +27,15 @@ export function resolvePort(value: unknown, command: string): number | undefined
27
27
 
28
28
  return port
29
29
  }
30
+
31
+ /**
32
+ * The HMR socket port for a book served on `devPort`. Derived from the dev port
33
+ * so each concurrent dev server's socket is as distinct as its book port (#175),
34
+ * offset clear of the usual dev-port range and wrapped so it stays a valid port
35
+ * for an unusually high `--port`. `@nuxt/vite-builder` otherwise pins every
36
+ * server to the framework default (24678), a constant that collides (#221).
37
+ */
38
+ export function hmrPortFor(devPort: number | undefined): number {
39
+ const base = devPort ?? 6006
40
+ return base < 45536 ? base + 20000 : base - 20000
41
+ }
@@ -15,7 +15,6 @@ import {
15
15
  mergeConfig as mergeViteConfig,
16
16
  resolveConfig as resolveViteConfig,
17
17
  } from 'vite'
18
- import { tailwindTokens } from './builtin-plugins/tailwind-tokens.js'
19
18
  import { vanillaSupport } from './builtin-plugins/vanilla-support/plugin.js'
20
19
  import { defaultColors } from './colors.js'
21
20
  import { findUp } from './util/find-up.js'
@@ -26,7 +25,6 @@ export function getDefaultConfig(): PovesteConfig {
26
25
  return {
27
26
  plugins: [
28
27
  vanillaSupport(),
29
- tailwindTokens(),
30
28
  ],
31
29
  outDir: '.poveste/dist',
32
30
  storyMatch: [
@@ -250,6 +248,16 @@ export const mergeConfig = createDefu((obj: any, key, value) => {
250
248
  return true
251
249
  }
252
250
 
251
+ // An ignore list adds to the defaults rather than replacing them. Under the
252
+ // replace rule below, `storyIgnored: ['**/fixtures/**']` silently dropped
253
+ // `**/node_modules/**`, and the watchers then crawled the pnpm store until
254
+ // the build died with EMFILE (#244). `storyMatch` stays on the replace rule:
255
+ // narrowing it is a legitimate use.
256
+ if (obj[key] && key === 'storyIgnored') {
257
+ obj[key] = [...new Set([...obj[key], ...value])]
258
+ return true
259
+ }
260
+
253
261
  // By default, arrays should be replaced
254
262
  if (obj[key] && Array.isArray(obj[key])) {
255
263
  obj[key] = value
package/src/node/index.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  export { defaultColors } from './colors.js'
2
2
  export * from './config.js'
3
3
  export * from './plugin.js'
4
+ // A plugin that generates story source needs to reference poveste's own
5
+ // vendored copies of Vue and the controls from inside that generated code.
6
+ export { getInjectedImport } from './util/vendors.js'
4
7
  export * from '@poveste/shared'
@@ -10,12 +10,12 @@ import MarkdownIt from 'markdown-it'
10
10
  import anchor from 'markdown-it-anchor'
11
11
  import attrs from 'markdown-it-attrs'
12
12
  import { full as emoji } from 'markdown-it-emoji'
13
- import micromatch from 'micromatch'
14
13
  import path from 'pathe'
15
14
  import pc from 'picocolors'
16
15
  import { bundledLanguages, createHighlighter } from 'shiki'
17
16
  import { addStory, notifyStoryChange, removeStory } from './stories.js'
18
17
  import { slugify } from './util/slugify.js'
18
+ import { createWatchIgnore } from './util/watch-ignore.js'
19
19
 
20
20
  const onMarkdownListChangeHandlers: (() => unknown)[] = []
21
21
 
@@ -161,16 +161,7 @@ export async function createMarkdownFilesWatcher(ctx: Context) {
161
161
 
162
162
  const watcher = chokidar.watch('.', {
163
163
  cwd: ctx.root,
164
- ignored: (path, stats) => {
165
- if (ctx.config.storyIgnored.some(pattern => micromatch.isMatch(path, pattern))) {
166
- return true
167
- }
168
- if (micromatch.isMatch(path, '**/*.story.md')) {
169
- return false
170
- }
171
-
172
- return stats?.isFile()
173
- },
164
+ ignored: createWatchIgnore(ctx.config.storyIgnored, ['**/*.story.md']),
174
165
  })
175
166
 
176
167
  /**
@@ -4,6 +4,7 @@ import { performance } from 'node:perf_hooks'
4
4
  import pc from 'picocolors'
5
5
  import { createServer as createViteServer, mergeConfig as mergeViteConfig } from 'vite'
6
6
  import { useCollectStories } from './collect/index.js'
7
+ import { hmrPortFor } from './commands/port.js'
7
8
  import { useModuleLoader } from './load.js'
8
9
  import { createMarkdownFilesWatcher, onMarkdownListChange } from './markdown.js'
9
10
  import { DevEventPluginApi, DevPluginApi } from './plugin.js'
@@ -22,7 +23,22 @@ export async function createServer(ctx: Context, options: CreateServerOptions =
22
23
  const getViteServer = async (collecting: boolean) => {
23
24
  const { viteConfig, viteConfigFile } = await getViteConfigWithPlugins(collecting, ctx)
24
25
 
25
- if (!collecting) {
26
+ if (collecting) {
27
+ // The collection server drives vite-node and has no browser, so it needs
28
+ // no HMR socket. Left enabled, `@nuxt/vite-builder` gives it one on the
29
+ // framework default port (24678) — see the book server below.
30
+ viteConfig.server.hmr = false
31
+ }
32
+ else {
33
+ // `@nuxt/vite-builder` pins the HMR socket to the constant 24678 on every
34
+ // server it configures, so two poveste dev servers on different `--port`s
35
+ // collide on it and the second silently loses HMR (#221). It only does
36
+ // this when `server.hmr` carries no port of its own (it fills the gap with
37
+ // `defu`), so pin the socket to a port derived from this book's `--port`,
38
+ // before the config is resolved: each dev server then owns a distinct one
39
+ // (#175).
40
+ viteConfig.server.hmr = { port: hmrPortFor(options.port ?? viteConfig.server.port) }
41
+
26
42
  if (options.open) {
27
43
  viteConfig.server.open = true
28
44
  }
@@ -5,6 +5,7 @@ import chokidar from 'chokidar'
5
5
  import { globby } from 'globby'
6
6
  import micromatch from 'micromatch'
7
7
  import { basename, resolve } from 'pathe'
8
+ import { createWatchIgnore } from './util/watch-ignore.js'
8
9
 
9
10
  type StoryChangeHandler = (file?: ServerStoryFile) => unknown
10
11
  const storyChangeHandlers: StoryChangeHandler[] = []
@@ -60,17 +61,7 @@ export async function watchStories(newContext: Context) {
60
61
 
61
62
  const watcher = chokidar.watch(baseWatchPaths, {
62
63
  cwd: context.root,
63
- ignored: (path, stats) => {
64
- if (resolvedStoryIgnored.some(pattern => micromatch.isMatch(path, pattern))) {
65
- return true
66
- }
67
- if (resolveStoryMatch.some(pattern => micromatch.isMatch(path, pattern))) {
68
- return false
69
- }
70
-
71
- // Allow directories to be traversed, only ignore files that don't match
72
- return stats?.isFile() ?? false
73
- },
64
+ ignored: createWatchIgnore(resolvedStoryIgnored, resolveStoryMatch),
74
65
  })
75
66
 
76
67
  watcher
@@ -0,0 +1,13 @@
1
+ import type { Stats } from 'node:fs'
2
+ import micromatch from 'micromatch'
3
+
4
+ // chokidar 4 passes absolute paths, so `**` must cross dot segments (`.claude/`, `node_modules/.pnpm`).
5
+ const options = { dot: true }
6
+
7
+ export function createWatchIgnore(ignored: string[], included: string[]) {
8
+ return (path: string, stats?: Stats): boolean => {
9
+ if (micromatch.isMatch(path, ignored, options)) return true
10
+ if (micromatch.isMatch(path, included, options)) return false
11
+ return stats?.isFile() ?? false
12
+ }
13
+ }
@@ -1,22 +1,30 @@
1
1
  import type { Context } from '../context.js'
2
2
  import { GENERATED_SETUP_CODE } from './index.js'
3
- import { declareEmptySetupFns, getSetupFnNames, ID_SEPARATOR } from './util.js'
3
+ import { declareEmptySetupFns, getSetupFnGroups, ID_SEPARATOR } from './util.js'
4
4
 
5
5
  export function resolvedGeneratedGlobalSetup(ctx: Context) {
6
6
  if (ctx.config.setupCode) {
7
+ const groups = getSetupFnGroups(ctx)
8
+ const canonicals = new Set(groups.map(g => g.canonical))
7
9
  return [
8
10
  // Import
9
11
  `${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
10
12
  // List
11
13
  `const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
12
- // Setups
13
- ...getSetupFnNames(ctx).map(fnName => `export async function ${fnName} (payload) {
14
+ // Setups: one real function per plugin, under its canonical name only.
15
+ ...groups.map(g => `export async function ${g.canonical} (payload) {
14
16
  for (const setup of setupList) {
15
- if (setup?.${fnName}) {
16
- await setup.${fnName}(payload)
17
+ if (setup?.${g.canonical}) {
18
+ await setup.${g.canonical}(payload)
17
19
  }
18
20
  }
19
21
  }`),
22
+ // Aliases are declared `undefined`, not emitted as functions: only the
23
+ // canonical hook is callable, so `getSetupHook` sees one hook in our own
24
+ // generated module rather than warning that it exports two aliases (#231).
25
+ ...[...new Set(groups.flatMap(g => g.aliases))]
26
+ .filter(name => !canonicals.has(name))
27
+ .map(name => `export const ${name} = undefined`),
20
28
  ].join('\n')
21
29
  }
22
30
  else {
@@ -10,6 +10,20 @@ export function getSetupFnNames(ctx: Context) {
10
10
  return [...new Set(ctx.supportPlugins.flatMap(p => p.setupFn))]
11
11
  }
12
12
 
13
+ /**
14
+ * Setup hooks grouped by the plugin that contributes them, canonical name first.
15
+ * A plugin's `setupFn` lists one hook under several names — `['setupVue3',
16
+ * 'setupVue']` is a single hook whose established name is `setupVue3` and whose
17
+ * alias is `setupVue`. The first is canonical; the rest are aliases kept so
18
+ * older setup files still resolve.
19
+ */
20
+ export function getSetupFnGroups(ctx: Context) {
21
+ return ctx.supportPlugins.map((p) => {
22
+ const names = typeof p.setupFn === 'string' ? [p.setupFn] : p.setupFn
23
+ return { canonical: names[0], aliases: names.slice(1) }
24
+ })
25
+ }
26
+
13
27
  /**
14
28
  * Declares the setup hooks as named exports set to `undefined`.
15
29
  *
package/src/node/vite.ts CHANGED
@@ -11,6 +11,7 @@ import { dirname, join, relative } from 'pathe'
11
11
  import {
12
12
  loadConfigFromFile as loadViteConfigFromFile,
13
13
  mergeConfig as mergeViteConfig,
14
+ searchForWorkspaceRoot,
14
15
  } from 'vite'
15
16
  import { APP_PATH, TEMP_PATH } from './alias.js'
16
17
  import { createMarkdownPlugins } from './markdown.js'
@@ -172,6 +173,10 @@ export async function getViteConfigWithPlugins(isServer: boolean, ctx: Context):
172
173
  TEMP_PATH,
173
174
  ctx.resolvedViteConfig.root,
174
175
  process.cwd(),
176
+ // Vite's own default, which setting `allow` at all replaces. In a
177
+ // workspace the app's deps resolve above the project — the
178
+ // bundled font under the root `node_modules` was refused (#252).
179
+ searchForWorkspaceRoot(process.cwd()),
175
180
  ...supportPluginAllowPaths,
176
181
  ...process.env.POVESTE_DEV
177
182
  ? [
@@ -188,7 +193,13 @@ export async function getViteConfigWithPlugins(isServer: boolean, ctx: Context):
188
193
  define: {
189
194
  // We need to force this to be able to use `devtoolsRawSetupState`
190
195
  '__VUE_PROD_DEVTOOLS__': 'true',
191
- // Disable warnings
196
+ // Vue's esm-bundler build expects every feature flag to be injected;
197
+ // in a production build an undefined one logs a warning per realm.
198
+ '__VUE_OPTIONS_API__': 'true',
199
+ '__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': 'false',
200
+ // `bin.ts` sets this per command: development for `dev`, production
201
+ // for `build` and `preview`. Every built book used to ship the
202
+ // development build of Vue, Pinia, vue-router and VueUse.
192
203
  'process.env.NODE_ENV': JSON.stringify(isServer ? 'production' : process.env.NODE_ENV ?? 'development'),
193
204
  ...!isServer
194
205
  ? {
@@ -1,11 +0,0 @@
1
- import type { Plugin } from '@poveste/shared';
2
- export interface TailwindTokensOptions {
3
- /**
4
- * Path to the CSS entrypoint that imports Tailwind and declares `@theme`.
5
- * Tailwind v4 is CSS-first, so the design tokens are read from CSS rather
6
- * than from a JS config. Auto-detected when omitted.
7
- */
8
- cssFile?: string;
9
- }
10
- export declare function tailwindTokens(options?: TailwindTokensOptions): Plugin;
11
- //# sourceMappingURL=tailwind-tokens.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tailwind-tokens.d.ts","sourceRoot":"","sources":["../../../src/node/builtin-plugins/tailwind-tokens.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAiB,MAAM,iBAAiB,CAAA;AAI5D,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAqBD,wBAAgB,cAAc,CAAC,OAAO,GAAE,qBAA0B,GAAG,MAAM,CAkE1E"}