Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 6781d26

Browse files
authored
Fixed issue where default parser/serializer stripped out legit/existing html/head/body tags. (#20)
* Fixed issue where default parser/serializer stripped out legit/existing html/head/body tags. * Added a <!DOCTYPE html> to the test document to ensure it is preserved too.
1 parent 20c3d04 commit 6781d26

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
## Unreleased
1414
- Fixed issue where `nodeResolve()` did not properly effect the level of the provided `logger` where individual specifier transform logging was concerned.
1515
- Fix `Module '@babel/generator' resolves to an untyped module` error for TypeScript users.
16+
- Fix issue where html/head/body tags were stripped out by default parser/serializer.
1617
<!-- Add new unreleased items here -->
1718

1819
## [1.0.0-pre.4] - 2019-06-07

src/koa-module-specifier-transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export type ModuleSpecifierTransformOptions = {
5050
};
5151

5252
const defaultHTMLParser = (html: string): Parse5Node =>
53-
parse5Parse(html) as Parse5Node;
53+
parse5Parse(html, {sourceCodeLocationInfo: true}) as Parse5Node;
5454

5555
const defaultHTMLSerializer = (ast: Parse5Node): string => {
5656
removeFakeRootElements(ast);

src/test/koa-module-specifier-transform.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,51 @@ import {moduleSpecifierTransform} from '../koa-module-specifier-transform';
1818

1919
import {createAndServe, squeeze, testLogger} from './test-utils';
2020

21+
test(
22+
'moduleSpecifierTransform default parse/serialize preserves tags',
23+
async (t) => {
24+
t.plan(1);
25+
const logger = testLogger();
26+
createAndServe(
27+
{
28+
middleware:
29+
[moduleSpecifierTransform((_, __, ___) => './x.js', {logger})],
30+
routes: {
31+
'/my-page.html': `
32+
<!DOCTYPE html>
33+
<html>
34+
<head>
35+
<script type="module">
36+
import * as x from 'x';
37+
</script>
38+
</head>
39+
<body>
40+
Cool
41+
</body>
42+
</html>
43+
`,
44+
},
45+
},
46+
async (server) => {
47+
t.equal(
48+
squeeze((await request(server).get('/my-page.html')).text),
49+
squeeze(`
50+
<!DOCTYPE html>
51+
<html>
52+
<head>
53+
<script type="module">
54+
import * as x from './x.js';
55+
</script>
56+
</head>
57+
<body>
58+
Cool
59+
</body>
60+
</html>
61+
`),
62+
`should preserve existing html/head/body elements`);
63+
});
64+
});
65+
2166
test('moduleSpecifierTransform callback returns undefined to noop', async (t) => {
2267
t.plan(3);
2368
const logger = testLogger();

0 commit comments

Comments
 (0)