react-freemium-ads 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -0
- package/dist/index.cjs +203 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +49 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +171 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tuki0918
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# react-freemium-ads
|
|
2
|
+
|
|
3
|
+
This package provides a freemium ads module (AdProvider, AdSlot) for route-based ad control and disabling ads for premium users.
|
|
4
|
+
|
|
5
|
+
## Usage Example (Freemium Ads)
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { AdProvider, AdSlot } from "npm-react-package-template";
|
|
9
|
+
|
|
10
|
+
type AppProps = {
|
|
11
|
+
isPremium: boolean;
|
|
12
|
+
pathname: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export function App({ isPremium, pathname }: AppProps) {
|
|
16
|
+
return (
|
|
17
|
+
<AdProvider
|
|
18
|
+
plan={isPremium ? "premium" : "free"}
|
|
19
|
+
currentPath={pathname}
|
|
20
|
+
config={{
|
|
21
|
+
adClient: "ca-pub-xxxxxxxxxxxxxxxx",
|
|
22
|
+
slots: {
|
|
23
|
+
banner: "1111111111",
|
|
24
|
+
multiplex: "2222222222",
|
|
25
|
+
},
|
|
26
|
+
allowedPaths: ["/", "/search", "/result"],
|
|
27
|
+
blockedPaths: [
|
|
28
|
+
"/checkout",
|
|
29
|
+
"/billing",
|
|
30
|
+
"/account",
|
|
31
|
+
"/login",
|
|
32
|
+
"/privacy",
|
|
33
|
+
"/terms",
|
|
34
|
+
],
|
|
35
|
+
}}
|
|
36
|
+
>
|
|
37
|
+
<header>
|
|
38
|
+
<AdSlot placement="banner" />
|
|
39
|
+
</header>
|
|
40
|
+
|
|
41
|
+
<main>
|
|
42
|
+
{/* Your search/result UI */}
|
|
43
|
+
<AdSlot placement="multiplex" />
|
|
44
|
+
</main>
|
|
45
|
+
</AdProvider>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Notes:
|
|
51
|
+
1. `premium` users do not load the AdSense script.
|
|
52
|
+
2. Use `allowedPaths` / `blockedPaths` to control ad visibility by route.
|
|
53
|
+
3. Use AdSense dashboard settings for Anchor/Vignette behavior.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
AdProvider: () => AdProvider,
|
|
24
|
+
AdSlot: () => AdSlot,
|
|
25
|
+
normalizePath: () => normalizePath,
|
|
26
|
+
shouldShowAds: () => shouldShowAds,
|
|
27
|
+
useAdContext: () => useAdContext,
|
|
28
|
+
useAdsenseScript: () => useAdsenseScript
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/AdProvider.tsx
|
|
33
|
+
var import_react2 = require("react");
|
|
34
|
+
|
|
35
|
+
// src/useAdsenseScript.ts
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
var ADSENSE_SCRIPT_ID = "adsbygoogle-script";
|
|
38
|
+
var ADSENSE_SCRIPT_URL = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
|
|
39
|
+
function buildScriptSrc(adClient, scriptBaseUrl) {
|
|
40
|
+
const separator = scriptBaseUrl.includes("?") ? "&" : "?";
|
|
41
|
+
return `${scriptBaseUrl}${separator}client=${encodeURIComponent(adClient)}`;
|
|
42
|
+
}
|
|
43
|
+
function useAdsenseScript({
|
|
44
|
+
enabled,
|
|
45
|
+
adClient,
|
|
46
|
+
scriptBaseUrl = ADSENSE_SCRIPT_URL
|
|
47
|
+
}) {
|
|
48
|
+
(0, import_react.useEffect)(() => {
|
|
49
|
+
if (!enabled || !adClient || typeof document === "undefined") {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const existingScript = document.getElementById(ADSENSE_SCRIPT_ID);
|
|
53
|
+
if (existingScript) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const script = document.createElement("script");
|
|
57
|
+
script.id = ADSENSE_SCRIPT_ID;
|
|
58
|
+
script.async = true;
|
|
59
|
+
script.crossOrigin = "anonymous";
|
|
60
|
+
script.src = buildScriptSrc(adClient, scriptBaseUrl);
|
|
61
|
+
document.head.appendChild(script);
|
|
62
|
+
}, [enabled, adClient, scriptBaseUrl]);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/visibility.ts
|
|
66
|
+
function normalizePath(path) {
|
|
67
|
+
if (!path) {
|
|
68
|
+
return "/";
|
|
69
|
+
}
|
|
70
|
+
const withoutQuery = path.split("?")[0] ?? "/";
|
|
71
|
+
const withoutHash = withoutQuery.split("#")[0] ?? "/";
|
|
72
|
+
if (!withoutHash || withoutHash === "") {
|
|
73
|
+
return "/";
|
|
74
|
+
}
|
|
75
|
+
return withoutHash.startsWith("/") ? withoutHash : `/${withoutHash}`;
|
|
76
|
+
}
|
|
77
|
+
function pathMatchesPrefix(path, prefix) {
|
|
78
|
+
const normalizedPath = normalizePath(path);
|
|
79
|
+
const normalizedPrefix = normalizePath(prefix);
|
|
80
|
+
if (normalizedPrefix === "/") {
|
|
81
|
+
return normalizedPath === "/";
|
|
82
|
+
}
|
|
83
|
+
return normalizedPath === normalizedPrefix || normalizedPath.startsWith(`${normalizedPrefix}/`);
|
|
84
|
+
}
|
|
85
|
+
function matchesAnyPrefix(path, prefixes) {
|
|
86
|
+
if (!prefixes || prefixes.length === 0) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return prefixes.some((prefix) => pathMatchesPrefix(path, prefix));
|
|
90
|
+
}
|
|
91
|
+
function shouldShowAds(plan, currentPath, config) {
|
|
92
|
+
if (plan === "premium") {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (matchesAnyPrefix(currentPath, config.blockedPaths)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (config.allowedPaths && config.allowedPaths.length > 0 && !matchesAnyPrefix(currentPath, config.allowedPaths)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/AdProvider.tsx
|
|
105
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
106
|
+
var AdContext = (0, import_react2.createContext)(null);
|
|
107
|
+
function AdProvider({
|
|
108
|
+
plan,
|
|
109
|
+
currentPath,
|
|
110
|
+
config,
|
|
111
|
+
children
|
|
112
|
+
}) {
|
|
113
|
+
const showAds = (0, import_react2.useMemo)(
|
|
114
|
+
() => shouldShowAds(plan, currentPath, config),
|
|
115
|
+
[plan, currentPath, config]
|
|
116
|
+
);
|
|
117
|
+
useAdsenseScript({
|
|
118
|
+
enabled: showAds,
|
|
119
|
+
adClient: config.adClient,
|
|
120
|
+
scriptBaseUrl: config.scriptBaseUrl
|
|
121
|
+
});
|
|
122
|
+
const value = (0, import_react2.useMemo)(
|
|
123
|
+
() => ({
|
|
124
|
+
showAds,
|
|
125
|
+
config
|
|
126
|
+
}),
|
|
127
|
+
[showAds, config]
|
|
128
|
+
);
|
|
129
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AdContext.Provider, { value, children });
|
|
130
|
+
}
|
|
131
|
+
function useAdContext() {
|
|
132
|
+
const context = (0, import_react2.useContext)(AdContext);
|
|
133
|
+
if (!context) {
|
|
134
|
+
throw new Error("useAdContext must be used within an AdProvider.");
|
|
135
|
+
}
|
|
136
|
+
return context;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// src/AdSlot.tsx
|
|
140
|
+
var import_react3 = require("react");
|
|
141
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
142
|
+
function AdSlot({
|
|
143
|
+
placement,
|
|
144
|
+
className = "",
|
|
145
|
+
style,
|
|
146
|
+
...insProps
|
|
147
|
+
}) {
|
|
148
|
+
const { showAds, config } = useAdContext();
|
|
149
|
+
const adRef = (0, import_react3.useRef)(null);
|
|
150
|
+
const slotId = config.slots[placement];
|
|
151
|
+
const isMultiplex = (0, import_react3.useMemo)(() => placement === "multiplex", [placement]);
|
|
152
|
+
const pushSignature = (0, import_react3.useMemo)(
|
|
153
|
+
() => `${placement}:${slotId ?? "missing-slot"}`,
|
|
154
|
+
[placement, slotId]
|
|
155
|
+
);
|
|
156
|
+
(0, import_react3.useEffect)(() => {
|
|
157
|
+
if (!showAds || !slotId || typeof window === "undefined") {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const adElement = adRef.current;
|
|
161
|
+
if (!adElement) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (adElement.dataset.codexAdSignature === pushSignature) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
window.adsbygoogle = window.adsbygoogle ?? [];
|
|
169
|
+
window.adsbygoogle.push({});
|
|
170
|
+
adElement.dataset.codexAdSignature = pushSignature;
|
|
171
|
+
} catch {
|
|
172
|
+
}
|
|
173
|
+
}, [showAds, slotId, pushSignature]);
|
|
174
|
+
if (!showAds || !slotId) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
178
|
+
"ins",
|
|
179
|
+
{
|
|
180
|
+
...insProps,
|
|
181
|
+
ref: adRef,
|
|
182
|
+
className: ["adsbygoogle", className].filter(Boolean).join(" "),
|
|
183
|
+
style: {
|
|
184
|
+
display: "block",
|
|
185
|
+
...style
|
|
186
|
+
},
|
|
187
|
+
"data-ad-client": config.adClient,
|
|
188
|
+
"data-ad-slot": slotId,
|
|
189
|
+
"data-ad-format": isMultiplex ? "autorelaxed" : "auto",
|
|
190
|
+
"data-full-width-responsive": isMultiplex ? void 0 : "true"
|
|
191
|
+
}
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
195
|
+
0 && (module.exports = {
|
|
196
|
+
AdProvider,
|
|
197
|
+
AdSlot,
|
|
198
|
+
normalizePath,
|
|
199
|
+
shouldShowAds,
|
|
200
|
+
useAdContext,
|
|
201
|
+
useAdsenseScript
|
|
202
|
+
});
|
|
203
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/AdProvider.tsx","../src/useAdsenseScript.ts","../src/visibility.ts","../src/AdSlot.tsx"],"sourcesContent":["export type { AdProviderProps } from \"./AdProvider\";\nexport { AdProvider, useAdContext } from \"./AdProvider\";\nexport type { AdSlotProps } from \"./AdSlot\";\nexport { AdSlot } from \"./AdSlot\";\nexport type { AdConfig, AdPlacement, AdSlots, Plan } from \"./types\";\nexport type { UseAdsenseScriptOptions } from \"./useAdsenseScript\";\nexport { useAdsenseScript } from \"./useAdsenseScript\";\nexport { normalizePath, shouldShowAds } from \"./visibility\";\n","import type { ReactNode } from \"react\";\nimport { createContext, useContext, useMemo } from \"react\";\nimport type { AdConfig, Plan } from \"./types\";\nimport { useAdsenseScript } from \"./useAdsenseScript\";\nimport { shouldShowAds } from \"./visibility\";\n\ntype AdContextValue = {\n showAds: boolean;\n config: AdConfig;\n};\n\nconst AdContext = createContext<AdContextValue | null>(null);\n\nexport type AdProviderProps = {\n plan: Plan;\n currentPath: string;\n config: AdConfig;\n children: ReactNode;\n};\n\nexport function AdProvider({\n plan,\n currentPath,\n config,\n children,\n}: AdProviderProps) {\n const showAds = useMemo(\n () => shouldShowAds(plan, currentPath, config),\n [plan, currentPath, config],\n );\n\n useAdsenseScript({\n enabled: showAds,\n adClient: config.adClient,\n scriptBaseUrl: config.scriptBaseUrl,\n });\n\n const value = useMemo(\n () => ({\n showAds,\n config,\n }),\n [showAds, config],\n );\n\n return <AdContext.Provider value={value}>{children}</AdContext.Provider>;\n}\n\nexport function useAdContext(): AdContextValue {\n const context = useContext(AdContext);\n if (!context) {\n throw new Error(\"useAdContext must be used within an AdProvider.\");\n }\n return context;\n}\n","import { useEffect } from \"react\";\n\nconst ADSENSE_SCRIPT_ID = \"adsbygoogle-script\";\nconst ADSENSE_SCRIPT_URL =\n \"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\";\n\nexport type UseAdsenseScriptOptions = {\n enabled: boolean;\n adClient: string;\n scriptBaseUrl?: string;\n};\n\nfunction buildScriptSrc(adClient: string, scriptBaseUrl: string): string {\n const separator = scriptBaseUrl.includes(\"?\") ? \"&\" : \"?\";\n return `${scriptBaseUrl}${separator}client=${encodeURIComponent(adClient)}`;\n}\n\nexport function useAdsenseScript({\n enabled,\n adClient,\n scriptBaseUrl = ADSENSE_SCRIPT_URL,\n}: UseAdsenseScriptOptions): void {\n useEffect(() => {\n if (!enabled || !adClient || typeof document === \"undefined\") {\n return;\n }\n\n const existingScript = document.getElementById(ADSENSE_SCRIPT_ID);\n if (existingScript) {\n return;\n }\n\n const script = document.createElement(\"script\");\n script.id = ADSENSE_SCRIPT_ID;\n script.async = true;\n script.crossOrigin = \"anonymous\";\n script.src = buildScriptSrc(adClient, scriptBaseUrl);\n document.head.appendChild(script);\n }, [enabled, adClient, scriptBaseUrl]);\n}\n","import type { AdConfig, Plan } from \"./types\";\n\nexport function normalizePath(path: string): string {\n if (!path) {\n return \"/\";\n }\n\n const withoutQuery = path.split(\"?\")[0] ?? \"/\";\n const withoutHash = withoutQuery.split(\"#\")[0] ?? \"/\";\n\n if (!withoutHash || withoutHash === \"\") {\n return \"/\";\n }\n\n return withoutHash.startsWith(\"/\") ? withoutHash : `/${withoutHash}`;\n}\n\nfunction pathMatchesPrefix(path: string, prefix: string): boolean {\n const normalizedPath = normalizePath(path);\n const normalizedPrefix = normalizePath(prefix);\n\n if (normalizedPrefix === \"/\") {\n return normalizedPath === \"/\";\n }\n\n return (\n normalizedPath === normalizedPrefix ||\n normalizedPath.startsWith(`${normalizedPrefix}/`)\n );\n}\n\nfunction matchesAnyPrefix(path: string, prefixes?: string[]): boolean {\n if (!prefixes || prefixes.length === 0) {\n return false;\n }\n\n return prefixes.some((prefix) => pathMatchesPrefix(path, prefix));\n}\n\nexport function shouldShowAds(\n plan: Plan,\n currentPath: string,\n config: AdConfig,\n): boolean {\n if (plan === \"premium\") {\n return false;\n }\n\n if (matchesAnyPrefix(currentPath, config.blockedPaths)) {\n return false;\n }\n\n if (\n config.allowedPaths &&\n config.allowedPaths.length > 0 &&\n !matchesAnyPrefix(currentPath, config.allowedPaths)\n ) {\n return false;\n }\n\n return true;\n}\n","import type * as React from \"react\";\nimport { useEffect, useMemo, useRef } from \"react\";\nimport { useAdContext } from \"./AdProvider\";\nimport type { AdPlacement } from \"./types\";\n\ndeclare global {\n interface Window {\n adsbygoogle?: unknown[];\n }\n}\n\nexport type AdSlotProps = React.HTMLAttributes<HTMLModElement> & {\n placement: AdPlacement;\n};\n\nexport function AdSlot({\n placement,\n className = \"\",\n style,\n ...insProps\n}: AdSlotProps) {\n const { showAds, config } = useAdContext();\n const adRef = useRef<HTMLModElement | null>(null);\n const slotId = config.slots[placement];\n const isMultiplex = useMemo(() => placement === \"multiplex\", [placement]);\n const pushSignature = useMemo(\n () => `${placement}:${slotId ?? \"missing-slot\"}`,\n [placement, slotId],\n );\n\n useEffect(() => {\n if (!showAds || !slotId || typeof window === \"undefined\") {\n return;\n }\n\n const adElement = adRef.current;\n if (!adElement) {\n return;\n }\n\n if (adElement.dataset.codexAdSignature === pushSignature) {\n return;\n }\n\n try {\n window.adsbygoogle = window.adsbygoogle ?? [];\n window.adsbygoogle.push({});\n adElement.dataset.codexAdSignature = pushSignature;\n } catch {\n // Keep rendering even when ad network init fails.\n }\n }, [showAds, slotId, pushSignature]);\n\n if (!showAds || !slotId) {\n return null;\n }\n\n return (\n <ins\n {...insProps}\n ref={adRef}\n className={[\"adsbygoogle\", className].filter(Boolean).join(\" \")}\n style={{\n display: \"block\",\n ...style,\n }}\n data-ad-client={config.adClient}\n data-ad-slot={slotId}\n data-ad-format={isMultiplex ? \"autorelaxed\" : \"auto\"}\n data-full-width-responsive={isMultiplex ? undefined : \"true\"}\n />\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,IAAAA,gBAAmD;;;ACDnD,mBAA0B;AAE1B,IAAM,oBAAoB;AAC1B,IAAM,qBACJ;AAQF,SAAS,eAAe,UAAkB,eAA+B;AACvE,QAAM,YAAY,cAAc,SAAS,GAAG,IAAI,MAAM;AACtD,SAAO,GAAG,aAAa,GAAG,SAAS,UAAU,mBAAmB,QAAQ,CAAC;AAC3E;AAEO,SAAS,iBAAiB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA,gBAAgB;AAClB,GAAkC;AAChC,8BAAU,MAAM;AACd,QAAI,CAAC,WAAW,CAAC,YAAY,OAAO,aAAa,aAAa;AAC5D;AAAA,IACF;AAEA,UAAM,iBAAiB,SAAS,eAAe,iBAAiB;AAChE,QAAI,gBAAgB;AAClB;AAAA,IACF;AAEA,UAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,WAAO,KAAK;AACZ,WAAO,QAAQ;AACf,WAAO,cAAc;AACrB,WAAO,MAAM,eAAe,UAAU,aAAa;AACnD,aAAS,KAAK,YAAY,MAAM;AAA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a,CAAC;AAEnC,MAAI,CAAC,WAAW,CAAC,QAAQ;AACvB,WAAO;AAAA,EACT;AAEA,SACE;AAAA,IAAC;AAAA;AAAA,MACE,GAAG;AAAA,MACJ,KAAK;AAAA,MACL,WAAW,CAAC,eAAe,SAAS,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG;AAAA,MAC9D,OAAO;AAAA,QACL,SAAS;AAAA,QACT,GAAG;AAAA,MACL;AAAA,MACA,kBAAgB,OAAO;AAAA,MACvB,gBAAc;AAAA,MACd,kBAAgB,cAAc,gBAAgB;AAAA,MAC9C,8BAA4B,cAAc,SAAY;AAAA;AAAA,EACxD;AAEJ;","names":["import_react","import_react","import_jsx_runtime"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
|
|
5
|
+
type Plan = "free" | "premium";
|
|
6
|
+
type AdPlacement = "banner" | "multiplex";
|
|
7
|
+
type AdSlots = Partial<Record<AdPlacement, string>>;
|
|
8
|
+
type AdConfig = {
|
|
9
|
+
adClient: string;
|
|
10
|
+
slots: AdSlots;
|
|
11
|
+
allowedPaths?: string[];
|
|
12
|
+
blockedPaths?: string[];
|
|
13
|
+
scriptBaseUrl?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type AdContextValue = {
|
|
17
|
+
showAds: boolean;
|
|
18
|
+
config: AdConfig;
|
|
19
|
+
};
|
|
20
|
+
type AdProviderProps = {
|
|
21
|
+
plan: Plan;
|
|
22
|
+
currentPath: string;
|
|
23
|
+
config: AdConfig;
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
};
|
|
26
|
+
declare function AdProvider({ plan, currentPath, config, children, }: AdProviderProps): react_jsx_runtime.JSX.Element;
|
|
27
|
+
declare function useAdContext(): AdContextValue;
|
|
28
|
+
|
|
29
|
+
declare global {
|
|
30
|
+
interface Window {
|
|
31
|
+
adsbygoogle?: unknown[];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
type AdSlotProps = React.HTMLAttributes<HTMLModElement> & {
|
|
35
|
+
placement: AdPlacement;
|
|
36
|
+
};
|
|
37
|
+
declare function AdSlot({ placement, className, style, ...insProps }: AdSlotProps): react_jsx_runtime.JSX.Element | null;
|
|
38
|
+
|
|
39
|
+
type UseAdsenseScriptOptions = {
|
|
40
|
+
enabled: boolean;
|
|
41
|
+
adClient: string;
|
|
42
|
+
scriptBaseUrl?: string;
|
|
43
|
+
};
|
|
44
|
+
declare function useAdsenseScript({ enabled, adClient, scriptBaseUrl, }: UseAdsenseScriptOptions): void;
|
|
45
|
+
|
|
46
|
+
declare function normalizePath(path: string): string;
|
|
47
|
+
declare function shouldShowAds(plan: Plan, currentPath: string, config: AdConfig): boolean;
|
|
48
|
+
|
|
49
|
+
export { type AdConfig, type AdPlacement, AdProvider, type AdProviderProps, AdSlot, type AdSlotProps, type AdSlots, type Plan, type UseAdsenseScriptOptions, normalizePath, shouldShowAds, useAdContext, useAdsenseScript };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
|
|
5
|
+
type Plan = "free" | "premium";
|
|
6
|
+
type AdPlacement = "banner" | "multiplex";
|
|
7
|
+
type AdSlots = Partial<Record<AdPlacement, string>>;
|
|
8
|
+
type AdConfig = {
|
|
9
|
+
adClient: string;
|
|
10
|
+
slots: AdSlots;
|
|
11
|
+
allowedPaths?: string[];
|
|
12
|
+
blockedPaths?: string[];
|
|
13
|
+
scriptBaseUrl?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type AdContextValue = {
|
|
17
|
+
showAds: boolean;
|
|
18
|
+
config: AdConfig;
|
|
19
|
+
};
|
|
20
|
+
type AdProviderProps = {
|
|
21
|
+
plan: Plan;
|
|
22
|
+
currentPath: string;
|
|
23
|
+
config: AdConfig;
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
};
|
|
26
|
+
declare function AdProvider({ plan, currentPath, config, children, }: AdProviderProps): react_jsx_runtime.JSX.Element;
|
|
27
|
+
declare function useAdContext(): AdContextValue;
|
|
28
|
+
|
|
29
|
+
declare global {
|
|
30
|
+
interface Window {
|
|
31
|
+
adsbygoogle?: unknown[];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
type AdSlotProps = React.HTMLAttributes<HTMLModElement> & {
|
|
35
|
+
placement: AdPlacement;
|
|
36
|
+
};
|
|
37
|
+
declare function AdSlot({ placement, className, style, ...insProps }: AdSlotProps): react_jsx_runtime.JSX.Element | null;
|
|
38
|
+
|
|
39
|
+
type UseAdsenseScriptOptions = {
|
|
40
|
+
enabled: boolean;
|
|
41
|
+
adClient: string;
|
|
42
|
+
scriptBaseUrl?: string;
|
|
43
|
+
};
|
|
44
|
+
declare function useAdsenseScript({ enabled, adClient, scriptBaseUrl, }: UseAdsenseScriptOptions): void;
|
|
45
|
+
|
|
46
|
+
declare function normalizePath(path: string): string;
|
|
47
|
+
declare function shouldShowAds(plan: Plan, currentPath: string, config: AdConfig): boolean;
|
|
48
|
+
|
|
49
|
+
export { type AdConfig, type AdPlacement, AdProvider, type AdProviderProps, AdSlot, type AdSlotProps, type AdSlots, type Plan, type UseAdsenseScriptOptions, normalizePath, shouldShowAds, useAdContext, useAdsenseScript };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// src/AdProvider.tsx
|
|
2
|
+
import { createContext, useContext, useMemo } from "react";
|
|
3
|
+
|
|
4
|
+
// src/useAdsenseScript.ts
|
|
5
|
+
import { useEffect } from "react";
|
|
6
|
+
var ADSENSE_SCRIPT_ID = "adsbygoogle-script";
|
|
7
|
+
var ADSENSE_SCRIPT_URL = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
|
|
8
|
+
function buildScriptSrc(adClient, scriptBaseUrl) {
|
|
9
|
+
const separator = scriptBaseUrl.includes("?") ? "&" : "?";
|
|
10
|
+
return `${scriptBaseUrl}${separator}client=${encodeURIComponent(adClient)}`;
|
|
11
|
+
}
|
|
12
|
+
function useAdsenseScript({
|
|
13
|
+
enabled,
|
|
14
|
+
adClient,
|
|
15
|
+
scriptBaseUrl = ADSENSE_SCRIPT_URL
|
|
16
|
+
}) {
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (!enabled || !adClient || typeof document === "undefined") {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const existingScript = document.getElementById(ADSENSE_SCRIPT_ID);
|
|
22
|
+
if (existingScript) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const script = document.createElement("script");
|
|
26
|
+
script.id = ADSENSE_SCRIPT_ID;
|
|
27
|
+
script.async = true;
|
|
28
|
+
script.crossOrigin = "anonymous";
|
|
29
|
+
script.src = buildScriptSrc(adClient, scriptBaseUrl);
|
|
30
|
+
document.head.appendChild(script);
|
|
31
|
+
}, [enabled, adClient, scriptBaseUrl]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/visibility.ts
|
|
35
|
+
function normalizePath(path) {
|
|
36
|
+
if (!path) {
|
|
37
|
+
return "/";
|
|
38
|
+
}
|
|
39
|
+
const withoutQuery = path.split("?")[0] ?? "/";
|
|
40
|
+
const withoutHash = withoutQuery.split("#")[0] ?? "/";
|
|
41
|
+
if (!withoutHash || withoutHash === "") {
|
|
42
|
+
return "/";
|
|
43
|
+
}
|
|
44
|
+
return withoutHash.startsWith("/") ? withoutHash : `/${withoutHash}`;
|
|
45
|
+
}
|
|
46
|
+
function pathMatchesPrefix(path, prefix) {
|
|
47
|
+
const normalizedPath = normalizePath(path);
|
|
48
|
+
const normalizedPrefix = normalizePath(prefix);
|
|
49
|
+
if (normalizedPrefix === "/") {
|
|
50
|
+
return normalizedPath === "/";
|
|
51
|
+
}
|
|
52
|
+
return normalizedPath === normalizedPrefix || normalizedPath.startsWith(`${normalizedPrefix}/`);
|
|
53
|
+
}
|
|
54
|
+
function matchesAnyPrefix(path, prefixes) {
|
|
55
|
+
if (!prefixes || prefixes.length === 0) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return prefixes.some((prefix) => pathMatchesPrefix(path, prefix));
|
|
59
|
+
}
|
|
60
|
+
function shouldShowAds(plan, currentPath, config) {
|
|
61
|
+
if (plan === "premium") {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (matchesAnyPrefix(currentPath, config.blockedPaths)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (config.allowedPaths && config.allowedPaths.length > 0 && !matchesAnyPrefix(currentPath, config.allowedPaths)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/AdProvider.tsx
|
|
74
|
+
import { jsx } from "react/jsx-runtime";
|
|
75
|
+
var AdContext = createContext(null);
|
|
76
|
+
function AdProvider({
|
|
77
|
+
plan,
|
|
78
|
+
currentPath,
|
|
79
|
+
config,
|
|
80
|
+
children
|
|
81
|
+
}) {
|
|
82
|
+
const showAds = useMemo(
|
|
83
|
+
() => shouldShowAds(plan, currentPath, config),
|
|
84
|
+
[plan, currentPath, config]
|
|
85
|
+
);
|
|
86
|
+
useAdsenseScript({
|
|
87
|
+
enabled: showAds,
|
|
88
|
+
adClient: config.adClient,
|
|
89
|
+
scriptBaseUrl: config.scriptBaseUrl
|
|
90
|
+
});
|
|
91
|
+
const value = useMemo(
|
|
92
|
+
() => ({
|
|
93
|
+
showAds,
|
|
94
|
+
config
|
|
95
|
+
}),
|
|
96
|
+
[showAds, config]
|
|
97
|
+
);
|
|
98
|
+
return /* @__PURE__ */ jsx(AdContext.Provider, { value, children });
|
|
99
|
+
}
|
|
100
|
+
function useAdContext() {
|
|
101
|
+
const context = useContext(AdContext);
|
|
102
|
+
if (!context) {
|
|
103
|
+
throw new Error("useAdContext must be used within an AdProvider.");
|
|
104
|
+
}
|
|
105
|
+
return context;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/AdSlot.tsx
|
|
109
|
+
import { useEffect as useEffect2, useMemo as useMemo2, useRef } from "react";
|
|
110
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
111
|
+
function AdSlot({
|
|
112
|
+
placement,
|
|
113
|
+
className = "",
|
|
114
|
+
style,
|
|
115
|
+
...insProps
|
|
116
|
+
}) {
|
|
117
|
+
const { showAds, config } = useAdContext();
|
|
118
|
+
const adRef = useRef(null);
|
|
119
|
+
const slotId = config.slots[placement];
|
|
120
|
+
const isMultiplex = useMemo2(() => placement === "multiplex", [placement]);
|
|
121
|
+
const pushSignature = useMemo2(
|
|
122
|
+
() => `${placement}:${slotId ?? "missing-slot"}`,
|
|
123
|
+
[placement, slotId]
|
|
124
|
+
);
|
|
125
|
+
useEffect2(() => {
|
|
126
|
+
if (!showAds || !slotId || typeof window === "undefined") {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const adElement = adRef.current;
|
|
130
|
+
if (!adElement) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (adElement.dataset.codexAdSignature === pushSignature) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
window.adsbygoogle = window.adsbygoogle ?? [];
|
|
138
|
+
window.adsbygoogle.push({});
|
|
139
|
+
adElement.dataset.codexAdSignature = pushSignature;
|
|
140
|
+
} catch {
|
|
141
|
+
}
|
|
142
|
+
}, [showAds, slotId, pushSignature]);
|
|
143
|
+
if (!showAds || !slotId) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
return /* @__PURE__ */ jsx2(
|
|
147
|
+
"ins",
|
|
148
|
+
{
|
|
149
|
+
...insProps,
|
|
150
|
+
ref: adRef,
|
|
151
|
+
className: ["adsbygoogle", className].filter(Boolean).join(" "),
|
|
152
|
+
style: {
|
|
153
|
+
display: "block",
|
|
154
|
+
...style
|
|
155
|
+
},
|
|
156
|
+
"data-ad-client": config.adClient,
|
|
157
|
+
"data-ad-slot": slotId,
|
|
158
|
+
"data-ad-format": isMultiplex ? "autorelaxed" : "auto",
|
|
159
|
+
"data-full-width-responsive": isMultiplex ? void 0 : "true"
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
export {
|
|
164
|
+
AdProvider,
|
|
165
|
+
AdSlot,
|
|
166
|
+
normalizePath,
|
|
167
|
+
shouldShowAds,
|
|
168
|
+
useAdContext,
|
|
169
|
+
useAdsenseScript
|
|
170
|
+
};
|
|
171
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/AdProvider.tsx","../src/useAdsenseScript.ts","../src/visibility.ts","../src/AdSlot.tsx"],"sourcesContent":["import type { ReactNode } from \"react\";\nimport { createContext, useContext, useMemo } from \"react\";\nimport type { AdConfig, Plan } from \"./types\";\nimport { useAdsenseScript } from \"./useAdsenseScript\";\nimport { shouldShowAds } from \"./visibility\";\n\ntype AdContextValue = {\n showAds: boolean;\n config: AdConfig;\n};\n\nconst AdContext = createContext<AdContextValue | null>(null);\n\nexport type AdProviderProps = {\n plan: Plan;\n currentPath: string;\n config: AdConfig;\n children: ReactNode;\n};\n\nexport function AdProvider({\n plan,\n currentPath,\n config,\n children,\n}: AdProviderProps) {\n const showAds = useMemo(\n () => shouldShowAds(plan, currentPath, config),\n [plan, currentPath, config],\n );\n\n useAdsenseScript({\n enabled: showAds,\n adClient: config.adClient,\n scriptBaseUrl: config.scriptBaseUrl,\n });\n\n const value = useMemo(\n () => ({\n showAds,\n config,\n }),\n [showAds, config],\n );\n\n return <AdContext.Provider value={value}>{children}</AdContext.Provider>;\n}\n\nexport function useAdContext(): AdContextValue {\n const context = useContext(AdContext);\n if (!context) {\n throw new Error(\"useAdContext must be used within an AdProvider.\");\n }\n return context;\n}\n","import { useEffect } from \"react\";\n\nconst ADSENSE_SCRIPT_ID = \"adsbygoogle-script\";\nconst ADSENSE_SCRIPT_URL =\n \"https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\";\n\nexport type UseAdsenseScriptOptions = {\n enabled: boolean;\n adClient: string;\n scriptBaseUrl?: string;\n};\n\nfunction buildScriptSrc(adClient: string, scriptBaseUrl: string): string {\n const separator = scriptBaseUrl.includes(\"?\") ? \"&\" : \"?\";\n return `${scriptBaseUrl}${separator}client=${encodeURIComponent(adClient)}`;\n}\n\nexport function useAdsenseScript({\n enabled,\n adClient,\n scriptBaseUrl = ADSENSE_SCRIPT_URL,\n}: UseAdsenseScriptOptions): void {\n useEffect(() => {\n if (!enabled || !adClient || typeof document === \"undefined\") {\n return;\n }\n\n const existingScript = document.getElementById(ADSENSE_SCRIPT_ID);\n if (existingScript) {\n return;\n }\n\n const script = document.createElement(\"script\");\n script.id = ADSENSE_SCRIPT_ID;\n script.async = true;\n script.crossOrigin = \"anonymous\";\n script.src = buildScriptSrc(adClient, scriptBaseUrl);\n document.head.appendChild(script);\n }, [enabled, adClient, scriptBaseUrl]);\n}\n","import type { AdConfig, Plan } from \"./types\";\n\nexport function normalizePath(path: string): string {\n if (!path) {\n return \"/\";\n }\n\n const withoutQuery = path.split(\"?\")[0] ?? \"/\";\n const withoutHash = withoutQuery.split(\"#\")[0] ?? \"/\";\n\n if (!withoutHash || withoutHash === \"\") {\n return \"/\";\n }\n\n return withoutHash.startsWith(\"/\") ? withoutHash : `/${withoutHash}`;\n}\n\nfunction pathMatchesPrefix(path: string, prefix: string): boolean {\n const normalizedPath = normalizePath(path);\n const normalizedPrefix = normalizePath(prefix);\n\n if (normalizedPrefix === \"/\") {\n return normalizedPath === \"/\";\n }\n\n return (\n normalizedPath === normalizedPrefix ||\n normalizedPath.startsWith(`${normalizedPrefix}/`)\n );\n}\n\nfunction matchesAnyPrefix(path: string, prefixes?: string[]): boolean {\n if (!prefixes || prefixes.length === 0) {\n return false;\n }\n\n return prefixes.some((prefix) => pathMatchesPrefix(path, prefix));\n}\n\nexport function shouldShowAds(\n plan: Plan,\n currentPath: string,\n config: AdConfig,\n): boolean {\n if (plan === \"premium\") {\n return false;\n }\n\n if (matchesAnyPrefix(currentPath, config.blockedPaths)) {\n return false;\n }\n\n if (\n config.allowedPaths &&\n config.allowedPaths.length > 0 &&\n !matchesAnyPrefix(currentPath, config.allowedPaths)\n ) {\n return false;\n }\n\n return true;\n}\n","import type * as React from \"react\";\nimport { useEffect, useMemo, useRef } from \"react\";\nimport { useAdContext } from \"./AdProvider\";\nimport type { AdPlacement } from \"./types\";\n\ndeclare global {\n interface Window {\n adsbygoogle?: unknown[];\n }\n}\n\nexport type AdSlotProps = React.HTMLAttributes<HTMLModElement> & {\n placement: AdPlacement;\n};\n\nexport function AdSlot({\n placement,\n className = \"\",\n style,\n ...insProps\n}: AdSlotProps) {\n const { showAds, config } = useAdContext();\n const adRef = useRef<HTMLModElement | null>(null);\n const slotId = config.slots[placement];\n const isMultiplex = useMemo(() => placement === \"multiplex\", [placement]);\n const pushSignature = useMemo(\n () => `${placement}:${slotId ?? \"missing-slot\"}`,\n [placement, slotId],\n );\n\n useEffect(() => {\n if (!showAds || !slotId || typeof window === \"undefined\") {\n return;\n }\n\n const adElement = adRef.current;\n if (!adElement) {\n return;\n }\n\n if (adElement.dataset.codexAdSignature === pushSignature) {\n return;\n }\n\n try {\n window.adsbygoogle = window.adsbygoogle ?? [];\n window.adsbygoogle.push({});\n adElement.dataset.codexAdSignature = pushSignature;\n } catch {\n // Keep rendering even when ad network init fails.\n }\n }, [showAds, slotId, pushSignature]);\n\n if (!showAds || !slotId) {\n return null;\n }\n\n return (\n <ins\n {...insProps}\n ref={adRef}\n className={[\"adsbygoogle\", className].filter(Boolean).join(\" \")}\n style={{\n display: \"block\",\n ...style,\n }}\n data-ad-client={config.adClient}\n data-ad-slot={slotId}\n data-ad-format={isM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|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-freemium-ads",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js",
|
|
18
|
+
"require": "./dist/index.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --sourcemap --clean",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"test:watch": "vitest",
|
|
25
|
+
"test:coverage": "vitest run --coverage",
|
|
26
|
+
"lint": "biome check .",
|
|
27
|
+
"lint:fix": "biome check --write .",
|
|
28
|
+
"prepublishOnly": "npm run build"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [],
|
|
31
|
+
"author": "tuki0918",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/tuki0918/react-freemium-ads.git"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"react": "^19.2.4",
|
|
41
|
+
"react-dom": "^19.2.4"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@biomejs/biome": "2.3.14",
|
|
45
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
46
|
+
"@testing-library/react": "^16.1.0",
|
|
47
|
+
"@types/react": "^19.2.13",
|
|
48
|
+
"@types/react-dom": "^19.2.3",
|
|
49
|
+
"@vitest/coverage-v8": "^3.0.5",
|
|
50
|
+
"jsdom": "^26.0.0",
|
|
51
|
+
"react": "^19.2.4",
|
|
52
|
+
"react-dom": "^19.2.4",
|
|
53
|
+
"tsup": "^8.5.1",
|
|
54
|
+
"typescript": "^5.9.3",
|
|
55
|
+
"vitest": "^3.0.5"
|
|
56
|
+
}
|
|
57
|
+
}
|