Skip to content

Commit

Permalink
fix: improve compatibility of buildConfig:pre when using Environment API
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Feb 20, 2025
1 parent 55cf9e9 commit e1751eb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
63 changes: 44 additions & 19 deletions vike/node/plugin/plugins/buildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,26 @@ const manifestTempFile = '_temp_manifest.json'

function buildConfig(): Plugin[] {
let isServerAssetsFixEnabled: boolean
let outDirs: OutDirs
let config: ResolvedConfig
let assetsJsonFilePath: string

// Ideally we'd move dist/_temp_manifest.json to dist/server/client-assets.json instead of dist/assets.json
// - But we can't because there is no guarentee whether dist/server/ is generated before or after dist/client/ (generating dist/server/ after dist/client/ erases dist/server/client-assets.json)
// - We'll able to do so once we replace `$ vite build` with `$ vike build`
async function writeTempManifest(outDirs: OutDirs) {
assetsJsonFilePath = path.posix.join(outDirs.outDirRoot, 'assets.json')
const clientManifestFilePath = path.posix.join(outDirs.outDirClient, manifestTempFile)
const serverManifestFilePath = path.posix.join(outDirs.outDirServer, manifestTempFile)
if (!isServerAssetsFixEnabled) {
await fs.copyFile(clientManifestFilePath, assetsJsonFilePath)
} else {
const { clientManifestMod } = await fixServerAssets(config)
await fs.writeFile(assetsJsonFilePath, JSON.stringify(clientManifestMod, null, 2), 'utf-8')
}
await fs.rm(clientManifestFilePath)
await fs.rm(serverManifestFilePath)
}

return [
{
name: 'vike:buildConfig:post',
Expand All @@ -61,7 +79,6 @@ function buildConfig(): Plugin[] {
assert(Object.keys(entries).length > 0)
config.build.rollupOptions.input = injectRollupInputs(entries, config)
addLogHook()
outDirs = getOutDirs(config)
{
isServerAssetsFixEnabled = fixServerAssets_isEnabled() && (await isV1Design(config))
if (isServerAssetsFixEnabled) {
Expand Down Expand Up @@ -102,9 +119,30 @@ function buildConfig(): Plugin[] {
{
name: 'vike:buildConfig:pre',
apply: 'build',
applyToEnvironment(env) {
return env.name === 'ssr'
// Compatiblity with Environment API. It replaces `vike:buildConfig:pre` when compatible
// See https://vite.dev/guide/api-environment-plugins.html#per-environment-plugins
applyToEnvironment() {
return {
name: 'vike:buildConfig:pre:env-api-compat',
apply: 'build',
enforce: 'pre',
writeBundle: {
order: 'pre',
sequential: true,
async handler(options, bundle) {
if (this.environment.name === 'ssr') {
await writeTempManifest(getOutDirs(this.environment.config));
}
if (viteIsSSR(this.environment.config)) {
// Replace __VITE_ASSETS_MANIFEST__ in all server-side bundles
await set_ASSETS_MANIFEST(options, bundle, assetsJsonFilePath)
}
}
}
}
},
// Ensures that we can reuse `assetsJsonFilePath`
sharedDuringBuild: true,
// Make sure other writeBundle() hooks are called after this writeBundle() hook.
// - set_ASSETS_MANIFEST() needs to be called before dist/server/ code is executed.
// - For example, the writeBundle() hook of vite-plugin-vercel needs to be called after this writeBundle() hook, otherwise: https://github.com/vikejs/vike/issues/1527
Expand All @@ -114,21 +152,8 @@ function buildConfig(): Plugin[] {
sequential: true,
async handler(options, bundle) {
if (viteIsSSR(config)) {
// Ideally we'd move dist/_temp_manifest.json to dist/server/client-assets.json instead of dist/assets.json
// - But we can't because there is no guarentee whether dist/server/ is generated before or after dist/client/ (generating dist/server/ after dist/client/ erases dist/server/client-assets.json)
// - We'll able to do so once we replace `$ vite build` with `$ vike build`
const assetsJsonFilePath = path.posix.join(outDirs.outDirRoot, 'assets.json')
const clientManifestFilePath = path.posix.join(outDirs.outDirClient, manifestTempFile)
const serverManifestFilePath = path.posix.join(outDirs.outDirServer, manifestTempFile)
if (!isServerAssetsFixEnabled) {
await fs.copyFile(clientManifestFilePath, assetsJsonFilePath)
} else {
const { clientManifestMod } = await fixServerAssets(config)
await fs.writeFile(assetsJsonFilePath, JSON.stringify(clientManifestMod, null, 2), 'utf-8')
}
await fs.rm(clientManifestFilePath)
await fs.rm(serverManifestFilePath)
await set_ASSETS_MANIFEST(options, bundle)
await writeTempManifest(getOutDirs(config));
await set_ASSETS_MANIFEST(options, bundle, assetsJsonFilePath)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions vike/node/plugin/plugins/buildEntry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ function getServerProductionEntryCode(config: ResolvedConfig): string {
return importerCode
}
/** Set the value of the ASSETS_MANIFEST constant inside dist/server/entry.js (or dist/server/index.js) */
async function set_ASSETS_MANIFEST(options: Options, bundle: Bundle) {
async function set_ASSETS_MANIFEST(options: Options, bundle: Bundle, assetsJsonFilePath: string) {
const { dir } = options
assert(dir)
const chunkPath = find_ASSETS_MANIFEST(bundle)
const chunkFilePath = path.join(dir, chunkPath)
const assetsJsonFilePath = path.join(dir, '..', 'assets.json')
const [assetsJsonString, chunkFileContent] = await Promise.all([
await fs.readFile(assetsJsonFilePath, 'utf8'),
await fs.readFile(chunkFilePath, 'utf8')
Expand Down

0 comments on commit e1751eb

Please sign in to comment.