-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the build to Server Islands (#11372)
* Add support for the build to Server Islands * Use command instead * editor tips * Add comment about defaultRoutes * Use renderChunk instead of generateBundle
- Loading branch information
Showing
16 changed files
with
102 additions
and
107 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
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
83 changes: 64 additions & 19 deletions
83
packages/astro/src/core/server-islands/vite-plugin-server-islands.ts
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 |
---|---|---|
@@ -1,45 +1,90 @@ | ||
import type { AstroPluginMetadata } from '../../vite-plugin-astro/index.js'; | ||
import type { AstroSettings, ComponentInstance } from '../../@types/astro.js'; | ||
import type { ViteDevServer, Plugin as VitePlugin } from 'vite'; | ||
import type { AstroSettings } from '../../@types/astro.js'; | ||
import type { ConfigEnv, ViteDevServer, Plugin as VitePlugin } from 'vite'; | ||
|
||
export const VIRTUAL_ISLAND_MAP_ID = '@astro-server-islands'; | ||
export const RESOLVED_VIRTUAL_ISLAND_MAP_ID = '\0' + VIRTUAL_ISLAND_MAP_ID; | ||
const serverIslandPlaceholder = '\'$$server-islands$$\''; | ||
|
||
export function vitePluginServerIslands({ settings }: { settings: AstroSettings }): VitePlugin { | ||
let command: ConfigEnv['command'] = 'serve'; | ||
let viteServer: ViteDevServer | null = null; | ||
const referenceIdMap = new Map<string, string>(); | ||
return { | ||
name: 'astro:server-islands', | ||
enforce: 'post', | ||
config(_config, { command: _command }) { | ||
command = _command; | ||
}, | ||
configureServer(_server) { | ||
viteServer = _server; | ||
}, | ||
transform(code, id, options) { | ||
resolveId(name) { | ||
if(name === VIRTUAL_ISLAND_MAP_ID) { | ||
return RESOLVED_VIRTUAL_ISLAND_MAP_ID; | ||
} | ||
}, | ||
load(id) { | ||
if(id === RESOLVED_VIRTUAL_ISLAND_MAP_ID) { | ||
return `export const serverIslandMap = ${serverIslandPlaceholder};`; | ||
} | ||
}, | ||
transform(_code, id) { | ||
if(id.endsWith('.astro')) { | ||
const info = this.getModuleInfo(id); | ||
if(info?.meta) { | ||
const astro = info.meta.astro as AstroPluginMetadata['astro'] | undefined; | ||
if(astro?.serverComponents.length) { | ||
if(viteServer) { | ||
for(const comp of astro.serverComponents) { | ||
if(!settings.serverIslandNameMap.has(comp.resolvedPath)) { | ||
let name = comp.localName; | ||
let idx = 1; | ||
for(const comp of astro.serverComponents) { | ||
if(!settings.serverIslandNameMap.has(comp.resolvedPath)) { | ||
let name = comp.localName; | ||
let idx = 1; | ||
|
||
while(true) { | ||
// Name not taken, let's use it. | ||
if(!settings.serverIslandMap.has(name)) { | ||
break; | ||
} | ||
// Increment a number onto the name: Avatar -> Avatar1 | ||
name += idx++; | ||
while(true) { | ||
// Name not taken, let's use it. | ||
if(!settings.serverIslandMap.has(name)) { | ||
break; | ||
} | ||
settings.serverIslandNameMap.set(comp.resolvedPath, name); | ||
settings.serverIslandMap.set(name, () => { | ||
return viteServer?.ssrLoadModule(comp.resolvedPath) as any; | ||
// Increment a number onto the name: Avatar -> Avatar1 | ||
name += idx++; | ||
} | ||
|
||
// Append the name map, for prod | ||
settings.serverIslandNameMap.set(comp.resolvedPath, name); | ||
|
||
settings.serverIslandMap.set(name, () => { | ||
return viteServer?.ssrLoadModule(comp.resolvedPath) as any; | ||
}); | ||
|
||
// Build mode | ||
if(command === 'build') { | ||
let referenceId = this.emitFile({ | ||
type: 'chunk', | ||
id: comp.specifier, | ||
importer: id, | ||
name: comp.localName | ||
}); | ||
|
||
referenceIdMap.set(comp.resolvedPath, referenceId); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
renderChunk(code) { | ||
if(code.includes(serverIslandPlaceholder)) { | ||
let mapSource = 'new Map(['; | ||
for(let [resolvedPath, referenceId] of referenceIdMap) { | ||
const fileName = this.getFileName(referenceId); | ||
const islandName = settings.serverIslandNameMap.get(resolvedPath)!; | ||
mapSource += `\n\t['${islandName}', () => import('./${fileName}')],` | ||
} | ||
mapSource += '\n]);'; | ||
referenceIdMap.clear(); | ||
return code.replace(serverIslandPlaceholder, mapSource); | ||
} | ||
}, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.