Skip to content

Commit f831844

Browse files
committed
fix: 🐛 fix missed headInlineStyles imports
1 parent f06dde1 commit f831844

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

.changeset/hot-tigers-post.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperse/html-webpack-plugin-loader": patch
3+
---
4+
5+
fix missed `headInlineStyles` imports

src/parser/TemplateParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class TemplateParser {
9999
* @returns The TemplateParser instance
100100
*/
101101
public upsertHeadInlineScripts(scripts: ScriptInlineItem[]): TemplateParser {
102-
upsertHeadInlineScripts(this.body, scripts);
102+
upsertHeadInlineScripts(this.head, scripts);
103103
return this;
104104
}
105105

src/parser/parseTemplate.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export const parseTemplate = (
3333
parser.upsertHeadStyles(options.headStyles);
3434
}
3535

36+
if (options.headInlineStyles?.length) {
37+
parser.upsertHeadInlineStyles(options.headInlineStyles);
38+
}
39+
3640
if (options.headScripts?.length) {
3741
parser.upsertHeadScripts(options.headScripts);
3842
}

src/parser/upsertHeadInlineScripts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ScriptInlineItem } from '../types.js';
33

44
/**
55
* Upsert the inline scripts
6-
* @param body - The body element
6+
* @param head - The body element
77
* @param scripts - The sorted script items to upsert, javascript code
88
*/
99
export const upsertHeadInlineScripts = (

tests/htmlPluginLoader.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import HtmlWebpackPlugin from 'html-webpack-plugin';
22
import { existsSync, rmSync } from 'node:fs';
33
import { join } from 'node:path';
4+
import type { TemplateOptions } from '../src/types.js';
45
import { getDirname, testWebpackPlugin } from './testUtils.js';
56

67
const fixtureCwd = getDirname(import.meta.url, './fixtures/loader');
@@ -35,13 +36,63 @@ describe('htmlPluginLoader', () => {
3536
rel: 'icon',
3637
attributes: {},
3738
},
38-
},
39+
headMetaTags: [
40+
'<meta name="description" content="default description">',
41+
],
42+
headStyles: [
43+
{
44+
id: 'style1',
45+
href: 'style1.css',
46+
position: 'beginning',
47+
},
48+
],
49+
headInlineStyles: [
50+
{
51+
id: 'style1',
52+
content: 'p {}',
53+
position: 'beginning',
54+
},
55+
],
56+
headScripts: [
57+
{
58+
id: 'script1',
59+
src: 'script1.js',
60+
position: 'beginning',
61+
},
62+
],
63+
headInlineScripts: [
64+
{
65+
id: 'script2',
66+
content: 'console.log("script2")',
67+
position: 'end',
68+
},
69+
],
70+
bodyScripts: [
71+
{
72+
id: 'script3',
73+
src: 'script3.js',
74+
position: 'end',
75+
},
76+
],
77+
} satisfies TemplateOptions,
3978
}),
4079
],
4180
},
4281
fixtureDist
4382
);
4483
expect(result).toContain('<title>default title</title>');
4584
expect(result).toContain('<link rel="icon" href="default favicon">');
85+
expect(result).toContain(
86+
'<meta name="description" content="default description">'
87+
);
88+
expect(result).toContain('<style id="style1">p {}</style>');
89+
expect(result).toContain(
90+
'<link rel="stylesheet" href="style1.css" id="style1">'
91+
);
92+
expect(result).toContain('<script id="script1" src="script1.js"></script>');
93+
expect(result).toContain(
94+
'<script id="script2">console.log("script2")</script>'
95+
);
96+
expect(result).toContain('<script id="script3" src="script3.js"></script>');
4697
});
4798
});

0 commit comments

Comments
 (0)