@unhead/solid-js 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,43 +1,64 @@
1
1
  import MagicString from 'magic-string';
2
- import { findStaticImports } from 'mlly';
3
- import { parseSync, Visitor } from 'oxc-parser';
2
+ import { parseAndWalk } from 'oxc-walker';
4
3
  import { createStreamingPlugin } from 'unhead/stream/vite';
5
4
 
6
5
  function transform(code, id, isSSR, s) {
7
6
  const lang = id.endsWith(".tsx") ? "tsx" : id.endsWith(".jsx") ? "jsx" : "tsx";
8
- const result = parseSync(id, code, { lang });
9
- if (result.errors.length > 0)
10
- return false;
11
7
  const returns = [];
12
- const visitor = new Visitor({
13
- FunctionDeclaration: (node) => processFunction(node),
14
- FunctionExpression: (node) => processFunction(node),
15
- ArrowFunctionExpression: (node) => processFunction(node)
16
- });
17
- function processFunction(node) {
18
- if (!node.body)
19
- return;
20
- const bodyCode = code.slice(node.body.start, node.body.end);
21
- if (!bodyCode.includes("useHead") && !bodyCode.includes("useSeoMeta") && !bodyCode.includes("useHeadSafe"))
22
- return;
23
- if (node.body.type === "JSXElement" || node.body.type === "JSXFragment") {
24
- returns.push({ jsxStart: node.body.start, jsxEnd: node.body.end });
25
- return;
26
- }
27
- const innerVisitor = new Visitor({
28
- ReturnStatement(innerNode) {
29
- if (!innerNode.argument)
8
+ let currentFnHasHead = false;
9
+ const fnStack = [];
10
+ const importPath = isSSR ? "@unhead/solid-js/stream/server" : "@unhead/solid-js/stream/client";
11
+ let existingImport = null;
12
+ let lastImportEnd = -1;
13
+ const result = parseAndWalk(code, id, {
14
+ parseOptions: { lang },
15
+ enter(node) {
16
+ if (node.type === "ImportDeclaration") {
17
+ if (node.source.value === importPath) {
18
+ existingImport = {
19
+ start: node.start,
20
+ end: node.end,
21
+ specifiers: node.specifiers?.map((spec) => spec.local?.name).filter(Boolean) || []
22
+ };
23
+ }
24
+ if (node.end > lastImportEnd)
25
+ lastImportEnd = node.end;
26
+ this.skip();
27
+ return;
28
+ }
29
+ const isFn = node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
30
+ if (isFn) {
31
+ fnStack.push(currentFnHasHead);
32
+ if (!node.body) {
33
+ currentFnHasHead = false;
34
+ return;
35
+ }
36
+ const bodyCode = code.slice(node.body.start, node.body.end);
37
+ currentFnHasHead = bodyCode.includes("useHead") || bodyCode.includes("useSeoMeta") || bodyCode.includes("useHeadSafe");
38
+ if (currentFnHasHead && (node.body.type === "JSXElement" || node.body.type === "JSXFragment")) {
39
+ returns.push({ jsxStart: node.body.start, jsxEnd: node.body.end });
40
+ }
41
+ return;
42
+ }
43
+ if (currentFnHasHead && node.type === "ReturnStatement") {
44
+ if (!node.argument)
30
45
  return;
31
- let arg = innerNode.argument;
46
+ let arg = node.argument;
32
47
  if (arg.type === "ParenthesizedExpression" && arg.expression)
33
48
  arg = arg.expression;
34
49
  if (arg.type === "JSXElement" || arg.type === "JSXFragment")
35
50
  returns.push({ jsxStart: arg.start, jsxEnd: arg.end });
36
51
  }
37
- });
38
- innerVisitor.visit(node.body);
39
- }
40
- visitor.visit(result.program);
52
+ },
53
+ leave(node) {
54
+ const isFn = node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
55
+ if (isFn) {
56
+ currentFnHasHead = fnStack.pop();
57
+ }
58
+ }
59
+ });
60
+ if (result.errors.length > 0)
61
+ return false;
41
62
  if (returns.length === 0)
42
63
  return false;
43
64
  returns.sort((a, b) => b.jsxStart - a.jsxStart);
@@ -45,22 +66,17 @@ function transform(code, id, isSSR, s) {
45
66
  const jsxCode = code.slice(ret.jsxStart, ret.jsxEnd);
46
67
  s.overwrite(ret.jsxStart, ret.jsxEnd, `<><HeadStream />${jsxCode}</>`);
47
68
  }
48
- const importPath = isSSR ? "@unhead/solid-js/stream/server" : "@unhead/solid-js/stream/client";
49
- const imports = findStaticImports(code);
50
- const existing = imports.find((i) => i.specifier === importPath);
51
- if (existing) {
52
- if (!existing.imports?.includes("HeadStream")) {
53
- const inner = existing.imports?.replace(/^\{\s*|\s*\}\s*$/g, "").trim() || "";
54
- s.overwrite(existing.start, existing.end, `import { ${inner ? `${inner}, ` : ""}HeadStream } from '${importPath}'
55
- `);
69
+ const foundImport = existingImport;
70
+ if (foundImport) {
71
+ if (!foundImport.specifiers.includes("HeadStream")) {
72
+ const inner = foundImport.specifiers.join(", ");
73
+ s.overwrite(foundImport.start, foundImport.end, `import { ${inner ? `${inner}, ` : ""}HeadStream } from '${importPath}'`);
56
74
  }
75
+ } else if (lastImportEnd > -1) {
76
+ s.appendLeft(lastImportEnd, `
77
+ import { HeadStream } from '${importPath}'`);
57
78
  } else {
58
- const last = imports[imports.length - 1];
59
- if (last)
60
- s.appendLeft(last.end, `import { HeadStream } from '${importPath}'
61
- `);
62
- else
63
- s.prepend(`import { HeadStream } from '${importPath}'
79
+ s.prepend(`import { HeadStream } from '${importPath}'
64
80
  `);
65
81
  }
66
82
  return true;
@@ -68,13 +84,11 @@ function transform(code, id, isSSR, s) {
68
84
  function unheadSolidPlugin(options) {
69
85
  return createStreamingPlugin({
70
86
  framework: "@unhead/solid-js",
87
+ filter: /\.[jt]sx$/,
71
88
  mode: options?.mode,
72
89
  transform(code, id, opts) {
73
- if (!/\.[jt]sx$/.test(id))
74
- return null;
75
90
  const s = new MagicString(code);
76
- const transformed = transform(code, id, opts?.ssr ?? false, s);
77
- if (!transformed)
91
+ if (!transform(code, id, opts?.ssr ?? false, s))
78
92
  return null;
79
93
  return {
80
94
  code: s.toString(),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/solid-js",
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 SolidJS.",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -98,16 +98,16 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "magic-string": "^0.30.21",
101
- "mlly": "^1.8.0",
102
101
  "oxc-parser": "^0.106.0",
103
- "unhead": "3.0.0-beta.5"
102
+ "oxc-walker": "^0.7.0",
103
+ "unhead": "3.0.0-beta.7"
104
104
  },
105
105
  "devDependencies": {
106
106
  "@solidjs/testing-library": "^0.8.10",
107
107
  "@testing-library/jest-dom": "^6.9.1",
108
108
  "@testing-library/user-event": "14.6.1",
109
109
  "solid-js": "^1.9.10",
110
- "vite": "7.2.2",
110
+ "vite": "^7.2.2",
111
111
  "vite-plugin-solid": "^2.11.10"
112
112
  },
113
113
  "scripts": {