Skip to content

Commit

Permalink
fix: do not process scripts with non-JS type attribute (#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
smartrejames authored Nov 6, 2021
1 parent b2c081d commit d406c77
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shy-kangaroos-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@web/dev-server-esbuild': patch
---

Do not process scripts with non-JS type attribute
9 changes: 8 additions & 1 deletion packages/dev-server-esbuild/src/EsbuildPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ export class EsbuildPlugin implements Plugin {
const documentAst = parseHtml(context.body as string);
const inlineScripts = queryAll(
documentAst,
predicates.AND(predicates.hasTagName('script'), predicates.NOT(predicates.hasAttr('src'))),
predicates.AND(
predicates.hasTagName('script'),
predicates.NOT(predicates.hasAttr('src')),
predicates.OR(
predicates.NOT(predicates.hasAttr('type')),
predicates.hasAttrValue('type', 'module'),
),
),
);

if (inlineScripts.length === 0) {
Expand Down
36 changes: 36 additions & 0 deletions packages/dev-server-esbuild/test/target.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,40 @@ describe('esbuildPlugin target', function () {
server.stop();
}
});

it('should leave non-js types as they are', async () => {
const importmapString = '{"imports":{"foo":"./bar.js"}}';
const jsonString = '{test:1}';
const { server, host } = await createTestServer({
rootDir: __dirname,
plugins: [
{
name: 'test',
serve(context) {
if (context.path === '/index.html') {
return `<html>
<body>
<script type="importmap">${importmapString}</script>
<script type="application/json">${jsonString}</script>
</body>
</html>`;
}
},
},
esbuildPlugin({ js: true, target: 'es2016' }),
],
});

try {
const response = await fetch(`${host}/index.html`);
const text = await response.text();

expect(response.status).to.equal(200);
expect(response.headers.get('content-type')).to.equal('text/html; charset=utf-8');
expect(text).to.include(importmapString);
expect(text).to.include(jsonString);
} finally {
server.stop();
}
});
});

0 comments on commit d406c77

Please sign in to comment.