create-abcart-theme 0.10.0 → 1.0.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 (251) hide show
  1. package/CHANGELOG.md +133 -127
  2. package/LICENSE +202 -202
  3. package/NOTICE +2 -2
  4. package/README.md +65 -65
  5. package/bin/create-abcart-theme.mjs +4 -4
  6. package/package.json +49 -49
  7. package/src/args.mjs +113 -113
  8. package/src/banner.mjs +21 -21
  9. package/src/cli.mjs +119 -119
  10. package/src/create.mjs +85 -82
  11. package/src/package-manager.mjs +32 -32
  12. package/src/style.mjs +17 -17
  13. package/templates/default/.agents/skills/{wocart-docs → abcart-docs}/SKILL.md +146 -146
  14. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/SKILL.md +58 -58
  15. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/inputs-and-rights.md +47 -47
  16. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/layout-rtl-and-phone.md +36 -36
  17. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/measure-the-design.md +60 -60
  18. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/tokens-sections-content.md +103 -103
  19. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/verify-and-report.md +102 -102
  20. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/compare-design.cjs +230 -230
  21. package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/qa-pages.cjs +219 -219
  22. package/templates/default/.agents/skills/shopify-theme-asset-collector/SKILL.md +125 -125
  23. package/templates/default/.agents/skills/shopify-theme-asset-collector/scripts/collect_public_assets.py +370 -370
  24. package/templates/default/.dev.vars.example +12 -12
  25. package/templates/default/AGENTS.md +240 -240
  26. package/templates/default/README.md +184 -184
  27. package/templates/default/_gitignore +11 -11
  28. package/templates/default/astro.config.mjs +85 -85
  29. package/templates/default/package.json +36 -36
  30. package/templates/default/public/fonts/OFL.txt +96 -96
  31. package/templates/default/src/actions/index.ts +22 -22
  32. package/templates/default/src/components/cart/AddToCartButton.astro +45 -45
  33. package/templates/default/src/components/cart/CartDrawer.astro +47 -47
  34. package/templates/default/src/components/cart/CartSummary.astro +11 -11
  35. package/templates/default/src/components/cart/FreeShipping.astro +7 -7
  36. package/templates/default/src/components/checkout/CheckoutMain.astro +29 -29
  37. package/templates/default/src/components/checkout/OrderForm.astro +280 -280
  38. package/templates/default/src/components/checkout/ThankYouMain.astro +68 -68
  39. package/templates/default/src/components/landing/LandingImages.astro +51 -51
  40. package/templates/default/src/components/landing/LandingOrder.astro +46 -46
  41. package/templates/default/src/components/landing/LandingProduct.astro +41 -41
  42. package/templates/default/src/components/listing/ListingMain.astro +93 -93
  43. package/templates/default/src/components/listing/PageMain.astro +52 -52
  44. package/templates/default/src/components/listing/Pagination.astro +31 -31
  45. package/templates/default/src/components/listing/SortControl.astro +35 -35
  46. package/templates/default/src/components/product/BuyBox.astro +96 -96
  47. package/templates/default/src/components/product/Description.astro +24 -24
  48. package/templates/default/src/components/product/Gallery.astro +66 -66
  49. package/templates/default/src/components/product/MobileBuyBar.astro +29 -29
  50. package/templates/default/src/components/product/OfferChoice.astro +66 -66
  51. package/templates/default/src/components/product/ProductMain.astro +40 -40
  52. package/templates/default/src/components/product/ProductReviews.astro +72 -72
  53. package/templates/default/src/components/product/RelatedProducts.astro +30 -30
  54. package/templates/default/src/components/product/blocks/BenefitsBlock.astro +13 -13
  55. package/templates/default/src/components/product/blocks/CollapsibleBlock.astro +20 -20
  56. package/templates/default/src/components/product/blocks/DescriptionBlock.astro +19 -19
  57. package/templates/default/src/components/product/blocks/DetailsBlock.astro +33 -33
  58. package/templates/default/src/components/product/blocks/GalleryBlock.astro +12 -12
  59. package/templates/default/src/components/product/blocks/OrderBlock.astro +30 -30
  60. package/templates/default/src/components/product/blocks/PriceBlock.astro +23 -23
  61. package/templates/default/src/components/product/blocks/RatingBlock.astro +16 -16
  62. package/templates/default/src/components/product/blocks/StockBlock.astro +22 -22
  63. package/templates/default/src/components/product/blocks/TextBlock.astro +11 -11
  64. package/templates/default/src/components/product/blocks/TitleBlock.astro +20 -20
  65. package/templates/default/src/components/reviews/ReviewCard.astro +46 -46
  66. package/templates/default/src/components/reviews/ReviewForm.astro +113 -113
  67. package/templates/default/src/components/reviews/VoiceNote.astro +60 -60
  68. package/templates/default/src/components/sections/CollectionCards.astro +65 -65
  69. package/templates/default/src/components/sections/Countdown.astro +57 -57
  70. package/templates/default/src/components/sections/Faq.astro +44 -44
  71. package/templates/default/src/components/sections/FeaturedProduct.astro +71 -71
  72. package/templates/default/src/components/sections/Hero.astro +99 -99
  73. package/templates/default/src/components/sections/ImageBanner.astro +46 -46
  74. package/templates/default/src/components/sections/ProductGrid.astro +47 -47
  75. package/templates/default/src/components/sections/RichText.astro +35 -35
  76. package/templates/default/src/components/sections/Steps.astro +46 -46
  77. package/templates/default/src/components/sections/Testimonials.astro +57 -57
  78. package/templates/default/src/components/shell/Footer.astro +80 -80
  79. package/templates/default/src/components/shell/Header.astro +89 -89
  80. package/templates/default/src/components/shell/LanguageMenu.astro +33 -33
  81. package/templates/default/src/components/shell/MobileMenu.astro +65 -65
  82. package/templates/default/src/components/shell/SearchDialog.astro +19 -19
  83. package/templates/default/src/components/shell/TopBar.astro +54 -54
  84. package/templates/default/src/components/shell/WhatsAppWidget.astro +111 -111
  85. package/templates/default/src/components/store/ContactMain.astro +79 -79
  86. package/templates/default/src/components/store/SearchMain.astro +56 -56
  87. package/templates/default/src/components/store/TrackOrderMain.astro +161 -161
  88. package/templates/default/src/components/surface/Area.astro +20 -20
  89. package/templates/default/src/components/surface/PreviewBridge.astro +24 -24
  90. package/templates/default/src/components/surface/Sections.astro +35 -35
  91. package/templates/default/src/components/ui/Breadcrumbs.astro +36 -36
  92. package/templates/default/src/components/ui/Button.astro +45 -45
  93. package/templates/default/src/components/ui/EditorPlaceholder.astro +24 -24
  94. package/templates/default/src/components/ui/Icon.astro +93 -93
  95. package/templates/default/src/components/ui/Logo.astro +36 -36
  96. package/templates/default/src/components/ui/Photo.astro +46 -46
  97. package/templates/default/src/components/ui/Price.astro +24 -24
  98. package/templates/default/src/components/ui/ProductCard.astro +41 -41
  99. package/templates/default/src/components/ui/Rating.astro +22 -22
  100. package/templates/default/src/components/ui/Section.astro +34 -34
  101. package/templates/default/src/components/ui/SectionHeading.astro +19 -19
  102. package/templates/default/src/env.d.ts +23 -23
  103. package/templates/default/src/layouts/StoreLayout.astro +145 -145
  104. package/templates/default/src/lib/browser-copy.ts +37 -37
  105. package/templates/default/src/lib/context.ts +89 -89
  106. package/templates/default/src/lib/coupon-notice.ts +47 -47
  107. package/templates/default/src/lib/i18n.ts +32 -32
  108. package/templates/default/src/lib/images.ts +38 -38
  109. package/templates/default/src/lib/listing-query.ts +41 -41
  110. package/templates/default/src/lib/money.ts +16 -16
  111. package/templates/default/src/lib/motion.ts +16 -16
  112. package/templates/default/src/lib/nav.ts +26 -26
  113. package/templates/default/src/lib/offer-tiles.ts +105 -105
  114. package/templates/default/src/lib/order-outcome.ts +66 -66
  115. package/templates/default/src/lib/otp-message.ts +21 -21
  116. package/templates/default/src/lib/products.ts +38 -38
  117. package/templates/default/src/lib/review-refusals.ts +31 -31
  118. package/templates/default/src/lib/store.ts +31 -31
  119. package/templates/default/src/lib/whatsapp.ts +29 -29
  120. package/templates/default/src/middleware.ts +23 -23
  121. package/templates/default/src/pages/404.astro +22 -22
  122. package/templates/default/src/pages/{wocart-preview.astro → abcart-preview.astro} +43 -43
  123. package/templates/default/src/pages/{wocart-revalidate.ts → abcart-revalidate.ts} +10 -10
  124. package/templates/default/src/pages/{wocart-styles.css.ts → abcart-styles.css.ts} +5 -5
  125. package/templates/default/src/pages/{wocart-theme.json.ts → abcart-theme.json.ts} +5 -5
  126. package/templates/default/src/pages/api/abandoned/convert.ts +4 -4
  127. package/templates/default/src/pages/api/abandoned/upsert.ts +4 -4
  128. package/templates/default/src/pages/api/cart/validate.ts +4 -4
  129. package/templates/default/src/pages/api/communes/[wilayaId].ts +4 -4
  130. package/templates/default/src/pages/api/otp/send.ts +4 -4
  131. package/templates/default/src/pages/api/otp/verify.ts +4 -4
  132. package/templates/default/src/pages/api/reviews/[productId].ts +3 -3
  133. package/templates/default/src/pages/api/reviews/eligibility.ts +3 -3
  134. package/templates/default/src/pages/api/reviews/submit.ts +3 -3
  135. package/templates/default/src/pages/api/reviews/upload.ts +3 -3
  136. package/templates/default/src/pages/category/[slug].astro +20 -20
  137. package/templates/default/src/pages/checkout.astro +25 -25
  138. package/templates/default/src/pages/collections/[handle].astro +20 -20
  139. package/templates/default/src/pages/contact.astro +17 -17
  140. package/templates/default/src/pages/index.astro +9 -9
  141. package/templates/default/src/pages/lp/[slug].astro +47 -47
  142. package/templates/default/src/pages/pages/[slug].astro +16 -16
  143. package/templates/default/src/pages/products/[handle].astro +37 -37
  144. package/templates/default/src/pages/products/index.astro +15 -15
  145. package/templates/default/src/pages/robots.txt.ts +9 -9
  146. package/templates/default/src/pages/search.astro +19 -19
  147. package/templates/default/src/pages/sitemap.xml.ts +12 -12
  148. package/templates/default/src/pages/thank-you.astro +33 -33
  149. package/templates/default/src/pages/track-order.astro +17 -17
  150. package/templates/default/src/scripts/cart.ts +371 -371
  151. package/templates/default/src/scripts/checkout/coupon.ts +96 -96
  152. package/templates/default/src/scripts/checkout/delivery.ts +86 -86
  153. package/templates/default/src/scripts/checkout/form.ts +54 -54
  154. package/templates/default/src/scripts/checkout/otp.ts +106 -106
  155. package/templates/default/src/scripts/checkout/picker.ts +169 -169
  156. package/templates/default/src/scripts/checkout/quote.ts +169 -169
  157. package/templates/default/src/scripts/checkout/submit.ts +113 -113
  158. package/templates/default/src/scripts/checkout/tracking.ts +74 -74
  159. package/templates/default/src/scripts/countdown.ts +29 -29
  160. package/templates/default/src/scripts/events.ts +4 -4
  161. package/templates/default/src/scripts/hero.ts +49 -49
  162. package/templates/default/src/scripts/listing.ts +13 -13
  163. package/templates/default/src/scripts/order-form.ts +100 -100
  164. package/templates/default/src/scripts/product.ts +306 -306
  165. package/templates/default/src/scripts/public-config.ts +79 -79
  166. package/templates/default/src/scripts/reviews.ts +358 -358
  167. package/templates/default/src/scripts/site.ts +131 -131
  168. package/templates/default/src/scripts/thank-you.ts +11 -11
  169. package/templates/default/src/scripts/voice-note.ts +94 -94
  170. package/templates/default/src/styles/base.css +1030 -1030
  171. package/templates/default/src/styles/checkout.css +1111 -1111
  172. package/templates/default/src/styles/global.css +12 -12
  173. package/templates/default/src/styles/product.css +871 -871
  174. package/templates/default/src/styles/reviews.css +533 -533
  175. package/templates/default/src/styles/sections.css +931 -931
  176. package/templates/default/src/styles/shell.css +970 -970
  177. package/templates/default/src/styles/tokens.css +583 -583
  178. package/templates/default/src/theme/api-routes.ts +10 -10
  179. package/templates/default/src/theme/content/catalog.ts +117 -117
  180. package/templates/default/src/theme/content/checkout.ts +168 -168
  181. package/templates/default/src/theme/content/editor.ts +21 -21
  182. package/templates/default/src/theme/content/index.ts +45 -45
  183. package/templates/default/src/theme/content/reviews.ts +76 -76
  184. package/templates/default/src/theme/content/shell.ts +59 -59
  185. package/templates/default/src/theme/content/types.ts +12 -12
  186. package/templates/default/src/theme/documents/catalog.json +11 -11
  187. package/templates/default/src/theme/documents/category.json +11 -11
  188. package/templates/default/src/theme/documents/checkout.json +11 -11
  189. package/templates/default/src/theme/documents/collection.json +11 -11
  190. package/templates/default/src/theme/documents/contact.json +11 -11
  191. package/templates/default/src/theme/documents/footer.area.json +11 -11
  192. package/templates/default/src/theme/documents/header.area.json +16 -16
  193. package/templates/default/src/theme/documents/home.json +313 -313
  194. package/templates/default/src/theme/documents/landing.json +8 -8
  195. package/templates/default/src/theme/documents/page.json +11 -11
  196. package/templates/default/src/theme/documents/product.json +125 -125
  197. package/templates/default/src/theme/documents/search.json +11 -11
  198. package/templates/default/src/theme/documents/thank-you.json +11 -11
  199. package/templates/default/src/theme/documents/track-order.json +11 -11
  200. package/templates/default/src/theme/fields.ts +61 -61
  201. package/templates/default/src/theme/key.ts +11 -11
  202. package/templates/default/src/theme/labels.ts +231 -231
  203. package/templates/default/src/theme/locales.ts +11 -11
  204. package/templates/default/src/theme/palette.ts +14 -14
  205. package/templates/default/src/theme/sections/collection-cards.ts +65 -65
  206. package/templates/default/src/theme/sections/faq.ts +74 -74
  207. package/templates/default/src/theme/sections/featured-product.ts +42 -42
  208. package/templates/default/src/theme/sections/hero.ts +47 -47
  209. package/templates/default/src/theme/sections/image-banner.ts +25 -25
  210. package/templates/default/src/theme/sections/index.ts +40 -40
  211. package/templates/default/src/theme/sections/landing.ts +150 -150
  212. package/templates/default/src/theme/sections/listing.ts +84 -84
  213. package/templates/default/src/theme/sections/product-grid.ts +42 -42
  214. package/templates/default/src/theme/sections/product.ts +297 -297
  215. package/templates/default/src/theme/sections/rich-text.ts +39 -39
  216. package/templates/default/src/theme/sections/shared.ts +54 -54
  217. package/templates/default/src/theme/sections/shell.ts +85 -85
  218. package/templates/default/src/theme/sections/store-pages.ts +101 -101
  219. package/templates/default/src/theme/sections/testimonials.ts +61 -61
  220. package/templates/default/src/theme/sections/thank-you.ts +35 -35
  221. package/templates/default/src/theme/settings.ts +124 -124
  222. package/templates/default/src/theme/theme.ts +65 -65
  223. package/templates/default/tests/fixtures/canvas-tote.json +62 -62
  224. package/templates/default/tests/fixtures/linen-shirt.json +128 -128
  225. package/templates/default/tests/unit/checkout.test.ts +241 -241
  226. package/templates/default/tests/unit/contract.test.ts +170 -169
  227. package/templates/default/tests/unit/contrast.test.ts +68 -68
  228. package/templates/default/tests/unit/copy.test.ts +101 -101
  229. package/templates/default/tests/unit/design-values.test.ts +95 -95
  230. package/templates/default/tests/unit/editability.test.ts +180 -180
  231. package/templates/default/tests/unit/fixtures.ts +91 -91
  232. package/templates/default/tests/unit/footer.test.ts +54 -54
  233. package/templates/default/tests/unit/guards.test.ts +340 -340
  234. package/templates/default/tests/unit/images.test.ts +26 -26
  235. package/templates/default/tests/unit/landing.test.ts +151 -151
  236. package/templates/default/tests/unit/languages.test.ts +42 -42
  237. package/templates/default/tests/unit/middleware.test.ts +53 -53
  238. package/templates/default/tests/unit/offer-tiles.test.ts +75 -75
  239. package/templates/default/tests/unit/render.ts +38 -38
  240. package/templates/default/tests/unit/reviews.test.ts +25 -25
  241. package/templates/default/tests/unit/rules.test.ts +59 -59
  242. package/templates/default/tests/unit/source.ts +75 -75
  243. package/templates/default/tests/unit/store-pages.test.ts +147 -147
  244. package/templates/default/tests/unit/studio.test.ts +202 -202
  245. package/templates/default/tests/unit/testimonials.test.ts +52 -52
  246. package/templates/default/tests/unit/thank-you.test.ts +52 -52
  247. package/templates/default/tests/unit/tracking.test.ts +29 -29
  248. package/templates/default/tests/unit/voice-note.test.ts +42 -42
  249. package/templates/default/tsconfig.json +13 -13
  250. package/templates/default/vitest.config.ts +15 -15
  251. package/templates/default/wrangler.jsonc +16 -16
@@ -1,230 +1,230 @@
1
- /**
2
- * Measures how closely a built page matches its design. The reference is a
3
- * design image, or a live page that the design came from. Both are captured
4
- * at the same width, with animations frozen and lazy photos loaded, then
5
- * compared pixel by pixel in the browser. The report names the overall
6
- * difference, the worst regions, and the section each region falls in.
7
- *
8
- * node compare-design.cjs --reference ./design/home.png --page http://localhost:4321/
9
- * node compare-design.cjs --reference https://example.com/ --page http://localhost:4321/ --width 1440
10
- *
11
- * Options:
12
- * --reference a PNG, JPG or WebP file, or a live page's URL
13
- * --page the built page's URL
14
- * --width the capture width for a live reference (default 1440); an
15
- * image reference sets its own width
16
- * --max-diff the largest share of pixels that may differ (default 4)
17
- * --out where to write the report and images (default ./design-compare)
18
- *
19
- * Writes report.json, reference.png, page.png and diff.png (differences in red)
20
- * to --out. Exit status 0 when the difference is within --max-diff, 1 when it
21
- * is not, 2 when the run itself failed. Set CHROME to a Chromium binary and
22
- * PLAYWRIGHT to where Playwright loads from if the defaults do not run.
23
- */
24
- const fs = require("fs");
25
- const path = require("path");
26
- const { chromium } = require(process.env.PLAYWRIGHT || "playwright");
27
-
28
- const args = Object.fromEntries(process.argv.slice(2).reduce((list, arg, i, all) => (arg.startsWith("--") ? [...list, [arg.slice(2), all[i + 1] && !all[i + 1].startsWith("--") ? all[i + 1] : "true"]] : list), []));
29
- const REFERENCE = args.reference;
30
- const PAGE = args.page;
31
- const WIDTH = Number(args.width || 1440);
32
- const MAX_DIFF = Number(args["max-diff"] || 4);
33
- const OUT = path.resolve(args.out || "design-compare");
34
- const CELL = 64;
35
- const CHANNEL_TOLERANCE = 24;
36
-
37
- if (!REFERENCE || !PAGE) {
38
- console.error("Usage: node compare-design.cjs --reference <image|url> --page <url> [--width 1440] [--max-diff 4] [--out dir]");
39
- process.exit(2);
40
- }
41
-
42
- const isUrl = (value) => /^https?:\/\//.test(value);
43
- const MIME = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp" };
44
-
45
- /** Freezes motion and loads every photo, so two captures of the same page match. */
46
- async function settle(page) {
47
- await page.addStyleTag({ content: "*,*::before,*::after{animation:none!important;transition:none!important;scroll-behavior:auto!important}" });
48
- await page.evaluate(async () => {
49
- document.querySelectorAll("img").forEach((img) => {
50
- img.loading = "eager";
51
- img.decoding = "sync";
52
- });
53
- const total = document.body.scrollHeight;
54
- for (let y = 0; y < total; y += 600) {
55
- window.scrollTo(0, y);
56
- await new Promise((resolve) => setTimeout(resolve, 60));
57
- }
58
- window.scrollTo(0, 0);
59
- const until = Date.now() + 25000;
60
- while (Date.now() < until && [...document.images].some((img) => !(img.complete && img.naturalWidth > 0) && img.getAttribute("src"))) {
61
- await new Promise((resolve) => setTimeout(resolve, 200));
62
- }
63
- await document.fonts.ready;
64
- });
65
- await page.waitForTimeout(400);
66
- }
67
-
68
- /** The sections of a built page, top to bottom, with the heading each one shows. */
69
- async function sectionsOf(page) {
70
- return page.evaluate(() => {
71
- const nodes = [...document.querySelectorAll("header, section, footer")];
72
- return nodes.map((node) => {
73
- const box = node.getBoundingClientRect();
74
- const heading = node.querySelector("h1, h2, h3")?.textContent?.replace(/\s+/g, " ").trim() ?? "";
75
- return { tag: node.tagName.toLowerCase(), label: heading || node.getAttribute("aria-label") || node.className.toString().split(" ")[0] || node.tagName.toLowerCase(), top: Math.round(box.top + window.scrollY), height: Math.round(box.height) };
76
- });
77
- });
78
- }
79
-
80
- async function captureUrl(browser, url, width, file) {
81
- const context = await browser.newContext({ viewport: { width, height: 900 }, deviceScaleFactor: 1, reducedMotion: "reduce" });
82
- const page = await context.newPage();
83
- await page.goto(url, { waitUntil: "networkidle", timeout: 90000 });
84
- await settle(page);
85
- const sections = await sectionsOf(page);
86
- await page.screenshot({ path: file, fullPage: true });
87
- await context.close();
88
- return sections;
89
- }
90
-
91
- async function main() {
92
- fs.mkdirSync(OUT, { recursive: true });
93
- const browser = await chromium.launch({ executablePath: process.env.CHROME || undefined });
94
- const referenceFile = path.join(OUT, "reference.png");
95
- const pageFile = path.join(OUT, "page.png");
96
-
97
- let referenceWidth = WIDTH;
98
- if (isUrl(REFERENCE)) {
99
- await captureUrl(browser, REFERENCE, WIDTH, referenceFile);
100
- } else {
101
- fs.copyFileSync(path.resolve(REFERENCE), referenceFile);
102
- const probe = await browser.newPage();
103
- const size = await probe.evaluate(
104
- (data) =>
105
- new Promise((resolve) => {
106
- const img = new Image();
107
- img.onload = () => resolve({ width: img.naturalWidth, height: img.naturalHeight });
108
- img.onerror = () => resolve({ width: 0, height: 0 });
109
- img.src = data;
110
- }),
111
- `data:${MIME[path.extname(REFERENCE).toLowerCase()] || "image/png"};base64,${fs.readFileSync(referenceFile).toString("base64")}`,
112
- );
113
- if (!size.width) throw new Error(`cannot read the reference image ${REFERENCE}`);
114
- referenceWidth = size.width;
115
- await probe.close();
116
- }
117
-
118
- const sections = await captureUrl(browser, PAGE, referenceWidth, pageFile);
119
- const compare = await browser.newPage();
120
- const result = await compare.evaluate(
121
- async ({ a, b, cell, tolerance }) => {
122
- const load = (src) =>
123
- new Promise((resolve, reject) => {
124
- const img = new Image();
125
- img.onload = () => resolve(img);
126
- img.onerror = () => reject(new Error("image failed to load"));
127
- img.src = src;
128
- });
129
- const [ia, ib] = await Promise.all([load(a), load(b)]);
130
- const width = Math.max(ia.naturalWidth, ib.naturalWidth);
131
- const height = Math.max(ia.naturalHeight, ib.naturalHeight);
132
- const draw = (img) => {
133
- const canvas = document.createElement("canvas");
134
- canvas.width = width;
135
- canvas.height = height;
136
- const ctx = canvas.getContext("2d", { willReadFrequently: true });
137
- ctx.fillStyle = "#ffffff";
138
- ctx.fillRect(0, 0, width, height);
139
- ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
140
- return ctx;
141
- };
142
- const ca = draw(ia);
143
- const cb = draw(ib);
144
- const da = ca.getImageData(0, 0, width, height).data;
145
- const db = cb.getImageData(0, 0, width, height).data;
146
- const diffCanvas = document.createElement("canvas");
147
- diffCanvas.width = width;
148
- diffCanvas.height = height;
149
- const dctx = diffCanvas.getContext("2d");
150
- const out = dctx.createImageData(width, height);
151
- const cellsX = Math.ceil(width / cell);
152
- const cellsY = Math.ceil(height / cell);
153
- const cellDiff = new Float64Array(cellsX * cellsY);
154
- const cellCount = new Float64Array(cellsX * cellsY);
155
- let differing = 0;
156
- for (let y = 0; y < height; y++) {
157
- for (let x = 0; x < width; x++) {
158
- const i = (y * width + x) * 4;
159
- const d = Math.max(Math.abs(da[i] - db[i]), Math.abs(da[i + 1] - db[i + 1]), Math.abs(da[i + 2] - db[i + 2]));
160
- const cell_ = Math.floor(y / cell) * cellsX + Math.floor(x / cell);
161
- cellCount[cell_] += 1;
162
- if (d > tolerance) {
163
- differing += 1;
164
- cellDiff[cell_] += 1;
165
- out.data[i] = 255;
166
- out.data[i + 1] = 0;
167
- out.data[i + 2] = 0;
168
- out.data[i + 3] = 255;
169
- } else {
170
- const g = (da[i] + da[i + 1] + da[i + 2]) / 3;
171
- out.data[i] = 255 - (255 - g) * 0.25;
172
- out.data[i + 1] = 255 - (255 - g) * 0.25;
173
- out.data[i + 2] = 255 - (255 - g) * 0.25;
174
- out.data[i + 3] = 255;
175
- }
176
- }
177
- }
178
- dctx.putImageData(out, 0, 0);
179
- const regions = [];
180
- for (let cy = 0; cy < cellsY; cy++)
181
- for (let cx = 0; cx < cellsX; cx++) {
182
- const k = cy * cellsX + cx;
183
- const share = cellCount[k] ? cellDiff[k] / cellCount[k] : 0;
184
- if (share > 0.02) regions.push({ x: cx * cell, y: cy * cell, width: Math.min(cell, width - cx * cell), height: Math.min(cell, height - cy * cell), differs: Math.round(share * 1000) / 10 });
185
- }
186
- regions.sort((p, q) => q.differs - p.differs);
187
- return {
188
- width,
189
- heightReference: ia.naturalHeight,
190
- heightPage: ib.naturalHeight,
191
- differingPixels: differing,
192
- differPercent: Math.round((differing / (width * height)) * 10000) / 100,
193
- regions: regions.slice(0, 12),
194
- diffPng: diffCanvas.toDataURL("image/png"),
195
- };
196
- },
197
- {
198
- a: `data:image/png;base64,${fs.readFileSync(referenceFile).toString("base64")}`,
199
- b: `data:image/png;base64,${fs.readFileSync(pageFile).toString("base64")}`,
200
- cell: CELL,
201
- tolerance: CHANNEL_TOLERANCE,
202
- },
203
- );
204
- await browser.close();
205
-
206
- fs.writeFileSync(path.join(OUT, "diff.png"), Buffer.from(result.diffPng.split(",")[1], "base64"));
207
- const sectionAt = (y) => sections.filter((s) => s.top <= y && y < s.top + s.height).map((s) => s.label)[0] ?? "outside the page's sections";
208
- const report = {
209
- reference: REFERENCE,
210
- page: PAGE,
211
- width: result.width,
212
- heights: { reference: result.heightReference, page: result.heightPage, differs: result.heightReference !== result.heightPage },
213
- differPercent: result.differPercent,
214
- maxDiffPercent: MAX_DIFF,
215
- verdict: result.differPercent <= MAX_DIFF ? "within the limit" : "over the limit",
216
- worstRegions: result.regions.map((r) => ({ ...r, section: sectionAt(r.y) })),
217
- sections,
218
- };
219
- fs.writeFileSync(path.join(OUT, "report.json"), JSON.stringify(report, null, 2));
220
- console.log(`width ${report.width}px, heights reference ${report.heights.reference} / page ${report.heights.page}`);
221
- console.log(`pixels that differ: ${report.differPercent}% (limit ${MAX_DIFF}%) — ${report.verdict}`);
222
- for (const region of report.worstRegions.slice(0, 6)) console.log(` x ${region.x}, y ${region.y}-${region.y + region.height}: ${region.differs}% differs, in "${region.section}"`);
223
- console.log(`report and images: ${OUT}`);
224
- process.exit(result.differPercent <= MAX_DIFF ? 0 : 1);
225
- }
226
-
227
- main().catch((error) => {
228
- console.error(`compare-design failed: ${error.message}`);
229
- process.exit(2);
230
- });
1
+ /**
2
+ * Measures how closely a built page matches its design. The reference is a
3
+ * design image, or a live page that the design came from. Both are captured
4
+ * at the same width, with animations frozen and lazy photos loaded, then
5
+ * compared pixel by pixel in the browser. The report names the overall
6
+ * difference, the worst regions, and the section each region falls in.
7
+ *
8
+ * node compare-design.cjs --reference ./design/home.png --page http://localhost:4321/
9
+ * node compare-design.cjs --reference https://example.com/ --page http://localhost:4321/ --width 1440
10
+ *
11
+ * Options:
12
+ * --reference a PNG, JPG or WebP file, or a live page's URL
13
+ * --page the built page's URL
14
+ * --width the capture width for a live reference (default 1440); an
15
+ * image reference sets its own width
16
+ * --max-diff the largest share of pixels that may differ (default 4)
17
+ * --out where to write the report and images (default ./design-compare)
18
+ *
19
+ * Writes report.json, reference.png, page.png and diff.png (differences in red)
20
+ * to --out. Exit status 0 when the difference is within --max-diff, 1 when it
21
+ * is not, 2 when the run itself failed. Set CHROME to a Chromium binary and
22
+ * PLAYWRIGHT to where Playwright loads from if the defaults do not run.
23
+ */
24
+ const fs = require("fs");
25
+ const path = require("path");
26
+ const { chromium } = require(process.env.PLAYWRIGHT || "playwright");
27
+
28
+ const args = Object.fromEntries(process.argv.slice(2).reduce((list, arg, i, all) => (arg.startsWith("--") ? [...list, [arg.slice(2), all[i + 1] && !all[i + 1].startsWith("--") ? all[i + 1] : "true"]] : list), []));
29
+ const REFERENCE = args.reference;
30
+ const PAGE = args.page;
31
+ const WIDTH = Number(args.width || 1440);
32
+ const MAX_DIFF = Number(args["max-diff"] || 4);
33
+ const OUT = path.resolve(args.out || "design-compare");
34
+ const CELL = 64;
35
+ const CHANNEL_TOLERANCE = 24;
36
+
37
+ if (!REFERENCE || !PAGE) {
38
+ console.error("Usage: node compare-design.cjs --reference <image|url> --page <url> [--width 1440] [--max-diff 4] [--out dir]");
39
+ process.exit(2);
40
+ }
41
+
42
+ const isUrl = (value) => /^https?:\/\//.test(value);
43
+ const MIME = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".webp": "image/webp" };
44
+
45
+ /** Freezes motion and loads every photo, so two captures of the same page match. */
46
+ async function settle(page) {
47
+ await page.addStyleTag({ content: "*,*::before,*::after{animation:none!important;transition:none!important;scroll-behavior:auto!important}" });
48
+ await page.evaluate(async () => {
49
+ document.querySelectorAll("img").forEach((img) => {
50
+ img.loading = "eager";
51
+ img.decoding = "sync";
52
+ });
53
+ const total = document.body.scrollHeight;
54
+ for (let y = 0; y < total; y += 600) {
55
+ window.scrollTo(0, y);
56
+ await new Promise((resolve) => setTimeout(resolve, 60));
57
+ }
58
+ window.scrollTo(0, 0);
59
+ const until = Date.now() + 25000;
60
+ while (Date.now() < until && [...document.images].some((img) => !(img.complete && img.naturalWidth > 0) && img.getAttribute("src"))) {
61
+ await new Promise((resolve) => setTimeout(resolve, 200));
62
+ }
63
+ await document.fonts.ready;
64
+ });
65
+ await page.waitForTimeout(400);
66
+ }
67
+
68
+ /** The sections of a built page, top to bottom, with the heading each one shows. */
69
+ async function sectionsOf(page) {
70
+ return page.evaluate(() => {
71
+ const nodes = [...document.querySelectorAll("header, section, footer")];
72
+ return nodes.map((node) => {
73
+ const box = node.getBoundingClientRect();
74
+ const heading = node.querySelector("h1, h2, h3")?.textContent?.replace(/\s+/g, " ").trim() ?? "";
75
+ return { tag: node.tagName.toLowerCase(), label: heading || node.getAttribute("aria-label") || node.className.toString().split(" ")[0] || node.tagName.toLowerCase(), top: Math.round(box.top + window.scrollY), height: Math.round(box.height) };
76
+ });
77
+ });
78
+ }
79
+
80
+ async function captureUrl(browser, url, width, file) {
81
+ const context = await browser.newContext({ viewport: { width, height: 900 }, deviceScaleFactor: 1, reducedMotion: "reduce" });
82
+ const page = await context.newPage();
83
+ await page.goto(url, { waitUntil: "networkidle", timeout: 90000 });
84
+ await settle(page);
85
+ const sections = await sectionsOf(page);
86
+ await page.screenshot({ path: file, fullPage: true });
87
+ await context.close();
88
+ return sections;
89
+ }
90
+
91
+ async function main() {
92
+ fs.mkdirSync(OUT, { recursive: true });
93
+ const browser = await chromium.launch({ executablePath: process.env.CHROME || undefined });
94
+ const referenceFile = path.join(OUT, "reference.png");
95
+ const pageFile = path.join(OUT, "page.png");
96
+
97
+ let referenceWidth = WIDTH;
98
+ if (isUrl(REFERENCE)) {
99
+ await captureUrl(browser, REFERENCE, WIDTH, referenceFile);
100
+ } else {
101
+ fs.copyFileSync(path.resolve(REFERENCE), referenceFile);
102
+ const probe = await browser.newPage();
103
+ const size = await probe.evaluate(
104
+ (data) =>
105
+ new Promise((resolve) => {
106
+ const img = new Image();
107
+ img.onload = () => resolve({ width: img.naturalWidth, height: img.naturalHeight });
108
+ img.onerror = () => resolve({ width: 0, height: 0 });
109
+ img.src = data;
110
+ }),
111
+ `data:${MIME[path.extname(REFERENCE).toLowerCase()] || "image/png"};base64,${fs.readFileSync(referenceFile).toString("base64")}`,
112
+ );
113
+ if (!size.width) throw new Error(`cannot read the reference image ${REFERENCE}`);
114
+ referenceWidth = size.width;
115
+ await probe.close();
116
+ }
117
+
118
+ const sections = await captureUrl(browser, PAGE, referenceWidth, pageFile);
119
+ const compare = await browser.newPage();
120
+ const result = await compare.evaluate(
121
+ async ({ a, b, cell, tolerance }) => {
122
+ const load = (src) =>
123
+ new Promise((resolve, reject) => {
124
+ const img = new Image();
125
+ img.onload = () => resolve(img);
126
+ img.onerror = () => reject(new Error("image failed to load"));
127
+ img.src = src;
128
+ });
129
+ const [ia, ib] = await Promise.all([load(a), load(b)]);
130
+ const width = Math.max(ia.naturalWidth, ib.naturalWidth);
131
+ const height = Math.max(ia.naturalHeight, ib.naturalHeight);
132
+ const draw = (img) => {
133
+ const canvas = document.createElement("canvas");
134
+ canvas.width = width;
135
+ canvas.height = height;
136
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
137
+ ctx.fillStyle = "#ffffff";
138
+ ctx.fillRect(0, 0, width, height);
139
+ ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
140
+ return ctx;
141
+ };
142
+ const ca = draw(ia);
143
+ const cb = draw(ib);
144
+ const da = ca.getImageData(0, 0, width, height).data;
145
+ const db = cb.getImageData(0, 0, width, height).data;
146
+ const diffCanvas = document.createElement("canvas");
147
+ diffCanvas.width = width;
148
+ diffCanvas.height = height;
149
+ const dctx = diffCanvas.getContext("2d");
150
+ const out = dctx.createImageData(width, height);
151
+ const cellsX = Math.ceil(width / cell);
152
+ const cellsY = Math.ceil(height / cell);
153
+ const cellDiff = new Float64Array(cellsX * cellsY);
154
+ const cellCount = new Float64Array(cellsX * cellsY);
155
+ let differing = 0;
156
+ for (let y = 0; y < height; y++) {
157
+ for (let x = 0; x < width; x++) {
158
+ const i = (y * width + x) * 4;
159
+ const d = Math.max(Math.abs(da[i] - db[i]), Math.abs(da[i + 1] - db[i + 1]), Math.abs(da[i + 2] - db[i + 2]));
160
+ const cell_ = Math.floor(y / cell) * cellsX + Math.floor(x / cell);
161
+ cellCount[cell_] += 1;
162
+ if (d > tolerance) {
163
+ differing += 1;
164
+ cellDiff[cell_] += 1;
165
+ out.data[i] = 255;
166
+ out.data[i + 1] = 0;
167
+ out.data[i + 2] = 0;
168
+ out.data[i + 3] = 255;
169
+ } else {
170
+ const g = (da[i] + da[i + 1] + da[i + 2]) / 3;
171
+ out.data[i] = 255 - (255 - g) * 0.25;
172
+ out.data[i + 1] = 255 - (255 - g) * 0.25;
173
+ out.data[i + 2] = 255 - (255 - g) * 0.25;
174
+ out.data[i + 3] = 255;
175
+ }
176
+ }
177
+ }
178
+ dctx.putImageData(out, 0, 0);
179
+ const regions = [];
180
+ for (let cy = 0; cy < cellsY; cy++)
181
+ for (let cx = 0; cx < cellsX; cx++) {
182
+ const k = cy * cellsX + cx;
183
+ const share = cellCount[k] ? cellDiff[k] / cellCount[k] : 0;
184
+ if (share > 0.02) regions.push({ x: cx * cell, y: cy * cell, width: Math.min(cell, width - cx * cell), height: Math.min(cell, height - cy * cell), differs: Math.round(share * 1000) / 10 });
185
+ }
186
+ regions.sort((p, q) => q.differs - p.differs);
187
+ return {
188
+ width,
189
+ heightReference: ia.naturalHeight,
190
+ heightPage: ib.naturalHeight,
191
+ differingPixels: differing,
192
+ differPercent: Math.round((differing / (width * height)) * 10000) / 100,
193
+ regions: regions.slice(0, 12),
194
+ diffPng: diffCanvas.toDataURL("image/png"),
195
+ };
196
+ },
197
+ {
198
+ a: `data:image/png;base64,${fs.readFileSync(referenceFile).toString("base64")}`,
199
+ b: `data:image/png;base64,${fs.readFileSync(pageFile).toString("base64")}`,
200
+ cell: CELL,
201
+ tolerance: CHANNEL_TOLERANCE,
202
+ },
203
+ );
204
+ await browser.close();
205
+
206
+ fs.writeFileSync(path.join(OUT, "diff.png"), Buffer.from(result.diffPng.split(",")[1], "base64"));
207
+ const sectionAt = (y) => sections.filter((s) => s.top <= y && y < s.top + s.height).map((s) => s.label)[0] ?? "outside the page's sections";
208
+ const report = {
209
+ reference: REFERENCE,
210
+ page: PAGE,
211
+ width: result.width,
212
+ heights: { reference: result.heightReference, page: result.heightPage, differs: result.heightReference !== result.heightPage },
213
+ differPercent: result.differPercent,
214
+ maxDiffPercent: MAX_DIFF,
215
+ verdict: result.differPercent <= MAX_DIFF ? "within the limit" : "over the limit",
216
+ worstRegions: result.regions.map((r) => ({ ...r, section: sectionAt(r.y) })),
217
+ sections,
218
+ };
219
+ fs.writeFileSync(path.join(OUT, "report.json"), JSON.stringify(report, null, 2));
220
+ console.log(`width ${report.width}px, heights reference ${report.heights.reference} / page ${report.heights.page}`);
221
+ console.log(`pixels that differ: ${report.differPercent}% (limit ${MAX_DIFF}%) — ${report.verdict}`);
222
+ for (const region of report.worstRegions.slice(0, 6)) console.log(` x ${region.x}, y ${region.y}-${region.y + region.height}: ${region.differs}% differs, in "${region.section}"`);
223
+ console.log(`report and images: ${OUT}`);
224
+ process.exit(result.differPercent <= MAX_DIFF ? 0 : 1);
225
+ }
226
+
227
+ main().catch((error) => {
228
+ console.error(`compare-design failed: ${error.message}`);
229
+ process.exit(2);
230
+ });