Repository: ProjectEvergreen/greenwood (js)

Issue:
`<script>` tags of type `application/json` are breaking production builds
## Summary

This was brought up in Slack, in that if you add a script tag of `type="application/json"`
```html
<html>
  <head>
    <script type="application/json">
      {"message": "calmer than you are"}
     </script>
  </head>
  <!-- ... --->
</html>
```


it will cause a build failure for Rollup / acorn, and was able to reproduce in one of the basic Greenwood test cases
<details>
  <pre>
  SyntaxError: Unexpected token (2:16)
    at pp$4.raise (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:19420:13)
    at pp$9.unexpected (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:16714:8)
    at pp$9.semicolon (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:16691:66)
    at pp$8.parseExpressionStatement (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:17174:8)
    at pp$8.parseStatement (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:16907:24)
    at pp$8.parseBlock (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:17190:21)
    at pp$8.parseStatement (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:16872:36)
    at pp$8.parseTopLevel (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:16771:21)
    at Parser.parse (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:16543:15)
    at Function.parse (file:///Users/owenbuckley/Workspace/project-evergreen/greenwood/node_modules/rollup/dist/es/shared/rollup.js:16593:35) {
  pos: 17,
  loc: Position { line: 2, column: 16 },
  raisedAt: 18,
  code: 'PLUGIN_ERROR',
  plugin: 'greenwood-import-meta-url',
  hook: 'transform',
  id: '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/.greenwood/1911554461.js',
  watchFiles: [
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/.greenwood/481423853.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/.greenwood/1165792367.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/.greenwood/439078047.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/.greenwood/2110337203.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/.greenwood/1911554461.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/.greenwood/758806547.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/src/scripts/other.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/src/scripts/non-module.js',
    '/Users/owenbuckley/Workspace/project-evergreen/greenwood/packages/cli/test/cases/build.default.workspace-javascript-css/src/scripts/popup.js'
  ]
}
  </pre>
</details>

## Details
Irony is, this is actually solved on the `v0.30.0-alpha.0` line but I snuck it into https://github.com/ProjectEvergreen/greenwood/commit/82460ea4f83a11810e24971d3008ab50f9dd3925#diff-1db487262eefc65529b67d5d3b1b1c040f9f0cb880d281965d61b11c5acaba4b (I got greedy and should have really made it its own bug) but since that release line is still a WIP with a couple heavy breaking changes, I think we should backport it.  (and I didn't write any test cases, shame on me 😭 )

Pull request description:
bug/issue 1230 exclude script tags of type application/json from bundling
<!--
## Submitting a Pull Request
We love contributions and appreciate any help you can offer!
-->

## Related Issue
resolves #1230 

## Summary of Changes
1. Back-porting this fix - https://github.com/ProjectEvergreen/greenwood/commit/82460ea4f83a11810e24971d3008ab50f9dd3925#diff-1db487262eefc65529b67d5d3b1b1c040f9f0cb880d281965d61b11c5acaba4b
1. Actually add tests 😅 

Patch:
diff --git a/packages/cli/src/lib/resource-utils.js b/packages/cli/src/lib/resource-utils.js
index a3d71e57..1032afc5 100644
--- a/packages/cli/src/lib/resource-utils.js
+++ b/packages/cli/src/lib/resource-utils.js
@@ -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');


Test patch:
diff --git a/packages/cli/test/cases/build.default.workspace-javascript-css/build.default.workspace-javascript-css.spec.js b/packages/cli/test/cases/build.default.workspace-javascript-css/build.default.workspace-javascript-css.spec.js
index 1265601d..8a537669 100644
--- a/packages/cli/test/cases/build.default.workspace-javascript-css/build.default.workspace-javascript-css.spec.js
+++ b/packages/cli/test/cases/build.default.workspace-javascript-css/build.default.workspace-javascript-css.spec.js
@@ -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() {
@@ -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() {
diff --git a/packages/cli/test/cases/build.default.workspace-javascript-css/src/pages/index.html b/packages/cli/test/cases/build.default.workspace-javascript-css/src/pages/index.html
index 75cb0dd3..fbb7de6f 100644
--- a/packages/cli/test/cases/build.default.workspace-javascript-css/src/pages/index.html
+++ b/packages/cli/test/cases/build.default.workspace-javascript-css/src/pages/index.html
@@ -47,6 +47,10 @@
     </script>
 
     <script src="/scripts/non-module.js"></script>
+
+    <script type="application/json">
+      {"message": "calmer than you are" }
+     </script>
   </head>
 
   <body>