Skip to content

Commit 3f79f4b

Browse files
authored
refactor: use rollup type from vite (#2723)
1 parent 4af5975 commit 3f79f4b

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

src/node/build/build.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { createRequire } from 'module'
44
import path from 'path'
55
import { packageDirectorySync } from 'pkg-dir'
66
import { rimraf } from 'rimraf'
7-
import type { OutputAsset, OutputChunk } from 'rollup'
87
import { pathToFileURL } from 'url'
9-
import type { BuildOptions } from 'vite'
8+
import type { BuildOptions, Rollup } from 'vite'
109
import { resolveConfig, type SiteConfig } from '../config'
1110
import { slash, type HeadConfig } from '../shared'
1211
import { deserializeFunctions, serializeFunctions } from '../utils/fnSerialize'
@@ -57,13 +56,13 @@ export async function build(
5756
chunk.type === 'chunk' &&
5857
chunk.isEntry &&
5958
chunk.facadeModuleId?.endsWith('.js')
60-
) as OutputChunk)
59+
) as Rollup.OutputChunk)
6160

6261
const cssChunk = (
6362
siteConfig.mpa ? serverResult : clientResult!
6463
).output.find(
6564
(chunk) => chunk.type === 'asset' && chunk.fileName.endsWith('.css')
66-
) as OutputAsset
65+
) as Rollup.OutputAsset
6766

6867
const assets = (siteConfig.mpa ? serverResult : clientResult!).output
6968
.filter(

src/node/build/buildMPAClient.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { build } from 'vite'
2-
import type { RollupOutput } from 'rollup'
1+
import { build, type Rollup } from 'vite'
32
import type { SiteConfig } from '..'
43

54
const virtualEntry = 'client.js'
65

76
export async function buildMPAClient(
87
js: Record<string, string>,
98
config: SiteConfig
10-
): Promise<RollupOutput> {
9+
): Promise<Rollup.RollupOutput> {
1110
const files = Object.keys(js)
1211
const themeFiles = files.filter((f) => !f.endsWith('.md'))
1312
const pages = files.filter((f) => f.endsWith('.md'))
@@ -43,5 +42,5 @@ export async function buildMPAClient(
4342
}
4443
}
4544
]
46-
}) as Promise<RollupOutput>
45+
}) as Promise<Rollup.RollupOutput>
4746
}

src/node/build/bundle.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import fs from 'fs-extra'
22
import path from 'path'
3-
import type { GetModuleInfo, RollupOutput } from 'rollup'
43
import { fileURLToPath } from 'url'
54
import {
65
build,
76
normalizePath,
87
type BuildOptions,
8+
type Rollup,
99
type UserConfig as ViteUserConfig
1010
} from 'vite'
1111
import { APP_PATH } from '../alias'
@@ -28,8 +28,8 @@ export async function bundle(
2828
config: SiteConfig,
2929
options: BuildOptions
3030
): Promise<{
31-
clientResult: RollupOutput | null
32-
serverResult: RollupOutput
31+
clientResult: Rollup.RollupOutput | null
32+
serverResult: Rollup.RollupOutput
3333
pageToHashMap: Record<string, string>
3434
}> {
3535
const pageToHashMap = Object.create(null)
@@ -139,14 +139,16 @@ export async function bundle(
139139
}
140140
})
141141

142-
let clientResult!: RollupOutput | null
143-
let serverResult!: RollupOutput
142+
let clientResult!: Rollup.RollupOutput | null
143+
let serverResult!: Rollup.RollupOutput
144144

145145
await task('building client + server bundles', async () => {
146146
clientResult = config.mpa
147147
? null
148-
: ((await build(await resolveViteConfig(false))) as RollupOutput)
149-
serverResult = (await build(await resolveViteConfig(true))) as RollupOutput
148+
: ((await build(await resolveViteConfig(false))) as Rollup.RollupOutput)
149+
serverResult = (await build(
150+
await resolveViteConfig(true)
151+
)) as Rollup.RollupOutput
150152
})
151153

152154
if (config.mpa) {
@@ -180,7 +182,7 @@ const cache = new Map<string, boolean>()
180182
/**
181183
* Check if a module is statically imported by at least one entry.
182184
*/
183-
function isEagerChunk(id: string, getModuleInfo: GetModuleInfo) {
185+
function isEagerChunk(id: string, getModuleInfo: Rollup.GetModuleInfo) {
184186
if (
185187
id.includes('node_modules') &&
186188
!/\.css($|\\?)/.test(id) &&
@@ -192,7 +194,7 @@ function isEagerChunk(id: string, getModuleInfo: GetModuleInfo) {
192194

193195
function staticImportedByEntry(
194196
id: string,
195-
getModuleInfo: GetModuleInfo,
197+
getModuleInfo: Rollup.GetModuleInfo,
196198
cache: Map<string, boolean>,
197199
importStack: string[] = []
198200
): boolean {

src/node/build/render.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { isBooleanAttr } from '@vue/shared'
22
import escape from 'escape-html'
33
import fs from 'fs-extra'
44
import path from 'path'
5-
import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
65
import { pathToFileURL } from 'url'
7-
import { normalizePath, transformWithEsbuild } from 'vite'
6+
import { normalizePath, transformWithEsbuild, type Rollup } from 'vite'
87
import type { SiteConfig } from '../config'
98
import {
109
EXTERNAL_URL_RE,
@@ -23,9 +22,9 @@ export async function renderPage(
2322
render: (path: string) => Promise<SSGContext>,
2423
config: SiteConfig,
2524
page: string, // foo.md
26-
result: RollupOutput | null,
27-
appChunk: OutputChunk | null,
28-
cssChunk: OutputAsset | null,
25+
result: Rollup.RollupOutput | null,
26+
appChunk: Rollup.OutputChunk | null,
27+
cssChunk: Rollup.OutputAsset | null,
2928
assets: string[],
3029
pageToHashMap: Record<string, string>,
3130
metadataScript: { html: string; inHead: boolean },
@@ -137,7 +136,7 @@ export async function renderPage(
137136
(chunk) =>
138137
chunk.type === 'chunk' &&
139138
chunk.facadeModuleId === slash(path.join(config.srcDir, page))
140-
) as OutputChunk
139+
) as Rollup.OutputChunk
141140
if (matchingChunk) {
142141
if (!matchingChunk.code.includes('import')) {
143142
inlinedScript = `<script type="module">${matchingChunk.code}</script>`
@@ -198,8 +197,8 @@ export async function renderPage(
198197
function resolvePageImports(
199198
config: SiteConfig,
200199
page: string,
201-
result: RollupOutput,
202-
appChunk: OutputChunk
200+
result: Rollup.RollupOutput,
201+
appChunk: Rollup.OutputChunk
203202
) {
204203
page = config.rewrites.inv[page] || page
205204
// find the page's js chunk and inject script tags for its imports so that
@@ -214,7 +213,7 @@ function resolvePageImports(
214213
srcPath = normalizePath(srcPath)
215214
const pageChunk = result.output.find(
216215
(chunk) => chunk.type === 'chunk' && chunk.facadeModuleId === srcPath
217-
) as OutputChunk
216+
) as Rollup.OutputChunk
218217
return [
219218
...appChunk.imports,
220219
...appChunk.dynamicImports,

src/node/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import path from 'path'
22
import c from 'picocolors'
3-
import type { OutputAsset, OutputChunk } from 'rollup'
43
import {
54
mergeConfig,
65
searchForWorkspaceRoot,
76
type Plugin,
87
type ResolvedConfig,
8+
type Rollup,
99
type UserConfig
1010
} from 'vite'
1111
import {
@@ -46,8 +46,8 @@ const staticRestoreRE = /__VP_STATIC_(START|END)__/g
4646
const scriptClientRE = /<script\b[^>]*client\b[^>]*>([^]*?)<\/script>/
4747

4848
const isPageChunk = (
49-
chunk: OutputAsset | OutputChunk
50-
): chunk is OutputChunk & { facadeModuleId: string } =>
49+
chunk: Rollup.OutputAsset | Rollup.OutputChunk
50+
): chunk is Rollup.OutputChunk & { facadeModuleId: string } =>
5151
!!(
5252
chunk.type === 'chunk' &&
5353
chunk.isEntry &&
@@ -266,7 +266,7 @@ export async function createVitePressPlugin(
266266
},
267267

268268
renderChunk(code, chunk) {
269-
if (!ssr && isPageChunk(chunk as OutputChunk)) {
269+
if (!ssr && isPageChunk(chunk as Rollup.OutputChunk)) {
270270
// For each page chunk, inject marker for start/end of static strings.
271271
// we do this here because in generateBundle the chunks would have been
272272
// minified and we won't be able to safely locate the strings.

0 commit comments

Comments
 (0)