-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(compiler): fix prerendering in next compiler
- Loading branch information
1 parent
b99344f
commit 4669ad9
Showing
6 changed files
with
123 additions
and
50 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
34 changes: 34 additions & 0 deletions
34
src/compiler_next/transformers/component-hydrate/hydrate-component.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as d from '../../../declarations'; | ||
import { addLazyElementGetter } from '../../../compiler/transformers/component-lazy/lazy-element-getter'; | ||
import { addHydrateRuntimeCmpMeta } from './hydrate-runtime-cmp-meta'; | ||
import { addWatchers } from '../../../compiler/transformers/watcher-meta-transform'; | ||
import { removeStaticMetaProperties } from '../../../compiler/transformers/remove-static-meta-properties'; | ||
import { transformHostData } from '../../../compiler/transformers/host-data-transform'; | ||
import { updateLazyComponentConstructor } from '../../../compiler/transformers/component-lazy/lazy-constructor'; | ||
import ts from 'typescript'; | ||
|
||
|
||
export const updateHydrateComponentClass = (classNode: ts.ClassDeclaration, moduleFile: d.Module, cmp: d.ComponentCompilerMeta) => { | ||
return ts.updateClassDeclaration( | ||
classNode, | ||
classNode.decorators, | ||
classNode.modifiers, | ||
classNode.name, | ||
classNode.typeParameters, | ||
classNode.heritageClauses, | ||
updateHydrateHostComponentMembers(classNode, moduleFile, cmp) | ||
); | ||
}; | ||
|
||
|
||
const updateHydrateHostComponentMembers = (classNode: ts.ClassDeclaration, moduleFile: d.Module, cmp: d.ComponentCompilerMeta) => { | ||
const classMembers = removeStaticMetaProperties(classNode); | ||
|
||
updateLazyComponentConstructor(classMembers, moduleFile, cmp); | ||
addLazyElementGetter(classMembers, moduleFile, cmp); | ||
addWatchers(classMembers, cmp); | ||
addHydrateRuntimeCmpMeta(classMembers, cmp); | ||
transformHostData(classMembers, moduleFile); | ||
|
||
return classMembers; | ||
}; |
38 changes: 38 additions & 0 deletions
38
src/compiler_next/transformers/component-hydrate/hydrate-runtime-cmp-meta.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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as d from '../../../declarations'; | ||
import { CMP_FLAGS } from '@utils'; | ||
import { convertValueToLiteral, createStaticGetter } from '../../../compiler/transformers/transform-utils'; | ||
import ts from 'typescript'; | ||
import { addStaticStyle } from '../../../compiler_next/transformers/add-static-style'; | ||
import { formatComponentRuntimeMeta } from '../../../compiler/app-core/format-component-runtime-meta'; | ||
|
||
|
||
export const addHydrateRuntimeCmpMeta = (classMembers: ts.ClassElement[], cmp: d.ComponentCompilerMeta) => { | ||
const compactMeta: d.ComponentRuntimeMetaCompact = formatComponentRuntimeMeta(cmp, true); | ||
const cmpMeta: d.ComponentRuntimeMeta = { | ||
$flags$: compactMeta[0], | ||
$tagName$: compactMeta[1], | ||
$members$: compactMeta[2], | ||
$listeners$: compactMeta[3], | ||
$lazyBundleIds$: fakeBundleIds(cmp), | ||
$attrsToReflect$: [] | ||
}; | ||
// We always need shadow-dom shim in hydrate runtime | ||
if (cmpMeta.$flags$ & CMP_FLAGS.shadowDomEncapsulation) { | ||
cmpMeta.$flags$ |= CMP_FLAGS.needsShadowDomShim; | ||
} | ||
const staticMember = createStaticGetter('cmpMeta', convertValueToLiteral(cmpMeta)); | ||
addStaticStyle(classMembers, cmp); | ||
|
||
classMembers.push(staticMember); | ||
}; | ||
|
||
const fakeBundleIds = (cmp: d.ComponentCompilerMeta) => { | ||
if (cmp.hasMode) { | ||
const modes: any = {}; | ||
cmp.styles.forEach(s => { | ||
modes[s.modeName] = '-'; | ||
}); | ||
return modes; | ||
} | ||
return '-'; | ||
}; |
41 changes: 41 additions & 0 deletions
41
src/compiler_next/transformers/component-hydrate/tranform-to-hydrate-component.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as d from '../../../declarations'; | ||
import { addImports } from '../../../compiler/transformers/add-imports'; | ||
import { getComponentMeta, getModuleFromSourceFile } from '../../../compiler/transformers/transform-utils'; | ||
import { updateHydrateComponentClass } from './hydrate-component'; | ||
import ts from 'typescript'; | ||
import { addLegacyApis } from '../../../compiler/transformers/core-runtime-apis'; | ||
import { updateStyleImports } from '../../../compiler/transformers/style-imports'; | ||
|
||
|
||
export const hydrateComponentTransform = (compilerCtx: d.CompilerCtx, transformOpts: d.TransformOptions): ts.TransformerFactory<ts.SourceFile> => { | ||
|
||
return transformCtx => { | ||
|
||
return tsSourceFile => { | ||
const moduleFile = getModuleFromSourceFile(compilerCtx, tsSourceFile); | ||
|
||
const visitNode = (node: ts.Node): any => { | ||
if (ts.isClassDeclaration(node)) { | ||
const cmp = getComponentMeta(compilerCtx, tsSourceFile, node); | ||
if (cmp != null) { | ||
return updateHydrateComponentClass(node, moduleFile, cmp); | ||
} | ||
} | ||
|
||
return ts.visitEachChild(node, visitNode, transformCtx); | ||
}; | ||
|
||
tsSourceFile = ts.visitEachChild(tsSourceFile, visitNode, transformCtx); | ||
|
||
if (moduleFile.cmps.length > 0) { | ||
tsSourceFile = updateStyleImports(transformOpts, tsSourceFile, moduleFile); | ||
} | ||
if (moduleFile.isLegacy) { | ||
addLegacyApis(moduleFile); | ||
} | ||
tsSourceFile = addImports(transformOpts, tsSourceFile, moduleFile.coreRuntimeApis, transformOpts.coreImportPath); | ||
|
||
return tsSourceFile; | ||
}; | ||
}; | ||
}; |