@unhead/bundler 3.0.5 → 3.1.0

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.
Files changed (66) hide show
  1. package/README.md +0 -15
  2. package/dist/devtools-ui/200.html +1 -1
  3. package/dist/devtools-ui/404.html +1 -1
  4. package/dist/devtools-ui/_nuxt/C0eBIUCY.js +1 -0
  5. package/dist/devtools-ui/_nuxt/C6FB9XR7.js +1 -0
  6. package/dist/devtools-ui/_nuxt/{ddqlfTNL.js → CTIDChy9.js} +1 -1
  7. package/dist/devtools-ui/_nuxt/Cl48MOdR.js +1 -0
  8. package/dist/devtools-ui/_nuxt/D0h8RKZP.js +1 -0
  9. package/dist/devtools-ui/_nuxt/D1euPyWC.js +1 -0
  10. package/dist/devtools-ui/_nuxt/DH2dVWx3.js +1 -0
  11. package/dist/devtools-ui/_nuxt/DMn5FtwN.js +1 -0
  12. package/dist/devtools-ui/_nuxt/DQzShJ2s.js +1 -0
  13. package/dist/devtools-ui/_nuxt/DevtoolsTagTable.J276FsWb.css +1 -0
  14. package/dist/devtools-ui/_nuxt/Dy309ItU.js +1 -0
  15. package/dist/devtools-ui/_nuxt/KktNLKXM.js +1 -0
  16. package/dist/devtools-ui/_nuxt/LXzUJvwE.js +184 -0
  17. package/dist/devtools-ui/_nuxt/OQB3ITYU.js +1 -0
  18. package/dist/devtools-ui/_nuxt/S60UZsZE.js +1 -0
  19. package/dist/devtools-ui/_nuxt/{oWUcG044.js → UEnd3JDt.js} +1 -1
  20. package/dist/devtools-ui/_nuxt/{CtFJRg95.js → UKe4OZRP.js} +1 -1
  21. package/dist/devtools-ui/_nuxt/builds/latest.json +1 -1
  22. package/dist/devtools-ui/_nuxt/builds/meta/74bfd4ea-28e2-442f-93ba-55b766075483.json +1 -0
  23. package/dist/devtools-ui/_nuxt/e38X2hNB.js +1 -0
  24. package/dist/devtools-ui/_nuxt/entry.C0A6_lH-.css +1 -0
  25. package/dist/devtools-ui/_nuxt/error-404.DPPgd-2E.css +1 -0
  26. package/dist/devtools-ui/_nuxt/error-500.Df2YE7Su.css +1 -0
  27. package/dist/devtools-ui/_nuxt/sJqYz6RP.js +1 -0
  28. package/dist/devtools-ui/index.html +1 -1
  29. package/dist/framework.d.mts +72 -0
  30. package/dist/framework.d.ts +72 -0
  31. package/dist/framework.mjs +102 -0
  32. package/dist/minify/esbuild.d.mts +1 -1
  33. package/dist/minify/esbuild.d.ts +1 -1
  34. package/dist/minify/lightningcss.d.mts +1 -1
  35. package/dist/minify/lightningcss.d.ts +1 -1
  36. package/dist/minify/rolldown.d.mts +1 -1
  37. package/dist/minify/rolldown.d.ts +1 -1
  38. package/dist/shared/{bundler.CXyDmCqn.mjs → bundler.D9eYJXZg.mjs} +312 -3
  39. package/dist/shared/{bundler.BwKIGaKX.d.mts → bundler.LjD9SWtb.d.mts} +17 -2
  40. package/dist/shared/{bundler.BwKIGaKX.d.ts → bundler.LjD9SWtb.d.ts} +17 -2
  41. package/dist/vite.d.mts +10 -2
  42. package/dist/vite.d.ts +10 -2
  43. package/dist/vite.mjs +9 -244
  44. package/package.json +22 -10
  45. package/dist/devtools-ui/_nuxt/B4E3K9Fu.js +0 -1
  46. package/dist/devtools-ui/_nuxt/BMUkMEmv.js +0 -1
  47. package/dist/devtools-ui/_nuxt/BWpSa8Y9.js +0 -1
  48. package/dist/devtools-ui/_nuxt/BhGkw7Dk.js +0 -1
  49. package/dist/devtools-ui/_nuxt/Bhqqi5fM.js +0 -1
  50. package/dist/devtools-ui/_nuxt/CbZZKQdd.js +0 -1
  51. package/dist/devtools-ui/_nuxt/Cmcm1MM8.js +0 -1
  52. package/dist/devtools-ui/_nuxt/D3kFDrsW.js +0 -1
  53. package/dist/devtools-ui/_nuxt/DBI97vI-.js +0 -1
  54. package/dist/devtools-ui/_nuxt/DF0wUzLT.js +0 -1
  55. package/dist/devtools-ui/_nuxt/DNL7PE-I.js +0 -1
  56. package/dist/devtools-ui/_nuxt/DVtdvxJ7.js +0 -184
  57. package/dist/devtools-ui/_nuxt/DevtoolsTagTable.Bi_gUiSE.css +0 -1
  58. package/dist/devtools-ui/_nuxt/builds/meta/35b427b0-d95c-4e8c-ba8f-41688b3cc0a3.json +0 -1
  59. package/dist/devtools-ui/_nuxt/entry.clcQIxBm.css +0 -1
  60. package/dist/devtools-ui/_nuxt/error-404.CfdWat2O.css +0 -1
  61. package/dist/devtools-ui/_nuxt/error-500.By77bS06.css +0 -1
  62. package/dist/devtools-ui/_nuxt/p7SZRf-U.js +0 -1
  63. package/dist/devtools-ui/_nuxt/zAl29A3_.js +0 -1
  64. package/dist/webpack.d.mts +0 -5
  65. package/dist/webpack.d.ts +0 -5
  66. package/dist/webpack.mjs +0 -31
@@ -1,12 +1,321 @@
1
- import { pathToFileURL } from 'node:url';
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { relative, resolve } from 'node:path';
3
+ import { fileURLToPath, pathToFileURL } from 'node:url';
2
4
  import MagicString from 'magic-string';
5
+ import { parseAndWalk, ScopeTracker, walk, ScopeTrackerImport } from 'oxc-walker';
6
+ import { defineRpcFunction } from '@vitejs/devtools-kit';
3
7
  import { parseSync } from 'oxc-parser';
4
- import { ScopeTracker, walk, ScopeTrackerImport } from 'oxc-walker';
5
8
  import { parseURL, parseQuery } from 'ufo';
6
9
  import { createUnplugin } from 'unplugin';
7
10
  import { createContext, runInContext } from 'node:vm';
8
11
  import { resolveMetaKeyType, resolveMetaKeyValue, resolvePackedMetaObjectValue } from 'unhead/utils';
9
12
 
13
+ const getConfigRpc = defineRpcFunction({
14
+ name: "unhead:get-config",
15
+ type: "static",
16
+ setup: (ctx) => ({
17
+ handler: () => ({
18
+ cwd: ctx.cwd,
19
+ mode: ctx.mode
20
+ })
21
+ })
22
+ });
23
+
24
+ async function tryRequire() {
25
+ try {
26
+ const mod = await import('@unhead/cli');
27
+ return { runLint: mod.runLint };
28
+ } catch {
29
+ return null;
30
+ }
31
+ }
32
+ const runLintRpc = defineRpcFunction({
33
+ name: "unhead:run-lint",
34
+ type: "static",
35
+ setup: (ctx) => ({
36
+ handler: async (args = {}) => {
37
+ const lib = await tryRequire();
38
+ if (!lib) {
39
+ return {
40
+ available: false,
41
+ message: "Install @unhead/cli (and eslint as a peer) to enable in-devtools auditing."
42
+ };
43
+ }
44
+ const start = Date.now();
45
+ const mode = args.mode === "migrate" ? "migrate" : "audit";
46
+ const cwd = ctx.cwd;
47
+ const { results, errorCount, warningCount, fixableErrorCount, fixableWarningCount } = await lib.runLint({
48
+ patterns: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx,vue,svelte}"],
49
+ mode,
50
+ cwd,
51
+ ignore: ["**/node_modules/**", "**/dist/**", "**/.output/**", "**/.nuxt/**"]
52
+ });
53
+ const files = results.map((r) => ({
54
+ filePath: r.filePath,
55
+ relativePath: relative(cwd, r.filePath),
56
+ errorCount: r.errorCount,
57
+ warningCount: r.warningCount,
58
+ fixableErrorCount: r.fixableErrorCount,
59
+ fixableWarningCount: r.fixableWarningCount,
60
+ fixed: typeof r.output === "string",
61
+ messages: (r.messages || []).map((m) => ({
62
+ ruleId: m.ruleId ?? null,
63
+ message: m.message,
64
+ severity: m.severity === 2 ? "error" : "warn",
65
+ line: m.line,
66
+ column: m.column,
67
+ endLine: m.endLine,
68
+ endColumn: m.endColumn,
69
+ // Only `fix` is auto-applied by `--fix`; suggestions are editor-only.
70
+ fixable: Boolean(m.fix)
71
+ }))
72
+ })).filter((f) => f.errorCount > 0 || f.warningCount > 0 || f.fixed);
73
+ const filesFixed = files.filter((f) => f.fixed).length;
74
+ return {
75
+ available: true,
76
+ mode,
77
+ files,
78
+ errorCount,
79
+ warningCount,
80
+ fixableErrorCount,
81
+ fixableWarningCount,
82
+ filesFixed,
83
+ durationMs: Date.now() - start
84
+ };
85
+ }
86
+ })
87
+ });
88
+
89
+ const HEAD_COMPOSABLES = ["useHead", "useSeoMeta", "useHeadSafe", "useScript"];
90
+ const FILE_RE$1 = /\.(vue|tsx?|jsx?|svelte)$/;
91
+ const LEADING_SLASH_RE = /^\//;
92
+ const UNHEAD_VERSION_RE = /__UNHEAD_VERSION__ = ['"]'?["']/;
93
+ const UNHEAD_ICON = `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32' viewBox='0 0 24 24'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0%25' y1='0%25' x2='100%25' y2='100%25'%3E%3Cstop offset='0%25' stop-color='%23FBBF24'/%3E%3Cstop offset='100%25' stop-color='%23f0db4f'/%3E%3C/linearGradient%3E%3Cmask id='m'%3E%3Crect width='100%25' height='100%25' fill='white'/%3E%3Cpath d='M12 32 L1 32 L15 15 Z' fill='black'/%3E%3C/mask%3E%3C/defs%3E%3Cpath fill='none' stroke='url(%23g)' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 4v14a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4' mask='url(%23m)'/%3E%3C/svg%3E`;
94
+ const DEVTOOLS_UI_ROUTE = "/__unhead/";
95
+ function transformSourceLocations(code, id, root) {
96
+ if (!HEAD_COMPOSABLES.some((c) => code.includes(c)))
97
+ return;
98
+ const s = new MagicString(code);
99
+ let transformed = false;
100
+ const relativePath = id.startsWith(root) ? id.slice(root.length).replace(LEADING_SLASH_RE, "") : id;
101
+ parseAndWalk(code, id, {
102
+ parseOptions: { lang: "ts" },
103
+ enter(node) {
104
+ if (node.type !== "CallExpression")
105
+ return;
106
+ const callee = node.callee;
107
+ if (!callee)
108
+ return;
109
+ const name = callee.type === "Identifier" ? callee.name : callee.type === "MemberExpression" && callee.property?.type === "Identifier" ? callee.property.name : null;
110
+ if (!name || !HEAD_COMPOSABLES.includes(name))
111
+ return;
112
+ const args = node.arguments;
113
+ if (!args || args.length === 0)
114
+ return;
115
+ const lineNumber = code.slice(0, node.start).split("\n").length;
116
+ const sourceValue = `${relativePath}:${lineNumber}`;
117
+ if (args.length === 1) {
118
+ const argEnd = args[0].end;
119
+ s.appendRight(argEnd, `, { _source: ${JSON.stringify(sourceValue)} }`);
120
+ transformed = true;
121
+ } else if (args.length >= 2 && args[1].type === "ObjectExpression") {
122
+ const objStart = args[1].start + 1;
123
+ s.appendRight(objStart, ` _source: ${JSON.stringify(sourceValue)},`);
124
+ transformed = true;
125
+ }
126
+ }
127
+ });
128
+ if (!transformed)
129
+ return;
130
+ return {
131
+ code: s.toString(),
132
+ map: s.generateMap({ includeContent: true, source: id })
133
+ };
134
+ }
135
+ function unheadDevtools(options) {
136
+ let root = "";
137
+ let enabled = false;
138
+ let bridgeCode;
139
+ let unheadVersion = "";
140
+ const pkgDir = fileURLToPath(new URL("..", import.meta.url));
141
+ const devtoolsUiDir = fileURLToPath(new URL("./devtools-ui", import.meta.url));
142
+ return {
143
+ name: "@unhead/devtools",
144
+ apply: "serve",
145
+ configResolved(config) {
146
+ root = config.root;
147
+ enabled = config.plugins.some((p) => p.name?.startsWith("vite:devtools"));
148
+ if (!enabled)
149
+ return;
150
+ if (options?._ctx) {
151
+ options._ctx.addRuntimePlugin({
152
+ import: { name: "devtoolsPlugin", source: "@unhead/bundler", as: "__unhead_devtoolsPlugin" },
153
+ client: "window.__unhead_devtools__=_h",
154
+ server: "_h.use(__unhead_devtoolsPlugin())"
155
+ });
156
+ }
157
+ try {
158
+ const unheadPkg = resolve(pkgDir, "node_modules/unhead/package.json");
159
+ if (existsSync(unheadPkg))
160
+ unheadVersion = JSON.parse(readFileSync(unheadPkg, "utf-8")).version || "";
161
+ } catch {
162
+ }
163
+ const bridgePath = resolve(pkgDir, "dist/devtools/bridge.mjs");
164
+ if (existsSync(bridgePath))
165
+ bridgeCode = readFileSync(bridgePath, "utf-8");
166
+ },
167
+ configureServer(server) {
168
+ if (!enabled)
169
+ return;
170
+ server.middlewares.use("/@unhead/bridge.mjs", async (_req, res) => {
171
+ const result = await server.transformRequest("/@unhead/bridge.mjs");
172
+ res.setHeader("Content-Type", "application/javascript");
173
+ res.end(result?.code || 'console.warn("[unhead devtools] bridge not built")');
174
+ });
175
+ },
176
+ resolveId(id) {
177
+ if (!enabled)
178
+ return;
179
+ if (id === "/@unhead/bridge.mjs")
180
+ return id;
181
+ },
182
+ load(id) {
183
+ if (!enabled || id !== "/@unhead/bridge.mjs")
184
+ return;
185
+ if (!bridgeCode)
186
+ return 'console.warn("[unhead devtools] bridge not built")';
187
+ let code = bridgeCode;
188
+ if (unheadVersion)
189
+ code = code.replace(UNHEAD_VERSION_RE, `__UNHEAD_VERSION__ = '${unheadVersion}'`);
190
+ const kitClientPath = resolve(pkgDir, "node_modules/@vitejs/devtools-kit/dist/client.js");
191
+ if (existsSync(kitClientPath))
192
+ return code.replace(`'@vitejs/devtools-kit/client'`, `'${kitClientPath}'`);
193
+ return code;
194
+ },
195
+ transform: {
196
+ filter: { id: FILE_RE$1 },
197
+ handler(code, id) {
198
+ if (!enabled)
199
+ return;
200
+ return transformSourceLocations(code, id, root);
201
+ }
202
+ },
203
+ transformIndexHtml: {
204
+ // Run before non-pre HTML transforms so the injected module import
205
+ // goes through the full Vite plugin pipeline.
206
+ order: "pre",
207
+ handler() {
208
+ if (!enabled)
209
+ return [];
210
+ return [{
211
+ tag: "script",
212
+ attrs: { type: "module" },
213
+ children: `import("/@unhead/bridge.mjs")`,
214
+ injectTo: "head"
215
+ }];
216
+ }
217
+ },
218
+ devtools: {
219
+ setup(ctx) {
220
+ if (existsSync(devtoolsUiDir)) {
221
+ ctx.views.hostStatic(DEVTOOLS_UI_ROUTE, devtoolsUiDir);
222
+ }
223
+ ctx.docks.register({
224
+ id: "unhead",
225
+ title: "Unhead",
226
+ icon: UNHEAD_ICON,
227
+ type: "iframe",
228
+ url: DEVTOOLS_UI_ROUTE
229
+ });
230
+ ctx.rpc.register(getConfigRpc);
231
+ ctx.rpc.register(runLintRpc);
232
+ }
233
+ }
234
+ };
235
+ }
236
+
237
+ const FILE_RE = /\.(vue|tsx?|jsx?|svelte)$/;
238
+ const UNHEAD_SOURCE_RE = /^(?:@unhead\/[^/]+|unhead)(?:\/[^?]*)?$/;
239
+ function createHeadTransformContext() {
240
+ const registrations = [];
241
+ return {
242
+ addRuntimePlugin(reg) {
243
+ registrations.push(reg);
244
+ },
245
+ getRegistrations() {
246
+ return registrations;
247
+ }
248
+ };
249
+ }
250
+ function CreateHeadTransform(ctx) {
251
+ let root = "";
252
+ return {
253
+ name: "@unhead/create-head-transform",
254
+ apply: "serve",
255
+ configResolved(config) {
256
+ root = config.root;
257
+ },
258
+ transform: {
259
+ filter: { id: FILE_RE },
260
+ handler(code, id) {
261
+ const registrations = ctx.getRegistrations();
262
+ if (!registrations.length)
263
+ return;
264
+ if (!code.includes("createHead"))
265
+ return;
266
+ const isServer = this.environment?.config?.consumer === "server";
267
+ const envRegistrations = registrations.filter((r) => isServer ? r.server : r.client);
268
+ if (!envRegistrations.length)
269
+ return;
270
+ const s = new MagicString(code);
271
+ let transformed = false;
272
+ const directCreateHeadNames = /* @__PURE__ */ new Set();
273
+ const namespaceNames = /* @__PURE__ */ new Set();
274
+ parseAndWalk(code, id, {
275
+ parseOptions: { lang: "ts" },
276
+ enter(node) {
277
+ if (node.type === "ImportDeclaration") {
278
+ const source = node.source?.value;
279
+ if (typeof source !== "string" || !UNHEAD_SOURCE_RE.test(source))
280
+ return;
281
+ for (const spec of node.specifiers || []) {
282
+ if (spec.type === "ImportSpecifier" && spec.imported?.name === "createHead")
283
+ directCreateHeadNames.add(spec.local.name);
284
+ else if (spec.type === "ImportNamespaceSpecifier")
285
+ namespaceNames.add(spec.local.name);
286
+ }
287
+ return;
288
+ }
289
+ if (node.type !== "CallExpression")
290
+ return;
291
+ const callee = node.callee;
292
+ if (!callee)
293
+ return;
294
+ const isDirect = callee.type === "Identifier" && directCreateHeadNames.has(callee.name);
295
+ const isNamespaced = callee.type === "MemberExpression" && callee.object?.type === "Identifier" && namespaceNames.has(callee.object.name) && callee.property?.type === "Identifier" && callee.property.name === "createHead";
296
+ if (!isDirect && !isNamespaced)
297
+ return;
298
+ const statements = envRegistrations.map((r) => (isServer ? r.server : r.client).replace(/__ROOT__/g, JSON.stringify(root))).join(",");
299
+ s.prependLeft(node.start, `((_h)=>(${statements},_h))(`);
300
+ s.appendRight(node.end, `)`);
301
+ transformed = true;
302
+ }
303
+ });
304
+ if (!transformed)
305
+ return;
306
+ for (const reg of envRegistrations) {
307
+ s.prepend(`import { ${reg.import.name} as ${reg.import.as} } from '${reg.import.source}';
308
+ `);
309
+ }
310
+ return {
311
+ code: s.toString(),
312
+ map: s.generateMap({ includeContent: true, source: id })
313
+ };
314
+ }
315
+ }
316
+ };
317
+ }
318
+
10
319
  const NODE_MODULES_RE$2 = /[\\/]node_modules[\\/]/;
11
320
  const TRANSFORM_RE$2 = /\.(?:(?:c|m)?j|t)sx?$/;
12
321
  const SKIP_JS_TYPES = /* @__PURE__ */ new Set(["application/json", "application/ld+json", "speculationrules", "importmap"]);
@@ -463,4 +772,4 @@ const UseSeoMetaTransform = createUnplugin((options = {}) => {
463
772
  };
464
773
  });
465
774
 
466
- export { MinifyTransform as M, SSRStaticReplace as S, TreeshakeServerComposables as T, UseSeoMetaTransform as U };
775
+ export { CreateHeadTransform as C, MinifyTransform as M, SSRStaticReplace as S, TreeshakeServerComposables as T, UseSeoMetaTransform as U, createHeadTransformContext as c, unheadDevtools as u };
@@ -51,10 +51,25 @@ interface VitePluginOptions extends UnpluginOptions {
51
51
  devtools?: UnheadDevtoolsOptions | false;
52
52
  /** Inject ValidatePlugin in dev to surface head tag warnings in the console. Enabled by default, set `false` to disable. */
53
53
  validate?: boolean;
54
- /** @internal */
54
+ /**
55
+ * @internal
56
+ * @deprecated Pass via the `internal` second argument of `Unhead()` instead.
57
+ * Retained as a passthrough so existing framework wrappers keep working.
58
+ */
55
59
  _framework?: string;
56
60
  }
61
+ /**
62
+ * Internal extension carrying the framework package name (e.g. `@unhead/vue`)
63
+ * so the base bundler factory can import runtime plugins from the right path.
64
+ * Never exposed on public option types; framework wrappers pass this via
65
+ * the factory helpers in `./framework`.
66
+ *
67
+ * @internal
68
+ */
69
+ interface InternalFrameworkContext {
70
+ framework?: string;
71
+ }
57
72
  interface UnheadDevtoolsOptions {
58
73
  }
59
74
 
60
- export type { MinifyFn as M, UnpluginOptions as U, VitePluginOptions as V };
75
+ export type { InternalFrameworkContext as I, MinifyFn as M, VitePluginOptions as V };
@@ -51,10 +51,25 @@ interface VitePluginOptions extends UnpluginOptions {
51
51
  devtools?: UnheadDevtoolsOptions | false;
52
52
  /** Inject ValidatePlugin in dev to surface head tag warnings in the console. Enabled by default, set `false` to disable. */
53
53
  validate?: boolean;
54
- /** @internal */
54
+ /**
55
+ * @internal
56
+ * @deprecated Pass via the `internal` second argument of `Unhead()` instead.
57
+ * Retained as a passthrough so existing framework wrappers keep working.
58
+ */
55
59
  _framework?: string;
56
60
  }
61
+ /**
62
+ * Internal extension carrying the framework package name (e.g. `@unhead/vue`)
63
+ * so the base bundler factory can import runtime plugins from the right path.
64
+ * Never exposed on public option types; framework wrappers pass this via
65
+ * the factory helpers in `./framework`.
66
+ *
67
+ * @internal
68
+ */
69
+ interface InternalFrameworkContext {
70
+ framework?: string;
71
+ }
57
72
  interface UnheadDevtoolsOptions {
58
73
  }
59
74
 
60
- export type { MinifyFn as M, UnpluginOptions as U, VitePluginOptions as V };
75
+ export type { InternalFrameworkContext as I, MinifyFn as M, VitePluginOptions as V };
package/dist/vite.d.mts CHANGED
@@ -1,6 +1,14 @@
1
1
  import { Plugin } from 'vite';
2
- import { V as VitePluginOptions } from './shared/bundler.BwKIGaKX.mjs';
2
+ import { V as VitePluginOptions, I as InternalFrameworkContext } from './shared/bundler.LjD9SWtb.mjs';
3
3
 
4
- declare function Unhead(options?: VitePluginOptions): Plugin[];
4
+ /**
5
+ * Vite plugin factory that composes the core Unhead build-time transforms
6
+ * (tree-shake, seo-meta, minify, SSR static replace, devtools).
7
+ *
8
+ * Framework packages (e.g. `@unhead/vue/vite`) should not call this directly;
9
+ * use the `createFrameworkVitePlugin` helper in `./framework` which threads
10
+ * `_framework` correctly without exposing it on public options.
11
+ */
12
+ declare function Unhead(options?: VitePluginOptions, internal?: InternalFrameworkContext): Plugin[];
5
13
 
6
14
  export { Unhead, VitePluginOptions };
package/dist/vite.d.ts CHANGED
@@ -1,6 +1,14 @@
1
1
  import { Plugin } from 'vite';
2
- import { V as VitePluginOptions } from './shared/bundler.BwKIGaKX.js';
2
+ import { V as VitePluginOptions, I as InternalFrameworkContext } from './shared/bundler.LjD9SWtb.js';
3
3
 
4
- declare function Unhead(options?: VitePluginOptions): Plugin[];
4
+ /**
5
+ * Vite plugin factory that composes the core Unhead build-time transforms
6
+ * (tree-shake, seo-meta, minify, SSR static replace, devtools).
7
+ *
8
+ * Framework packages (e.g. `@unhead/vue/vite`) should not call this directly;
9
+ * use the `createFrameworkVitePlugin` helper in `./framework` which threads
10
+ * `_framework` correctly without exposing it on public options.
11
+ */
12
+ declare function Unhead(options?: VitePluginOptions, internal?: InternalFrameworkContext): Plugin[];
5
13
 
6
14
  export { Unhead, VitePluginOptions };
package/dist/vite.mjs CHANGED
@@ -1,255 +1,20 @@
1
- import { existsSync, readFileSync } from 'node:fs';
2
- import { resolve } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
- import MagicString from 'magic-string';
5
- import { parseAndWalk } from 'oxc-walker';
6
- import { defineRpcFunction } from '@vitejs/devtools-kit';
7
- import { T as TreeshakeServerComposables, U as UseSeoMetaTransform, M as MinifyTransform, S as SSRStaticReplace } from './shared/bundler.CXyDmCqn.mjs';
1
+ import { T as TreeshakeServerComposables, U as UseSeoMetaTransform, M as MinifyTransform, u as unheadDevtools, S as SSRStaticReplace, C as CreateHeadTransform, c as createHeadTransformContext } from './shared/bundler.D9eYJXZg.mjs';
2
+ import 'node:fs';
3
+ import 'node:path';
4
+ import 'node:url';
5
+ import 'magic-string';
6
+ import 'oxc-walker';
7
+ import '@vitejs/devtools-kit';
8
8
  import 'oxc-parser';
9
9
  import 'ufo';
10
10
  import 'unplugin';
11
11
  import 'node:vm';
12
12
  import 'unhead/utils';
13
13
 
14
- const getConfigRpc = defineRpcFunction({
15
- name: "unhead:get-config",
16
- type: "static",
17
- setup: (ctx) => ({
18
- handler: () => ({
19
- cwd: ctx.cwd,
20
- mode: ctx.mode
21
- })
22
- })
23
- });
24
-
25
- const HEAD_COMPOSABLES = ["useHead", "useSeoMeta", "useHeadSafe", "useScript"];
26
- const FILE_RE$1 = /\.(vue|tsx?|jsx?|svelte)$/;
27
- const LEADING_SLASH_RE = /^\//;
28
- const UNHEAD_VERSION_RE = /__UNHEAD_VERSION__ = ['"]'?["']/;
29
- const UNHEAD_ICON = `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32' viewBox='0 0 24 24'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0%25' y1='0%25' x2='100%25' y2='100%25'%3E%3Cstop offset='0%25' stop-color='%23FBBF24'/%3E%3Cstop offset='100%25' stop-color='%23f0db4f'/%3E%3C/linearGradient%3E%3Cmask id='m'%3E%3Crect width='100%25' height='100%25' fill='white'/%3E%3Cpath d='M12 32 L1 32 L15 15 Z' fill='black'/%3E%3C/mask%3E%3C/defs%3E%3Cpath fill='none' stroke='url(%23g)' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 4v14a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V4' mask='url(%23m)'/%3E%3C/svg%3E`;
30
- const DEVTOOLS_UI_ROUTE = "/__unhead/";
31
- function transformSourceLocations(code, id, root) {
32
- if (!HEAD_COMPOSABLES.some((c) => code.includes(c)))
33
- return;
34
- const s = new MagicString(code);
35
- let transformed = false;
36
- const relativePath = id.startsWith(root) ? id.slice(root.length).replace(LEADING_SLASH_RE, "") : id;
37
- parseAndWalk(code, id, {
38
- parseOptions: { lang: "ts" },
39
- enter(node) {
40
- if (node.type !== "CallExpression")
41
- return;
42
- const callee = node.callee;
43
- if (!callee)
44
- return;
45
- const name = callee.type === "Identifier" ? callee.name : callee.type === "MemberExpression" && callee.property?.type === "Identifier" ? callee.property.name : null;
46
- if (!name || !HEAD_COMPOSABLES.includes(name))
47
- return;
48
- const args = node.arguments;
49
- if (!args || args.length === 0)
50
- return;
51
- const lineNumber = code.slice(0, node.start).split("\n").length;
52
- const sourceValue = `${relativePath}:${lineNumber}`;
53
- if (args.length === 1) {
54
- const argEnd = args[0].end;
55
- s.appendRight(argEnd, `, { _source: ${JSON.stringify(sourceValue)} }`);
56
- transformed = true;
57
- } else if (args.length >= 2 && args[1].type === "ObjectExpression") {
58
- const objStart = args[1].start + 1;
59
- s.appendRight(objStart, ` _source: ${JSON.stringify(sourceValue)},`);
60
- transformed = true;
61
- }
62
- }
63
- });
64
- if (!transformed)
65
- return;
66
- return {
67
- code: s.toString(),
68
- map: s.generateMap({ includeContent: true, source: id })
69
- };
70
- }
71
- function unheadDevtools(options) {
72
- let root = "";
73
- let enabled = false;
74
- let bridgeCode;
75
- let unheadVersion = "";
76
- const pkgDir = fileURLToPath(new URL("..", import.meta.url));
77
- const devtoolsUiDir = fileURLToPath(new URL("./devtools-ui", import.meta.url));
78
- return {
79
- name: "@unhead/devtools",
80
- apply: "serve",
81
- configResolved(config) {
82
- root = config.root;
83
- enabled = config.plugins.some((p) => p.name?.startsWith("vite:devtools"));
84
- if (!enabled)
85
- return;
86
- if (options?._ctx) {
87
- options._ctx.addRuntimePlugin({
88
- import: { name: "devtoolsPlugin", source: "@unhead/bundler", as: "__unhead_devtoolsPlugin" },
89
- client: "window.__unhead_devtools__=_h",
90
- server: "_h.use(__unhead_devtoolsPlugin())"
91
- });
92
- }
93
- try {
94
- const unheadPkg = resolve(pkgDir, "node_modules/unhead/package.json");
95
- if (existsSync(unheadPkg))
96
- unheadVersion = JSON.parse(readFileSync(unheadPkg, "utf-8")).version || "";
97
- } catch {
98
- }
99
- const bridgePath = resolve(pkgDir, "dist/devtools/bridge.mjs");
100
- if (existsSync(bridgePath))
101
- bridgeCode = readFileSync(bridgePath, "utf-8");
102
- },
103
- configureServer(server) {
104
- if (!enabled)
105
- return;
106
- server.middlewares.use("/@unhead/bridge.mjs", async (_req, res) => {
107
- const result = await server.transformRequest("/@unhead/bridge.mjs");
108
- res.setHeader("Content-Type", "application/javascript");
109
- res.end(result?.code || 'console.warn("[unhead devtools] bridge not built")');
110
- });
111
- },
112
- resolveId(id) {
113
- if (!enabled)
114
- return;
115
- if (id === "/@unhead/bridge.mjs")
116
- return id;
117
- },
118
- load(id) {
119
- if (!enabled || id !== "/@unhead/bridge.mjs")
120
- return;
121
- if (!bridgeCode)
122
- return 'console.warn("[unhead devtools] bridge not built")';
123
- let code = bridgeCode;
124
- if (unheadVersion)
125
- code = code.replace(UNHEAD_VERSION_RE, `__UNHEAD_VERSION__ = '${unheadVersion}'`);
126
- const kitClientPath = resolve(pkgDir, "node_modules/@vitejs/devtools-kit/dist/client.js");
127
- if (existsSync(kitClientPath))
128
- return code.replace(`'@vitejs/devtools-kit/client'`, `'${kitClientPath}'`);
129
- return code;
130
- },
131
- transform: {
132
- filter: { id: FILE_RE$1 },
133
- handler(code, id) {
134
- if (!enabled)
135
- return;
136
- return transformSourceLocations(code, id, root);
137
- }
138
- },
139
- transformIndexHtml() {
140
- if (!enabled)
141
- return [];
142
- return [{
143
- tag: "script",
144
- attrs: { type: "module" },
145
- children: `import("/@unhead/bridge.mjs")`,
146
- injectTo: "head"
147
- }];
148
- },
149
- devtools: {
150
- setup(ctx) {
151
- if (existsSync(devtoolsUiDir)) {
152
- ctx.views.hostStatic(DEVTOOLS_UI_ROUTE, devtoolsUiDir);
153
- }
154
- ctx.docks.register({
155
- id: "unhead",
156
- title: "Unhead",
157
- icon: UNHEAD_ICON,
158
- type: "iframe",
159
- url: DEVTOOLS_UI_ROUTE
160
- });
161
- ctx.rpc.register(getConfigRpc);
162
- }
163
- }
164
- };
165
- }
166
-
167
- const FILE_RE = /\.(vue|tsx?|jsx?|svelte)$/;
168
- const UNHEAD_SOURCE_RE = /^(?:@unhead\/[^/]+|unhead)(?:\/[^?]*)?$/;
169
- function createHeadTransformContext() {
170
- const registrations = [];
171
- return {
172
- addRuntimePlugin(reg) {
173
- registrations.push(reg);
174
- },
175
- getRegistrations() {
176
- return registrations;
177
- }
178
- };
179
- }
180
- function CreateHeadTransform(ctx) {
181
- let root = "";
182
- return {
183
- name: "@unhead/create-head-transform",
184
- apply: "serve",
185
- configResolved(config) {
186
- root = config.root;
187
- },
188
- transform: {
189
- filter: { id: FILE_RE },
190
- handler(code, id) {
191
- const registrations = ctx.getRegistrations();
192
- if (!registrations.length)
193
- return;
194
- if (!code.includes("createHead"))
195
- return;
196
- const isServer = this.environment?.config?.consumer === "server";
197
- const envRegistrations = registrations.filter((r) => isServer ? r.server : r.client);
198
- if (!envRegistrations.length)
199
- return;
200
- const s = new MagicString(code);
201
- let transformed = false;
202
- const directCreateHeadNames = /* @__PURE__ */ new Set();
203
- const namespaceNames = /* @__PURE__ */ new Set();
204
- parseAndWalk(code, id, {
205
- parseOptions: { lang: "ts" },
206
- enter(node) {
207
- if (node.type === "ImportDeclaration") {
208
- const source = node.source?.value;
209
- if (typeof source !== "string" || !UNHEAD_SOURCE_RE.test(source))
210
- return;
211
- for (const spec of node.specifiers || []) {
212
- if (spec.type === "ImportSpecifier" && spec.imported?.name === "createHead")
213
- directCreateHeadNames.add(spec.local.name);
214
- else if (spec.type === "ImportNamespaceSpecifier")
215
- namespaceNames.add(spec.local.name);
216
- }
217
- return;
218
- }
219
- if (node.type !== "CallExpression")
220
- return;
221
- const callee = node.callee;
222
- if (!callee)
223
- return;
224
- const isDirect = callee.type === "Identifier" && directCreateHeadNames.has(callee.name);
225
- const isNamespaced = callee.type === "MemberExpression" && callee.object?.type === "Identifier" && namespaceNames.has(callee.object.name) && callee.property?.type === "Identifier" && callee.property.name === "createHead";
226
- if (!isDirect && !isNamespaced)
227
- return;
228
- const statements = envRegistrations.map((r) => (isServer ? r.server : r.client).replace(/__ROOT__/g, JSON.stringify(root))).join(",");
229
- s.prependLeft(node.start, `((_h)=>(${statements},_h))(`);
230
- s.appendRight(node.end, `)`);
231
- transformed = true;
232
- }
233
- });
234
- if (!transformed)
235
- return;
236
- for (const reg of envRegistrations) {
237
- s.prepend(`import { ${reg.import.name} as ${reg.import.as} } from '${reg.import.source}';
238
- `);
239
- }
240
- return {
241
- code: s.toString(),
242
- map: s.generateMap({ includeContent: true, source: id })
243
- };
244
- }
245
- }
246
- };
247
- }
248
-
249
- function Unhead(options = {}) {
14
+ function Unhead(options = {}, internal = {}) {
250
15
  const plugins = [];
251
16
  const ctx = createHeadTransformContext();
252
- const framework = options._framework;
17
+ const framework = internal.framework ?? options._framework;
253
18
  if (options.treeshake !== false) {
254
19
  const treeshakeOpts = typeof options.treeshake === "object" ? options.treeshake : {};
255
20
  plugins.push(TreeshakeServerComposables.vite({ filter: options.filter, sourcemap: options.sourcemap, ...treeshakeOpts }));