-
-
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.
Move hydration to the compiler (#1547)
* Move hydration to the compiler * Move extracting url, export to util fn
- Loading branch information
Showing
8 changed files
with
132 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { pathToFileURL } from 'url'; | ||
|
||
interface ModuleInfo { | ||
module: Record<string, any>, | ||
specifier: string; | ||
} | ||
|
||
interface ComponentMetadata { | ||
componentExport: string; | ||
componentUrl: string | ||
} | ||
|
||
class HydrationMap { | ||
public fileURL: URL; | ||
private metadataCache: Map<any, ComponentMetadata | null>; | ||
constructor(fileURL: string, public modules: ModuleInfo[], components: any[]) { | ||
this.fileURL = pathToFileURL(fileURL); | ||
this.metadataCache = new Map<any, ComponentMetadata | null>(); | ||
} | ||
|
||
getPath(Component: any): string | null { | ||
const metadata = this.getComponentMetadata(Component); | ||
return metadata?.componentUrl || null; | ||
} | ||
|
||
getExport(Component: any): string | null { | ||
const metadata = this.getComponentMetadata(Component); | ||
return metadata?.componentExport || null; | ||
} | ||
|
||
private getComponentMetadata(Component: any): ComponentMetadata | null { | ||
if(this.metadataCache.has(Component)) { | ||
return this.metadataCache.get(Component)!; | ||
} | ||
const metadata = this.findComponentMetadata(Component); | ||
this.metadataCache.set(Component, metadata); | ||
return metadata; | ||
} | ||
|
||
private findComponentMetadata(Component: any): ComponentMetadata | null { | ||
const isCustomElement = typeof Component === 'string'; | ||
for (const { module, specifier } of this.modules) { | ||
const id = specifier.startsWith('.') ? new URL(specifier, this.fileURL).pathname : specifier; | ||
for (const [key, value] of Object.entries(module)) { | ||
if(isCustomElement) { | ||
if (key === 'tagName' && Component === value) { | ||
return { | ||
componentExport: key, | ||
componentUrl: id | ||
}; | ||
} | ||
} else if(Component === value) { | ||
return { | ||
componentExport: key, | ||
componentUrl: id | ||
}; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export function createHydrationMap(fileURL: string, modules: ModuleInfo[], components: any[]) { | ||
return new HydrationMap(fileURL, modules, components); | ||
} |
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