-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ssr manifest for preload inference
- Loading branch information
Showing
6 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ResolvedConfig } from '..' | ||
import { Plugin } from '../plugin' | ||
import { chunkToEmittedCssFileMap } from '../plugins/css' | ||
|
||
export function ssrManifestPlugin(config: ResolvedConfig): Plugin { | ||
// module id => preload assets mapping | ||
const ssrManifest: Record<string, string[]> = {} | ||
const base = config.build.base | ||
|
||
return { | ||
name: 'vite:manifest', | ||
generateBundle(_options, bundle) { | ||
for (const file in bundle) { | ||
const chunk = bundle[file] | ||
if (chunk.type === 'chunk' && !chunk.isEntry) { | ||
// links for entry chunks are already generated in static HTML | ||
// so we only need to record info for non-entry chunks | ||
// TODO: also include non-CSS assets | ||
const cssFileHandle = chunkToEmittedCssFileMap.get(chunk) | ||
const cssFile = cssFileHandle && this.getFileName(cssFileHandle) | ||
for (const id in chunk.modules) { | ||
const mappedChunks = ssrManifest[id] || (ssrManifest[id] = []) | ||
mappedChunks.push(base + chunk.fileName) | ||
if (cssFile) { | ||
mappedChunks.push(base + cssFile) | ||
} | ||
} | ||
} | ||
} | ||
|
||
this.emitFile({ | ||
fileName: 'ssr-manifest.json', | ||
type: 'asset', | ||
source: JSON.stringify(ssrManifest, null, 2) | ||
}) | ||
} | ||
} | ||
} |