@cedarjs/ogimage-gen 0.0.4
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/cjsWrappers/hooks.js +3 -0
- package/cjsWrappers/middleware.js +3 -0
- package/cjsWrappers/plugin.js +3 -0
- package/dist/OgImageMiddleware.d.ts +59 -0
- package/dist/OgImageMiddleware.d.ts.map +1 -0
- package/dist/OgImageMiddleware.js +258 -0
- package/dist/getRoutesList.d.ts +3 -0
- package/dist/getRoutesList.d.ts.map +1 -0
- package/dist/getRoutesList.js +50 -0
- package/dist/hooks.d.ts +27 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +85 -0
- package/dist/vite-plugin-ogimage-gen.d.ts +11 -0
- package/dist/vite-plugin-ogimage-gen.d.ts.map +1 -0
- package/dist/vite-plugin-ogimage-gen.js +70 -0
- package/empty.js +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Cedar
|
|
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.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { RWRouteManifestItem } from '@cedarjs/internal';
|
|
2
|
+
import type { MiddlewareInvokeOptions, MiddlewareRequest, MiddlewareResponse } from '@cedarjs/web/middleware' with { 'resolution-mode': 'import' };
|
|
3
|
+
interface MwOptions {
|
|
4
|
+
App: React.FC;
|
|
5
|
+
Document: React.FC;
|
|
6
|
+
/**
|
|
7
|
+
* Override the css paths that'll be included
|
|
8
|
+
*/
|
|
9
|
+
cssPaths?: string[];
|
|
10
|
+
}
|
|
11
|
+
interface ComponentElementProps {
|
|
12
|
+
Component: React.FC<{
|
|
13
|
+
data: unknown;
|
|
14
|
+
}>;
|
|
15
|
+
data: unknown;
|
|
16
|
+
routeParams: Record<string, unknown>;
|
|
17
|
+
debug: boolean;
|
|
18
|
+
}
|
|
19
|
+
export default class OgImageMiddleware {
|
|
20
|
+
options: MwOptions;
|
|
21
|
+
App: React.FC;
|
|
22
|
+
Document: React.FC<{
|
|
23
|
+
css: string[];
|
|
24
|
+
meta: string[];
|
|
25
|
+
}>;
|
|
26
|
+
imageProps?: {
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
quality: number;
|
|
30
|
+
};
|
|
31
|
+
constructor(options: MwOptions);
|
|
32
|
+
invoke(req: MiddlewareRequest, mwResponse: MiddlewareResponse, invokeOptions: MiddlewareInvokeOptions): Promise<MiddlewareResponse>;
|
|
33
|
+
private matchRoute;
|
|
34
|
+
getOgComponentPath(currentRoute: RWRouteManifestItem): string;
|
|
35
|
+
get debugElement(): import("react").DetailedReactHTMLElement<{
|
|
36
|
+
style: {
|
|
37
|
+
position: "absolute";
|
|
38
|
+
top: string;
|
|
39
|
+
left: string;
|
|
40
|
+
border: string;
|
|
41
|
+
pointerEvents: "none";
|
|
42
|
+
width: number | undefined;
|
|
43
|
+
height: number | undefined;
|
|
44
|
+
};
|
|
45
|
+
}, HTMLElement>;
|
|
46
|
+
componentElements({ Component, data, routeParams, debug, }: ComponentElementProps): import("react").FunctionComponentElement<{
|
|
47
|
+
data: unknown;
|
|
48
|
+
}> | import("react").DetailedReactHTMLElement<{
|
|
49
|
+
style: {
|
|
50
|
+
width: number | undefined;
|
|
51
|
+
};
|
|
52
|
+
}, HTMLElement>[];
|
|
53
|
+
importComponent(filePath: string, invokeOptions: MiddlewareInvokeOptions): Promise<{
|
|
54
|
+
data: any;
|
|
55
|
+
Component: any;
|
|
56
|
+
}>;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=OgImageMiddleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OgImageMiddleware.d.ts","sourceRoot":"","sources":["../src/OgImageMiddleware.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAG5D,OAAO,KAAK,EACV,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,yBAAyB,CAAC,OAAO,iBAAiB,EAAE,QAAQ,EAAE,CAAA;AAKrE,UAAU,SAAS;IACjB,GAAG,EAAE,KAAK,CAAC,EAAE,CAAA;IACb,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAA;IAClB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;CACpB;AAMD,UAAU,qBAAqB;IAC7B,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC;QAAE,IAAI,EAAE,OAAO,CAAA;KAAE,CAAC,CAAA;IACtC,IAAI,EAAE,OAAO,CAAA;IACb,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACpC,KAAK,EAAE,OAAO,CAAA;CACf;AAED,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,OAAO,EAAE,SAAS,CAAA;IAClB,GAAG,EAAE,KAAK,CAAC,EAAE,CAAA;IACb,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC;QAAE,GAAG,EAAE,MAAM,EAAE,CAAC;QAAC,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAA;IAGrD,UAAU,CAAC,EAAE;QACX,KAAK,EAAE,MAAM,CAAA;QACb,MAAM,EAAE,MAAM,CAAA;QACd,OAAO,EAAE,MAAM,CAAA;KAChB,CAAA;gBAEW,OAAO,EAAE,SAAS;IAOxB,MAAM,CACV,GAAG,EAAE,iBAAiB,EACtB,UAAU,EAAE,kBAAkB,EAC9B,aAAa,EAAE,uBAAuB;IA8IxC,OAAO,CAAC,UAAU,CAoBjB;IAEM,kBAAkB,CAAC,YAAY,EAAE,mBAAmB;IAe3D,IAAI,YAAY;;;;;;;;;;oBA8Bf;IAED,iBAAiB,CAAC,EAChB,SAAS,EACT,IAAI,EACJ,WAAW,EACX,KAAK,GACN,EAAE,qBAAqB;cApPI,OAAO;;;;;;IA0QtB,eAAe,CAC1B,QAAQ,EAAE,MAAM,EAChB,aAAa,EAAE,uBAAuB;;;;CAmBzC"}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var OgImageMiddleware_exports = {};
|
|
30
|
+
__export(OgImageMiddleware_exports, {
|
|
31
|
+
default: () => OgImageMiddleware
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(OgImageMiddleware_exports);
|
|
34
|
+
var import_node_path = __toESM(require("node:path"));
|
|
35
|
+
var import_react = require("react");
|
|
36
|
+
var import_memoize = __toESM(require("lodash/memoize.js"));
|
|
37
|
+
var import_mime_types = __toESM(require("mime-types"));
|
|
38
|
+
var import_server = require("react-dom/server");
|
|
39
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
40
|
+
var import_router = require("@cedarjs/router");
|
|
41
|
+
var import_getRoutesList = require("./getRoutesList.js");
|
|
42
|
+
var import_hooks = require("./hooks.js");
|
|
43
|
+
const supportedExtensions = ["jpg", "png"];
|
|
44
|
+
class OgImageMiddleware {
|
|
45
|
+
options;
|
|
46
|
+
App;
|
|
47
|
+
Document;
|
|
48
|
+
// Initialized in invoke() 👇
|
|
49
|
+
imageProps;
|
|
50
|
+
constructor(options) {
|
|
51
|
+
this.options = options;
|
|
52
|
+
this.App = options.App;
|
|
53
|
+
this.Document = options.Document;
|
|
54
|
+
}
|
|
55
|
+
async invoke(req, mwResponse, invokeOptions) {
|
|
56
|
+
const url = new URL(req.url);
|
|
57
|
+
const { pathname } = url;
|
|
58
|
+
let currentRoute = void 0;
|
|
59
|
+
let parsedParams = {};
|
|
60
|
+
if (!pathname.includes(".")) {
|
|
61
|
+
return mwResponse;
|
|
62
|
+
}
|
|
63
|
+
const [routePathname, extension] = pathname.split(".");
|
|
64
|
+
({ currentRoute, parsedParams } = await this.matchRoute(
|
|
65
|
+
// This is a special case for the index route
|
|
66
|
+
// because they can't go to mywebsite.com/.png -> mywebsite.com/index.png instead
|
|
67
|
+
routePathname === "/index" ? "/" : routePathname,
|
|
68
|
+
parsedParams
|
|
69
|
+
));
|
|
70
|
+
if (!currentRoute || !supportedExtensions.includes(extension)) {
|
|
71
|
+
console.log(
|
|
72
|
+
"OGMiddleware: No match with the Routes, or not a supported extension"
|
|
73
|
+
);
|
|
74
|
+
return mwResponse;
|
|
75
|
+
}
|
|
76
|
+
const mergedParams = {
|
|
77
|
+
...Object.fromEntries(url.searchParams.entries()),
|
|
78
|
+
...parsedParams.params || {}
|
|
79
|
+
};
|
|
80
|
+
this.imageProps = {
|
|
81
|
+
width: mergedParams.width ? parseInt(mergedParams.width) : import_hooks.OGIMAGE_DEFAULTS.width,
|
|
82
|
+
height: mergedParams.height ? parseInt(mergedParams.height) : import_hooks.OGIMAGE_DEFAULTS.height,
|
|
83
|
+
quality: mergedParams.quality ? parseInt(mergedParams.quality) : import_hooks.OGIMAGE_DEFAULTS.quality
|
|
84
|
+
};
|
|
85
|
+
const debug = !!mergedParams.debug;
|
|
86
|
+
const screenshotOptionsByFormat = {
|
|
87
|
+
png: { type: "png" },
|
|
88
|
+
jpg: {
|
|
89
|
+
type: "jpeg",
|
|
90
|
+
quality: this.imageProps.quality
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const pageViewPort = {
|
|
94
|
+
width: this.imageProps.width,
|
|
95
|
+
height: this.imageProps.height
|
|
96
|
+
};
|
|
97
|
+
const ogImgFilePath = this.getOgComponentPath(currentRoute);
|
|
98
|
+
const { data, Component } = await this.importComponent(
|
|
99
|
+
ogImgFilePath,
|
|
100
|
+
invokeOptions
|
|
101
|
+
);
|
|
102
|
+
let dataOut;
|
|
103
|
+
if (data && typeof data === "function") {
|
|
104
|
+
dataOut = await data(mergedParams);
|
|
105
|
+
}
|
|
106
|
+
const { chromium } = await import("playwright");
|
|
107
|
+
const browser = await chromium.launch();
|
|
108
|
+
const page = await browser.newPage({ viewport: pageViewPort });
|
|
109
|
+
const cssPathsToUse = this.options.cssPaths || invokeOptions.cssPaths || [];
|
|
110
|
+
const htmlOutput = (0, import_server.renderToString)(
|
|
111
|
+
(0, import_react.createElement)(
|
|
112
|
+
import_router.LocationProvider,
|
|
113
|
+
{
|
|
114
|
+
location: url
|
|
115
|
+
},
|
|
116
|
+
(0, import_react.createElement)(
|
|
117
|
+
this.Document,
|
|
118
|
+
{
|
|
119
|
+
css: cssPathsToUse,
|
|
120
|
+
meta: []
|
|
121
|
+
},
|
|
122
|
+
(0, import_react.createElement)(
|
|
123
|
+
this.App,
|
|
124
|
+
{},
|
|
125
|
+
this.componentElements({
|
|
126
|
+
Component,
|
|
127
|
+
data: dataOut,
|
|
128
|
+
routeParams: mergedParams,
|
|
129
|
+
debug
|
|
130
|
+
})
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
);
|
|
135
|
+
if (debug) {
|
|
136
|
+
mwResponse.headers.append("Content-Type", "text/html");
|
|
137
|
+
mwResponse.body = htmlOutput;
|
|
138
|
+
} else {
|
|
139
|
+
const baseUrl = url.origin;
|
|
140
|
+
await page.goto(baseUrl);
|
|
141
|
+
await page.setContent(htmlOutput);
|
|
142
|
+
const image = await page.screenshot(screenshotOptionsByFormat[extension]);
|
|
143
|
+
await browser.close();
|
|
144
|
+
mwResponse.headers.append(
|
|
145
|
+
"Content-Type",
|
|
146
|
+
// as string, because the lookup is guaranteed in this case
|
|
147
|
+
import_mime_types.default.lookup(extension)
|
|
148
|
+
);
|
|
149
|
+
mwResponse.body = image;
|
|
150
|
+
}
|
|
151
|
+
return mwResponse;
|
|
152
|
+
}
|
|
153
|
+
matchRoute = (0, import_memoize.default)(
|
|
154
|
+
async (routePathname, parsedParams) => {
|
|
155
|
+
let currentRoute;
|
|
156
|
+
const routes = await (0, import_getRoutesList.getRoutesList)();
|
|
157
|
+
for (const route of routes) {
|
|
158
|
+
const { match, ...rest } = (0, import_router.matchPath)(
|
|
159
|
+
route.pathDefinition,
|
|
160
|
+
routePathname
|
|
161
|
+
);
|
|
162
|
+
if (match) {
|
|
163
|
+
currentRoute = route;
|
|
164
|
+
parsedParams = rest;
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return { currentRoute, parsedParams };
|
|
169
|
+
}
|
|
170
|
+
);
|
|
171
|
+
getOgComponentPath(currentRoute) {
|
|
172
|
+
if (process.env.NODE_ENV === "development") {
|
|
173
|
+
return import_node_path.default.join(
|
|
174
|
+
(0, import_project_config.getPaths)().web.src,
|
|
175
|
+
currentRoute.relativeFilePath.replace(/\.([jt]sx)/, `.og.$1`)
|
|
176
|
+
);
|
|
177
|
+
} else {
|
|
178
|
+
return `${import_node_path.default.join(
|
|
179
|
+
(0, import_project_config.getPaths)().web.distSsr,
|
|
180
|
+
"ogImage",
|
|
181
|
+
currentRoute.relativeFilePath.replace(/\.([jt]sx)/, "")
|
|
182
|
+
)}.og.mjs`;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
get debugElement() {
|
|
186
|
+
return (0, import_react.createElement)(
|
|
187
|
+
"div",
|
|
188
|
+
{
|
|
189
|
+
style: {
|
|
190
|
+
position: "absolute",
|
|
191
|
+
top: "0",
|
|
192
|
+
left: "0",
|
|
193
|
+
border: "1px dashed red",
|
|
194
|
+
pointerEvents: "none",
|
|
195
|
+
width: this.imageProps?.width,
|
|
196
|
+
height: this.imageProps?.height
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
(0, import_react.createElement)(
|
|
200
|
+
"div",
|
|
201
|
+
{
|
|
202
|
+
style: {
|
|
203
|
+
position: "absolute",
|
|
204
|
+
left: "0",
|
|
205
|
+
right: "0",
|
|
206
|
+
bottom: "-1.5rem",
|
|
207
|
+
textAlign: "center",
|
|
208
|
+
color: "red",
|
|
209
|
+
fontWeight: "normal"
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
`${this.imageProps?.width} x ${this.imageProps?.height}`
|
|
213
|
+
)
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
componentElements({
|
|
217
|
+
Component,
|
|
218
|
+
data,
|
|
219
|
+
routeParams,
|
|
220
|
+
debug
|
|
221
|
+
}) {
|
|
222
|
+
const element = (0, import_react.createElement)(Component, {
|
|
223
|
+
data,
|
|
224
|
+
...routeParams
|
|
225
|
+
});
|
|
226
|
+
if (debug) {
|
|
227
|
+
return [
|
|
228
|
+
(0, import_react.createElement)(
|
|
229
|
+
"div",
|
|
230
|
+
{
|
|
231
|
+
style: { width: this.imageProps?.width }
|
|
232
|
+
},
|
|
233
|
+
element
|
|
234
|
+
),
|
|
235
|
+
this.debugElement
|
|
236
|
+
];
|
|
237
|
+
} else {
|
|
238
|
+
return element;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async importComponent(filePath, invokeOptions) {
|
|
242
|
+
try {
|
|
243
|
+
if (invokeOptions.viteDevServer) {
|
|
244
|
+
const { data, output } = await invokeOptions.viteDevServer.ssrLoadModule(filePath);
|
|
245
|
+
return { data, Component: output };
|
|
246
|
+
} else {
|
|
247
|
+
const { data, output } = await import(filePath);
|
|
248
|
+
return { data, Component: output };
|
|
249
|
+
}
|
|
250
|
+
} catch (e) {
|
|
251
|
+
console.error(
|
|
252
|
+
`OGMiddleware: OG Image component import failed: ${filePath}`
|
|
253
|
+
);
|
|
254
|
+
console.error(e);
|
|
255
|
+
throw e;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getRoutesList.d.ts","sourceRoot":"","sources":["../src/getRoutesList.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAG5D,eAAO,MAAM,aAAa,sCAgBzB,CAAA"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var getRoutesList_exports = {};
|
|
30
|
+
__export(getRoutesList_exports, {
|
|
31
|
+
getRoutesList: () => getRoutesList
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(getRoutesList_exports);
|
|
34
|
+
var import_node_url = __toESM(require("node:url"));
|
|
35
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
36
|
+
const getRoutesList = async () => {
|
|
37
|
+
const rwPaths = (0, import_project_config.getPaths)();
|
|
38
|
+
if (process.env.NODE_ENV === "development") {
|
|
39
|
+
const { getProjectRoutes } = await import("@cedarjs/internal/dist/routes.js");
|
|
40
|
+
return getProjectRoutes();
|
|
41
|
+
} else {
|
|
42
|
+
const routeManifestUrl = import_node_url.default.pathToFileURL(rwPaths.web.routeManifest).href;
|
|
43
|
+
const routeManifest = (await import(routeManifestUrl, { with: { type: "json" } })).default;
|
|
44
|
+
return Object.values(routeManifest);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
getRoutesList
|
|
50
|
+
});
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type OgImageUrlOptions = {
|
|
2
|
+
extension?: 'png' | 'jpg';
|
|
3
|
+
width?: number;
|
|
4
|
+
height?: number;
|
|
5
|
+
quality?: number;
|
|
6
|
+
searchParams?: URLSearchParams | Record<string, string>;
|
|
7
|
+
};
|
|
8
|
+
export declare const OGIMAGE_DEFAULTS: {
|
|
9
|
+
extension: string;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
quality: number;
|
|
13
|
+
};
|
|
14
|
+
export declare const useOgImage: (options?: OgImageUrlOptions) => {
|
|
15
|
+
url: string;
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
quality: number;
|
|
19
|
+
extension: string;
|
|
20
|
+
ogProps: {
|
|
21
|
+
image: (string | {
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
})[];
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,iBAAiB,GAAG;IAC9B,SAAS,CAAC,EAAE,KAAK,GAAG,KAAK,CAAA;IACzB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACxD,CAAA;AAED,eAAO,MAAM,gBAAgB;;;;;CAK5B,CAAA;AAED,eAAO,MAAM,UAAU,aAAc,iBAAiB;;;;;;;;;;;;CA2DrD,CAAA"}
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
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
|
+
var hooks_exports = {};
|
|
20
|
+
__export(hooks_exports, {
|
|
21
|
+
OGIMAGE_DEFAULTS: () => OGIMAGE_DEFAULTS,
|
|
22
|
+
useOgImage: () => useOgImage
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(hooks_exports);
|
|
25
|
+
var import_router = require("@cedarjs/router");
|
|
26
|
+
const OGIMAGE_DEFAULTS = {
|
|
27
|
+
extension: "png",
|
|
28
|
+
width: 1200,
|
|
29
|
+
height: 630,
|
|
30
|
+
quality: 100
|
|
31
|
+
};
|
|
32
|
+
const useOgImage = (options) => {
|
|
33
|
+
const { origin, pathname, searchParams: locationSearchParams } = (0, import_router.useLocation)();
|
|
34
|
+
const ext = options?.extension || OGIMAGE_DEFAULTS.extension;
|
|
35
|
+
const width = options?.width;
|
|
36
|
+
const height = options?.height;
|
|
37
|
+
const quality = options?.quality;
|
|
38
|
+
const searchParams = options?.searchParams instanceof URLSearchParams ? options?.searchParams : new URLSearchParams(options?.searchParams || {});
|
|
39
|
+
const outputSearchParams = new URLSearchParams({
|
|
40
|
+
...Object.fromEntries(locationSearchParams),
|
|
41
|
+
...Object.fromEntries(searchParams)
|
|
42
|
+
});
|
|
43
|
+
const output = [origin];
|
|
44
|
+
if (pathname === "/") {
|
|
45
|
+
output.push("/index");
|
|
46
|
+
} else {
|
|
47
|
+
output.push(pathname);
|
|
48
|
+
}
|
|
49
|
+
output.push(`.${ext}`);
|
|
50
|
+
if (width) {
|
|
51
|
+
outputSearchParams.append("width", width.toString());
|
|
52
|
+
}
|
|
53
|
+
if (height) {
|
|
54
|
+
outputSearchParams.append("height", height.toString());
|
|
55
|
+
}
|
|
56
|
+
if (quality) {
|
|
57
|
+
outputSearchParams.append("quality", quality.toString());
|
|
58
|
+
}
|
|
59
|
+
if (outputSearchParams.size) {
|
|
60
|
+
output.push(`?${outputSearchParams}`);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
url: output.join(""),
|
|
64
|
+
width: width || OGIMAGE_DEFAULTS.width,
|
|
65
|
+
height: height || OGIMAGE_DEFAULTS.height,
|
|
66
|
+
quality: quality || OGIMAGE_DEFAULTS.quality,
|
|
67
|
+
extension: ext,
|
|
68
|
+
ogProps: {
|
|
69
|
+
image: [
|
|
70
|
+
output.join(""),
|
|
71
|
+
{
|
|
72
|
+
width: width || OGIMAGE_DEFAULTS.width,
|
|
73
|
+
height: height || OGIMAGE_DEFAULTS.height
|
|
74
|
+
}
|
|
75
|
+
]
|
|
76
|
+
// TODO: Haven't determined if this is required for Twitter to pick up the og:image. Needs some testing.
|
|
77
|
+
// twitter: { image: { src: output.join('') } },
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
82
|
+
0 && (module.exports = {
|
|
83
|
+
OGIMAGE_DEFAULTS,
|
|
84
|
+
useOgImage
|
|
85
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { O } from 'ts-toolbelt';
|
|
2
|
+
import type { Plugin as VitePlugin } from 'vite';
|
|
3
|
+
type ConfigPlugin = O.Required<VitePlugin, 'config'>;
|
|
4
|
+
/**
|
|
5
|
+
* This plugin updates the rollup inputs to include all OG components.
|
|
6
|
+
*
|
|
7
|
+
* Internally, Redwood's vite settings will merge this with the default vite config, and any user config
|
|
8
|
+
*/
|
|
9
|
+
declare function vitePluginOgImageGen(): ConfigPlugin;
|
|
10
|
+
export default vitePluginOgImageGen;
|
|
11
|
+
//# sourceMappingURL=vite-plugin-ogimage-gen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-plugin-ogimage-gen.d.ts","sourceRoot":"","sources":["../src/vite-plugin-ogimage-gen.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,KAAK,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,MAAM,CAAA;AAIhD,KAAK,YAAY,GAAG,CAAC,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAA;AAEpD;;;;GAIG;AACH,iBAAS,oBAAoB,IAAI,YAAY,CAyC5C;AAED,eAAe,oBAAoB,CAAA"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var vite_plugin_ogimage_gen_exports = {};
|
|
30
|
+
__export(vite_plugin_ogimage_gen_exports, {
|
|
31
|
+
default: () => vite_plugin_ogimage_gen_default
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(vite_plugin_ogimage_gen_exports);
|
|
34
|
+
var import_fs = __toESM(require("fs"));
|
|
35
|
+
var import_node_path = __toESM(require("node:path"));
|
|
36
|
+
var import_fast_glob = __toESM(require("fast-glob"));
|
|
37
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
38
|
+
function vitePluginOgImageGen() {
|
|
39
|
+
const rwPaths = (0, import_project_config.getPaths)();
|
|
40
|
+
const allOgComponents = import_fast_glob.default.sync("pages/**/*.og.{jsx,tsx}", {
|
|
41
|
+
cwd: rwPaths.web.src,
|
|
42
|
+
absolute: true,
|
|
43
|
+
// @MARK This allows us to mock the fs module in tests
|
|
44
|
+
fs: import_fs.default
|
|
45
|
+
});
|
|
46
|
+
const ogComponentInput = {};
|
|
47
|
+
allOgComponents.forEach((ogComponentPath) => {
|
|
48
|
+
const pathKey = import_node_path.default.relative(rwPaths.web.src, ogComponentPath).replace(/\.[jt]sx?$/, "");
|
|
49
|
+
ogComponentInput[`ogImage/${(0, import_project_config.ensurePosixPath)(pathKey)}`] = ogComponentPath;
|
|
50
|
+
});
|
|
51
|
+
return {
|
|
52
|
+
name: "rw-vite-plugin-ogimage-gen",
|
|
53
|
+
apply: "build",
|
|
54
|
+
// We only need to update rollup inputs for build
|
|
55
|
+
config: (_config, env) => {
|
|
56
|
+
if (env.isSsrBuild) {
|
|
57
|
+
return {
|
|
58
|
+
build: {
|
|
59
|
+
rollupOptions: {
|
|
60
|
+
input: ogComponentInput
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
} else {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
var vite_plugin_ogimage_gen_default = vitePluginOgImageGen;
|
package/empty.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// FIND ME ROB!
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cedarjs/ogimage-gen",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
7
|
+
"directory": "packages/ogimage-gen"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"type": "commonjs",
|
|
11
|
+
"exports": {
|
|
12
|
+
"./plugin": {
|
|
13
|
+
"types": "./dist/vite-plugin-ogimage-gen.d.ts",
|
|
14
|
+
"import": "./dist/vite-plugin-ogimage-gen.js",
|
|
15
|
+
"default": "./cjsWrappers/plugin.js"
|
|
16
|
+
},
|
|
17
|
+
"./middleware": {
|
|
18
|
+
"types": "./dist/OgImageMiddleware.d.ts",
|
|
19
|
+
"import": "./dist/OgImageMiddleware.js",
|
|
20
|
+
"react-server": "./empty.js",
|
|
21
|
+
"default": "./cjsWrappers/middleware.js"
|
|
22
|
+
},
|
|
23
|
+
"./hooks": {
|
|
24
|
+
"types": "./dist/hooks.d.ts",
|
|
25
|
+
"import": "./dist/hooks.js",
|
|
26
|
+
"default": "./cjsWrappers/hooks.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"cjsWrappers",
|
|
32
|
+
"empty.js"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsx ./build.mts && yarn build:types",
|
|
36
|
+
"build:pack": "yarn pack -o cedar-ogimage-gen.tgz",
|
|
37
|
+
"build:types": "tsc --build --verbose",
|
|
38
|
+
"prepublishOnly": "NODE_ENV=production yarn build",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"test:watch": "vitest watch"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@cedarjs/internal": "0.0.4",
|
|
44
|
+
"@cedarjs/project-config": "0.0.4",
|
|
45
|
+
"@cedarjs/router": "0.0.4",
|
|
46
|
+
"@cedarjs/web": "0.0.4",
|
|
47
|
+
"fast-glob": "3.3.2",
|
|
48
|
+
"lodash": "4.17.21",
|
|
49
|
+
"react": "18.3.1",
|
|
50
|
+
"react-dom": "18.3.1"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@cedarjs/framework-tools": "0.0.4",
|
|
54
|
+
"@playwright/test": "1.49.1",
|
|
55
|
+
"ts-toolbelt": "9.6.0",
|
|
56
|
+
"tsx": "4.19.3",
|
|
57
|
+
"typescript": "5.6.2",
|
|
58
|
+
"vite": "5.4.16",
|
|
59
|
+
"vitest": "2.1.9"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
64
|
+
"gitHead": "5b4f77f985bd86ee31ee7338312627accf0cb85b"
|
|
65
|
+
}
|