-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03a3196
commit 563c286
Showing
10 changed files
with
67 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@modern-js/runtime': patch | ||
--- | ||
|
||
fix: the Helment replace may meets special char | ||
fix: the Helment 替换可能会遇到特殊字符,导致替换出问题 |
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
11 changes: 11 additions & 0 deletions
11
packages/runtime/plugin-runtime/src/ssr/serverRender/constants.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,11 @@ | ||
export const HTML_PLACEHOLDER = '<!--<?- html ?>-->'; | ||
|
||
export const SSR_DATA_PLACEHOLDER = '<!--<?- SSRDataScript ?>-->'; | ||
|
||
export const CHUNK_JS_PLACEHOLDER = '<!--<?- chunksMap.js ?>-->'; | ||
|
||
export const CHUNK_CSS_PLACEHOLDER = '<!--<?- chunksMap.css ?>-->'; | ||
|
||
export const SSR_DATA_JSON_ID = '__MODERN_SSR_DATA__'; | ||
|
||
export const ROUTER_DATA_JSON_ID = '__MODERN_ROUTER_DATA__'; |
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
39 changes: 15 additions & 24 deletions
39
packages/runtime/plugin-runtime/src/ssr/serverRender/renderToString/buildHtml.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,41 +1,32 @@ | ||
export type BuildHtmlCb = (tempalte: string) => string; | ||
import { unsafeReplace } from '../utils'; | ||
import { | ||
HTML_PLACEHOLDER, | ||
SSR_DATA_PLACEHOLDER, | ||
CHUNK_JS_PLACEHOLDER, | ||
CHUNK_CSS_PLACEHOLDER, | ||
} from '../constants'; | ||
|
||
/** | ||
* It is unsafe unsafeReplace, only support serachValue exsit one time. | ||
* @param source | ||
* @param searchValue | ||
* @param replaceValue | ||
* @returns | ||
*/ | ||
function unsafeReplace( | ||
source: string, | ||
searchValue: RegExp | string, | ||
replaceValue: string, | ||
) { | ||
const [s1, s2] = source.split(searchValue); | ||
return s1 + replaceValue + s2; | ||
} | ||
export type BuildHtmlCb = (tempalte: string) => string; | ||
|
||
export function buildHtml(template: string, callbacks: BuildHtmlCb[]) { | ||
return callbacks.reduce((tmp, cb) => cb(tmp), template); | ||
} | ||
|
||
export function createReplaceHtml(html: string): BuildHtmlCb { | ||
const HTML_REMARK = '<!--<?- html ?>-->'; | ||
return (template: string) => unsafeReplace(template, HTML_REMARK, html); | ||
return (template: string) => unsafeReplace(template, HTML_PLACEHOLDER, html); | ||
} | ||
|
||
export function createReplaceSSRDataScript(data: string): BuildHtmlCb { | ||
const SSR_DATA_REMARK = '<!--<?- SSRDataScript ?>-->'; | ||
return (template: string) => unsafeReplace(template, SSR_DATA_REMARK, data); | ||
return (template: string) => | ||
unsafeReplace(template, SSR_DATA_PLACEHOLDER, data); | ||
} | ||
|
||
export function createReplaceChunkJs(js: string): BuildHtmlCb { | ||
const CHUNK_JS_REMARK = '<!--<?- chunksMap.js ?>-->'; | ||
return (template: string) => unsafeReplace(template, CHUNK_JS_REMARK, js); | ||
return (template: string) => | ||
unsafeReplace(template, CHUNK_JS_PLACEHOLDER, js); | ||
} | ||
|
||
export function createReplaceChunkCss(css: string): BuildHtmlCb { | ||
const CHUNK_CSS_REG = '<!--<?- chunksMap.css ?>-->'; | ||
return (template: string) => unsafeReplace(template, CHUNK_CSS_REG, css); | ||
return (template: string) => | ||
unsafeReplace(template, CHUNK_CSS_PLACEHOLDER, css); | ||
} |
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
22 changes: 16 additions & 6 deletions
22
packages/runtime/plugin-runtime/src/ssr/serverRender/utils.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,12 +1,22 @@ | ||
export const CSS_CHUNKS_PLACEHOLDER = '<!--<?- chunksMap.css ?>-->'; | ||
|
||
export const SSR_DATA_JSON_ID = '__MODERN_SSR_DATA__'; | ||
|
||
export const ROUTER_DATA_JSON_ID = '__MODERN_ROUTER_DATA__'; | ||
|
||
export function attributesToString(attributes: Record<string, any>) { | ||
// Iterate through the properties and convert them into a string, only including properties that are not undefined. | ||
return Object.entries(attributes).reduce((str, [key, value]) => { | ||
return value === undefined ? str : `${str} ${key}="${value}"`; | ||
}, ''); | ||
} | ||
|
||
/** | ||
* It is unsafe unsafeReplace, only support serachValue exsit one time. | ||
* @param source | ||
* @param searchValue | ||
* @param replaceValue | ||
* @returns | ||
*/ | ||
export function unsafeReplace( | ||
source: string, | ||
searchValue: RegExp | string, | ||
replaceValue: string, | ||
) { | ||
const [s1, s2] = source.split(searchValue); | ||
return s1 + replaceValue + s2; | ||
} |