histoire 0.0.4 → 0.0.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.
@@ -8,6 +8,14 @@ import { APP_PATH } from './alias.js';
8
8
  import { createVitePlugins } from './plugin.js';
9
9
  import { findAllStories } from './stories.js';
10
10
  import { useCollectStories } from './collect.js';
11
+ const PRELOAD_MODULES = [
12
+ 'vendor',
13
+ ];
14
+ const PREFETCHED_MODULES = [
15
+ 'StoryView',
16
+ 'reactivity',
17
+ 'global-components',
18
+ ];
11
19
  export async function build(ctx) {
12
20
  const startTime = performance.now();
13
21
  await findAllStories(ctx);
@@ -44,19 +52,27 @@ export async function build(ctx) {
44
52
  });
45
53
  const result = Array.isArray(results) ? results[0] : results;
46
54
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset');
55
+ // Preload
56
+ const preloadOutputs = result.output.filter(o => PRELOAD_MODULES.includes(o.name) && o.type === 'chunk');
57
+ const preloadHtml = generateScriptLinks(preloadOutputs.map(o => o.fileName), 'preload');
58
+ // Prefetch
59
+ const prefetchOutputs = result.output.filter(o => PREFETCHED_MODULES.includes(o.name) && o.type === 'chunk');
60
+ const prefetchHtml = generateScriptLinks(prefetchOutputs.map(o => o.fileName), 'prefetch');
47
61
  // Index
48
62
  const indexOutput = result.output.find(o => o.name === 'index' && o.type === 'chunk');
49
- await writeHtml(indexOutput.fileName, styleOutput.fileName, 'index.html', ctx);
63
+ await writeHtml(indexOutput.fileName, styleOutput.fileName, 'index.html', {
64
+ HEAD: `${preloadHtml}${prefetchHtml}`,
65
+ }, ctx);
50
66
  // Sandbox
51
67
  const sandboxOutput = result.output.find(o => o.name === 'sandbox' && o.type === 'chunk');
52
- await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', ctx);
68
+ await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', {}, ctx);
53
69
  const duration = performance.now() - startTime;
54
70
  if (emptyStoryCount) {
55
71
  console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`));
56
72
  }
57
73
  console.log(pc.green(`✅ Built ${storyCount} stories (${variantCount} variants) in ${Math.round(duration / 1000 * 100) / 100}s`));
58
74
  }
59
- async function writeHtml(jsEntryFile, cssEntryFile, htmlFileName, ctx) {
75
+ async function writeHtml(jsEntryFile, cssEntryFile, htmlFileName, variables, ctx) {
60
76
  const code = `<!DOCTYPE html>
61
77
  <html>
62
78
  <head>
@@ -66,6 +82,7 @@ async function writeHtml(jsEntryFile, cssEntryFile, htmlFileName, ctx) {
66
82
  <meta name="description" content="">
67
83
  <link rel="stylesheet" href="/${cssEntryFile}">
68
84
  ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
85
+ ${variables.HEAD ?? ''}
69
86
  </head>
70
87
  <body>
71
88
  <div id="app"></div>
@@ -74,3 +91,6 @@ async function writeHtml(jsEntryFile, cssEntryFile, htmlFileName, ctx) {
74
91
  </html>`;
75
92
  await fs.writeFile(join(ctx.config.outDir, htmlFileName), code, 'utf8');
76
93
  }
94
+ function generateScriptLinks(prefetchScripts, rel) {
95
+ return prefetchScripts.map(s => `<link rel="${rel}" href="${s}" as="script" crossOrigin="anonymous">`).join('');
96
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -33,7 +33,8 @@
33
33
  "histoire": "./bin.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@histoire/controls": "^0.0.4",
36
+ "@iconify/vue": "^3.1.3",
37
+ "@histoire/controls": "^0.0.5",
37
38
  "@vitejs/plugin-vue": "^2.2.0",
38
39
  "@vueuse/core": "^7.6.2",
39
40
  "case": "^1.6.3",
@@ -63,7 +64,6 @@
63
64
  "vue": "^3.2.31"
64
65
  },
65
66
  "devDependencies": {
66
- "@iconify/vue": "^3.1.3",
67
67
  "@peeky/test": "^0.13.5",
68
68
  "@tailwindcss/typography": "^0.5.2",
69
69
  "@types/fs-extra": "^9.0.13",
@@ -71,7 +71,6 @@
71
71
  "@types/node": "^17.0.18",
72
72
  "autoprefixer": "^10.4.2",
73
73
  "concurrently": "^7.0.0",
74
- "globby": "^13.1.1",
75
74
  "postcss": "^8.4.6",
76
75
  "postcss-import": "^14.0.2",
77
76
  "tailwindcss": "^3.0.23",
package/src/node/build.ts CHANGED
@@ -11,6 +11,16 @@ import { findAllStories } from './stories.js'
11
11
  import type { RollupOutput } from 'rollup'
12
12
  import { useCollectStories } from './collect.js'
13
13
 
14
+ const PRELOAD_MODULES = [
15
+ 'vendor',
16
+ ]
17
+
18
+ const PREFETCHED_MODULES = [
19
+ 'StoryView',
20
+ 'reactivity',
21
+ 'global-components',
22
+ ]
23
+
14
24
  export async function build (ctx: Context) {
15
25
  const startTime = performance.now()
16
26
  await findAllStories(ctx)
@@ -52,13 +62,23 @@ export async function build (ctx: Context) {
52
62
 
53
63
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset')
54
64
 
65
+ // Preload
66
+ const preloadOutputs = result.output.filter(o => PRELOAD_MODULES.includes(o.name) && o.type === 'chunk')
67
+ const preloadHtml = generateScriptLinks(preloadOutputs.map(o => o.fileName), 'preload')
68
+
69
+ // Prefetch
70
+ const prefetchOutputs = result.output.filter(o => PREFETCHED_MODULES.includes(o.name) && o.type === 'chunk')
71
+ const prefetchHtml = generateScriptLinks(prefetchOutputs.map(o => o.fileName), 'prefetch')
72
+
55
73
  // Index
56
74
  const indexOutput = result.output.find(o => o.name === 'index' && o.type === 'chunk')
57
- await writeHtml(indexOutput.fileName, styleOutput.fileName, 'index.html', ctx)
75
+ await writeHtml(indexOutput.fileName, styleOutput.fileName, 'index.html', {
76
+ HEAD: `${preloadHtml}${prefetchHtml}`,
77
+ }, ctx)
58
78
 
59
79
  // Sandbox
60
80
  const sandboxOutput = result.output.find(o => o.name === 'sandbox' && o.type === 'chunk')
61
- await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', ctx)
81
+ await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', {}, ctx)
62
82
 
63
83
  const duration = performance.now() - startTime
64
84
  if (emptyStoryCount) {
@@ -67,7 +87,7 @@ export async function build (ctx: Context) {
67
87
  console.log(pc.green(`✅ Built ${storyCount} stories (${variantCount} variants) in ${Math.round(duration / 1000 * 100) / 100}s`))
68
88
  }
69
89
 
70
- async function writeHtml (jsEntryFile: string, cssEntryFile: string, htmlFileName: string, ctx: Context) {
90
+ async function writeHtml (jsEntryFile: string, cssEntryFile: string, htmlFileName: string, variables: { HEAD?: string }, ctx: Context) {
71
91
  const code = `<!DOCTYPE html>
72
92
  <html>
73
93
  <head>
@@ -77,6 +97,7 @@ async function writeHtml (jsEntryFile: string, cssEntryFile: string, htmlFileNam
77
97
  <meta name="description" content="">
78
98
  <link rel="stylesheet" href="/${cssEntryFile}">
79
99
  ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
100
+ ${variables.HEAD ?? ''}
80
101
  </head>
81
102
  <body>
82
103
  <div id="app"></div>
@@ -85,3 +106,7 @@ async function writeHtml (jsEntryFile: string, cssEntryFile: string, htmlFileNam
85
106
  </html>`
86
107
  await fs.writeFile(join(ctx.config.outDir, htmlFileName), code, 'utf8')
87
108
  }
109
+
110
+ function generateScriptLinks (prefetchScripts: string[], rel: string) {
111
+ return prefetchScripts.map(s => `<link rel="${rel}" href="${s}" as="script" crossOrigin="anonymous">`).join('')
112
+ }