Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8f1a17

Browse files
authored
fix(vue): expose @unhead/vue/stream/iife with correct types (#707)
The `unhead` build hook overwrites `dist/stream/iife.mjs` post-build to export `streamingIifeCode` (string) and `streamingIifeSize` (number), but the generated `.d.ts` still declared the source-level `init` export. This caused consumers to need `as unknown as` casts when importing. Now the build hook writes correct `.d.ts` files alongside the overwritten `.mjs`, and `@unhead/vue` re-exports the subpath so Vue users can import from `@unhead/vue/stream/iife`.
1 parent e413c5c commit a8f1a17

9 files changed

Lines changed: 29 additions & 4 deletions

File tree

packages/unhead/build.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ export default defineBuildConfig({
3838
`export const streamingIifeCode = ${JSON.stringify(code)};\nexport const streamingIifeSize = ${code.length};\n`,
3939
)
4040

41+
// Write correct type declarations for the post-build exports
42+
writeFileSync(
43+
resolve(ctx.options.rootDir, 'dist/stream/iife.d.ts'),
44+
`export declare const streamingIifeCode: string;\nexport declare const streamingIifeSize: number;\n`,
45+
)
46+
writeFileSync(
47+
resolve(ctx.options.rootDir, 'dist/stream/iife.d.mts'),
48+
`export declare const streamingIifeCode: string;\nexport declare const streamingIifeSize: number;\n`,
49+
)
50+
4151
console.log(`Built streaming IIFE: ${code.length} bytes`)
4252
await bundle.close()
4353
},
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** The bundled IIFE code as a string, for inlining in HTML */
2+
export declare const streamingIifeCode: string
3+
/** Byte length of the bundled IIFE code */
4+
export declare const streamingIifeSize: number

packages/unhead/src/stream/vite.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ export function createStreamingPlugin(options: StreamingPluginOptions): Plugin {
3838
async configResolved() {
3939
if (!iifeCodeLoaded) {
4040
iifeCodeLoaded = true
41-
// After build, iife.mjs exports streamingIifeCode string (not the source module)
42-
const mod = await import('unhead/stream/iife') as unknown as { streamingIifeCode: string }
41+
const mod = await import('unhead/stream/iife')
4342
iifeCode = mod.streamingIifeCode
4443
}
4544
},

packages/vue/build.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineBuildConfig({
1515
{ input: 'src/scripts', name: 'scripts' },
1616
{ input: 'src/utils', name: 'utils' },
1717
{ input: 'src/stream/vite', name: 'stream/vite' },
18+
{ input: 'src/stream/iife', name: 'stream/iife' },
1819
],
1920
hooks: {
2021
'rollup:options': (_, options) => {

packages/vue/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
"./stream/vite": {
6565
"types": "./dist/stream/vite.d.ts",
6666
"default": "./dist/stream/vite.mjs"
67+
},
68+
"./stream/iife": {
69+
"types": "./dist/stream/iife.d.ts",
70+
"default": "./dist/stream/iife.mjs"
6771
}
6872
},
6973
"main": "dist/index.mjs",
@@ -100,6 +104,9 @@
100104
],
101105
"stream/vite": [
102106
"dist/stream/vite"
107+
],
108+
"stream/iife": [
109+
"dist/stream/iife"
103110
]
104111
}
105112
},

packages/vue/src/stream/iife.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { streamingIifeCode, streamingIifeSize } from 'unhead/stream/iife'

packages/vue/test/client-hydration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JSDOM } from 'jsdom'
2-
import { init as initIife } from 'unhead/stream/iife'
32
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
3+
import { init as initIife } from '../../unhead/src/stream/iife'
44
import { createStreamableHead } from '../src/stream/client'
55

66
let originalWindow: any

test/exports/vue.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
createStreamableHead: function
3838
HeadStream: object
3939
VueHeadMixin: object
40+
./stream/iife:
41+
streamingIifeCode: string
42+
streamingIifeSize: number
4043
./stream/server:
4144
createBootstrapScript: function
4245
createStreamableHead: function

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"unhead/stream/client": ["packages/unhead/src/stream/client"],
1919
"unhead/stream/server": ["packages/unhead/src/stream/server"],
2020
"unhead/stream/vite": ["packages/unhead/src/stream/vite"],
21-
"unhead/stream/iife": ["packages/unhead/src/stream/iife"],
21+
"unhead/stream/iife": ["packages/unhead/src/stream/iife-code"],
2222
"@unhead/ssr": ["packages/unhead/src/server/index"],
2323
"@unhead/dom": ["packages/unhead/src/client/index"],
2424
"@unhead/vue": ["packages/vue/src/index"],

0 commit comments

Comments
 (0)