refactor: use rollup type from vite (#2723)

pull/2724/head
Bjorn Lu 1 year ago committed by GitHub
parent 4af597582c
commit 3f79f4bb6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,9 +4,8 @@ import { createRequire } from 'module'
import path from 'path' import path from 'path'
import { packageDirectorySync } from 'pkg-dir' import { packageDirectorySync } from 'pkg-dir'
import { rimraf } from 'rimraf' import { rimraf } from 'rimraf'
import type { OutputAsset, OutputChunk } from 'rollup'
import { pathToFileURL } from 'url' import { pathToFileURL } from 'url'
import type { BuildOptions } from 'vite' import type { BuildOptions, Rollup } from 'vite'
import { resolveConfig, type SiteConfig } from '../config' import { resolveConfig, type SiteConfig } from '../config'
import { slash, type HeadConfig } from '../shared' import { slash, type HeadConfig } from '../shared'
import { deserializeFunctions, serializeFunctions } from '../utils/fnSerialize' import { deserializeFunctions, serializeFunctions } from '../utils/fnSerialize'
@ -57,13 +56,13 @@ export async function build(
chunk.type === 'chunk' && chunk.type === 'chunk' &&
chunk.isEntry && chunk.isEntry &&
chunk.facadeModuleId?.endsWith('.js') chunk.facadeModuleId?.endsWith('.js')
) as OutputChunk) ) as Rollup.OutputChunk)
const cssChunk = ( const cssChunk = (
siteConfig.mpa ? serverResult : clientResult! siteConfig.mpa ? serverResult : clientResult!
).output.find( ).output.find(
(chunk) => chunk.type === 'asset' && chunk.fileName.endsWith('.css') (chunk) => chunk.type === 'asset' && chunk.fileName.endsWith('.css')
) as OutputAsset ) as Rollup.OutputAsset
const assets = (siteConfig.mpa ? serverResult : clientResult!).output const assets = (siteConfig.mpa ? serverResult : clientResult!).output
.filter( .filter(

@ -1,5 +1,4 @@
import { build } from 'vite' import { build, type Rollup } from 'vite'
import type { RollupOutput } from 'rollup'
import type { SiteConfig } from '..' import type { SiteConfig } from '..'
const virtualEntry = 'client.js' const virtualEntry = 'client.js'
@ -7,7 +6,7 @@ const virtualEntry = 'client.js'
export async function buildMPAClient( export async function buildMPAClient(
js: Record<string, string>, js: Record<string, string>,
config: SiteConfig config: SiteConfig
): Promise<RollupOutput> { ): Promise<Rollup.RollupOutput> {
const files = Object.keys(js) const files = Object.keys(js)
const themeFiles = files.filter((f) => !f.endsWith('.md')) const themeFiles = files.filter((f) => !f.endsWith('.md'))
const pages = files.filter((f) => f.endsWith('.md')) const pages = files.filter((f) => f.endsWith('.md'))
@ -43,5 +42,5 @@ export async function buildMPAClient(
} }
} }
] ]
}) as Promise<RollupOutput> }) as Promise<Rollup.RollupOutput>
} }

@ -1,11 +1,11 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import path from 'path' import path from 'path'
import type { GetModuleInfo, RollupOutput } from 'rollup'
import { fileURLToPath } from 'url' import { fileURLToPath } from 'url'
import { import {
build, build,
normalizePath, normalizePath,
type BuildOptions, type BuildOptions,
type Rollup,
type UserConfig as ViteUserConfig type UserConfig as ViteUserConfig
} from 'vite' } from 'vite'
import { APP_PATH } from '../alias' import { APP_PATH } from '../alias'
@ -28,8 +28,8 @@ export async function bundle(
config: SiteConfig, config: SiteConfig,
options: BuildOptions options: BuildOptions
): Promise<{ ): Promise<{
clientResult: RollupOutput | null clientResult: Rollup.RollupOutput | null
serverResult: RollupOutput serverResult: Rollup.RollupOutput
pageToHashMap: Record<string, string> pageToHashMap: Record<string, string>
}> { }> {
const pageToHashMap = Object.create(null) const pageToHashMap = Object.create(null)
@ -139,14 +139,16 @@ export async function bundle(
} }
}) })
let clientResult!: RollupOutput | null let clientResult!: Rollup.RollupOutput | null
let serverResult!: RollupOutput let serverResult!: Rollup.RollupOutput
await task('building client + server bundles', async () => { await task('building client + server bundles', async () => {
clientResult = config.mpa clientResult = config.mpa
? null ? null
: ((await build(await resolveViteConfig(false))) as RollupOutput) : ((await build(await resolveViteConfig(false))) as Rollup.RollupOutput)
serverResult = (await build(await resolveViteConfig(true))) as RollupOutput serverResult = (await build(
await resolveViteConfig(true)
)) as Rollup.RollupOutput
}) })
if (config.mpa) { if (config.mpa) {
@ -180,7 +182,7 @@ const cache = new Map<string, boolean>()
/** /**
* Check if a module is statically imported by at least one entry. * Check if a module is statically imported by at least one entry.
*/ */
function isEagerChunk(id: string, getModuleInfo: GetModuleInfo) { function isEagerChunk(id: string, getModuleInfo: Rollup.GetModuleInfo) {
if ( if (
id.includes('node_modules') && id.includes('node_modules') &&
!/\.css($|\\?)/.test(id) && !/\.css($|\\?)/.test(id) &&
@ -192,7 +194,7 @@ function isEagerChunk(id: string, getModuleInfo: GetModuleInfo) {
function staticImportedByEntry( function staticImportedByEntry(
id: string, id: string,
getModuleInfo: GetModuleInfo, getModuleInfo: Rollup.GetModuleInfo,
cache: Map<string, boolean>, cache: Map<string, boolean>,
importStack: string[] = [] importStack: string[] = []
): boolean { ): boolean {

@ -2,9 +2,8 @@ import { isBooleanAttr } from '@vue/shared'
import escape from 'escape-html' import escape from 'escape-html'
import fs from 'fs-extra' import fs from 'fs-extra'
import path from 'path' import path from 'path'
import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
import { pathToFileURL } from 'url' import { pathToFileURL } from 'url'
import { normalizePath, transformWithEsbuild } from 'vite' import { normalizePath, transformWithEsbuild, type Rollup } from 'vite'
import type { SiteConfig } from '../config' import type { SiteConfig } from '../config'
import { import {
EXTERNAL_URL_RE, EXTERNAL_URL_RE,
@ -23,9 +22,9 @@ export async function renderPage(
render: (path: string) => Promise<SSGContext>, render: (path: string) => Promise<SSGContext>,
config: SiteConfig, config: SiteConfig,
page: string, // foo.md page: string, // foo.md
result: RollupOutput | null, result: Rollup.RollupOutput | null,
appChunk: OutputChunk | null, appChunk: Rollup.OutputChunk | null,
cssChunk: OutputAsset | null, cssChunk: Rollup.OutputAsset | null,
assets: string[], assets: string[],
pageToHashMap: Record<string, string>, pageToHashMap: Record<string, string>,
metadataScript: { html: string; inHead: boolean }, metadataScript: { html: string; inHead: boolean },
@ -137,7 +136,7 @@ export async function renderPage(
(chunk) => (chunk) =>
chunk.type === 'chunk' && chunk.type === 'chunk' &&
chunk.facadeModuleId === slash(path.join(config.srcDir, page)) chunk.facadeModuleId === slash(path.join(config.srcDir, page))
) as OutputChunk ) as Rollup.OutputChunk
if (matchingChunk) { if (matchingChunk) {
if (!matchingChunk.code.includes('import')) { if (!matchingChunk.code.includes('import')) {
inlinedScript = `<script type="module">${matchingChunk.code}</script>` inlinedScript = `<script type="module">${matchingChunk.code}</script>`
@ -198,8 +197,8 @@ export async function renderPage(
function resolvePageImports( function resolvePageImports(
config: SiteConfig, config: SiteConfig,
page: string, page: string,
result: RollupOutput, result: Rollup.RollupOutput,
appChunk: OutputChunk appChunk: Rollup.OutputChunk
) { ) {
page = config.rewrites.inv[page] || page page = config.rewrites.inv[page] || page
// find the page's js chunk and inject script tags for its imports so that // find the page's js chunk and inject script tags for its imports so that
@ -214,7 +213,7 @@ function resolvePageImports(
srcPath = normalizePath(srcPath) srcPath = normalizePath(srcPath)
const pageChunk = result.output.find( const pageChunk = result.output.find(
(chunk) => chunk.type === 'chunk' && chunk.facadeModuleId === srcPath (chunk) => chunk.type === 'chunk' && chunk.facadeModuleId === srcPath
) as OutputChunk ) as Rollup.OutputChunk
return [ return [
...appChunk.imports, ...appChunk.imports,
...appChunk.dynamicImports, ...appChunk.dynamicImports,

@ -1,11 +1,11 @@
import path from 'path' import path from 'path'
import c from 'picocolors' import c from 'picocolors'
import type { OutputAsset, OutputChunk } from 'rollup'
import { import {
mergeConfig, mergeConfig,
searchForWorkspaceRoot, searchForWorkspaceRoot,
type Plugin, type Plugin,
type ResolvedConfig, type ResolvedConfig,
type Rollup,
type UserConfig type UserConfig
} from 'vite' } from 'vite'
import { import {
@ -46,8 +46,8 @@ const staticRestoreRE = /__VP_STATIC_(START|END)__/g
const scriptClientRE = /<script\b[^>]*client\b[^>]*>([^]*?)<\/script>/ const scriptClientRE = /<script\b[^>]*client\b[^>]*>([^]*?)<\/script>/
const isPageChunk = ( const isPageChunk = (
chunk: OutputAsset | OutputChunk chunk: Rollup.OutputAsset | Rollup.OutputChunk
): chunk is OutputChunk & { facadeModuleId: string } => ): chunk is Rollup.OutputChunk & { facadeModuleId: string } =>
!!( !!(
chunk.type === 'chunk' && chunk.type === 'chunk' &&
chunk.isEntry && chunk.isEntry &&
@ -266,7 +266,7 @@ export async function createVitePressPlugin(
}, },
renderChunk(code, chunk) { renderChunk(code, chunk) {
if (!ssr && isPageChunk(chunk as OutputChunk)) { if (!ssr && isPageChunk(chunk as Rollup.OutputChunk)) {
// For each page chunk, inject marker for start/end of static strings. // For each page chunk, inject marker for start/end of static strings.
// we do this here because in generateBundle the chunks would have been // we do this here because in generateBundle the chunks would have been
// minified and we won't be able to safely locate the strings. // minified and we won't be able to safely locate the strings.

Loading…
Cancel
Save