Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not set partials html receive extra placeholder #4520

Merged
merged 5 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/chilled-walls-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime': patch
---

fix: html has placeholder when not set partials
fix: html 在没有设置 partials 的时候,会多出占位符
23 changes: 16 additions & 7 deletions packages/runtime/plugin-runtime/src/document/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,25 @@ export const documentPlugin = (): CliPlugin<AppTools> => ({
htmlWebpackPlugin.tags.bodyTags.toString(),
].join('');
// support partials html
const partialsContent = {
partialsTop: '',
partialsHead: '',
partialsBody: '',
};
if (partialsByEntrypoint?.[entryName]) {
const partialsTop = partialsByEntrypoint[entryName].top.join('\n');
const partialsHead = partialsByEntrypoint[entryName].head.join('\n');
const partialsBody = partialsByEntrypoint[entryName].body.join('\n');
html = html
.replace(TOP_PARTICALS_SEPARATOR, partialsTop)
.replace(HEAD_PARTICALS_SEPARATOR, partialsHead)
.replace(BODY_PARTICALS_SEPARATOR, partialsBody);
partialsContent.partialsTop =
partialsByEntrypoint[entryName].top.join('\n');
partialsContent.partialsHead =
partialsByEntrypoint[entryName].head.join('\n');
partialsContent.partialsBody =
partialsByEntrypoint[entryName].body.join('\n');
}

html = html
.replace(TOP_PARTICALS_SEPARATOR, partialsContent.partialsTop)
.replace(HEAD_PARTICALS_SEPARATOR, partialsContent.partialsHead)
.replace(BODY_PARTICALS_SEPARATOR, partialsContent.partialsBody);

const links = [
htmlWebpackPlugin.tags.headTags
.filter((item: any) => item.tagName === 'link')
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/app-document/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {
} from '@modern-js/app-tools';
import { routerPlugin } from '@modern-js/plugin-router-v5';

const tmpTest = (): CliPlugin<AppTools> => ({
export const tmpTest = (): CliPlugin<AppTools> => ({
name: 'tmpTest',
setup: () => {
return {
htmlPartials({ entrypoint, partials }) {
partials.top.push('<script>window.abc = "hjk"</script>');
partials.head.push('<script>console.log("abc")</script>');
partials.body.push('<script>console.log(abc)</script>');
if (entrypoint.entryName === 'sub') {
partials.top.push('<script>window.abc = "hjk"</script>');
partials.head.push('<script>console.log("abc")</script>');
partials.body.push('<script>console.log(abc)</script>');
}

return { partials, entrypoint };
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Link } from '@modern-js/runtime/router-v5';

const App = () => (
<>
<Link to={'/a'}>去 A</Link>
<Link to={'/b'}>去 B</Link>
</>
);

export default App;
9 changes: 9 additions & 0 deletions tests/integration/app-document/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ describe('test build', () => {
expect(htmlWithDoc.includes(`head class="head"`)).toBe(true);
});

test('when not set partial html should normal', async () => {
const normalHtml = fs.readFileSync(
path.join(appDir, 'dist', 'html/differentProperities/index.html'),
'utf-8',
);
const partialPlaceholder = encodeURIComponent('<!--<?- partials.top ?>-->');
expect(new RegExp(partialPlaceholder).test(normalHtml)).toBe(false);
});

test('should injected partial html content to html', async () => {
const htmlWithDoc = fs.readFileSync(
path.join(appDir, 'dist', 'html/sub/index.html'),
Expand Down