roxxie-proxy-u-prod 0.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 (481) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +123 -0
  3. package/SOURCE.md +29 -0
  4. package/THIRD_PARTY_NOTICES.md +24 -0
  5. package/dist/assets/filters-Bu_3nHVy.bin +0 -0
  6. package/dist/assets/index-BdRlQ7d_.css +1 -0
  7. package/dist/assets/index-DcvPYWS3.js +3621 -0
  8. package/dist/assets/inject-BnVzMVpl.js +40 -0
  9. package/dist/assets/scramjet-C9HLwI-y.wasm +0 -0
  10. package/dist/defaultfavicon.png +0 -0
  11. package/dist/icon.png +0 -0
  12. package/dist/roxxie-icon.svg +12 -0
  13. package/embed.js +70 -0
  14. package/examples/single-page.html +25 -0
  15. package/package.json +42 -0
  16. package/runtime/controller.sw.js +273 -0
  17. package/runtime/index.html +18 -0
  18. package/runtime/localcontrollersw.js +7 -0
  19. package/runtime/roxxie-network.json +13 -0
  20. package/runtime/sw.js +6 -0
  21. package/scripts/build.mjs +344 -0
  22. package/scripts/smoke.mjs +123 -0
  23. package/scripts/verify.mjs +122 -0
  24. package/source/external/dreamlandjs/LICENSE +21 -0
  25. package/source/external/dreamlandjs/README.md +22 -0
  26. package/source/external/dreamlandjs/dist/core.d.ts +167 -0
  27. package/source/external/dreamlandjs/dist/core.js +2 -0
  28. package/source/external/dreamlandjs/dist/core.js.map +1 -0
  29. package/source/external/dreamlandjs/dist/core.size.html +4950 -0
  30. package/source/external/dreamlandjs/dist/js-runtime.d.ts +3 -0
  31. package/source/external/dreamlandjs/dist/js-runtime.js +2 -0
  32. package/source/external/dreamlandjs/dist/js-runtime.js.map +1 -0
  33. package/source/external/dreamlandjs/dist/jsx-runtime.d.ts +1 -0
  34. package/source/external/dreamlandjs/dist/jsx-runtime.js +2 -0
  35. package/source/external/dreamlandjs/dist/jsx-runtime.js.map +1 -0
  36. package/source/external/dreamlandjs/dist/motion.d.ts +19 -0
  37. package/source/external/dreamlandjs/dist/motion.js +2 -0
  38. package/source/external/dreamlandjs/dist/motion.js.map +1 -0
  39. package/source/external/dreamlandjs/dist/router.d.ts +48 -0
  40. package/source/external/dreamlandjs/dist/router.js +2 -0
  41. package/source/external/dreamlandjs/dist/router.js.map +1 -0
  42. package/source/external/dreamlandjs/dist/ssr.client.d.ts +3 -0
  43. package/source/external/dreamlandjs/dist/ssr.client.js +2 -0
  44. package/source/external/dreamlandjs/dist/ssr.client.js.map +1 -0
  45. package/source/external/dreamlandjs/dist/ssr.client.size.html +4950 -0
  46. package/source/external/dreamlandjs/dist/ssr.hybrid.d.ts +7 -0
  47. package/source/external/dreamlandjs/dist/ssr.hybrid.js +2 -0
  48. package/source/external/dreamlandjs/dist/ssr.hybrid.js.map +1 -0
  49. package/source/external/dreamlandjs/dist/ssr.hybrid.size.html +4950 -0
  50. package/source/external/dreamlandjs/dist/ssr.server.d.ts +11 -0
  51. package/source/external/dreamlandjs/dist/ssr.server.js +2373 -0
  52. package/source/external/dreamlandjs/dist/ssr.server.js.map +1 -0
  53. package/source/external/dreamlandjs/dist/types/core/consts.d.ts +16 -0
  54. package/source/external/dreamlandjs/dist/types/core/css/index.d.ts +11 -0
  55. package/source/external/dreamlandjs/dist/types/core/css/selectorParser.d.ts +50 -0
  56. package/source/external/dreamlandjs/dist/types/core/delegate.d.ts +10 -0
  57. package/source/external/dreamlandjs/dist/types/core/index.d.ts +9 -0
  58. package/source/external/dreamlandjs/dist/types/core/jsx/child.d.ts +2 -0
  59. package/source/external/dreamlandjs/dist/types/core/jsx/definitions.d.ts +76 -0
  60. package/source/external/dreamlandjs/dist/types/core/jsx/dom.d.ts +28 -0
  61. package/source/external/dreamlandjs/dist/types/core/jsx/index.d.ts +14 -0
  62. package/source/external/dreamlandjs/dist/types/core/state/index.d.ts +1 -0
  63. package/source/external/dreamlandjs/dist/types/core/state/pointers.d.ts +37 -0
  64. package/source/external/dreamlandjs/dist/types/core/state/state.d.ts +13 -0
  65. package/source/external/dreamlandjs/dist/types/core/state/use.d.ts +11 -0
  66. package/source/external/dreamlandjs/dist/types/core/utils.d.ts +4 -0
  67. package/source/external/dreamlandjs/dist/types/js-runtime/index.d.ts +1 -0
  68. package/source/external/dreamlandjs/dist/types/jsx-runtime/index.d.ts +2 -0
  69. package/source/external/dreamlandjs/dist/types/motion/index.d.ts +15 -0
  70. package/source/external/dreamlandjs/dist/types/router/index.d.ts +45 -0
  71. package/source/external/dreamlandjs/dist/types/ssr/client/index.d.ts +1 -0
  72. package/source/external/dreamlandjs/dist/types/ssr/common/consts.d.ts +4 -0
  73. package/source/external/dreamlandjs/dist/types/ssr/common/serialize.d.ts +4 -0
  74. package/source/external/dreamlandjs/dist/types/ssr/common/types.d.ts +21 -0
  75. package/source/external/dreamlandjs/dist/types/ssr/hybrid/index.d.ts +4 -0
  76. package/source/external/dreamlandjs/dist/types/ssr/server/index.d.ts +7 -0
  77. package/source/external/dreamlandjs/dist/types/ssr/server/vdom.d.ts +71 -0
  78. package/source/external/dreamlandjs/dist/types/util/index.d.ts +1 -0
  79. package/source/external/dreamlandjs/dist/types/util/store.d.ts +26 -0
  80. package/source/external/dreamlandjs/dist/types/vite/css-minifier.d.ts +7 -0
  81. package/source/external/dreamlandjs/dist/types/vite/index.d.ts +11 -0
  82. package/source/external/dreamlandjs/dist/util.d.ts +29 -0
  83. package/source/external/dreamlandjs/dist/util.js +2 -0
  84. package/source/external/dreamlandjs/dist/util.js.map +1 -0
  85. package/source/external/dreamlandjs/dist/vite.d.ts +19 -0
  86. package/source/external/dreamlandjs/dist/vite.js +2 -0
  87. package/source/external/dreamlandjs/dist/vite.js.map +1 -0
  88. package/source/external/dreamlandjs/package.json +101 -0
  89. package/source/external/dreamlandjs/rollup.config.ts +302 -0
  90. package/source/external/dreamlandjs/rollup.plugins.ts +1134 -0
  91. package/source/external/dreamlandjs/src/core/consts.d.ts +16 -0
  92. package/source/external/dreamlandjs/src/core/consts.js +18 -0
  93. package/source/external/dreamlandjs/src/core/css/index.ts +111 -0
  94. package/source/external/dreamlandjs/src/core/css/selectorParser.ts +313 -0
  95. package/source/external/dreamlandjs/src/core/delegate.ts +28 -0
  96. package/source/external/dreamlandjs/src/core/index.ts +31 -0
  97. package/source/external/dreamlandjs/src/core/jsx/child.ts +91 -0
  98. package/source/external/dreamlandjs/src/core/jsx/definitions.ts +125 -0
  99. package/source/external/dreamlandjs/src/core/jsx/dom.ts +49 -0
  100. package/source/external/dreamlandjs/src/core/jsx/index.ts +289 -0
  101. package/source/external/dreamlandjs/src/core/state/index.ts +1 -0
  102. package/source/external/dreamlandjs/src/core/state/pointers.ts +310 -0
  103. package/source/external/dreamlandjs/src/core/state/state.ts +112 -0
  104. package/source/external/dreamlandjs/src/core/state/use.ts +82 -0
  105. package/source/external/dreamlandjs/src/core/utils.ts +48 -0
  106. package/source/external/dreamlandjs/src/js-runtime/index.ts +4 -0
  107. package/source/external/dreamlandjs/src/jsx-runtime/index.ts +3 -0
  108. package/source/external/dreamlandjs/src/motion/index.ts +156 -0
  109. package/source/external/dreamlandjs/src/router/index.tsx +394 -0
  110. package/source/external/dreamlandjs/src/router/tsconfig.json +9 -0
  111. package/source/external/dreamlandjs/src/ssr/client/index.ts +193 -0
  112. package/source/external/dreamlandjs/src/ssr/common/consts.ts +5 -0
  113. package/source/external/dreamlandjs/src/ssr/common/serialize.ts +115 -0
  114. package/source/external/dreamlandjs/src/ssr/common/types.ts +28 -0
  115. package/source/external/dreamlandjs/src/ssr/hybrid/index.ts +144 -0
  116. package/source/external/dreamlandjs/src/ssr/server/index.ts +157 -0
  117. package/source/external/dreamlandjs/src/ssr/server/tsconfig.json +7 -0
  118. package/source/external/dreamlandjs/src/ssr/server/vdom.ts +285 -0
  119. package/source/external/dreamlandjs/src/util/index.ts +10 -0
  120. package/source/external/dreamlandjs/src/util/store.ts +134 -0
  121. package/source/external/dreamlandjs/src/vite/css-minifier.ts +222 -0
  122. package/source/external/dreamlandjs/src/vite/index.ts +60 -0
  123. package/source/external/dreamlandjs/src/vite/tsconfig.json +7 -0
  124. package/source/external/dreamlandjs/tsconfig.json +15 -0
  125. package/source/package.json +48 -0
  126. package/source/packages/adrift-protocol/package.json +31 -0
  127. package/source/packages/adrift-protocol/src/index.ts +348 -0
  128. package/source/packages/adrift-protocol/tsconfig.json +18 -0
  129. package/source/packages/cdp/LICENSE +21 -0
  130. package/source/packages/cdp/README.md +47 -0
  131. package/source/packages/cdp/dist/chobitsu.js +2 -0
  132. package/source/packages/cdp/dist/chobitsu.js.map +1 -0
  133. package/source/packages/cdp/package.json +73 -0
  134. package/source/packages/cdp/script/build.js +15 -0
  135. package/source/packages/cdp/src/Chobitsu.ts +110 -0
  136. package/source/packages/cdp/src/domains/CSS.ts +378 -0
  137. package/source/packages/cdp/src/domains/CacheStorage.ts +8 -0
  138. package/source/packages/cdp/src/domains/DOM.ts +487 -0
  139. package/source/packages/cdp/src/domains/DOMDebugger.ts +109 -0
  140. package/source/packages/cdp/src/domains/DOMStorage.ts +122 -0
  141. package/source/packages/cdp/src/domains/Debugger.ts +25 -0
  142. package/source/packages/cdp/src/domains/IndexedDB.ts +207 -0
  143. package/source/packages/cdp/src/domains/Input.ts +177 -0
  144. package/source/packages/cdp/src/domains/Network.ts +251 -0
  145. package/source/packages/cdp/src/domains/Overlay.ts +230 -0
  146. package/source/packages/cdp/src/domains/Page.ts +296 -0
  147. package/source/packages/cdp/src/domains/Runtime.ts +273 -0
  148. package/source/packages/cdp/src/domains/Storage.ts +71 -0
  149. package/source/packages/cdp/src/index.d.ts +8 -0
  150. package/source/packages/cdp/src/index.ts +153 -0
  151. package/source/packages/cdp/src/lib/connector.ts +15 -0
  152. package/source/packages/cdp/src/lib/constants.ts +1 -0
  153. package/source/packages/cdp/src/lib/evaluate.ts +59 -0
  154. package/source/packages/cdp/src/lib/mutationObserver.ts +39 -0
  155. package/source/packages/cdp/src/lib/nodeManager.ts +142 -0
  156. package/source/packages/cdp/src/lib/objManager.ts +422 -0
  157. package/source/packages/cdp/src/lib/request.ts +313 -0
  158. package/source/packages/cdp/src/lib/resources.ts +59 -0
  159. package/source/packages/cdp/src/lib/scriptMananger.ts +62 -0
  160. package/source/packages/cdp/src/lib/stylesheet.ts +146 -0
  161. package/source/packages/cdp/src/lib/util.ts +105 -0
  162. package/source/packages/cdp/tsconfig.json +16 -0
  163. package/source/packages/cdp/webpack.config.js +48 -0
  164. package/source/packages/chrome/index.html +17 -0
  165. package/source/packages/chrome/package.json +41 -0
  166. package/source/packages/chrome/public/ROXXIE_NETWORK.md +22 -0
  167. package/source/packages/chrome/public/controller.sw.js +273 -0
  168. package/source/packages/chrome/public/defaultfavicon.png +0 -0
  169. package/source/packages/chrome/public/icon.png +0 -0
  170. package/source/packages/chrome/public/localcontrollersw.js +7 -0
  171. package/source/packages/chrome/public/roxxie-icon.svg +12 -0
  172. package/source/packages/chrome/public/roxxie-network.json +13 -0
  173. package/source/packages/chrome/public/sw.js +6 -0
  174. package/source/packages/chrome/src/App.tsx +269 -0
  175. package/source/packages/chrome/src/CDP.ts +285 -0
  176. package/source/packages/chrome/src/Tab/History.ts +186 -0
  177. package/source/packages/chrome/src/Tab/Tab.tsx +219 -0
  178. package/source/packages/chrome/src/Tab/screenshot.ts +66 -0
  179. package/source/packages/chrome/src/Tab/serialize.ts +221 -0
  180. package/source/packages/chrome/src/adblock/AdBlockPlugin.test.ts +159 -0
  181. package/source/packages/chrome/src/adblock/AdBlockPlugin.ts +232 -0
  182. package/source/packages/chrome/src/adblock/AdBlockService.test.ts +148 -0
  183. package/source/packages/chrome/src/adblock/AdBlockService.ts +166 -0
  184. package/source/packages/chrome/src/adblock/NOTICE.md +14 -0
  185. package/source/packages/chrome/src/adblock/assets/filters.bin +0 -0
  186. package/source/packages/chrome/src/adblock/index.ts +18 -0
  187. package/source/packages/chrome/src/assets/favicon.ts +4 -0
  188. package/source/packages/chrome/src/components/BookmarkPopup.tsx +101 -0
  189. package/source/packages/chrome/src/components/BookmarksStrip.tsx +150 -0
  190. package/source/packages/chrome/src/components/Button.tsx +99 -0
  191. package/source/packages/chrome/src/components/Checkbox.tsx +89 -0
  192. package/source/packages/chrome/src/components/DownloadsPopup.tsx +230 -0
  193. package/source/packages/chrome/src/components/Favicon.tsx +65 -0
  194. package/source/packages/chrome/src/components/Icon.tsx +29 -0
  195. package/source/packages/chrome/src/components/Input.tsx +82 -0
  196. package/source/packages/chrome/src/components/Menu.tsx +287 -0
  197. package/source/packages/chrome/src/components/Omnibar/BookmarkButton.tsx +73 -0
  198. package/source/packages/chrome/src/components/Omnibar/CircularProgress.tsx +83 -0
  199. package/source/packages/chrome/src/components/Omnibar/Omnibar.tsx +455 -0
  200. package/source/packages/chrome/src/components/Omnibar/OmnibarButton.tsx +48 -0
  201. package/source/packages/chrome/src/components/Omnibar/Omnibox.tsx +466 -0
  202. package/source/packages/chrome/src/components/Omnibar/SiteOptionsButton.tsx +52 -0
  203. package/source/packages/chrome/src/components/Omnibar/Suggestion.tsx +186 -0
  204. package/source/packages/chrome/src/components/Omnibar/UrlInput.tsx +198 -0
  205. package/source/packages/chrome/src/components/Omnibar/suggestions.ts +468 -0
  206. package/source/packages/chrome/src/components/Omnibar/utils.ts +11 -0
  207. package/source/packages/chrome/src/components/ReportBrokenSiteModal.tsx +279 -0
  208. package/source/packages/chrome/src/components/Shell.tsx +191 -0
  209. package/source/packages/chrome/src/components/SiteInformationPopup.tsx +96 -0
  210. package/source/packages/chrome/src/components/SmallIconButton.tsx +36 -0
  211. package/source/packages/chrome/src/components/StartScreen.tsx +386 -0
  212. package/source/packages/chrome/src/components/TabStrip/DragTab.tsx +546 -0
  213. package/source/packages/chrome/src/components/TabStrip/Sidebar.tsx +820 -0
  214. package/source/packages/chrome/src/components/TabStrip/TabHoverCard.tsx +87 -0
  215. package/source/packages/chrome/src/components/TabStrip/TabStrip.tsx +510 -0
  216. package/source/packages/chrome/src/components/TopSiteButton.tsx +234 -0
  217. package/source/packages/chrome/src/consts.ts +3 -0
  218. package/source/packages/chrome/src/icons.ts +70 -0
  219. package/source/packages/chrome/src/index.ts +168 -0
  220. package/source/packages/chrome/src/migrations/0-from-legacy.ts +6 -0
  221. package/source/packages/chrome/src/migrations/1-vertical-tab-settings.ts +15 -0
  222. package/source/packages/chrome/src/migrations/index.ts +42 -0
  223. package/source/packages/chrome/src/pages/AboutPage.tsx +37 -0
  224. package/source/packages/chrome/src/pages/DownloadsPage.tsx +133 -0
  225. package/source/packages/chrome/src/pages/HistoryPage.tsx +99 -0
  226. package/source/packages/chrome/src/pages/NewTabPage.tsx +264 -0
  227. package/source/packages/chrome/src/pages/PlaygroundPage.tsx +43 -0
  228. package/source/packages/chrome/src/pages/SettingsPage.tsx +1931 -0
  229. package/source/packages/chrome/src/proxy/Controller.ts +331 -0
  230. package/source/packages/chrome/src/proxy/ProxyFrame.ts +26 -0
  231. package/source/packages/chrome/src/proxy/cache.ts +487 -0
  232. package/source/packages/chrome/src/proxy/codec.ts +11 -0
  233. package/source/packages/chrome/src/proxy/contextitems.ts +180 -0
  234. package/source/packages/chrome/src/proxy/index.ts +5 -0
  235. package/source/packages/chrome/src/proxy/roxxie.ts +85 -0
  236. package/source/packages/chrome/src/proxy/scramjet.ts +642 -0
  237. package/source/packages/chrome/src/reset.css +124 -0
  238. package/source/packages/chrome/src/runtime-base.ts +16 -0
  239. package/source/packages/chrome/src/sentry.tsx +45 -0
  240. package/source/packages/chrome/src/services/DownloadsService.ts +136 -0
  241. package/source/packages/chrome/src/services/FaviconService.ts +161 -0
  242. package/source/packages/chrome/src/services/KVWrapper.ts +169 -0
  243. package/source/packages/chrome/src/services/ProfileService.ts +94 -0
  244. package/source/packages/chrome/src/services/Service.ts +10 -0
  245. package/source/packages/chrome/src/services/SettingsService.ts +108 -0
  246. package/source/packages/chrome/src/services/TabsService.ts +161 -0
  247. package/source/packages/chrome/src/startup.test.ts +129 -0
  248. package/source/packages/chrome/src/startup.ts +161 -0
  249. package/source/packages/chrome/src/style.css +229 -0
  250. package/source/packages/chrome/src/themes.ts +756 -0
  251. package/source/packages/chrome/src/util/StatefulClass.ts +31 -0
  252. package/source/packages/chrome/src/util/index.ts +74 -0
  253. package/source/packages/chrome/src/util/memoize.ts +13 -0
  254. package/source/packages/chrome/src/vite-env.d.ts +16 -0
  255. package/source/packages/chrome/tsconfig.json +30 -0
  256. package/source/packages/chrome/vite.config.ts +47 -0
  257. package/source/packages/inject/package.json +20 -0
  258. package/source/packages/inject/src/context.ts +194 -0
  259. package/source/packages/inject/src/emulators/alwaysLastBubble.ts +168 -0
  260. package/source/packages/inject/src/emulators/anchors.ts +55 -0
  261. package/source/packages/inject/src/emulators/contextmenu.ts +46 -0
  262. package/source/packages/inject/src/emulators/history.ts +51 -0
  263. package/source/packages/inject/src/emulators/titlewatcher.ts +42 -0
  264. package/source/packages/inject/src/emulators/windowopen.ts +134 -0
  265. package/source/packages/inject/src/errorpage/errorpage.css +234 -0
  266. package/source/packages/inject/src/errorpage/errorpage.html +29 -0
  267. package/source/packages/inject/src/errorpage/errorpage.ts +76 -0
  268. package/source/packages/inject/src/index.ts +62 -0
  269. package/source/packages/inject/src/transport.ts +97 -0
  270. package/source/packages/inject/src/types.d.ts +136 -0
  271. package/source/packages/roxxie-transport/package.json +36 -0
  272. package/source/packages/roxxie-transport/src/binary.ts +398 -0
  273. package/source/packages/roxxie-transport/src/config.ts +254 -0
  274. package/source/packages/roxxie-transport/src/connection.ts +767 -0
  275. package/source/packages/roxxie-transport/src/index.ts +5 -0
  276. package/source/packages/roxxie-transport/src/tracker.ts +651 -0
  277. package/source/packages/roxxie-transport/src/transport.ts +414 -0
  278. package/source/packages/roxxie-transport/tsconfig.json +18 -0
  279. package/source/packages/scramjet/packages/controller/package.json +37 -0
  280. package/source/packages/scramjet/packages/controller/src/index.ts +861 -0
  281. package/source/packages/scramjet/packages/controller/src/inject.ts +365 -0
  282. package/source/packages/scramjet/packages/controller/src/sw.ts +231 -0
  283. package/source/packages/scramjet/packages/controller/src/symbols.ts +1 -0
  284. package/source/packages/scramjet/packages/controller/src/types.d.ts +140 -0
  285. package/source/packages/scramjet/packages/controller/src/typesEntry.ts +6 -0
  286. package/source/packages/scramjet/packages/controller/src/version.ts +30 -0
  287. package/source/packages/scramjet/packages/controller/tsconfig.json +24 -0
  288. package/source/packages/scramjet/packages/core/dist/scramjet-external.mjs +57 -0
  289. package/source/packages/scramjet/packages/core/dist/scramjet.js +13599 -0
  290. package/source/packages/scramjet/packages/core/dist/scramjet.js.map +1 -0
  291. package/source/packages/scramjet/packages/core/dist/scramjet.mjs +13649 -0
  292. package/source/packages/scramjet/packages/core/dist/scramjet.mjs.map +1 -0
  293. package/source/packages/scramjet/packages/core/dist/scramjet.wasm +0 -0
  294. package/source/packages/scramjet/packages/core/dist/scramjet_bundled.js +13601 -0
  295. package/source/packages/scramjet/packages/core/dist/scramjet_bundled.js.map +1 -0
  296. package/source/packages/scramjet/packages/core/dist/scramjet_bundled.mjs +13651 -0
  297. package/source/packages/scramjet/packages/core/dist/scramjet_bundled.mjs.map +1 -0
  298. package/source/packages/scramjet/packages/core/dist/temp-types-build/index.js +23 -0
  299. package/source/packages/scramjet/packages/core/dist/temp-types-build/index.js.map +1 -0
  300. package/source/packages/scramjet/packages/core/dist/types/Tap.d.ts +39 -0
  301. package/source/packages/scramjet/packages/core/dist/types/client/client.d.ts +115 -0
  302. package/source/packages/scramjet/packages/core/dist/types/client/entry.d.ts +5 -0
  303. package/source/packages/scramjet/packages/core/dist/types/client/events.d.ts +10 -0
  304. package/source/packages/scramjet/packages/core/dist/types/client/helpers.d.ts +1 -0
  305. package/source/packages/scramjet/packages/core/dist/types/client/index.d.ts +7 -0
  306. package/source/packages/scramjet/packages/core/dist/types/client/location.d.ts +2 -0
  307. package/source/packages/scramjet/packages/core/dist/types/client/shared/eval.d.ts +3 -0
  308. package/source/packages/scramjet/packages/core/dist/types/client/shared/sourcemaps.d.ts +19 -0
  309. package/source/packages/scramjet/packages/core/dist/types/client/shared/wrap.d.ts +4 -0
  310. package/source/packages/scramjet/packages/core/dist/types/client/singletonbox.d.ts +18 -0
  311. package/source/packages/scramjet/packages/core/dist/types/fetch/body.d.ts +3 -0
  312. package/source/packages/scramjet/packages/core/dist/types/fetch/fetch.d.ts +7 -0
  313. package/source/packages/scramjet/packages/core/dist/types/fetch/headers.d.ts +19 -0
  314. package/source/packages/scramjet/packages/core/dist/types/fetch/index.d.ts +128 -0
  315. package/source/packages/scramjet/packages/core/dist/types/fetch/parse.d.ts +23 -0
  316. package/source/packages/scramjet/packages/core/dist/types/fetch/util.d.ts +7 -0
  317. package/source/packages/scramjet/packages/core/dist/types/index.d.ts +12 -0
  318. package/source/packages/scramjet/packages/core/dist/types/shared/cookie.d.ts +26 -0
  319. package/source/packages/scramjet/packages/core/dist/types/shared/headers.d.ts +13 -0
  320. package/source/packages/scramjet/packages/core/dist/types/shared/htmlRules.d.ts +6 -0
  321. package/source/packages/scramjet/packages/core/dist/types/shared/index.d.ts +50 -0
  322. package/source/packages/scramjet/packages/core/dist/types/shared/mime.d.ts +53 -0
  323. package/source/packages/scramjet/packages/core/dist/types/shared/refresh.d.ts +7 -0
  324. package/source/packages/scramjet/packages/core/dist/types/shared/rewriters/css.d.ts +4 -0
  325. package/source/packages/scramjet/packages/core/dist/types/shared/rewriters/html.d.ts +33 -0
  326. package/source/packages/scramjet/packages/core/dist/types/shared/rewriters/index.d.ts +6 -0
  327. package/source/packages/scramjet/packages/core/dist/types/shared/rewriters/js.d.ts +11 -0
  328. package/source/packages/scramjet/packages/core/dist/types/shared/rewriters/url.d.ts +24 -0
  329. package/source/packages/scramjet/packages/core/dist/types/shared/rewriters/wasm.d.ts +7 -0
  330. package/source/packages/scramjet/packages/core/dist/types/shared/rewriters/worker.d.ts +3 -0
  331. package/source/packages/scramjet/packages/core/dist/types/shared/set-cookie-parser.d.ts +20 -0
  332. package/source/packages/scramjet/packages/core/dist/types/shared/snapshot.d.ts +186 -0
  333. package/source/packages/scramjet/packages/core/dist/types/shared/sniffEncoding.d.ts +65 -0
  334. package/source/packages/scramjet/packages/core/dist/types/shared/util.d.ts +2 -0
  335. package/source/packages/scramjet/packages/core/dist/types/symbols.d.ts +6 -0
  336. package/source/packages/scramjet/packages/core/dist/types/types.d.ts +60 -0
  337. package/source/packages/scramjet/packages/core/dist/types/typesEntry.d.ts +5 -0
  338. package/source/packages/scramjet/packages/core/lib/index.cjs +7 -0
  339. package/source/packages/scramjet/packages/core/lib/index.d.ts +8 -0
  340. package/source/packages/scramjet/packages/core/package.json +69 -0
  341. package/source/packages/scramjet/packages/core/rewriter/Cargo.lock +1985 -0
  342. package/source/packages/scramjet/packages/core/rewriter/Cargo.toml +20 -0
  343. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/Cargo.toml +15 -0
  344. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/src/analyze.rs +1118 -0
  345. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/src/ast_table.rs +927 -0
  346. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/src/helper_index.rs +556 -0
  347. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/src/lib.rs +455 -0
  348. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/src/reachability.rs +85 -0
  349. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/src/templates.rs +192 -0
  350. package/source/packages/scramjet/packages/core/rewriter/coverage-macro/src/witness.rs +597 -0
  351. package/source/packages/scramjet/packages/core/rewriter/js/Cargo.toml +18 -0
  352. package/source/packages/scramjet/packages/core/rewriter/js/src/cfg.rs +45 -0
  353. package/source/packages/scramjet/packages/core/rewriter/js/src/changes.rs +379 -0
  354. package/source/packages/scramjet/packages/core/rewriter/js/src/lib.rs +171 -0
  355. package/source/packages/scramjet/packages/core/rewriter/js/src/rewrite.rs +290 -0
  356. package/source/packages/scramjet/packages/core/rewriter/js/src/visitor.rs +967 -0
  357. package/source/packages/scramjet/packages/core/rewriter/native/Cargo.toml +22 -0
  358. package/source/packages/scramjet/packages/core/rewriter/native/src/main.rs +166 -0
  359. package/source/packages/scramjet/packages/core/rewriter/native/src/rewriter.rs +161 -0
  360. package/source/packages/scramjet/packages/core/rewriter/native/src/test_runner.rs +156 -0
  361. package/source/packages/scramjet/packages/core/rewriter/rust-toolchain.toml +5 -0
  362. package/source/packages/scramjet/packages/core/rewriter/rustfmt.toml +2 -0
  363. package/source/packages/scramjet/packages/core/rewriter/transform/Cargo.toml +16 -0
  364. package/source/packages/scramjet/packages/core/rewriter/transform/src/lib.rs +242 -0
  365. package/source/packages/scramjet/packages/core/rewriter/transform/src/transform.rs +123 -0
  366. package/source/packages/scramjet/packages/core/rewriter/wasm/Cargo.toml +22 -0
  367. package/source/packages/scramjet/packages/core/rewriter/wasm/build.sh +128 -0
  368. package/source/packages/scramjet/packages/core/rewriter/wasm/src/error.rs +56 -0
  369. package/source/packages/scramjet/packages/core/rewriter/wasm/src/jsr.rs +125 -0
  370. package/source/packages/scramjet/packages/core/rewriter/wasm/src/lib.rs +144 -0
  371. package/source/packages/scramjet/packages/core/src/Tap.ts +166 -0
  372. package/source/packages/scramjet/packages/core/src/client/client.ts +916 -0
  373. package/source/packages/scramjet/packages/core/src/client/dom/attr.ts +94 -0
  374. package/source/packages/scramjet/packages/core/src/client/dom/beacon.ts +11 -0
  375. package/source/packages/scramjet/packages/core/src/client/dom/cookie.ts +21 -0
  376. package/source/packages/scramjet/packages/core/src/client/dom/css.ts +106 -0
  377. package/source/packages/scramjet/packages/core/src/client/dom/document.ts +140 -0
  378. package/source/packages/scramjet/packages/core/src/client/dom/element.ts +783 -0
  379. package/source/packages/scramjet/packages/core/src/client/dom/fontface.ts +11 -0
  380. package/source/packages/scramjet/packages/core/src/client/dom/fragments.ts +32 -0
  381. package/source/packages/scramjet/packages/core/src/client/dom/history.ts +35 -0
  382. package/source/packages/scramjet/packages/core/src/client/dom/open.ts +59 -0
  383. package/source/packages/scramjet/packages/core/src/client/dom/origin.ts +13 -0
  384. package/source/packages/scramjet/packages/core/src/client/dom/performance.ts +50 -0
  385. package/source/packages/scramjet/packages/core/src/client/dom/protocol.ts +17 -0
  386. package/source/packages/scramjet/packages/core/src/client/dom/storage.ts +107 -0
  387. package/source/packages/scramjet/packages/core/src/client/entry.ts +12 -0
  388. package/source/packages/scramjet/packages/core/src/client/events.ts +10 -0
  389. package/source/packages/scramjet/packages/core/src/client/helpers.ts +7 -0
  390. package/source/packages/scramjet/packages/core/src/client/index.ts +7 -0
  391. package/source/packages/scramjet/packages/core/src/client/location.ts +139 -0
  392. package/source/packages/scramjet/packages/core/src/client/shared/antiantidebugger.ts +20 -0
  393. package/source/packages/scramjet/packages/core/src/client/shared/blob.ts +35 -0
  394. package/source/packages/scramjet/packages/core/src/client/shared/caches.ts +31 -0
  395. package/source/packages/scramjet/packages/core/src/client/shared/chrome.ts +90 -0
  396. package/source/packages/scramjet/packages/core/src/client/shared/err.ts +55 -0
  397. package/source/packages/scramjet/packages/core/src/client/shared/error.ts +42 -0
  398. package/source/packages/scramjet/packages/core/src/client/shared/eval.ts +40 -0
  399. package/source/packages/scramjet/packages/core/src/client/shared/event.ts +215 -0
  400. package/source/packages/scramjet/packages/core/src/client/shared/function.ts +59 -0
  401. package/source/packages/scramjet/packages/core/src/client/shared/import.ts +46 -0
  402. package/source/packages/scramjet/packages/core/src/client/shared/indexeddb.ts +18 -0
  403. package/source/packages/scramjet/packages/core/src/client/shared/opfs.ts +27 -0
  404. package/source/packages/scramjet/packages/core/src/client/shared/postmessage.ts +113 -0
  405. package/source/packages/scramjet/packages/core/src/client/shared/realm.ts +26 -0
  406. package/source/packages/scramjet/packages/core/src/client/shared/requests/eventsource.ts +17 -0
  407. package/source/packages/scramjet/packages/core/src/client/shared/requests/fetch.ts +67 -0
  408. package/source/packages/scramjet/packages/core/src/client/shared/requests/websocket.ts +389 -0
  409. package/source/packages/scramjet/packages/core/src/client/shared/requests/xmlhttprequest.ts +163 -0
  410. package/source/packages/scramjet/packages/core/src/client/shared/settimeout.ts +20 -0
  411. package/source/packages/scramjet/packages/core/src/client/shared/sourcemaps.ts +203 -0
  412. package/source/packages/scramjet/packages/core/src/client/shared/worker.ts +72 -0
  413. package/source/packages/scramjet/packages/core/src/client/shared/wrap.ts +197 -0
  414. package/source/packages/scramjet/packages/core/src/client/singletonbox.ts +52 -0
  415. package/source/packages/scramjet/packages/core/src/client/worker/importScripts.ts +13 -0
  416. package/source/packages/scramjet/packages/core/src/fetch/body.ts +93 -0
  417. package/source/packages/scramjet/packages/core/src/fetch/fetch.ts +323 -0
  418. package/source/packages/scramjet/packages/core/src/fetch/headers.ts +528 -0
  419. package/source/packages/scramjet/packages/core/src/fetch/index.ts +198 -0
  420. package/source/packages/scramjet/packages/core/src/fetch/parse.ts +149 -0
  421. package/source/packages/scramjet/packages/core/src/fetch/util.ts +81 -0
  422. package/source/packages/scramjet/packages/core/src/global.d.ts +32 -0
  423. package/source/packages/scramjet/packages/core/src/index.ts +78 -0
  424. package/source/packages/scramjet/packages/core/src/log.ts +117 -0
  425. package/source/packages/scramjet/packages/core/src/shared/cookie.ts +199 -0
  426. package/source/packages/scramjet/packages/core/src/shared/headers.ts +76 -0
  427. package/source/packages/scramjet/packages/core/src/shared/htmlRules.ts +154 -0
  428. package/source/packages/scramjet/packages/core/src/shared/index.ts +79 -0
  429. package/source/packages/scramjet/packages/core/src/shared/mime.ts +234 -0
  430. package/source/packages/scramjet/packages/core/src/shared/refresh.ts +164 -0
  431. package/source/packages/scramjet/packages/core/src/shared/rewriters/css.ts +67 -0
  432. package/source/packages/scramjet/packages/core/src/shared/rewriters/html.ts +603 -0
  433. package/source/packages/scramjet/packages/core/src/shared/rewriters/index.ts +6 -0
  434. package/source/packages/scramjet/packages/core/src/shared/rewriters/js.ts +158 -0
  435. package/source/packages/scramjet/packages/core/src/shared/rewriters/url.ts +248 -0
  436. package/source/packages/scramjet/packages/core/src/shared/rewriters/wasm.ts +58 -0
  437. package/source/packages/scramjet/packages/core/src/shared/rewriters/worker.ts +37 -0
  438. package/source/packages/scramjet/packages/core/src/shared/set-cookie-parser.ts +195 -0
  439. package/source/packages/scramjet/packages/core/src/shared/snapshot.ts +245 -0
  440. package/source/packages/scramjet/packages/core/src/shared/sniffEncoding.ts +1061 -0
  441. package/source/packages/scramjet/packages/core/src/shared/util.ts +35 -0
  442. package/source/packages/scramjet/packages/core/src/symbols.ts +9 -0
  443. package/source/packages/scramjet/packages/core/src/sync.ts +63 -0
  444. package/source/packages/scramjet/packages/core/src/types.ts +66 -0
  445. package/source/packages/scramjet/packages/core/src/typesEntry.ts +6 -0
  446. package/source/packages/scramjet/packages/core/tsconfig.json +21 -0
  447. package/source/packages/scramjet/packages/rpc/index.ts +100 -0
  448. package/source/packages/scramjet/packages/rpc/package.json +7 -0
  449. package/source/packages/scramjet/packages/utils/package.json +39 -0
  450. package/source/packages/scramjet/packages/utils/src/alwaysLastBubble.ts +172 -0
  451. package/source/packages/scramjet/packages/utils/src/catch-escaped-links.ts +34 -0
  452. package/source/packages/scramjet/packages/utils/src/event-handler-plugin.ts +86 -0
  453. package/source/packages/scramjet/packages/utils/src/global.d.ts +8 -0
  454. package/source/packages/scramjet/packages/utils/src/http-cache-plugin.ts +475 -0
  455. package/source/packages/scramjet/packages/utils/src/index.ts +26 -0
  456. package/source/packages/scramjet/packages/utils/src/link-handler-plugin.ts +75 -0
  457. package/source/packages/scramjet/packages/utils/src/typesEntry.ts +5 -0
  458. package/source/packages/scramjet/packages/utils/src/url-watcher.ts +37 -0
  459. package/source/packages/scramjet/packages/utils/src/version.ts +40 -0
  460. package/source/packages/scramjet/packages/utils/tsconfig.json +11 -0
  461. package/source/packages/scramjet/patches/htmlparser2@12.0.0.patch +124 -0
  462. package/source/packages/scramjet/rspack.config.ts +613 -0
  463. package/source/packages/scramjet/tsconfig.json +11 -0
  464. package/source/packages/tracker-protocol/package.json +31 -0
  465. package/source/packages/tracker-protocol/src/index.ts +745 -0
  466. package/source/packages/tracker-protocol/tsconfig.json +18 -0
  467. package/source/pnpm-lock.yaml +12018 -0
  468. package/source/pnpm-workspace.yaml +31 -0
  469. package/source/rspack.config.ts +103 -0
  470. package/source/scripts/update-adblock-assets.mjs +164 -0
  471. package/third_party/filter-lists/LICENSE-CC-BY-SA-3.0.txt +359 -0
  472. package/third_party/filter-lists/LICENSE-GPL-3.0.txt +674 -0
  473. package/third_party/filter-lists/LICENSE-MPL-2.0.txt +375 -0
  474. package/third_party/filter-lists/README.md +24 -0
  475. package/third_party/filter-lists/easylist.txt +83088 -0
  476. package/third_party/filter-lists/easyprivacy.txt +56361 -0
  477. package/third_party/filter-lists/manifest.json +56 -0
  478. package/third_party/filter-lists/ublock-filters.txt +10937 -0
  479. package/third_party/filter-lists/ublock-privacy.txt +3209 -0
  480. package/third_party/filter-lists/ublock-resources.json +1852 -0
  481. package/third_party/filter-lists/ublock-unbreak.txt +6026 -0
@@ -0,0 +1,1134 @@
1
+ import MagicString from "magic-string";
2
+
3
+ export const propertyHoister = () => {
4
+ return {
5
+ name: "property-hoister",
6
+ renderChunk: {
7
+ order: "pre" as const,
8
+ handler(code: string) {
9
+ const propertyUsage = new Map<string, number>();
10
+ let match;
11
+
12
+ // Count property accesses
13
+ const patterns = [
14
+ /(?<!\.)\.([a-zA-Z_$][a-zA-Z0-9_$]*)/g, // .property
15
+ /\?\.([a-zA-Z_$][a-zA-Z0-9_$]*)/g, // ?.property
16
+ /\["([^"]+)"\]/g, // ["property"]
17
+ /}([a-zA-Z_$][a-zA-Z0-9_$]*)\(/g, // }method(
18
+ /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\([^)]*\)\s*\{/g, // method() {
19
+ ];
20
+
21
+ const keywords = [
22
+ "if",
23
+ "for",
24
+ "while",
25
+ "switch",
26
+ "catch",
27
+ "function",
28
+ "return",
29
+ "get",
30
+ "set",
31
+ ];
32
+
33
+ for (const regex of patterns) {
34
+ regex.lastIndex = 0;
35
+ while ((match = regex.exec(code)) !== null) {
36
+ const propName = match[1];
37
+ if (
38
+ propName.length <= 3 ||
39
+ propName.startsWith("_") ||
40
+ keywords.includes(propName)
41
+ )
42
+ continue;
43
+ propertyUsage.set(propName, (propertyUsage.get(propName) || 0) + 1);
44
+ }
45
+ }
46
+
47
+ // Calculate which properties to hoist
48
+ const toHoist: Array<[string, string]> = [];
49
+ const minifiedVarLen = 2;
50
+
51
+ for (const [propName] of propertyUsage) {
52
+ const varName = `__autofolded_${propName}`;
53
+ const escaped = RegExp.escape(propName);
54
+
55
+ const dotCount = (
56
+ code.match(new RegExp(`(?<!\\?|\\.)\\.${escaped}\\b`, "g")) || []
57
+ ).length;
58
+ const optionalCount = (
59
+ code.match(new RegExp(`\\?\\.${escaped}\\b`, "g")) || []
60
+ ).length;
61
+ const bracketCount = (
62
+ code.match(new RegExp(`\\["${escaped}"\\]`, "g")) || []
63
+ ).length;
64
+ const objectKeyCount = (
65
+ code.match(new RegExp(`([,{]\\s*)${escaped}(\\s*:)`, "g")) || []
66
+ ).length;
67
+ const methodMinCount = (
68
+ code.match(new RegExp(`}${escaped}\\(`, "g")) || []
69
+ ).length;
70
+ const methodUnminCount = (
71
+ code.match(
72
+ new RegExp(`\\b${escaped}\\s*\\([^)]*\\)\\s*\\{`, "g")
73
+ ) || []
74
+ ).length;
75
+
76
+ const declarationCost = minifiedVarLen + propName.length + 4; // var="prop",
77
+ const savings =
78
+ dotCount * (propName.length - minifiedVarLen - 1) +
79
+ optionalCount * (propName.length - minifiedVarLen - 2) +
80
+ bracketCount * propName.length +
81
+ objectKeyCount * (propName.length - minifiedVarLen - 2) +
82
+ methodMinCount * (propName.length - minifiedVarLen - 2) +
83
+ methodUnminCount * (propName.length - minifiedVarLen - 2) -
84
+ declarationCost;
85
+
86
+ if (savings > 0) toHoist.push([propName, varName]);
87
+ }
88
+
89
+ if (toHoist.length === 0) return null;
90
+
91
+ // Collect all transformations
92
+ const replacements: Array<[number, number, string]> = [];
93
+ const declarations = toHoist.map(
94
+ ([prop, varName]) => `${varName}="${prop}"`
95
+ );
96
+
97
+ for (const [propName, varName] of toHoist) {
98
+ const patterns: Array<[string, string]> = [
99
+ [`?.${propName}`, `?.[${varName}]`],
100
+ [`.${propName}`, `[${varName}]`],
101
+ [`["${propName}"]`, `[${varName}]`],
102
+ [`}${propName}(`, `}[${varName}](`],
103
+ ];
104
+
105
+ for (const [find, replace] of patterns) {
106
+ let idx = 0;
107
+ while ((idx = code.indexOf(find, idx)) !== -1) {
108
+ // Validate context for .property to avoid ...spread
109
+ if (
110
+ find.startsWith(".") &&
111
+ !find.startsWith("?.") &&
112
+ idx > 0 &&
113
+ code[idx - 1] === "."
114
+ ) {
115
+ idx++;
116
+ continue;
117
+ }
118
+ // Validate word boundary
119
+ if (find.includes(propName)) {
120
+ const endIdx = idx + find.length;
121
+ if (
122
+ endIdx < code.length &&
123
+ /[a-zA-Z0-9_$]/.test(code[endIdx])
124
+ ) {
125
+ idx++;
126
+ continue;
127
+ }
128
+ }
129
+ replacements.push([idx, idx + find.length, replace]);
130
+ idx += find.length;
131
+ }
132
+ }
133
+
134
+ // Handle object keys: { foo: x } and ,foo: x -> {[__foo]:x}
135
+ const objectKeyRegex = new RegExp(
136
+ `([,{]\\s*)${RegExp.escape(propName)}(\\s*:)`,
137
+ "g"
138
+ );
139
+ let objectKeyMatch;
140
+ while ((objectKeyMatch = objectKeyRegex.exec(code)) !== null) {
141
+ const [full, prefix, suffix] = objectKeyMatch;
142
+ const start = objectKeyMatch.index;
143
+ const end = start + full.length;
144
+ replacements.push([start, end, `${prefix}[${varName}]${suffix}`]);
145
+ }
146
+
147
+ // Handle unminified methods: methodName(...) {
148
+ let idx = 0;
149
+ while ((idx = code.indexOf(propName, idx)) !== -1) {
150
+ if (idx > 0 && /[a-zA-Z0-9_$]/.test(code[idx - 1])) {
151
+ idx++;
152
+ continue;
153
+ }
154
+ let pos = idx + propName.length;
155
+ while (pos < code.length && /\s/.test(code[pos])) pos++;
156
+ if (code[pos] !== "(") {
157
+ idx++;
158
+ continue;
159
+ }
160
+ let depth = 1,
161
+ close = pos + 1;
162
+ while (close < code.length && depth > 0) {
163
+ if (code[close] === "(") depth++;
164
+ else if (code[close] === ")") depth--;
165
+ close++;
166
+ }
167
+ while (close < code.length && /\s/.test(code[close])) close++;
168
+ if (code[close] === "{") {
169
+ replacements.push([idx, idx + propName.length, `[${varName}]`]);
170
+ idx += propName.length;
171
+ continue;
172
+ }
173
+ idx++;
174
+ }
175
+ }
176
+
177
+ // Sort and deduplicate replacements (reverse order for application)
178
+ replacements.sort((a, b) => b[0] - a[0]);
179
+ const seen = new Set<string>();
180
+ const uniqueReplacements = replacements.filter(([start, end]) => {
181
+ const key = `${start}-${end}`;
182
+ if (seen.has(key)) return false;
183
+ seen.add(key);
184
+ return true;
185
+ });
186
+
187
+ // Apply transformations with MagicString
188
+ const s = new MagicString(code);
189
+ for (const [start, end, replace] of uniqueReplacements) {
190
+ s.overwrite(start, end, replace);
191
+ }
192
+
193
+ let lastImportIndex = 0;
194
+ for (let i = 0; i < code.length; i++) {
195
+ if (/\s/.test(code[i])) continue;
196
+ if (code.startsWith("//", i)) {
197
+ const idx = code.indexOf("\n", i);
198
+ i = idx === -1 ? code.length : idx;
199
+ continue;
200
+ }
201
+ if (code.startsWith("/*", i)) {
202
+ const idx = code.indexOf("*/", i);
203
+ i = idx === -1 ? code.length : idx + 1;
204
+ continue;
205
+ }
206
+ if (code.startsWith("import", i)) {
207
+ const next = code[i + 6];
208
+ if (!next || !/[a-zA-Z0-9_$]/.test(next)) {
209
+ let inQuote: string | null = null;
210
+ let depth = 0;
211
+ for (let j = i; j < code.length; j++) {
212
+ const ch = code[j];
213
+ if (inQuote) {
214
+ if (ch === "\\" && code[j + 1]) j++;
215
+ else if (ch === inQuote) inQuote = null;
216
+ } else {
217
+ if (ch === "'" || ch === '"') inQuote = ch;
218
+ else if (ch === "{" || ch === "(") depth++;
219
+ else if (ch === "}" || ch === ")") depth--;
220
+ else if (ch === ";" && depth === 0) {
221
+ lastImportIndex = j + 1;
222
+ i = j;
223
+ break;
224
+ }
225
+ }
226
+ }
227
+ continue;
228
+ }
229
+ }
230
+ break;
231
+ }
232
+
233
+ s.appendRight(lastImportIndex, `let ${declarations.join(",")};`);
234
+
235
+ return {
236
+ code: s.toString(),
237
+ map: s.generateMap({ hires: true }),
238
+ };
239
+ },
240
+ },
241
+ };
242
+ };
243
+
244
+ export const stringHoister = () => {
245
+ return {
246
+ name: "string-hoister",
247
+ renderChunk: {
248
+ order: "pre" as const,
249
+ handler(code: string) {
250
+ // Detect existing property hoister declarations to reuse their variables.
251
+ // The property hoister inserts: let __autofolded_foo="foo",__autofolded_bar="bar";
252
+ const existingVars = new Map<string, string>(); // "value" -> varName
253
+ const propHoistMatch = code.match(
254
+ /let (__autofolded_[a-zA-Z_$][a-zA-Z0-9_$]*="[^"]*"(?:,__autofolded_[a-zA-Z_$][a-zA-Z0-9_$]*="[^"]*")*);/
255
+ );
256
+ if (propHoistMatch) {
257
+ const declStr = propHoistMatch[0].slice(4, -1); // strip "let " and ";"
258
+ const declRegex =
259
+ /(__autofolded_[a-zA-Z_$][a-zA-Z0-9_$]*)="([^"]*)"/g;
260
+ let m;
261
+ while ((m = declRegex.exec(declStr)) !== null) {
262
+ // Map both quote styles to the same variable
263
+ existingVars.set(`"${m[2]}"`, m[1]);
264
+ existingVars.set(`'${m[2]}'`, m[1]);
265
+ }
266
+ }
267
+
268
+ // Find all string literal positions and values
269
+ const stringOccurrences = new Map<string, Array<[number, number]>>();
270
+
271
+ for (let i = 0; i < code.length; i++) {
272
+ const ch = code[i];
273
+
274
+ // Skip line comments
275
+ if (ch === "/" && code[i + 1] === "/") {
276
+ const end = code.indexOf("\n", i);
277
+ i = end === -1 ? code.length : end;
278
+ continue;
279
+ }
280
+
281
+ // Skip block comments
282
+ if (ch === "/" && code[i + 1] === "*") {
283
+ const end = code.indexOf("*/", i + 2);
284
+ i = end === -1 ? code.length : end + 1;
285
+ continue;
286
+ }
287
+
288
+ // Skip template literals
289
+ if (ch === "`") {
290
+ i++;
291
+ let depth = 0;
292
+ while (i < code.length) {
293
+ if (code[i] === "\\" && i + 1 < code.length) {
294
+ i += 2;
295
+ continue;
296
+ }
297
+ if (code[i] === "$" && code[i + 1] === "{" && depth === 0) {
298
+ depth++;
299
+ i += 2;
300
+ continue;
301
+ }
302
+ if (code[i] === "{" && depth > 0) {
303
+ depth++;
304
+ i++;
305
+ continue;
306
+ }
307
+ if (code[i] === "}" && depth > 0) {
308
+ depth--;
309
+ i++;
310
+ continue;
311
+ }
312
+ if (code[i] === "`" && depth === 0) break;
313
+ i++;
314
+ }
315
+ continue;
316
+ }
317
+
318
+ if (ch !== '"' && ch !== "'") continue;
319
+
320
+ const quote = ch;
321
+ const start = i;
322
+ i++;
323
+ let value = "";
324
+ let valid = true;
325
+
326
+ while (i < code.length && code[i] !== quote) {
327
+ if (code[i] === "\\") {
328
+ if (i + 1 < code.length) {
329
+ value += code[i] + code[i + 1];
330
+ i += 2;
331
+ } else {
332
+ valid = false;
333
+ break;
334
+ }
335
+ } else if (code[i] === "\n") {
336
+ valid = false;
337
+ break;
338
+ } else {
339
+ value += code[i];
340
+ i++;
341
+ }
342
+ }
343
+
344
+ if (!valid || i >= code.length) continue;
345
+
346
+ const end = i + 1; // include closing quote
347
+
348
+ // Skip very short strings (hoisting won't save bytes)
349
+ if (value.length < 2) continue;
350
+
351
+ // Skip import/export paths
352
+ let skipAsImport = false;
353
+ let j = start - 1;
354
+ while (j >= 0 && /\s/.test(code[j])) j--;
355
+ if (j >= 3 && code.substring(j - 3, j + 1) === "from") {
356
+ skipAsImport = true;
357
+ }
358
+ if (j >= 5 && code.substring(j - 5, j + 1) === "import") {
359
+ skipAsImport = true;
360
+ }
361
+ if (j >= 0 && code[j] === "(") {
362
+ let k = j - 1;
363
+ while (k >= 0 && /\s/.test(code[k])) k--;
364
+ if (k >= 5 && code.substring(k - 5, k + 1) === "import") {
365
+ skipAsImport = true;
366
+ }
367
+ }
368
+ if (skipAsImport) continue;
369
+
370
+ // Skip strings that are part of the property hoister's own declarations
371
+ if (
372
+ propHoistMatch &&
373
+ start >= code.indexOf(propHoistMatch[0]) &&
374
+ end <= code.indexOf(propHoistMatch[0]) + propHoistMatch[0].length
375
+ ) {
376
+ continue;
377
+ }
378
+
379
+ const key = quote + value + quote;
380
+ if (!stringOccurrences.has(key)) {
381
+ stringOccurrences.set(key, []);
382
+ }
383
+ stringOccurrences.get(key)!.push([start, end]);
384
+ }
385
+
386
+ // Calculate which strings to hoist
387
+ const toHoist: Array<{
388
+ literal: string;
389
+ varName: string;
390
+ positions: Array<[number, number]>;
391
+ needsDeclaration: boolean;
392
+ }> = [];
393
+ const minifiedVarLen = 2;
394
+
395
+ let hoistIdx = 0;
396
+ for (const [literal, positions] of stringOccurrences) {
397
+ const count = positions.length;
398
+ const litLen = literal.length;
399
+
400
+ // Check if there's an existing property hoister variable for this string
401
+ const existingVar = existingVars.get(literal);
402
+
403
+ if (existingVar) {
404
+ // No declaration cost - the variable already exists
405
+ // Savings: each occurrence saves (litLen - minifiedVarLen)
406
+ const savings = count * (litLen - minifiedVarLen);
407
+ if (savings > 0) {
408
+ toHoist.push({
409
+ literal,
410
+ varName: existingVar,
411
+ positions,
412
+ needsDeclaration: false,
413
+ });
414
+ }
415
+ } else if (count >= 2) {
416
+ // Need our own declaration
417
+ const declarationCost = minifiedVarLen + litLen + 2;
418
+ const savings =
419
+ count * litLen - (declarationCost + count * minifiedVarLen);
420
+
421
+ if (savings > 0) {
422
+ toHoist.push({
423
+ literal,
424
+ varName: `__hoisted_str_${hoistIdx++}`,
425
+ positions,
426
+ needsDeclaration: true,
427
+ });
428
+ }
429
+ }
430
+ }
431
+
432
+ if (toHoist.length === 0) return null;
433
+
434
+ // Build replacements sorted in reverse order
435
+ const replacements: Array<[number, number, string]> = [];
436
+ for (const { varName, positions } of toHoist) {
437
+ for (const [start, end] of positions) {
438
+ replacements.push([start, end, varName]);
439
+ }
440
+ }
441
+ replacements.sort((a, b) => b[0] - a[0]);
442
+
443
+ // Deduplicate overlapping replacements
444
+ const seen = new Set<string>();
445
+ const uniqueReplacements = replacements.filter(([start, end]) => {
446
+ const key = `${start}-${end}`;
447
+ if (seen.has(key)) return false;
448
+ seen.add(key);
449
+ return true;
450
+ });
451
+
452
+ // Apply with MagicString
453
+ const s = new MagicString(code);
454
+ for (const [start, end, replace] of uniqueReplacements) {
455
+ // If this string is an object property key (next non-whitespace is ':',
456
+ // and not a ternary or case statement), wrap in [] for computed property syntax
457
+ let nextChar = end;
458
+ while (nextChar < code.length && /\s/.test(code[nextChar]))
459
+ nextChar++;
460
+ let isObjectKey = false;
461
+ if (nextChar < code.length && code[nextChar] === ":") {
462
+ // Check it's not a ternary — look backward for preceding context
463
+ let prevChar = start - 1;
464
+ while (prevChar >= 0 && /\s/.test(code[prevChar])) prevChar--;
465
+ // Object key if preceded by '{', ',', or '(' (destructuring/object literal)
466
+ if (
467
+ prevChar >= 0 &&
468
+ (code[prevChar] === "{" ||
469
+ code[prevChar] === "," ||
470
+ code[prevChar] === "(")
471
+ ) {
472
+ isObjectKey = true;
473
+ }
474
+ }
475
+ if (isObjectKey) {
476
+ s.overwrite(start, end, `[${replace}]`);
477
+ } else {
478
+ s.overwrite(start, end, replace);
479
+ }
480
+ }
481
+
482
+ // Only add declarations for strings that need them
483
+ const newDeclarations = toHoist
484
+ .filter((h) => h.needsDeclaration)
485
+ .map(({ literal, varName }) => `${varName}=${literal}`);
486
+
487
+ if (newDeclarations.length > 0) {
488
+ // Find insertion point after imports
489
+ let lastImportIndex = 0;
490
+ for (let i = 0; i < code.length; i++) {
491
+ if (/\s/.test(code[i])) continue;
492
+ if (code.startsWith("//", i)) {
493
+ const idx = code.indexOf("\n", i);
494
+ i = idx === -1 ? code.length : idx;
495
+ continue;
496
+ }
497
+ if (code.startsWith("/*", i)) {
498
+ const idx = code.indexOf("*/", i);
499
+ i = idx === -1 ? code.length : idx + 1;
500
+ continue;
501
+ }
502
+ if (code.startsWith("import", i)) {
503
+ const next = code[i + 6];
504
+ if (!next || !/[a-zA-Z0-9_$]/.test(next)) {
505
+ let inQuote: string | null = null;
506
+ let depth = 0;
507
+ for (let j = i; j < code.length; j++) {
508
+ const ch = code[j];
509
+ if (inQuote) {
510
+ if (ch === "\\" && code[j + 1]) j++;
511
+ else if (ch === inQuote) inQuote = null;
512
+ } else {
513
+ if (ch === "'" || ch === '"') inQuote = ch;
514
+ else if (ch === "{" || ch === "(") depth++;
515
+ else if (ch === "}" || ch === ")") depth--;
516
+ else if (ch === ";" && depth === 0) {
517
+ lastImportIndex = j + 1;
518
+ i = j;
519
+ break;
520
+ }
521
+ }
522
+ }
523
+ continue;
524
+ }
525
+ }
526
+ break;
527
+ }
528
+
529
+ s.appendRight(lastImportIndex, `let ${newDeclarations.join(",")};`);
530
+ }
531
+
532
+ return {
533
+ code: s.toString(),
534
+ map: s.generateMap({ hires: true }),
535
+ };
536
+ },
537
+ },
538
+ };
539
+ };
540
+
541
+ export const globalHoister = (patterns: string[]) => {
542
+ // Sort so compound accesses (e.g. Object.assign) come before their simple
543
+ // base identifiers (e.g. Object). This ensures the compound form gets
544
+ // priority during replacement and is not partially matched by the simple one.
545
+ const sortedPatterns = [...patterns].sort((a, b) => {
546
+ const aDot = a.includes(".");
547
+ const bDot = b.includes(".");
548
+ if (aDot && !bDot) return -1;
549
+ if (!aDot && bDot) return 1;
550
+ // Among same type, longer patterns first
551
+ return b.length - a.length;
552
+ });
553
+
554
+ return {
555
+ name: "global-hoister",
556
+ renderChunk: {
557
+ order: "pre" as const,
558
+ handler(code: string) {
559
+ const idChar = /[a-zA-Z0-9_$]/;
560
+ const minifiedVarLen = 2;
561
+
562
+ // For each global, find all replaceable positions
563
+ const toHoist: Array<{
564
+ pattern: string;
565
+ varName: string;
566
+ positions: Array<[number, number]>;
567
+ }> = [];
568
+
569
+ // Build a set of ranges that are inside strings, comments, or template literals
570
+ // to avoid replacing inside them
571
+ const skipRanges: Array<[number, number]> = [];
572
+ const canStartRegex = (index: number): boolean => {
573
+ let j = index - 1;
574
+ while (j >= 0 && /\s/.test(code[j])) j--;
575
+ if (j < 0) return true;
576
+
577
+ const prev = code[j];
578
+ if (prev === ")" || prev === "]" || prev === "}") return false;
579
+
580
+ if (idChar.test(prev)) {
581
+ let k = j;
582
+ while (k >= 0 && idChar.test(code[k])) k--;
583
+ const prevWord = code.slice(k + 1, j + 1);
584
+ if (
585
+ [
586
+ "return",
587
+ "throw",
588
+ "case",
589
+ "delete",
590
+ "void",
591
+ "typeof",
592
+ "instanceof",
593
+ "in",
594
+ "of",
595
+ "new",
596
+ "yield",
597
+ "await",
598
+ ].includes(prevWord)
599
+ ) {
600
+ return true;
601
+ }
602
+ return false;
603
+ }
604
+
605
+ return true;
606
+ };
607
+
608
+ for (let i = 0; i < code.length; i++) {
609
+ const ch = code[i];
610
+ if (ch === "/" && code[i + 1] === "/") {
611
+ const start = i;
612
+ const end = code.indexOf("\n", i);
613
+ i = end === -1 ? code.length - 1 : end;
614
+ skipRanges.push([start, i + 1]);
615
+ continue;
616
+ }
617
+ if (ch === "/" && code[i + 1] === "*") {
618
+ const start = i;
619
+ const end = code.indexOf("*/", i + 2);
620
+ i = end === -1 ? code.length - 1 : end + 1;
621
+ skipRanges.push([start, i + 1]);
622
+ continue;
623
+ }
624
+ if (ch === "/" && canStartRegex(i)) {
625
+ const start = i;
626
+ i++;
627
+ let inCharClass = false;
628
+ while (i < code.length) {
629
+ if (code[i] === "\\" && i + 1 < code.length) {
630
+ i += 2;
631
+ continue;
632
+ }
633
+ if (code[i] === "[" && !inCharClass) {
634
+ inCharClass = true;
635
+ i++;
636
+ continue;
637
+ }
638
+ if (code[i] === "]" && inCharClass) {
639
+ inCharClass = false;
640
+ i++;
641
+ continue;
642
+ }
643
+ if (code[i] === "/" && !inCharClass) {
644
+ i++;
645
+ while (i < code.length && /[a-z]/i.test(code[i])) i++;
646
+ break;
647
+ }
648
+ i++;
649
+ }
650
+ skipRanges.push([start, i]);
651
+ i--;
652
+ continue;
653
+ }
654
+ if (ch === '"' || ch === "'") {
655
+ const start = i;
656
+ const quote = ch;
657
+ i++;
658
+ while (i < code.length && code[i] !== quote) {
659
+ if (code[i] === "\\") i++;
660
+ i++;
661
+ }
662
+ skipRanges.push([start, i + 1]);
663
+ continue;
664
+ }
665
+ if (ch === "`") {
666
+ const start = i;
667
+ i++;
668
+ let depth = 0;
669
+ while (i < code.length) {
670
+ if (code[i] === "\\" && i + 1 < code.length) {
671
+ i += 2;
672
+ continue;
673
+ }
674
+ if (code[i] === "$" && code[i + 1] === "{" && depth === 0) {
675
+ depth++;
676
+ i += 2;
677
+ continue;
678
+ }
679
+ if (code[i] === "{" && depth > 0) {
680
+ depth++;
681
+ i++;
682
+ continue;
683
+ }
684
+ if (code[i] === "}" && depth > 0) {
685
+ depth--;
686
+ i++;
687
+ continue;
688
+ }
689
+ if (code[i] === "`" && depth === 0) break;
690
+ i++;
691
+ }
692
+ skipRanges.push([start, i + 1]);
693
+ continue;
694
+ }
695
+ }
696
+
697
+ const inSkipRange = (pos: number, end: number): boolean => {
698
+ for (const [s, e] of skipRanges) {
699
+ if (pos >= s && end <= e) return true;
700
+ if (s > end) break;
701
+ }
702
+ return false;
703
+ };
704
+
705
+ // Track which positions are already claimed by compound patterns
706
+ const claimed = new Set<string>();
707
+
708
+ let globalIdx = 0;
709
+ for (const pattern of sortedPatterns) {
710
+ const varName = `__hoisted_global_${globalIdx++}`;
711
+ const positions: Array<[number, number]> = [];
712
+ let idx = 0;
713
+ while ((idx = code.indexOf(pattern, idx)) !== -1) {
714
+ const end = idx + pattern.length;
715
+
716
+ // Check word boundaries
717
+ if (idx > 0 && idChar.test(code[idx - 1])) {
718
+ idx++;
719
+ continue;
720
+ }
721
+ // For simple identifiers, check after; for compound (X.Y), the char after should not be an id char
722
+ if (end < code.length && idChar.test(code[end])) {
723
+ idx++;
724
+ continue;
725
+ }
726
+
727
+ // Skip if inside string/comment
728
+ if (inSkipRange(idx, end)) {
729
+ idx++;
730
+ continue;
731
+ }
732
+
733
+ // Skip if already claimed by a compound pattern
734
+ const key = `${idx}-${end}`;
735
+ let overlapsClaimed = false;
736
+ for (let p = idx; p < end; p++) {
737
+ if (claimed.has(String(p))) {
738
+ overlapsClaimed = true;
739
+ break;
740
+ }
741
+ }
742
+ if (overlapsClaimed) {
743
+ idx++;
744
+ continue;
745
+ }
746
+
747
+ // Skip if it looks like a declaration (let/const/var/class/function before it)
748
+ let j = idx - 1;
749
+ while (j >= 0 && /\s/.test(code[j])) j--;
750
+ // Check for property access: don't replace x.Object
751
+ if (j >= 0 && code[j] === ".") {
752
+ idx++;
753
+ continue;
754
+ }
755
+
756
+ positions.push([idx, end]);
757
+ idx = end;
758
+ }
759
+
760
+ if (positions.length > 0) {
761
+ // Calculate savings
762
+ const patternLen = pattern.length;
763
+ const count = positions.length;
764
+ // Declaration cost: varName=pattern, (before minification the var name is long,
765
+ // but terser will shorten it. After minification: minifiedVarLen=pattern,)
766
+ const declarationCost = minifiedVarLen + patternLen + 2;
767
+ const savings =
768
+ count * patternLen - (declarationCost + count * minifiedVarLen);
769
+
770
+ if (savings > 0) {
771
+ toHoist.push({ pattern, varName, positions });
772
+ // Claim these positions
773
+ for (const [start, end] of positions) {
774
+ for (let p = start; p < end; p++) {
775
+ claimed.add(String(p));
776
+ }
777
+ }
778
+ }
779
+ }
780
+ }
781
+
782
+ if (toHoist.length === 0) return null;
783
+
784
+ // Build a map from base identifier -> varName for reuse in compound declarations
785
+ const baseVarMap = new Map<string, string>();
786
+ for (const { pattern, varName } of toHoist) {
787
+ if (!pattern.includes(".")) {
788
+ baseVarMap.set(pattern, varName);
789
+ }
790
+ }
791
+
792
+ // Build replacements in reverse order
793
+ const replacements: Array<[number, number, string]> = [];
794
+ for (const { varName, positions } of toHoist) {
795
+ for (const [start, end] of positions) {
796
+ replacements.push([start, end, varName]);
797
+ }
798
+ }
799
+ replacements.sort((a, b) => b[0] - a[0]);
800
+
801
+ const s = new MagicString(code);
802
+ for (const [start, end, replace] of replacements) {
803
+ s.overwrite(start, end, replace);
804
+ }
805
+
806
+ // Find insertion point after imports
807
+ let lastImportIndex = 0;
808
+ for (let i = 0; i < code.length; i++) {
809
+ if (/\s/.test(code[i])) continue;
810
+ if (code.startsWith("//", i)) {
811
+ const idx = code.indexOf("\n", i);
812
+ i = idx === -1 ? code.length : idx;
813
+ continue;
814
+ }
815
+ if (code.startsWith("/*", i)) {
816
+ const idx = code.indexOf("*/", i);
817
+ i = idx === -1 ? code.length : idx + 1;
818
+ continue;
819
+ }
820
+ if (code.startsWith("import", i)) {
821
+ const next = code[i + 6];
822
+ if (!next || !/[a-zA-Z0-9_$]/.test(next)) {
823
+ let inQuote: string | null = null;
824
+ let depth = 0;
825
+ for (let j = i; j < code.length; j++) {
826
+ const ch = code[j];
827
+ if (inQuote) {
828
+ if (ch === "\\" && code[j + 1]) j++;
829
+ else if (ch === inQuote) inQuote = null;
830
+ } else {
831
+ if (ch === "'" || ch === '"') inQuote = ch;
832
+ else if (ch === "{" || ch === "(") depth++;
833
+ else if (ch === "}" || ch === ")") depth--;
834
+ else if (ch === ";" && depth === 0) {
835
+ lastImportIndex = j + 1;
836
+ i = j;
837
+ break;
838
+ }
839
+ }
840
+ }
841
+ continue;
842
+ }
843
+ }
844
+ break;
845
+ }
846
+
847
+ // Generate declarations with simple identifiers first, so compound
848
+ // patterns can reference them (e.g. `b=Symbol,a=b.toPrimitive`
849
+ // instead of `a=Symbol.toPrimitive,b=Symbol`).
850
+ const simpleDecls: string[] = [];
851
+ const compoundDecls: string[] = [];
852
+ for (const { pattern, varName } of toHoist) {
853
+ if (pattern.includes(".")) {
854
+ const dotIdx = pattern.indexOf(".");
855
+ const base = pattern.substring(0, dotIdx);
856
+ const rest = pattern.substring(dotIdx);
857
+ const baseVar = baseVarMap.get(base);
858
+ // If the base is also hoisted, reference its variable
859
+ compoundDecls.push(
860
+ `${varName}=${baseVar ? baseVar + rest : pattern}`
861
+ );
862
+ } else {
863
+ simpleDecls.push(`${varName}=${pattern}`);
864
+ }
865
+ }
866
+ const declarations = [...simpleDecls, ...compoundDecls];
867
+ s.appendRight(lastImportIndex, `let ${declarations.join(",")};`);
868
+
869
+ return {
870
+ code: s.toString(),
871
+ map: s.generateMap({ hires: true }),
872
+ };
873
+ },
874
+ },
875
+ };
876
+ };
877
+
878
+ export const classToDecl = () => ({
879
+ name: "classToDecl",
880
+ transform(source: string) {
881
+ let code = new MagicString(source);
882
+ code.replace(/class ([a-zA-Z]*) *{/g, "let $1 = class {");
883
+ return {
884
+ code: code.toString(),
885
+ map: code.generateMap({ hires: true }),
886
+ };
887
+ },
888
+ });
889
+
890
+ export const instanceofHoister = () => ({
891
+ name: "hoist-instanceof-invocation",
892
+ renderChunk: {
893
+ order: "pre" as const,
894
+ handler(code: string) {
895
+ if (!code.includes("instanceof")) return null;
896
+
897
+ const ast = this.parse(code);
898
+ const rewritten = new MagicString(code);
899
+ const replacements: Array<[number, number, string]> = [];
900
+
901
+ const visit = (node: any) => {
902
+ if (!node || typeof node !== "object") return;
903
+ if (
904
+ node.type === "BinaryExpression" &&
905
+ node.operator === "instanceof" &&
906
+ typeof node.start === "number" &&
907
+ typeof node.end === "number" &&
908
+ typeof node.left?.start === "number" &&
909
+ typeof node.left?.end === "number" &&
910
+ typeof node.right?.start === "number" &&
911
+ typeof node.right?.end === "number"
912
+ ) {
913
+ const left = code.slice(node.left.start, node.left.end);
914
+ const right = code.slice(node.right.start, node.right.end);
915
+ replacements.push([
916
+ node.start,
917
+ node.end,
918
+ `__instanceof(${left}, ${right})`,
919
+ ]);
920
+ }
921
+
922
+ for (const value of Object.values(node)) {
923
+ if (!value) continue;
924
+ if (Array.isArray(value)) {
925
+ for (const child of value) {
926
+ if (child && typeof child === "object") visit(child);
927
+ }
928
+ } else if (typeof value === "object") {
929
+ visit(value);
930
+ }
931
+ }
932
+ };
933
+
934
+ visit(ast);
935
+ if (replacements.length === 0) return null;
936
+
937
+ replacements.sort((a, b) => b[0] - a[0]);
938
+ for (const [start, end, replacement] of replacements) {
939
+ rewritten.overwrite(start, end, replacement);
940
+ }
941
+
942
+ const helperDecl = "let __instanceof = (a, b) => a instanceof b;";
943
+ if (!code.includes(helperDecl)) {
944
+ let lastImportIndex = 0;
945
+ for (let i = 0; i < code.length; i++) {
946
+ if (/\s/.test(code[i])) continue;
947
+ if (code.startsWith("//", i)) {
948
+ const idx = code.indexOf("\n", i);
949
+ i = idx === -1 ? code.length : idx;
950
+ continue;
951
+ }
952
+ if (code.startsWith("/*", i)) {
953
+ const idx = code.indexOf("*/", i);
954
+ i = idx === -1 ? code.length : idx + 1;
955
+ continue;
956
+ }
957
+ if (code.startsWith("import", i)) {
958
+ const next = code[i + 6];
959
+ if (!next || !/[a-zA-Z0-9_$]/.test(next)) {
960
+ let inQuote: string | null = null;
961
+ let depth = 0;
962
+ for (let j = i; j < code.length; j++) {
963
+ const ch = code[j];
964
+ if (inQuote) {
965
+ if (ch === "\\" && code[j + 1]) j++;
966
+ else if (ch === inQuote) inQuote = null;
967
+ } else {
968
+ if (ch === "'" || ch === '"') inQuote = ch;
969
+ else if (ch === "{" || ch === "(") depth++;
970
+ else if (ch === "}" || ch === ")") depth--;
971
+ else if (ch === ";" && depth === 0) {
972
+ lastImportIndex = j + 1;
973
+ i = j;
974
+ break;
975
+ }
976
+ }
977
+ }
978
+ continue;
979
+ }
980
+ }
981
+ break;
982
+ }
983
+ rewritten.appendRight(lastImportIndex, helperDecl);
984
+ }
985
+
986
+ return {
987
+ code: rewritten.toString(),
988
+ map: rewritten.generateMap({ hires: true }),
989
+ };
990
+ },
991
+ },
992
+ });
993
+
994
+ export const typeofHoister = () => ({
995
+ name: "hoist-typeof-invocation",
996
+ renderChunk: {
997
+ order: "pre" as const,
998
+ handler(code: string) {
999
+ if (!code.includes("typeof")) return null;
1000
+
1001
+ const ast = this.parse(code);
1002
+ const rewritten = new MagicString(code);
1003
+ const replacements: Array<[number, number, string]> = [];
1004
+
1005
+ const visit = (node: any) => {
1006
+ if (!node || typeof node !== "object") return;
1007
+ if (
1008
+ node.type === "BinaryExpression" &&
1009
+ ["==", "===", "!=", "!=="].includes(node.operator) &&
1010
+ typeof node.start === "number" &&
1011
+ typeof node.end === "number" &&
1012
+ typeof node.left?.start === "number" &&
1013
+ typeof node.left?.end === "number" &&
1014
+ typeof node.right?.start === "number" &&
1015
+ typeof node.right?.end === "number"
1016
+ ) {
1017
+ const leftIsTypeof =
1018
+ node.left.type === "UnaryExpression" &&
1019
+ node.left.operator === "typeof" &&
1020
+ typeof node.left.argument?.start === "number" &&
1021
+ typeof node.left.argument?.end === "number";
1022
+ const rightIsTypeof =
1023
+ node.right.type === "UnaryExpression" &&
1024
+ node.right.operator === "typeof" &&
1025
+ typeof node.right.argument?.start === "number" &&
1026
+ typeof node.right.argument?.end === "number";
1027
+
1028
+ if (leftIsTypeof || rightIsTypeof) {
1029
+ const isNegated = node.operator === "!=" || node.operator === "!==";
1030
+ const typeofArg = leftIsTypeof
1031
+ ? code.slice(node.left.argument.start, node.left.argument.end)
1032
+ : code.slice(node.right.argument.start, node.right.argument.end);
1033
+ const compareArg = leftIsTypeof
1034
+ ? code.slice(node.right.start, node.right.end)
1035
+ : code.slice(node.left.start, node.left.end);
1036
+ const helperCall = `__typeof(${typeofArg}, ${compareArg})`;
1037
+ replacements.push([
1038
+ node.start,
1039
+ node.end,
1040
+ isNegated ? `!${helperCall}` : helperCall,
1041
+ ]);
1042
+ }
1043
+ }
1044
+
1045
+ for (const value of Object.values(node)) {
1046
+ if (!value) continue;
1047
+ if (Array.isArray(value)) {
1048
+ for (const child of value) {
1049
+ if (child && typeof child === "object") visit(child);
1050
+ }
1051
+ } else if (typeof value === "object") {
1052
+ visit(value);
1053
+ }
1054
+ }
1055
+ };
1056
+
1057
+ visit(ast);
1058
+ if (replacements.length === 0) return null;
1059
+
1060
+ replacements.sort((a, b) => b[0] - a[0]);
1061
+ for (const [start, end, replacement] of replacements) {
1062
+ rewritten.overwrite(start, end, replacement);
1063
+ }
1064
+
1065
+ const helperDecl = "let __typeof = (x, a) => typeof x == a;";
1066
+ if (!code.includes(helperDecl)) {
1067
+ let lastImportIndex = 0;
1068
+ for (let i = 0; i < code.length; i++) {
1069
+ if (/\s/.test(code[i])) continue;
1070
+ if (code.startsWith("//", i)) {
1071
+ const idx = code.indexOf("\n", i);
1072
+ i = idx === -1 ? code.length : idx;
1073
+ continue;
1074
+ }
1075
+ if (code.startsWith("/*", i)) {
1076
+ const idx = code.indexOf("*/", i);
1077
+ i = idx === -1 ? code.length : idx + 1;
1078
+ continue;
1079
+ }
1080
+ if (code.startsWith("import", i)) {
1081
+ const next = code[i + 6];
1082
+ if (!next || !/[a-zA-Z0-9_$]/.test(next)) {
1083
+ let inQuote: string | null = null;
1084
+ let depth = 0;
1085
+ for (let j = i; j < code.length; j++) {
1086
+ const ch = code[j];
1087
+ if (inQuote) {
1088
+ if (ch === "\\" && code[j + 1]) j++;
1089
+ else if (ch === inQuote) inQuote = null;
1090
+ } else {
1091
+ if (ch === "'" || ch === '"') inQuote = ch;
1092
+ else if (ch === "{" || ch === "(") depth++;
1093
+ else if (ch === "}" || ch === ")") depth--;
1094
+ else if (ch === ";" && depth === 0) {
1095
+ lastImportIndex = j + 1;
1096
+ i = j;
1097
+ break;
1098
+ }
1099
+ }
1100
+ }
1101
+ continue;
1102
+ }
1103
+ }
1104
+ break;
1105
+ }
1106
+ rewritten.appendRight(lastImportIndex, helperDecl);
1107
+ }
1108
+
1109
+ return {
1110
+ code: rewritten.toString(),
1111
+ map: rewritten.generateMap({ hires: true }),
1112
+ };
1113
+ },
1114
+ },
1115
+ });
1116
+
1117
+ export const stripBetweenComments = (
1118
+ startComment: string,
1119
+ endComment: string
1120
+ ) => ({
1121
+ name: "stripBetweenComments",
1122
+ transform(source: string) {
1123
+ let code = new MagicString(source);
1124
+ const pattern = new RegExp(
1125
+ `([\\t ]*\\/\\* ?${startComment} ?\\*\\/)[\\s\\S]*?(\\/\\* ?${endComment} ?\\*\\/[\\t ]*\\n?)`,
1126
+ "g"
1127
+ );
1128
+ code.replace(pattern, "");
1129
+ return {
1130
+ code: code.toString(),
1131
+ map: code.generateMap({ hires: true }),
1132
+ };
1133
+ },
1134
+ });