starpro-plugin-sdk 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -34
- package/dist/build/index.d.mts +87 -0
- package/dist/build/index.d.ts +87 -0
- package/dist/build/index.js +413 -0
- package/dist/build/index.mjs +380 -0
- package/dist/chunk-HFHUB6W5.mjs +677 -0
- package/dist/chunk-Y6FXYEAI.mjs +10 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +725 -0
- package/dist/index.mjs +39 -0
- package/dist/react-DS5S7U6B.d.mts +280 -0
- package/dist/react-DS5S7U6B.d.ts +280 -0
- package/dist/react.d.mts +2 -0
- package/dist/react.d.ts +2 -0
- package/dist/react.js +717 -0
- package/dist/react.mjs +33 -0
- package/package.json +17 -2
- package/src/bridge.ts +0 -94
- package/src/define-plugin.ts +0 -79
- package/src/index.ts +0 -6
- package/src/react.tsx +0 -94
- package/src/types.ts +0 -66
- package/tsconfig.json +0 -17
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__require
|
|
3
|
+
} from "../chunk-Y6FXYEAI.mjs";
|
|
4
|
+
|
|
5
|
+
// src/build/index.ts
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
function walkDir(dir, fileList = []) {
|
|
9
|
+
if (!fs.existsSync(dir)) return fileList;
|
|
10
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
11
|
+
for (const entry of entries) {
|
|
12
|
+
const fullPath = path.join(dir, entry.name);
|
|
13
|
+
if (entry.isDirectory()) {
|
|
14
|
+
walkDir(fullPath, fileList);
|
|
15
|
+
} else if (entry.isFile()) {
|
|
16
|
+
fileList.push(fullPath);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return fileList;
|
|
20
|
+
}
|
|
21
|
+
function discoverRoutes(appDir, options = {}) {
|
|
22
|
+
const { stripRouteGroups = true } = options;
|
|
23
|
+
const allFiles = walkDir(appDir);
|
|
24
|
+
const pageFiles = [];
|
|
25
|
+
let layoutFile = null;
|
|
26
|
+
for (const fullPath of allFiles) {
|
|
27
|
+
const rel = path.relative(appDir, fullPath).replace(/\\/g, "/");
|
|
28
|
+
if (/^(\([^)]+\)\/)?layout\.(tsx|jsx|js|ts)$/.test(rel) && !layoutFile) {
|
|
29
|
+
layoutFile = fullPath;
|
|
30
|
+
}
|
|
31
|
+
if (!/(^|\/)(page|index)\.(tsx|jsx|js|ts)$/.test(rel)) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
let cleaned = rel.replace(/\/(page|index)\.(tsx|jsx|js|ts)$/, "").replace(/^(page|index)\.(tsx|jsx|js|ts)$/, "");
|
|
35
|
+
if (stripRouteGroups) {
|
|
36
|
+
cleaned = cleaned.split("/").filter((part) => !/^\([^)]+\)$/.test(part)).join("/");
|
|
37
|
+
}
|
|
38
|
+
cleaned = cleaned.replace(/\[\.\.\.([^\]]+)\]/g, "*$1").replace(/\[([^\]]+)\]/g, ":$1");
|
|
39
|
+
let routePath = cleaned ? cleaned.startsWith("/") ? cleaned : `/${cleaned}` : "/";
|
|
40
|
+
let label = void 0;
|
|
41
|
+
const isDynamic = routePath.includes(":") || routePath.includes("*");
|
|
42
|
+
if (!isDynamic && routePath !== "/") {
|
|
43
|
+
const last = routePath.split("/").pop() || "";
|
|
44
|
+
label = last.split(/[-_]+/).map((w) => w.charAt(0).toUpperCase() + w.slice(1)).join(" ");
|
|
45
|
+
if (label) {
|
|
46
|
+
label = label.replace(/\bWater Waste Water\b/gi, "Water & Wastewater");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
pageFiles.push({
|
|
50
|
+
filePath: fullPath,
|
|
51
|
+
routePath,
|
|
52
|
+
label
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
pageFiles.sort((a, b) => {
|
|
56
|
+
if (a.routePath === "/") return -1;
|
|
57
|
+
if (b.routePath === "/") return 1;
|
|
58
|
+
const aDyn = a.routePath.includes(":") || a.routePath.includes("*");
|
|
59
|
+
const bDyn = b.routePath.includes(":") || b.routePath.includes("*");
|
|
60
|
+
if (aDyn && !bDyn) return 1;
|
|
61
|
+
if (!aDyn && bDyn) return -1;
|
|
62
|
+
return a.routePath.localeCompare(b.routePath);
|
|
63
|
+
});
|
|
64
|
+
return { routes: pageFiles, layoutFile };
|
|
65
|
+
}
|
|
66
|
+
function starproEsbuildPlugin(options = {}) {
|
|
67
|
+
const cwd = options.cwd || process.cwd();
|
|
68
|
+
const mockNext = options.mockNext !== false;
|
|
69
|
+
const stripRouteGroups = options.stripRouteGroups !== false;
|
|
70
|
+
let appDir = options.appDir;
|
|
71
|
+
if (!appDir) {
|
|
72
|
+
const candidates = [
|
|
73
|
+
path.join(cwd, "src/app"),
|
|
74
|
+
path.join(cwd, "app"),
|
|
75
|
+
path.join(cwd, "src/pages"),
|
|
76
|
+
path.join(cwd, "pages")
|
|
77
|
+
];
|
|
78
|
+
for (const cand of candidates) {
|
|
79
|
+
if (fs.existsSync(cand)) {
|
|
80
|
+
appDir = cand;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
name: "starpro-plugin-bundler",
|
|
87
|
+
setup(build) {
|
|
88
|
+
build.onResolve({ filter: /^starpro:routes$/ }, (args) => ({
|
|
89
|
+
path: args.path,
|
|
90
|
+
namespace: "starpro-routes-virtual"
|
|
91
|
+
}));
|
|
92
|
+
build.onLoad(
|
|
93
|
+
{ filter: /.*/, namespace: "starpro-routes-virtual" },
|
|
94
|
+
() => {
|
|
95
|
+
if (!appDir || !fs.existsSync(appDir)) {
|
|
96
|
+
return {
|
|
97
|
+
contents: `
|
|
98
|
+
export const routes = [];
|
|
99
|
+
export const layout = undefined;
|
|
100
|
+
export default routes;
|
|
101
|
+
`,
|
|
102
|
+
loader: "ts"
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const { routes, layoutFile } = discoverRoutes(appDir, {
|
|
106
|
+
stripRouteGroups
|
|
107
|
+
});
|
|
108
|
+
const importLines = [];
|
|
109
|
+
const routeObjects = [];
|
|
110
|
+
routes.forEach((r, idx) => {
|
|
111
|
+
const varName = `Page_${idx}`;
|
|
112
|
+
const normalizedPath = r.filePath.replace(/\\/g, "/");
|
|
113
|
+
importLines.push(`import ${varName} from ${JSON.stringify(normalizedPath)};`);
|
|
114
|
+
routeObjects.push(`{
|
|
115
|
+
path: ${JSON.stringify(r.routePath)},
|
|
116
|
+
component: ${varName},
|
|
117
|
+
label: ${r.label ? JSON.stringify(r.label) : "undefined"},
|
|
118
|
+
exact: ${r.routePath === "/"}
|
|
119
|
+
}`);
|
|
120
|
+
});
|
|
121
|
+
let layoutImport = "export const layout = undefined;";
|
|
122
|
+
if (layoutFile) {
|
|
123
|
+
const normLayout = layoutFile.replace(/\\/g, "/");
|
|
124
|
+
importLines.push(`import LayoutComponent from ${JSON.stringify(normLayout)};`);
|
|
125
|
+
layoutImport = "export const layout = LayoutComponent;";
|
|
126
|
+
}
|
|
127
|
+
const defaultRoute = routes.find((r) => r.routePath !== "/" && !r.routePath.includes(":"))?.routePath || (routes.length > 0 ? routes[0].routePath : "/");
|
|
128
|
+
const contents = `
|
|
129
|
+
${importLines.join("\n")}
|
|
130
|
+
|
|
131
|
+
export const routes = [
|
|
132
|
+
${routeObjects.join(",\n")}
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
${layoutImport}
|
|
136
|
+
export const defaultRoute = ${JSON.stringify(defaultRoute)};
|
|
137
|
+
export default routes;
|
|
138
|
+
`;
|
|
139
|
+
return {
|
|
140
|
+
contents,
|
|
141
|
+
loader: "tsx",
|
|
142
|
+
resolveDir: cwd
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
if (mockNext) {
|
|
147
|
+
build.onResolve({ filter: /^next\/navigation$/ }, (args) => ({
|
|
148
|
+
path: args.path,
|
|
149
|
+
namespace: "starpro-mock-nav"
|
|
150
|
+
}));
|
|
151
|
+
build.onLoad(
|
|
152
|
+
{ filter: /.*/, namespace: "starpro-mock-nav" },
|
|
153
|
+
() => ({
|
|
154
|
+
contents: `
|
|
155
|
+
import React from 'react';
|
|
156
|
+
import {
|
|
157
|
+
starproRouter,
|
|
158
|
+
useStarProPathname,
|
|
159
|
+
useStarProSearchParams,
|
|
160
|
+
useStarProParams,
|
|
161
|
+
} from 'starpro-plugin-sdk';
|
|
162
|
+
|
|
163
|
+
export const useRouter = () => ({
|
|
164
|
+
push: (url) => starproRouter.push(url),
|
|
165
|
+
replace: (url) => starproRouter.replace(url),
|
|
166
|
+
back: () => starproRouter.back(),
|
|
167
|
+
forward: () => starproRouter.forward(),
|
|
168
|
+
refresh: () => {},
|
|
169
|
+
prefetch: () => {},
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
export const usePathname = useStarProPathname;
|
|
173
|
+
export const useSearchParams = useStarProSearchParams;
|
|
174
|
+
export const useParams = useStarProParams;
|
|
175
|
+
export const useSelectedLayoutSegment = () => null;
|
|
176
|
+
export const useSelectedLayoutSegments = () => [];
|
|
177
|
+
|
|
178
|
+
export const redirect = (url) => {
|
|
179
|
+
starproRouter.replace(url);
|
|
180
|
+
const err = new Error('NEXT_REDIRECT');
|
|
181
|
+
err.digest = 'NEXT_REDIRECT;replace;' + url + ';307;';
|
|
182
|
+
err.url = url;
|
|
183
|
+
err.isStarProRedirect = true;
|
|
184
|
+
throw err;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export const notFound = () => {};
|
|
188
|
+
export default { useRouter, usePathname, useSearchParams, useParams, redirect, notFound };
|
|
189
|
+
`,
|
|
190
|
+
loader: "tsx",
|
|
191
|
+
resolveDir: cwd
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
build.onResolve({ filter: /^next\/link$/ }, (args) => ({
|
|
195
|
+
path: args.path,
|
|
196
|
+
namespace: "starpro-mock-link"
|
|
197
|
+
}));
|
|
198
|
+
build.onLoad(
|
|
199
|
+
{ filter: /.*/, namespace: "starpro-mock-link" },
|
|
200
|
+
() => ({
|
|
201
|
+
contents: `
|
|
202
|
+
import React, { forwardRef } from 'react';
|
|
203
|
+
import { starproRouter } from 'starpro-plugin-sdk';
|
|
204
|
+
|
|
205
|
+
export const Link = forwardRef((props, ref) => {
|
|
206
|
+
const { href, children, onClick, ...rest } = props;
|
|
207
|
+
const targetHref = typeof href === 'object' && href !== null ? (href.pathname || '#') : (href || '#');
|
|
208
|
+
return React.createElement('a', {
|
|
209
|
+
...rest,
|
|
210
|
+
ref,
|
|
211
|
+
href: targetHref,
|
|
212
|
+
onClick: (e) => {
|
|
213
|
+
if (onClick) onClick(e);
|
|
214
|
+
if (targetHref.startsWith('http://') || targetHref.startsWith('https://')) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
e.preventDefault();
|
|
218
|
+
starproRouter.push(targetHref);
|
|
219
|
+
}
|
|
220
|
+
}, children);
|
|
221
|
+
});
|
|
222
|
+
export default Link;
|
|
223
|
+
`,
|
|
224
|
+
loader: "tsx",
|
|
225
|
+
resolveDir: cwd
|
|
226
|
+
})
|
|
227
|
+
);
|
|
228
|
+
build.onResolve({ filter: /^next\/image$/ }, (args) => ({
|
|
229
|
+
path: args.path,
|
|
230
|
+
namespace: "starpro-mock-image"
|
|
231
|
+
}));
|
|
232
|
+
build.onLoad(
|
|
233
|
+
{ filter: /.*/, namespace: "starpro-mock-image" },
|
|
234
|
+
() => ({
|
|
235
|
+
contents: `
|
|
236
|
+
import React from 'react';
|
|
237
|
+
export default function Image(props) {
|
|
238
|
+
const { src, alt, ...rest } = props;
|
|
239
|
+
const targetSrc = typeof src === 'object' && src !== null ? (src.src || '') : src;
|
|
240
|
+
return React.createElement('img', { ...rest, src: targetSrc, alt: alt || '' });
|
|
241
|
+
}
|
|
242
|
+
`,
|
|
243
|
+
loader: "tsx",
|
|
244
|
+
resolveDir: cwd
|
|
245
|
+
})
|
|
246
|
+
);
|
|
247
|
+
build.onResolve({ filter: /^next\/router$/ }, (args) => ({
|
|
248
|
+
path: args.path,
|
|
249
|
+
namespace: "starpro-mock-pages-router"
|
|
250
|
+
}));
|
|
251
|
+
build.onLoad(
|
|
252
|
+
{ filter: /.*/, namespace: "starpro-mock-pages-router" },
|
|
253
|
+
() => ({
|
|
254
|
+
contents: `
|
|
255
|
+
import { starproRouter } from 'starpro-plugin-sdk';
|
|
256
|
+
export const useRouter = () => ({
|
|
257
|
+
push: (url) => starproRouter.push(url),
|
|
258
|
+
replace: (url) => starproRouter.replace(url),
|
|
259
|
+
pathname: starproRouter.getState().pathname,
|
|
260
|
+
query: Object.fromEntries(starproRouter.getState().searchParams.entries()),
|
|
261
|
+
asPath: starproRouter.getState().url,
|
|
262
|
+
});
|
|
263
|
+
export default { useRouter };
|
|
264
|
+
`,
|
|
265
|
+
loader: "ts",
|
|
266
|
+
resolveDir: cwd
|
|
267
|
+
})
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function loadProjectEnv(cwd, mode) {
|
|
274
|
+
const envFiles = [
|
|
275
|
+
`.env.${mode}.local`,
|
|
276
|
+
`.env.local`,
|
|
277
|
+
`.env.${mode}`,
|
|
278
|
+
`.env`
|
|
279
|
+
];
|
|
280
|
+
const envMap = {};
|
|
281
|
+
for (const file of envFiles) {
|
|
282
|
+
const fullPath = path.resolve(cwd, file);
|
|
283
|
+
if (fs.existsSync(fullPath)) {
|
|
284
|
+
try {
|
|
285
|
+
const lines = fs.readFileSync(fullPath, "utf-8").split("\n");
|
|
286
|
+
for (const line of lines) {
|
|
287
|
+
const trimmed = line.trim();
|
|
288
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
289
|
+
const idx = trimmed.indexOf("=");
|
|
290
|
+
if (idx !== -1) {
|
|
291
|
+
const key = trimmed.slice(0, idx).trim();
|
|
292
|
+
let val = trimmed.slice(idx + 1).trim();
|
|
293
|
+
if (val.startsWith('"') && val.endsWith('"') || val.startsWith("'") && val.endsWith("'")) {
|
|
294
|
+
val = val.slice(1, -1);
|
|
295
|
+
}
|
|
296
|
+
if (!(key in envMap)) {
|
|
297
|
+
envMap[key] = val;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
} catch (e) {
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return envMap;
|
|
306
|
+
}
|
|
307
|
+
async function buildStarProPlugin(options = {}) {
|
|
308
|
+
const cwd = options.cwd || process.cwd();
|
|
309
|
+
const envMode = options.env || process.env.NODE_ENV || "production";
|
|
310
|
+
const entry = options.entry || (fs.existsSync(path.join(cwd, "src/plugin-entry.tsx")) ? "src/plugin-entry.tsx" : "src/plugin-entry.ts");
|
|
311
|
+
const outFile = options.out || "public/plugin.js";
|
|
312
|
+
const minify = options.minify !== false;
|
|
313
|
+
let esbuild;
|
|
314
|
+
try {
|
|
315
|
+
esbuild = __require("esbuild");
|
|
316
|
+
} catch (e) {
|
|
317
|
+
console.error(
|
|
318
|
+
"\u274C [StarPro Build] 'esbuild' is required. Please install it with: npm install -D esbuild"
|
|
319
|
+
);
|
|
320
|
+
throw e;
|
|
321
|
+
}
|
|
322
|
+
const envVars = loadProjectEnv(cwd, envMode);
|
|
323
|
+
const apiUrl = process.env.NEXT_PUBLIC_API_URL || envVars["NEXT_PUBLIC_API_URL"] || "";
|
|
324
|
+
console.log(
|
|
325
|
+
`\u{1F4E6} [StarPro Plugin SDK] Building ${entry} -> ${outFile} (mode: ${envMode})`
|
|
326
|
+
);
|
|
327
|
+
const defineRecord = {
|
|
328
|
+
"process.env.NODE_ENV": JSON.stringify(envMode),
|
|
329
|
+
"process.env.NEXT_PUBLIC_API_URL": JSON.stringify(apiUrl),
|
|
330
|
+
...options.define
|
|
331
|
+
};
|
|
332
|
+
for (const [k, v] of Object.entries(envVars)) {
|
|
333
|
+
if (k.startsWith("NEXT_PUBLIC_") && !(`process.env.${k}` in defineRecord)) {
|
|
334
|
+
defineRecord[`process.env.${k}`] = JSON.stringify(v);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const targetNodeModules = path.resolve(cwd, "node_modules");
|
|
338
|
+
const alias = {};
|
|
339
|
+
if (fs.existsSync(path.join(targetNodeModules, "react"))) {
|
|
340
|
+
alias["react"] = path.join(targetNodeModules, "react");
|
|
341
|
+
}
|
|
342
|
+
if (fs.existsSync(path.join(targetNodeModules, "react-dom"))) {
|
|
343
|
+
alias["react-dom"] = path.join(targetNodeModules, "react-dom");
|
|
344
|
+
}
|
|
345
|
+
if (fs.existsSync(path.join(targetNodeModules, "react/jsx-runtime"))) {
|
|
346
|
+
alias["react/jsx-runtime"] = path.join(targetNodeModules, "react/jsx-runtime");
|
|
347
|
+
}
|
|
348
|
+
const plugins = [
|
|
349
|
+
starproEsbuildPlugin({
|
|
350
|
+
appDir: options.appDir,
|
|
351
|
+
cwd,
|
|
352
|
+
mockNext: options.mockNext,
|
|
353
|
+
stripRouteGroups: options.stripRouteGroups
|
|
354
|
+
}),
|
|
355
|
+
...options.plugins || []
|
|
356
|
+
];
|
|
357
|
+
try {
|
|
358
|
+
const result = await esbuild.build({
|
|
359
|
+
entryPoints: [entry],
|
|
360
|
+
bundle: true,
|
|
361
|
+
minify,
|
|
362
|
+
format: "iife",
|
|
363
|
+
jsx: "automatic",
|
|
364
|
+
outfile: outFile,
|
|
365
|
+
alias,
|
|
366
|
+
define: defineRecord,
|
|
367
|
+
plugins
|
|
368
|
+
});
|
|
369
|
+
console.log(`\u2705 [StarPro Plugin SDK] Successfully built ${outFile}!`);
|
|
370
|
+
return result;
|
|
371
|
+
} catch (err) {
|
|
372
|
+
console.error("\u274C [StarPro Plugin SDK] Build failed:", err);
|
|
373
|
+
throw err;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
export {
|
|
377
|
+
buildStarProPlugin,
|
|
378
|
+
discoverRoutes,
|
|
379
|
+
starproEsbuildPlugin
|
|
380
|
+
};
|