@nuxt/scripts 0.0.0 → 0.1.1

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 (256) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +255 -0
  3. package/dist/client/200.html +238 -0
  4. package/dist/client/404.html +238 -0
  5. package/dist/client/_nuxt/-7WWnkdy.js +517 -0
  6. package/dist/client/_nuxt/1Je9_Reo.js +136 -0
  7. package/dist/client/_nuxt/37ic6j3l.js +1 -0
  8. package/dist/client/_nuxt/3ghuYFLd.js +1 -0
  9. package/dist/client/_nuxt/6Kuza3Wr.js +1 -0
  10. package/dist/client/_nuxt/6XBPEST2.js +1 -0
  11. package/dist/client/_nuxt/7fd6vGzb.js +1 -0
  12. package/dist/client/_nuxt/8t4jSAMm.js +1 -0
  13. package/dist/client/_nuxt/9B1nZgL-.js +1 -0
  14. package/dist/client/_nuxt/9ZOzOPqN.js +1 -0
  15. package/dist/client/_nuxt/A1WiD9SJ.js +1 -0
  16. package/dist/client/_nuxt/AgnqFTPX.js +13 -0
  17. package/dist/client/_nuxt/B1nm99XP.js +2 -0
  18. package/dist/client/_nuxt/B2BuIiKK.js +1 -0
  19. package/dist/client/_nuxt/B2DkETJQ.js +1 -0
  20. package/dist/client/_nuxt/B4yE-MfL.js +38 -0
  21. package/dist/client/_nuxt/B6nY7VDA.js +25 -0
  22. package/dist/client/_nuxt/B9CT-1u6.js +7 -0
  23. package/dist/client/_nuxt/B9E_xdxs.js +666 -0
  24. package/dist/client/_nuxt/B9S9BZZh.js +1 -0
  25. package/dist/client/_nuxt/BBDuFDsq.js +1 -0
  26. package/dist/client/_nuxt/BBJYjQ0k.js +1 -0
  27. package/dist/client/_nuxt/BBv1LFm6.js +6 -0
  28. package/dist/client/_nuxt/BF6G6X0H.js +1 -0
  29. package/dist/client/_nuxt/BH1M7C1g.js +1 -0
  30. package/dist/client/_nuxt/BH_RS3WO.js +1 -0
  31. package/dist/client/_nuxt/BICIrM8O.js +1 -0
  32. package/dist/client/_nuxt/BILqrcpa.js +1 -0
  33. package/dist/client/_nuxt/BIMuYTWL.js +3 -0
  34. package/dist/client/_nuxt/BITGhEdf.js +1 -0
  35. package/dist/client/_nuxt/BIxS-Weu.js +1 -0
  36. package/dist/client/_nuxt/BLluXI4E.js +1 -0
  37. package/dist/client/_nuxt/BN4LRXf6.js +4 -0
  38. package/dist/client/_nuxt/BOnd8D4O.js +186 -0
  39. package/dist/client/_nuxt/BOpLLL-w.js +1 -0
  40. package/dist/client/_nuxt/BPiaQZfK.js +1 -0
  41. package/dist/client/_nuxt/BR6CMsBL.js +1 -0
  42. package/dist/client/_nuxt/BSB_bK09.js +1 -0
  43. package/dist/client/_nuxt/BSyl5yf0.js +1 -0
  44. package/dist/client/_nuxt/BT9ZzGyQ.js +69 -0
  45. package/dist/client/_nuxt/BTtaZsq5.js +1 -0
  46. package/dist/client/_nuxt/BUpeXbsf.js +25 -0
  47. package/dist/client/_nuxt/BWKJ1FCf.js +239 -0
  48. package/dist/client/_nuxt/BXF7Vm5l.js +1 -0
  49. package/dist/client/_nuxt/BXfENWL6.js +1 -0
  50. package/dist/client/_nuxt/BZz1Hhek.js +1 -0
  51. package/dist/client/_nuxt/B_SUYfiV.js +1 -0
  52. package/dist/client/_nuxt/BaNxgKSR.js +17 -0
  53. package/dist/client/_nuxt/BaWyeHV_.js +1 -0
  54. package/dist/client/_nuxt/BaedD2tq.js +1 -0
  55. package/dist/client/_nuxt/BbJj1K1w.js +1 -0
  56. package/dist/client/_nuxt/Bbv8ARUV.js +13 -0
  57. package/dist/client/_nuxt/Bd7iooY8.js +8 -0
  58. package/dist/client/_nuxt/Bdb36Id5.js +1 -0
  59. package/dist/client/_nuxt/BeX7Iu5M.js +90 -0
  60. package/dist/client/_nuxt/BeocmOPF.js +1 -0
  61. package/dist/client/_nuxt/Bg0wAK5a.js +100 -0
  62. package/dist/client/_nuxt/BgBwMnrY.js +76 -0
  63. package/dist/client/_nuxt/Bgpqy2XC.js +1 -0
  64. package/dist/client/_nuxt/Bh8mriwU.js +17 -0
  65. package/dist/client/_nuxt/BiDE021q.js +1 -0
  66. package/dist/client/_nuxt/Bj5xdiaE.js +1 -0
  67. package/dist/client/_nuxt/BlxWTfDV.js +699 -0
  68. package/dist/client/_nuxt/BmCW-tYz.js +86 -0
  69. package/dist/client/_nuxt/Bn5gmY5k.js +1 -0
  70. package/dist/client/_nuxt/BrGg7AAd.js +1 -0
  71. package/dist/client/_nuxt/BsfQk1vf.js +1 -0
  72. package/dist/client/_nuxt/Bsp1M94H.js +1 -0
  73. package/dist/client/_nuxt/BsvsQ1iS.js +1 -0
  74. package/dist/client/_nuxt/BswfO-CF.js +1 -0
  75. package/dist/client/_nuxt/Btshr8M_.js +70 -0
  76. package/dist/client/_nuxt/BxwAa5i0.js +1 -0
  77. package/dist/client/_nuxt/ByMv4Xf1.js +1 -0
  78. package/dist/client/_nuxt/Bz4xi2rU.js +1 -0
  79. package/dist/client/_nuxt/C15OxdQ5.js +1 -0
  80. package/dist/client/_nuxt/C1DVX8_8.js +831 -0
  81. package/dist/client/_nuxt/C2TBxDwV.js +1 -0
  82. package/dist/client/_nuxt/C3YqBJkQ.js +1 -0
  83. package/dist/client/_nuxt/C5gCGmDW.js +200 -0
  84. package/dist/client/_nuxt/C6Aznpr-.js +1 -0
  85. package/dist/client/_nuxt/C7oZ9nno.js +1 -0
  86. package/dist/client/_nuxt/C86elO-m.js +1 -0
  87. package/dist/client/_nuxt/C9d3aiqh.js +1 -0
  88. package/dist/client/_nuxt/CApMHEaB.js +1 -0
  89. package/dist/client/_nuxt/CBL0qBdF.js +1 -0
  90. package/dist/client/_nuxt/CBPJd_fO.js +1 -0
  91. package/dist/client/_nuxt/CG4fK2Nq.js +21 -0
  92. package/dist/client/_nuxt/CGQbO34C.js +1 -0
  93. package/dist/client/_nuxt/CIekO_uJ.js +1 -0
  94. package/dist/client/_nuxt/CJQrLysU.js +1 -0
  95. package/dist/client/_nuxt/CJxX2Ta8.js +1 -0
  96. package/dist/client/_nuxt/CL7Ixq1B.js +1 -0
  97. package/dist/client/_nuxt/CMDmr8et.js +2 -0
  98. package/dist/client/_nuxt/CPHkTjfH.js +1 -0
  99. package/dist/client/_nuxt/CQTpvb7m.js +1 -0
  100. package/dist/client/_nuxt/CQUCfALx.js +13 -0
  101. package/dist/client/_nuxt/CRDKj6ck.js +1 -0
  102. package/dist/client/_nuxt/CRWwmX0L.js +1 -0
  103. package/dist/client/_nuxt/CRlnGVMD.js +1 -0
  104. package/dist/client/_nuxt/CThH5sBG.js +1 -0
  105. package/dist/client/_nuxt/CUVblut_.js +1 -0
  106. package/dist/client/_nuxt/CX_FIdg1.js +1 -0
  107. package/dist/client/_nuxt/CZZ6oYdA.js +1 -0
  108. package/dist/client/_nuxt/C_8Fx7bH.js +1 -0
  109. package/dist/client/_nuxt/CbViG2Xw.js +74 -0
  110. package/dist/client/_nuxt/CbYhyuC0.js +505 -0
  111. package/dist/client/_nuxt/Cda-MJFk.js +1 -0
  112. package/dist/client/_nuxt/CdedUr_-.js +1 -0
  113. package/dist/client/_nuxt/CfPQhs6g.js +7 -0
  114. package/dist/client/_nuxt/Cicx_DS6.js +1 -0
  115. package/dist/client/_nuxt/CkMwVfOK.js +1 -0
  116. package/dist/client/_nuxt/ClXEvkw9.js +68 -0
  117. package/dist/client/_nuxt/CoEqzGFw.js +44 -0
  118. package/dist/client/_nuxt/CowR2XfX.js +1 -0
  119. package/dist/client/_nuxt/CrhH3_Og.js +1 -0
  120. package/dist/client/_nuxt/CrjQeCwm.js +1 -0
  121. package/dist/client/_nuxt/CsTmP73Z.js +1 -0
  122. package/dist/client/_nuxt/CsYA0RN7.js +1 -0
  123. package/dist/client/_nuxt/CsyjKwr8.js +1 -0
  124. package/dist/client/_nuxt/CsyrCbsw.js +699 -0
  125. package/dist/client/_nuxt/Cthz8VOO.js +771 -0
  126. package/dist/client/_nuxt/CupVZNk8.js +25 -0
  127. package/dist/client/_nuxt/CxrokwfH.js +1 -0
  128. package/dist/client/_nuxt/CzPA46E-.js +1 -0
  129. package/dist/client/_nuxt/D-pQtGOP.js +1 -0
  130. package/dist/client/_nuxt/D384ylkT.js +1 -0
  131. package/dist/client/_nuxt/D3VXSfF0.js +1 -0
  132. package/dist/client/_nuxt/D4LaosRr.js +1 -0
  133. package/dist/client/_nuxt/D4LhxNOI.js +1 -0
  134. package/dist/client/_nuxt/D4k753MY.js +1 -0
  135. package/dist/client/_nuxt/D6NljDpC.js +1 -0
  136. package/dist/client/_nuxt/D6pmzCqS.js +1 -0
  137. package/dist/client/_nuxt/D7gg1Usp.js +1 -0
  138. package/dist/client/_nuxt/DAGYewaG.js +559 -0
  139. package/dist/client/_nuxt/DAVo6uMX.js +1 -0
  140. package/dist/client/_nuxt/DBxHOdLe.js +1 -0
  141. package/dist/client/_nuxt/DC4hdoM9.js +87 -0
  142. package/dist/client/_nuxt/DC8MraHL.js +666 -0
  143. package/dist/client/_nuxt/DCAtC51B.js +80 -0
  144. package/dist/client/_nuxt/DCdPDLy4.js +40 -0
  145. package/dist/client/_nuxt/DCj4DH6i.js +1 -0
  146. package/dist/client/_nuxt/DCk2z-Tu.js +129 -0
  147. package/dist/client/_nuxt/DDRuGeQH.js +1 -0
  148. package/dist/client/_nuxt/DGO8GyiP.js +1 -0
  149. package/dist/client/_nuxt/DHmGyzbM.js +109 -0
  150. package/dist/client/_nuxt/DISxKEhY.js +756 -0
  151. package/dist/client/_nuxt/DIr-ordK.js +1 -0
  152. package/dist/client/_nuxt/DJZWsvj0.js +1 -0
  153. package/dist/client/_nuxt/DLP9ZvWb.js +3 -0
  154. package/dist/client/_nuxt/DNR26wTC.js +1 -0
  155. package/dist/client/_nuxt/DOtRmeXT.js +1 -0
  156. package/dist/client/_nuxt/DSrLtGYv.js +15 -0
  157. package/dist/client/_nuxt/DSsji4Hu.js +1 -0
  158. package/dist/client/_nuxt/DUdlC5k_.js +1 -0
  159. package/dist/client/_nuxt/DVfdqzEq.js +1 -0
  160. package/dist/client/_nuxt/DXFkqnOI.js +816 -0
  161. package/dist/client/_nuxt/DXT9h7v2.js +1 -0
  162. package/dist/client/_nuxt/DXZpi2gR.js +1 -0
  163. package/dist/client/_nuxt/DXpYegZJ.js +1 -0
  164. package/dist/client/_nuxt/DY7_9Ga-.js +1 -0
  165. package/dist/client/_nuxt/DYI1rfmx.js +25 -0
  166. package/dist/client/_nuxt/DZ3fVoEH.js +1 -0
  167. package/dist/client/_nuxt/DZqG9GXz.js +50 -0
  168. package/dist/client/_nuxt/DbK06e1c.js +21 -0
  169. package/dist/client/_nuxt/Dble9ECP.js +1 -0
  170. package/dist/client/_nuxt/Dch3xQiY.js +699 -0
  171. package/dist/client/_nuxt/DdacRhvC.js +1 -0
  172. package/dist/client/_nuxt/DeBxnUN1.js +1 -0
  173. package/dist/client/_nuxt/DeVv6D4_.js +1 -0
  174. package/dist/client/_nuxt/Dez-Qvcc.js +544 -0
  175. package/dist/client/_nuxt/DfUNg_8t.js +1 -0
  176. package/dist/client/_nuxt/DfeQjIbs.js +5 -0
  177. package/dist/client/_nuxt/Dg0DGYq4.js +14 -0
  178. package/dist/client/_nuxt/DggdVF2v.js +1 -0
  179. package/dist/client/_nuxt/DhFF74a2.js +1 -0
  180. package/dist/client/_nuxt/DjTlIhuc.js +1 -0
  181. package/dist/client/_nuxt/DmDrTTlz.js +1 -0
  182. package/dist/client/_nuxt/DnhXxWz-.js +1 -0
  183. package/dist/client/_nuxt/DouzTJIq.js +1 -0
  184. package/dist/client/_nuxt/DsfXcHUZ.js +29 -0
  185. package/dist/client/_nuxt/DsudziMp.js +16 -0
  186. package/dist/client/_nuxt/Dszwcx0o.js +1 -0
  187. package/dist/client/_nuxt/Due3_iNp.js +36 -0
  188. package/dist/client/_nuxt/DvyTQcux.js +1 -0
  189. package/dist/client/_nuxt/Dwo2i-71.js +1 -0
  190. package/dist/client/_nuxt/DwuVtWc2.js +518 -0
  191. package/dist/client/_nuxt/DywwDckn.js +159 -0
  192. package/dist/client/_nuxt/Dz2Ca8U2.js +1 -0
  193. package/dist/client/_nuxt/DzPyIVdT.js +9 -0
  194. package/dist/client/_nuxt/DzwdaLuO.js +223 -0
  195. package/dist/client/_nuxt/Eh5U-gDp.js +52 -0
  196. package/dist/client/_nuxt/I4qd5QHW.js +10 -0
  197. package/dist/client/_nuxt/IHMnEr_d.js +1 -0
  198. package/dist/client/_nuxt/KEYLhlmT.js +1 -0
  199. package/dist/client/_nuxt/NL97_oaV.js +1 -0
  200. package/dist/client/_nuxt/PWN5J14X.js +1 -0
  201. package/dist/client/_nuxt/PifQWv0n.js +1 -0
  202. package/dist/client/_nuxt/QZ3jNtnE.js +515 -0
  203. package/dist/client/_nuxt/RCJZWN-0.js +81 -0
  204. package/dist/client/_nuxt/RE3ujT-k.js +1 -0
  205. package/dist/client/_nuxt/RgJsN3zu.js +1 -0
  206. package/dist/client/_nuxt/RqY_Nz63.js +1 -0
  207. package/dist/client/_nuxt/Sfo2eW0G.js +274 -0
  208. package/dist/client/_nuxt/T9ysyp6P.js +62 -0
  209. package/dist/client/_nuxt/TQ9oWRfF.js +1 -0
  210. package/dist/client/_nuxt/UREJT2Bw.js +1 -0
  211. package/dist/client/_nuxt/Urb1RsFe.js +1 -0
  212. package/dist/client/_nuxt/WoBtJUue.js +1 -0
  213. package/dist/client/_nuxt/YxQm0tCS.js +1 -0
  214. package/dist/client/_nuxt/ZQC2c-5V.js +9 -0
  215. package/dist/client/_nuxt/beD-FUib.js +1 -0
  216. package/dist/client/_nuxt/bmJ2objS.js +1 -0
  217. package/dist/client/_nuxt/builds/latest.json +1 -0
  218. package/dist/client/_nuxt/builds/meta/40295b3c-3eb9-4043-bd2e-dac38ac5d90a.json +1 -0
  219. package/dist/client/_nuxt/doGI2cFk.js +1 -0
  220. package/dist/client/_nuxt/eJfcURhx.js +1 -0
  221. package/dist/client/_nuxt/entry.D91GVkD7.css +1 -0
  222. package/dist/client/_nuxt/error-404.ORekjfyJ.css +1 -0
  223. package/dist/client/_nuxt/error-500.BIuFL0tW.css +1 -0
  224. package/dist/client/_nuxt/hXH8Gyq8.js +1 -0
  225. package/dist/client/_nuxt/i0IPvypD.js +1 -0
  226. package/dist/client/_nuxt/iSbrOpM4.js +1 -0
  227. package/dist/client/_nuxt/ithNYyXa.js +1 -0
  228. package/dist/client/_nuxt/j8KhMG0x.js +1 -0
  229. package/dist/client/_nuxt/jaXbsbtS.js +1 -0
  230. package/dist/client/_nuxt/nclm9rTJ.js +266 -0
  231. package/dist/client/_nuxt/o-cPXEvd.js +575 -0
  232. package/dist/client/_nuxt/rS0jd3Ly.js +1 -0
  233. package/dist/client/_nuxt/sMI-pExk.js +1 -0
  234. package/dist/client/_nuxt/sQpxpyUs.js +1 -0
  235. package/dist/client/_nuxt/vMsNTMhM.js +5 -0
  236. package/dist/client/_nuxt/wChcbJ1V.js +37 -0
  237. package/dist/client/_nuxt/wORFcijT.js +32 -0
  238. package/dist/client/_nuxt/wPLeKH6q.js +40 -0
  239. package/dist/client/_nuxt/xPNGhBYe.js +1 -0
  240. package/dist/client/index.html +238 -0
  241. package/dist/module.cjs +5 -0
  242. package/dist/module.d.mts +67 -0
  243. package/dist/module.d.ts +67 -0
  244. package/dist/module.json +9 -0
  245. package/dist/module.mjs +289 -0
  246. package/dist/runtime/composables/createScriptConsentTrigger.d.ts +6 -0
  247. package/dist/runtime/composables/createScriptConsentTrigger.mjs +36 -0
  248. package/dist/runtime/composables/useAnalyticsPageEvent.d.ts +2 -0
  249. package/dist/runtime/composables/useAnalyticsPageEvent.mjs +35 -0
  250. package/dist/runtime/composables/useScript.d.ts +5 -0
  251. package/dist/runtime/composables/useScript.mjs +57 -0
  252. package/dist/runtime/types.d.ts +35 -0
  253. package/dist/types.d.mts +18 -0
  254. package/dist/types.d.ts +18 -0
  255. package/package.json +83 -3
  256. /package/dist/{.gitkeep → runtime/types.mjs} +0 -0
@@ -0,0 +1,289 @@
1
+ import { useNuxt, useLogger, addDevServerHandler, defineNuxtModule, createResolver, addImportsDir, addTemplate, addPlugin, addBuildPlugin } from '@nuxt/kit';
2
+ import { readPackageJSON } from 'pkg-types';
3
+ import { existsSync } from 'node:fs';
4
+ import { pathToFileURL } from 'node:url';
5
+ import { createUnplugin } from 'unplugin';
6
+ import { parseURL, parseQuery, joinURL, hasProtocol } from 'ufo';
7
+ import MagicString from 'magic-string';
8
+ import { walk } from 'estree-walker';
9
+ import fsp from 'node:fs/promises';
10
+ import { lazyEventHandler, eventHandler, createError } from 'h3';
11
+ import { fetch } from 'ofetch';
12
+ import { colors } from 'consola/utils';
13
+ import { defu } from 'defu';
14
+ import { join } from 'pathe';
15
+ import { hash } from 'ohash';
16
+ import { createStorage } from 'unstorage';
17
+ import fsDriver from 'unstorage/drivers/fs-lite';
18
+
19
+ const DEVTOOLS_UI_ROUTE = "/__nuxt-scripts";
20
+ const DEVTOOLS_UI_LOCAL_PORT = 3030;
21
+ function setupDevToolsUI(options, resolve, nuxt = useNuxt()) {
22
+ const clientPath = resolve("./client");
23
+ const isProductionBuild = existsSync(clientPath);
24
+ if (isProductionBuild) {
25
+ nuxt.hook("vite:serverCreated", async (server) => {
26
+ const sirv = await import('sirv').then((r) => r.default || r);
27
+ server.middlewares.use(
28
+ DEVTOOLS_UI_ROUTE,
29
+ sirv(clientPath, { dev: true, single: true })
30
+ );
31
+ });
32
+ } else {
33
+ nuxt.hook("vite:extendConfig", (config) => {
34
+ config.server = config.server || {};
35
+ config.server.proxy = config.server.proxy || {};
36
+ config.server.proxy[DEVTOOLS_UI_ROUTE] = {
37
+ target: `http://localhost:${DEVTOOLS_UI_LOCAL_PORT}${DEVTOOLS_UI_ROUTE}`,
38
+ changeOrigin: true,
39
+ followRedirects: true,
40
+ rewrite: (path) => path.replace(DEVTOOLS_UI_ROUTE, "")
41
+ };
42
+ });
43
+ }
44
+ nuxt.hook("devtools:customTabs", (tabs) => {
45
+ tabs.push({
46
+ // unique identifier
47
+ name: "nuxt-scripts",
48
+ // title to display in the tab
49
+ title: "Scripts",
50
+ // any icon from Iconify, or a URL to an image
51
+ icon: "carbon:script",
52
+ // iframe view
53
+ view: {
54
+ type: "iframe",
55
+ src: DEVTOOLS_UI_ROUTE
56
+ }
57
+ });
58
+ });
59
+ }
60
+
61
+ function NuxtScriptAssetBundlerTransformer(options) {
62
+ return createUnplugin(() => {
63
+ return {
64
+ name: "nuxt:scripts:asset-bundler-transformer",
65
+ enforce: "post",
66
+ transformInclude(id) {
67
+ const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href));
68
+ const { type } = parseQuery(search);
69
+ if (pathname.endsWith(".vue") && (type === "script" || !search))
70
+ return true;
71
+ if (pathname.match(/\.((c|m)?j|t)sx?$/g))
72
+ return true;
73
+ if (pathname.includes("node_modules/@unhead") || pathname.includes("node_modules/vueuse"))
74
+ return false;
75
+ return false;
76
+ },
77
+ async transform(code, id) {
78
+ if (!code.includes("useScript"))
79
+ return;
80
+ const ast = this.parse(code);
81
+ const s = new MagicString(code);
82
+ walk(ast, {
83
+ enter(_node) {
84
+ if (_node.type === "CallExpression" && _node.callee.type === "Identifier" && _node.callee?.name === "useScript") {
85
+ const node = _node;
86
+ let scriptKey;
87
+ let scriptNode;
88
+ if (node.arguments[0].type === "Literal") {
89
+ scriptNode = node.arguments[0];
90
+ scriptKey = scriptNode.value;
91
+ } else if (node.arguments[0].type === "ObjectExpression") {
92
+ const srcProperty = node.arguments[0].properties.find(
93
+ (p) => p.key?.name === "src" || p.key?.value === "src"
94
+ );
95
+ const keyProperty = node.arguments[0].properties.find(
96
+ (p) => p.key?.name === "key" || p.key?.value === "key"
97
+ );
98
+ scriptKey = keyProperty?.value?.value || srcProperty?.value;
99
+ scriptNode = srcProperty?.value;
100
+ }
101
+ if (scriptNode) {
102
+ const src = scriptNode.value;
103
+ if (src) {
104
+ let hasAssetStrategy = false;
105
+ if (node.arguments[1]?.type === "ObjectExpression") {
106
+ const assetStrategyProperty = node.arguments[1]?.properties.find(
107
+ (p) => p.key?.name === "assetStrategy" || p.key?.value === "assetStrategy"
108
+ );
109
+ if (assetStrategyProperty) {
110
+ if (assetStrategyProperty?.value?.value !== "bundle")
111
+ return;
112
+ s.remove(assetStrategyProperty.start, assetStrategyProperty.end + 1);
113
+ hasAssetStrategy = true;
114
+ }
115
+ }
116
+ hasAssetStrategy = hasAssetStrategy || options.overrides?.[scriptKey]?.assetStrategy === "bundle";
117
+ if (hasAssetStrategy) {
118
+ const newSrc = options.resolveScript(src);
119
+ s.overwrite(scriptNode.start, scriptNode.end, `'${newSrc}'`);
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+ });
126
+ if (s.hasChanged()) {
127
+ return {
128
+ code: s.toString(),
129
+ map: s.generateMap({ includeContent: true, source: id })
130
+ };
131
+ }
132
+ }
133
+ };
134
+ });
135
+ }
136
+
137
+ const logger = useLogger("@nuxt/scripts");
138
+
139
+ const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;
140
+ function setupPublicAssetStrategy(options = {}) {
141
+ const assetsBaseURL = options.prefix || "/_scripts";
142
+ const nuxt = useNuxt();
143
+ const renderedScriptSrc = /* @__PURE__ */ new Map();
144
+ const storage = createStorage(fsDriver({
145
+ base: "node_modules/.cache/nuxt/scripts"
146
+ }));
147
+ function normalizeScriptData(src) {
148
+ if (hasProtocol(src, { acceptRelative: true })) {
149
+ src = src.replace(/^\/\//, "https://");
150
+ const url = parseURL(src);
151
+ const file = [
152
+ `${hash(url)}.js`
153
+ // force an extension
154
+ ].filter(Boolean).join("-");
155
+ renderedScriptSrc.set(file, src);
156
+ return joinURL(assetsBaseURL, file);
157
+ }
158
+ return src;
159
+ }
160
+ addDevServerHandler({
161
+ route: assetsBaseURL,
162
+ handler: lazyEventHandler(async () => {
163
+ return eventHandler(async (event) => {
164
+ const filename = event.path.slice(1);
165
+ const url = renderedScriptSrc.get(event.path.slice(1));
166
+ if (!url)
167
+ throw createError({ statusCode: 404 });
168
+ const key = `data:scripts:${filename}`;
169
+ let res = await storage.getItemRaw(key);
170
+ if (!res) {
171
+ res = await fetch(url).then((r) => r.arrayBuffer()).then((r) => Buffer.from(r));
172
+ await storage.setItemRaw(key, res);
173
+ }
174
+ return res;
175
+ });
176
+ })
177
+ });
178
+ if (nuxt.options.dev) {
179
+ nuxt.options.routeRules ||= {};
180
+ nuxt.options.routeRules[joinURL(assetsBaseURL, "**")] = {
181
+ cache: {
182
+ maxAge: ONE_YEAR_IN_SECONDS
183
+ }
184
+ };
185
+ }
186
+ nuxt.options.nitro.publicAssets ||= [];
187
+ const cacheDir = join(nuxt.options.buildDir, "cache", "scripts");
188
+ nuxt.options.nitro.publicAssets.push();
189
+ nuxt.options.nitro = defu(nuxt.options.nitro, {
190
+ publicAssets: [{
191
+ dir: cacheDir,
192
+ maxAge: ONE_YEAR_IN_SECONDS,
193
+ baseURL: assetsBaseURL
194
+ }],
195
+ prerender: {
196
+ ignore: [assetsBaseURL]
197
+ }
198
+ });
199
+ nuxt.hook("nitro:init", async (nitro) => {
200
+ if (nuxt.options.dev)
201
+ return;
202
+ nitro.hooks.hook("rollup:before", async () => {
203
+ await fsp.rm(cacheDir, { recursive: true, force: true });
204
+ await fsp.mkdir(cacheDir, { recursive: true });
205
+ let banner = false;
206
+ for (const [filename, url] of renderedScriptSrc) {
207
+ const key = `data:scripts:${filename}`;
208
+ let res = await storage.getItemRaw(key);
209
+ if (!res) {
210
+ if (!banner) {
211
+ banner = true;
212
+ logger.info("Downloading scripts...");
213
+ }
214
+ let encoding;
215
+ let size = 0;
216
+ res = await fetch(url).then((r) => {
217
+ encoding = r.headers.get("content-encoding");
218
+ size = Number(r.headers.get("content-length") / 1024);
219
+ return r.arrayBuffer();
220
+ }).then((r) => Buffer.from(r));
221
+ logger.log(colors.gray(` \u251C\u2500 ${url} \u2192 ${joinURL(assetsBaseURL, filename)} (${size.toFixed(2)} kB ${encoding})`));
222
+ await storage.setItemRaw(key, res);
223
+ }
224
+ await fsp.writeFile(join(cacheDir, filename), res);
225
+ }
226
+ if (banner)
227
+ logger.success("Scripts downloaded and cached.");
228
+ });
229
+ });
230
+ return { normalizeScriptData };
231
+ }
232
+
233
+ const module = defineNuxtModule({
234
+ meta: {
235
+ name: "@nuxt/scripts",
236
+ configKey: "scripts",
237
+ compatibility: {
238
+ nuxt: "^3.11.1",
239
+ bridge: false
240
+ }
241
+ },
242
+ defaults: {
243
+ enabled: true,
244
+ debug: false
245
+ },
246
+ async setup(config, nuxt) {
247
+ const { resolve } = createResolver(import.meta.url);
248
+ const { version } = await readPackageJSON(resolve("../package.json"));
249
+ if (!config.enabled) {
250
+ logger.debug("The module is disabled, skipping setup.");
251
+ return;
252
+ }
253
+ nuxt.options.alias["#nuxt-scripts"] = resolve("./runtime/types");
254
+ nuxt.options.runtimeConfig["nuxt-scripts"] = { version };
255
+ addImportsDir(resolve("./runtime/composables"));
256
+ if (config.globals?.length) {
257
+ const template = addTemplate({
258
+ filename: "modules/nuxt-scripts/plugin.client.mjs",
259
+ getContents() {
260
+ return `import { defineNuxtPlugin, useScript } from '#imports'
261
+ export default defineNuxtPlugin({
262
+ setup() {
263
+ ${config.globals?.map((g) => !Array.isArray(g) ? ` useScript("${g.toString()}")` : g.length === 2 ? ` useScript(${JSON.stringify(g[0])}, ${JSON.stringify(g[1])} })` : ` useScript(${JSON.stringify(g[0])})`).join("\n")}
264
+ })`;
265
+ }
266
+ });
267
+ addPlugin({
268
+ src: template.dst,
269
+ mode: "client"
270
+ });
271
+ }
272
+ const scriptMap = /* @__PURE__ */ new Map();
273
+ const { normalizeScriptData } = setupPublicAssetStrategy(config.assets);
274
+ addBuildPlugin(NuxtScriptAssetBundlerTransformer({
275
+ overrides: config.overrides,
276
+ resolveScript(src) {
277
+ if (scriptMap.has(src))
278
+ return scriptMap.get(src);
279
+ const url = normalizeScriptData(src);
280
+ scriptMap.set(src, url);
281
+ return url;
282
+ }
283
+ }));
284
+ if (nuxt.options.dev)
285
+ setupDevToolsUI(config, resolve);
286
+ }
287
+ });
288
+
289
+ export { module as default };
@@ -0,0 +1,6 @@
1
+ import type { ConsentPromiseOptions } from '../types';
2
+ type CreateScriptConsentTriggerApi = {
3
+ accept: () => void;
4
+ } & Promise<void>;
5
+ export declare function createScriptConsentTrigger(options?: ConsentPromiseOptions): CreateScriptConsentTriggerApi;
6
+ export {};
@@ -0,0 +1,36 @@
1
+ import { isRef, onNuxtReady, ref, requestIdleCallback, toValue, tryUseNuxtApp, watch } from "#imports";
2
+ export function createScriptConsentTrigger(options) {
3
+ if (import.meta.server)
4
+ return new Promise(() => {
5
+ });
6
+ const consented = ref(false);
7
+ const nuxtApp = tryUseNuxtApp();
8
+ const promise = new Promise((resolve) => {
9
+ watch(consented, (ready) => {
10
+ if (ready) {
11
+ const runner = nuxtApp?.runWithContext || ((cb) => cb());
12
+ const idleTimeout = options?.loadOnNuxtReady ? nuxtApp ? onNuxtReady : requestIdleCallback : (cb) => cb();
13
+ runner(() => idleTimeout(resolve));
14
+ }
15
+ });
16
+ if (options?.consent) {
17
+ if (typeof options?.consent === "boolean") {
18
+ consented.value = true;
19
+ } else if (options?.consent instanceof Promise) {
20
+ options?.consent.then((res) => {
21
+ consented.value = typeof res === "boolean" ? res : true;
22
+ });
23
+ } else if (isRef(options?.consent)) {
24
+ watch(options.consent, (_val) => {
25
+ const val = toValue(_val);
26
+ if (typeof val === "boolean")
27
+ consented.value = val;
28
+ }, { immediate: true });
29
+ }
30
+ }
31
+ });
32
+ promise.accept = () => {
33
+ consented.value = true;
34
+ };
35
+ return promise;
36
+ }
@@ -0,0 +1,2 @@
1
+ import type { TrackedPage } from '#nuxt-scripts';
2
+ export declare function useAnalyticsPageEvent(onChange?: (payload: TrackedPage) => void): any;
@@ -0,0 +1,35 @@
1
+ import { injectHead, ref, useNuxtApp, useRoute } from "#imports";
2
+ export function useAnalyticsPageEvent(onChange) {
3
+ const nuxt = useNuxtApp();
4
+ const route = useRoute();
5
+ const head = injectHead();
6
+ const payload = ref({
7
+ path: route.fullPath,
8
+ title: import.meta.client ? document.title : ""
9
+ });
10
+ if (import.meta.server)
11
+ return payload;
12
+ let lastPayload = { path: "", title: "" };
13
+ let stopDomWatcher;
14
+ nuxt.hooks.hook("page:finish", () => {
15
+ Promise.race([
16
+ // possibly no head update is needed
17
+ new Promise((resolve) => setTimeout(resolve, 100)),
18
+ new Promise((resolve) => {
19
+ stopDomWatcher = head.hooks.hook("dom:rendered", () => resolve());
20
+ })
21
+ ]).finally(() => {
22
+ stopDomWatcher && stopDomWatcher();
23
+ }).then(() => {
24
+ payload.value = {
25
+ path: route.fullPath,
26
+ title: document.title
27
+ };
28
+ if (lastPayload.path !== payload.value.path || lastPayload.title !== payload.value.title) {
29
+ onChange && onChange(payload.value);
30
+ lastPayload = payload.value;
31
+ }
32
+ });
33
+ });
34
+ return payload;
35
+ }
@@ -0,0 +1,5 @@
1
+ import { type UseScriptInput } from '@unhead/vue';
2
+ import type { UseScriptOptions } from '@unhead/schema';
3
+ export declare function useScript<T>(input: UseScriptInput, options?: Omit<UseScriptOptions<T>, 'trigger'> & {
4
+ trigger?: UseScriptOptions<T>['trigger'] | 'onNuxtReady';
5
+ }): any;
@@ -0,0 +1,57 @@
1
+ import { useScript as _useScript, injectHead } from "@unhead/vue";
2
+ import { onNuxtReady, useNuxtApp } from "#imports";
3
+ export function useScript(input, options) {
4
+ input = typeof input === "string" ? { src: input } : input;
5
+ options = options || {};
6
+ if (options.trigger === "onNuxtReady")
7
+ options.trigger = onNuxtReady;
8
+ const nuxtApp = useNuxtApp();
9
+ const instance = _useScript(input, options);
10
+ if (import.meta.dev && import.meta.client) {
11
+ let syncScripts = function() {
12
+ nuxtApp._scripts[instance.$script.id] = payload;
13
+ nuxtApp.hooks.callHook("scripts:updated", { scripts: nuxtApp._scripts });
14
+ };
15
+ const payload = {
16
+ key: input.key || input.src,
17
+ src: input.src,
18
+ $script: null,
19
+ events: []
20
+ };
21
+ nuxtApp._scripts = nuxtApp._scripts || {};
22
+ if (!nuxtApp._scripts[instance.$script.id]) {
23
+ const head = injectHead();
24
+ head.hooks.hook("script:updated", (ctx) => {
25
+ if (ctx.script.id !== instance.$script.id)
26
+ return;
27
+ payload.events.push({
28
+ type: "status",
29
+ status: ctx.script.status,
30
+ at: Date.now()
31
+ });
32
+ payload.$script = instance.$script;
33
+ syncScripts();
34
+ });
35
+ head.hooks.hook("script:instance-fn", (ctx) => {
36
+ if (ctx.script.id !== instance.$script.id)
37
+ return;
38
+ payload.events.push({
39
+ type: "fn-call",
40
+ fn: ctx.fn,
41
+ args: ctx.args,
42
+ at: Date.now()
43
+ });
44
+ syncScripts();
45
+ });
46
+ payload.$script = instance.$script;
47
+ payload.events.push({
48
+ type: "status",
49
+ status: "awaitingLoad",
50
+ trigger: options?.trigger,
51
+ at: Date.now()
52
+ });
53
+ syncScripts();
54
+ }
55
+ }
56
+ return instance;
57
+ }
@@ -0,0 +1,35 @@
1
+ import type { UseScriptOptions } from '@unhead/schema';
2
+ import type { UseScriptInput } from '@unhead/vue';
3
+ import type { ComputedRef, Ref } from 'vue';
4
+ export type NuxtUseScriptOptions<T = any> = Omit<UseScriptOptions<T>, 'trigger'> & {
5
+ /**
6
+ * The trigger to load the script:
7
+ * - `onNuxtReady` - Load the script when Nuxt is ready.
8
+ * - `manual` - Load the script manually by calling `$script.load()` or `$script.waitForLoad()`.
9
+ * - `Promise` - Load the script when the promise resolves.
10
+ */
11
+ trigger?: UseScriptOptions<T>['trigger'] | 'onNuxtReady';
12
+ /**
13
+ * Should the script be bundled as an asset and loaded from your server. This is useful for improving the
14
+ * performance by avoiding the extra DNS lookup and reducing the number of requests. It also
15
+ * improves privacy by not sharing the user's IP address with third-party servers.
16
+ */
17
+ assetStrategy?: 'bundle';
18
+ };
19
+ export type NuxtUseScriptInput = UseScriptInput;
20
+ export interface TrackedPage {
21
+ title?: string;
22
+ path: string;
23
+ }
24
+ export interface ConsentPromiseOptions {
25
+ /**
26
+ * An optional reactive (or promise) reference to the consent state. You can use this to accept the consent for scripts
27
+ * instead of using the accept() method.
28
+ */
29
+ consent?: Promise<boolean | void> | Ref<boolean> | ComputedRef<boolean> | boolean;
30
+ /**
31
+ * Should the script be loaded on the `requestIdleCallback` callback. This is useful for non-essential scripts that
32
+ * have already been consented to be loaded.
33
+ */
34
+ loadOnNuxtReady?: boolean;
35
+ }
@@ -0,0 +1,18 @@
1
+
2
+ import type { ModuleOptions, ModuleHooks } from './module.js'
3
+
4
+
5
+ declare module '@nuxt/schema' {
6
+ interface NuxtConfig { ['scripts']?: Partial<ModuleOptions> }
7
+ interface NuxtOptions { ['scripts']?: ModuleOptions }
8
+ interface NuxtHooks extends ModuleHooks {}
9
+ }
10
+
11
+ declare module 'nuxt/schema' {
12
+ interface NuxtConfig { ['scripts']?: Partial<ModuleOptions> }
13
+ interface NuxtOptions { ['scripts']?: ModuleOptions }
14
+ interface NuxtHooks extends ModuleHooks {}
15
+ }
16
+
17
+
18
+ export type { ModuleHooks, ModuleOptions, default } from './module.js'
@@ -0,0 +1,18 @@
1
+
2
+ import type { ModuleOptions, ModuleHooks } from './module'
3
+
4
+
5
+ declare module '@nuxt/schema' {
6
+ interface NuxtConfig { ['scripts']?: Partial<ModuleOptions> }
7
+ interface NuxtOptions { ['scripts']?: ModuleOptions }
8
+ interface NuxtHooks extends ModuleHooks {}
9
+ }
10
+
11
+ declare module 'nuxt/schema' {
12
+ interface NuxtConfig { ['scripts']?: Partial<ModuleOptions> }
13
+ interface NuxtOptions { ['scripts']?: ModuleOptions }
14
+ interface NuxtHooks extends ModuleHooks {}
15
+ }
16
+
17
+
18
+ export type { ModuleHooks, ModuleOptions, default } from './module'
package/package.json CHANGED
@@ -1,8 +1,88 @@
1
1
  {
2
2
  "name": "@nuxt/scripts",
3
- "version": "0.0.0",
3
+ "type": "module",
4
+ "version": "0.1.1",
5
+ "packageManager": "pnpm@8.15.5",
6
+ "description": "Next-gen Scripts for Nuxt",
7
+ "author": {
8
+ "website": "https://harlanzw.com",
9
+ "name": "Harlan Wilton",
10
+ "url": "harlan@harlanzw.com"
11
+ },
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/nuxt/scripts.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/nuxt/scripts/issues"
19
+ },
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/types.d.ts",
23
+ "import": "./dist/module.mjs",
24
+ "require": "./dist/module.cjs"
25
+ }
26
+ },
27
+ "main": "./dist/module.cjs",
28
+ "types": "./dist/types.d.ts",
4
29
  "files": [
5
30
  "dist"
6
31
  ],
7
- "author": "Daniel Roe <daniel@roe.dev>"
8
- }
32
+ "build": {
33
+ "externals": [
34
+ "@unhead/vue",
35
+ "@unhead/schema"
36
+ ]
37
+ },
38
+ "dependencies": {
39
+ "@nuxt/devtools-kit": "^1.1.1",
40
+ "@nuxt/devtools-ui-kit": "^1.1.1",
41
+ "@nuxt/kit": "^3.11.1",
42
+ "consola": "^3.2.3",
43
+ "defu": "^6.1.4",
44
+ "estree-walker": "^3.0.3",
45
+ "h3": "^1.11.1",
46
+ "magic-string": "^0.30.8",
47
+ "ofetch": "^1.3.3",
48
+ "ohash": "^1.1.3",
49
+ "pathe": "^1.1.2",
50
+ "pkg-types": "^1.0.3",
51
+ "shiki": "^1.2.0",
52
+ "sirv": "^2.0.4",
53
+ "ufo": "^1.5.3",
54
+ "unplugin": "^1.10.0",
55
+ "unstorage": "^1.10.2"
56
+ },
57
+ "devDependencies": {
58
+ "@antfu/eslint-config": "2.8.3",
59
+ "@nuxt/devtools-ui-kit": "^1.1.1",
60
+ "@nuxt/module-builder": "^0.5.5",
61
+ "@nuxt/test-utils": "3.12.0",
62
+ "@unhead/schema": "^1.8.20",
63
+ "acorn-loose": "^8.4.0",
64
+ "bumpp": "^9.4.0",
65
+ "eslint": "8.57.0",
66
+ "nuxt": "^3.11.1",
67
+ "nuxt-scripts-devtools": "latest",
68
+ "playwright-core": "^1.42.1",
69
+ "typescript": "^5.4.3",
70
+ "vitest": "^1.4.0"
71
+ },
72
+ "resolutions": {
73
+ "@nuxt/scripts": "workspace:*"
74
+ },
75
+ "scripts": {
76
+ "build": "pnpm prepack",
77
+ "client:build": "nuxi generate client",
78
+ "client:dev": "nuxi dev client --port 3300",
79
+ "dev": "nuxi dev playground",
80
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
81
+ "typecheck": "pnpm dlx vue-tsc --noEmit",
82
+ "release": "pnpm build && bumpp && pnpm -r publish --no-git-checks",
83
+ "lint": "eslint .",
84
+ "lint:fix": "eslint . --fix",
85
+ "test": "true",
86
+ "test:types": "true"
87
+ }
88
+ }
File without changes