Skip to content

Commit

Permalink
exclude JSON script tags from JS bundling (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 authored May 23, 2024
1 parent d0a01a7 commit 28eedde
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/cli/src/lib/resource-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ async function trackResourcesForRoute(html, compilation, route) {
const scripts = await Promise.all(root.querySelectorAll('script')
.filter(script => (
isLocalLink(script.getAttribute('src')) || script.rawText)
&& script.rawAttrs.indexOf('importmap') < 0)
&& script.rawAttrs.indexOf('importmap') < 0
&& script.getAttribute('type') !== 'application/json')
.map(async(script) => {
const src = script.getAttribute('src');
const optimizationAttr = script.getAttribute('data-gwd-opt');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Build Greenwood With: ', function() {
it('should have two <script> tag with inline script in the <head>', function() {
const scriptTagInline = Array.from(dom.window.document.querySelectorAll('head > script:not([src])')).filter(tag => !tag.getAttribute('data-gwd'));

expect(scriptTagInline.length).to.be.equal(4);
expect(scriptTagInline.length).to.be.equal(5);
});

it('should have the expected inline content from inline <script> tag one in index.html', async function() {
Expand All @@ -99,6 +99,13 @@ describe('Build Greenwood With: ', function() {
expect(scriptTagSrcTwo.textContent).to.contain('document.getElementsByClassName("output-script-inline-three")[0].innerHTML="script tag module inline three"');
});

it('should have the expected <script> tag of type application/json', async function() {
const scriptJson = Array.from(dom.window.document.querySelectorAll('head > script[type="application/json"]'));

expect(scriptJson.length).to.equal(1);
expect(JSON.parse(scriptJson[0].textContent).message).to.equal('calmer than you are');
});

});

describe('non module <script src="..."></script> tag in the <head>', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
</script>

<script src="/scripts/non-module.js"></script>

<script type="application/json">
{"message": "calmer than you are" }
</script>
</head>

<body>
Expand Down

0 comments on commit 28eedde

Please sign in to comment.