Repository: sveltejs/kit (JavaScript)

Issue:
svelte-package removes type="application/ld+json" from script tags
### Describe the bug

Running `svelte-package` removes `type="application/ld+json"` from script tags even with `preserve: ['ld+json']` using:
```
<svelte:head>
  {@html `<script type="application/ld+json" >${JSON.stringify(jsonLd)}</script>`}
</svelte:head>
```
is compiled to:
```
<svelte:head>
  {@html `<script >${JSON.stringify(jsonLd)}</script>`}
</svelte:head>
```

### Reproduction

https://github.com/TeemuKoivisto/svelte-package-removes-jsonld-type

### Logs

_No response_

### System Info

```shell
System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 75.28 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
  Browsers:
    Chrome: 105.0.5195.125
    Firefox: 104.0.1
    Safari: 15.6.1
  npmPackages:
    @sveltejs/kit: ^1.0.0-next.484 => 1.0.0-next.484 
    @sveltejs/package: ^1.0.0-next.3 => 1.0.0-next.3 
    svelte: ^3.50.1 => 3.50.1 
    vite: ^3.1.1 => 3.1.1
```


### Severity

serious, but I can work around it

### Additional Information

_No response_


Discussion:
The cause of this is that parsing script blocks to pass to preprocessors is done in a very fast and simplistic way. It also finds script tags nested in strings. 

The workaround you linked to breaks apart the script tags in the sourcecode, so that they are not found by the parser, not passed through svelte-preprocess - which ultimately strips the type.

You already found the preserve option, but i think it would have to be `preserve: 'application/ld+json'` unfortunately it looks like that produces a different error

```
> Cannot find module './transformers/ld+json'
Require stack:
- /home/dominikg/develop/reproductions/svelte-package-removes-jsonld-type/node_modules/.pnpm/svelte-preprocess@4.10.7_52dhhvjbzvlifk77xwmrmntvea/node_modules/svelte-preprocess/dist/autoProcess.js
- /home/dominikg/develop/reproductions/svelte-package-removes-jsonld-type/node_modules/.pnpm/svelte-preprocess@4.10.7_52dhhvjbzvlifk77xwmrmntvea/node_modules/svelte-preprocess/dist/index.js
Require stack:
- /home/dominikg/develop/reproductions/svelte-package-removes-jsonld-type/node_modules/.pnpm/svelte-preprocess@4.10.7_52dhhvjbzvlifk77xwmrmntvea/node_modules/svelte-preprocess/dist/autoProcess.js
- /home/dominikg/develop/reproductions/svelte-package-removes-jsonld-type/node_modules/.pnpm/svelte-preprocess@4.10.7_52dhhvjbzvlifk77xwmrmntvea/node_modules/svelte-preprocess/dist/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
```

so for now if you have to inject script tags via `svelte:head` + `@html` - which i am not too fond of in general. The workaround with splitting the script tag is what makes it work.

In a future version of svelte and/or svelte-preprocess, the way script tags are processed might change to avoid this.

There isn't much sveltekit or svelte-package could do about this, the issue should be transferred to either svelte or svelte-preprocess repo, or maybe closed with a ref to an existing issue related to this
eg  https://github.com/sveltejs/svelte-preprocess/issues/305  or https://github.com/sveltejs/svelte/issues/5292
@dominikg okay, yeah I think passing preserve should preserve the attributes as well so svelte-preprocess might be the right place. Agree about injecting script tags not being ideal yet that's how they decided to distribute the graph data.
This is a bug in `svelte-package`, adding `preserve: ['ld+json']` should work.
Yup, fixed with the latest package, amazing - thanks!



Patch:
diff --git a/.changeset/cyan-pianos-bake.md b/.changeset/cyan-pianos-bake.md
new file mode 100644
index 000000000000..7991f006db89
--- /dev/null
+++ b/.changeset/cyan-pianos-bake.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/package': patch
+---
+
+[fix] don't strip `type="application/.."` tags
diff --git a/packages/package/src/utils.js b/packages/package/src/utils.js
index 97c3b03dbf3c..0b263857b54c 100644
--- a/packages/package/src/utils.js
+++ b/packages/package/src/utils.js
@@ -49,7 +49,11 @@ export function resolve_lib_alias(file, content, config) {
  */
 export function strip_lang_tags(content) {
 	return content
-		.replace(/(<!--[^]*?-->)|(<script[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2')
+		.replace(
+			/(<!--[^]*?-->)|(<script[^>]*?)\s(?:type|lang)=(["'])(.*?)\3/g,
+			// things like application/ld+json should be kept as-is. Preprocessed languages are "ts" etc
+			(match, s1, s2, _, s4) => (s4.startsWith('application/') ? match : (s1 ?? '') + (s2 ?? ''))
+		)
 		.replace(/(<!--[^]*?-->)|(<style[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2');
 }
 


Test patch:
diff --git a/packages/package/test/fixtures/typescript/expected/Plain.svelte b/packages/package/test/fixtures/typescript/expected/Plain.svelte
index b49daed30015..29329add3320 100644
--- a/packages/package/test/fixtures/typescript/expected/Plain.svelte
+++ b/packages/package/test/fixtures/typescript/expected/Plain.svelte
@@ -2,3 +2,7 @@
 	/** @type {import('./foo').Foo} */
 	export let foo;
 </script>
+
+<svelte:head>
+  <script type="application/ld+json">{JSON.stringify(jsonLd)}</script>
+</svelte:head>
diff --git a/packages/package/test/fixtures/typescript/src/lib/Plain.svelte b/packages/package/test/fixtures/typescript/src/lib/Plain.svelte
index b49daed30015..29329add3320 100644
--- a/packages/package/test/fixtures/typescript/src/lib/Plain.svelte
+++ b/packages/package/test/fixtures/typescript/src/lib/Plain.svelte
@@ -2,3 +2,7 @@
 	/** @type {import('./foo').Foo} */
 	export let foo;
 </script>
+
+<svelte:head>
+  <script type="application/ld+json">{JSON.stringify(jsonLd)}</script>
+</svelte:head>
diff --git a/packages/package/test/fixtures/typescript/svelte.config.js b/packages/package/test/fixtures/typescript/svelte.config.js
index 6bd253bdf27d..8e74287e522a 100644
--- a/packages/package/test/fixtures/typescript/svelte.config.js
+++ b/packages/package/test/fixtures/typescript/svelte.config.js
@@ -1,5 +1,7 @@
 import preprocess from 'svelte-preprocess';
 
 export default {
-	preprocess: preprocess()
+	preprocess: preprocess({
+		preserve: ['ld+json']
+	})
 };