@unhead/svelte 3.0.0-beta.5 → 3.0.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import MagicString from 'magic-string';
2
- import { findStaticImports } from 'mlly';
2
+ import { parseAndWalk } from 'oxc-walker';
3
3
  import { createStreamingPlugin } from 'unhead/stream/vite';
4
4
 
5
5
  function transform(code, id, isSSR, s) {
@@ -15,14 +15,30 @@ function transform(code, id, isSSR, s) {
15
15
  if (!scriptMatch)
16
16
  return true;
17
17
  const scriptEnd = scriptMatch.index + scriptMatch[0].length;
18
- const imports = findStaticImports(code);
19
- const existing = imports.find((i) => i.specifier === importPath);
20
- if (existing) {
21
- if (!existing.imports?.includes("HeadStream")) {
22
- const inner = existing.imports?.replace(/^\{\s*|\s*\}\s*$/g, "").trim() || "";
18
+ const scriptCloseIndex = code.indexOf("<\/script>", scriptEnd);
19
+ if (scriptCloseIndex === -1)
20
+ return true;
21
+ const scriptContent = code.slice(scriptEnd, scriptCloseIndex);
22
+ let existingImport = null;
23
+ parseAndWalk(scriptContent, id, {
24
+ parseOptions: { lang: "ts" },
25
+ enter(node) {
26
+ if (node.type === "ImportDeclaration" && node.source.value === importPath) {
27
+ existingImport = {
28
+ start: scriptEnd + node.start,
29
+ end: scriptEnd + node.end,
30
+ specifiers: node.specifiers?.map((spec) => spec.local?.name).filter(Boolean) || []
31
+ };
32
+ this.skip();
33
+ }
34
+ }
35
+ });
36
+ const foundImport = existingImport;
37
+ if (foundImport) {
38
+ if (!foundImport.specifiers.includes("HeadStream")) {
39
+ const inner = foundImport.specifiers.join(", ");
23
40
  const newImports = inner ? `${inner}, HeadStream` : "HeadStream";
24
- s.overwrite(existing.start, existing.end, `import { ${newImports} } from '${importPath}'
25
- `);
41
+ s.overwrite(foundImport.start, foundImport.end, `import { ${newImports} } from '${importPath}'`);
26
42
  }
27
43
  } else {
28
44
  s.appendRight(scriptEnd, `
@@ -33,10 +49,9 @@ import { HeadStream } from '${importPath}'`);
33
49
  function unheadSveltePlugin(options) {
34
50
  return createStreamingPlugin({
35
51
  framework: "@unhead/svelte",
52
+ filter: /\.svelte$/,
36
53
  mode: options?.mode,
37
54
  transform(code, id, opts) {
38
- if (!/\.svelte$/.test(id))
39
- return null;
40
55
  const s = new MagicString(code);
41
56
  if (!transform(code, id, opts?.ssr ?? false, s))
42
57
  return null;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/svelte",
3
3
  "type": "module",
4
- "version": "3.0.0-beta.5",
4
+ "version": "3.0.0-beta.7",
5
5
  "description": "Full-stack <head> manager built for Svelte.",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -98,14 +98,15 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "magic-string": "^0.30.21",
101
- "mlly": "^1.8.0",
102
- "unhead": "3.0.0-beta.5"
101
+ "oxc-parser": "^0.106.0",
102
+ "oxc-walker": "^0.7.0",
103
+ "unhead": "3.0.0-beta.7"
103
104
  },
104
105
  "devDependencies": {
105
- "@sveltejs/vite-plugin-svelte": "^6.2.1",
106
+ "@sveltejs/vite-plugin-svelte": "^6.2.4",
106
107
  "@testing-library/svelte": "^5.3.1",
107
108
  "svelte": "^5.46.1",
108
- "vite": "7.2.2"
109
+ "vite": "^7.2.2"
109
110
  },
110
111
  "scripts": {
111
112
  "build": "unbuild .",