resuxjs 0.2.26 → 0.2.27
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 +99 -3
- package/dist/cli.js +65 -13
- package/dist/cli.js.map +1 -1
- package/dist/compiler/index.d.ts +5 -2
- package/dist/compiler/index.js +125 -54
- package/dist/compiler/index.js.map +1 -1
- package/dist/runtime/index.d.ts +30 -2
- package/dist/runtime/index.js +267 -35
- package/dist/runtime/index.js.map +1 -1
- package/package.json +1 -1
- package/templates/default/README.md +1 -1
- package/templates/default/middleware/auth.ts +5 -0
- package/templates/default/middleware/log.global.ts +3 -0
- package/templates/default/nitro.config.ts +10 -0
- package/templates/default/pages/index.vue +6 -0
- package/templates/default/plugins/01.app.ts +3 -0
package/dist/compiler/index.js
CHANGED
|
@@ -81,15 +81,19 @@ export async function buildProject(appRoot, outDir = path.join(appRoot, ".resux"
|
|
|
81
81
|
};
|
|
82
82
|
await cleanGeneratedOutput(absoluteOut);
|
|
83
83
|
await mkdir(path.join(absoluteOut, "server"), { recursive: true });
|
|
84
|
-
await mkdir(path.join(absoluteOut, "server", "plugins"), { recursive: true });
|
|
85
|
-
await mkdir(path.join(absoluteOut, "server", "middleware"), { recursive: true });
|
|
84
|
+
await mkdir(path.join(absoluteOut, "server", "resux-plugins"), { recursive: true });
|
|
85
|
+
await mkdir(path.join(absoluteOut, "server", "resux-middleware"), { recursive: true });
|
|
86
86
|
await mkdir(path.join(absoluteOut, "server", "request-middleware"), { recursive: true });
|
|
87
87
|
await mkdir(path.join(absoluteOut, "server", "handlers"), { recursive: true });
|
|
88
88
|
await mkdir(path.join(absoluteOut, "server", "config-modules"), { recursive: true });
|
|
89
89
|
await mkdir(path.join(absoluteOut, "client", "handlers"), { recursive: true });
|
|
90
90
|
await mkdir(path.join(absoluteOut, "client", "vue-islands"), { recursive: true });
|
|
91
|
+
await mkdir(path.join(absoluteOut, "client", "plugins"), { recursive: true });
|
|
92
|
+
await mkdir(path.join(absoluteOut, "client", "middleware"), { recursive: true });
|
|
91
93
|
await mkdir(path.join(absoluteOut, "vite-client", "handlers"), { recursive: true });
|
|
92
94
|
await mkdir(path.join(absoluteOut, "vite-client", "vue-islands"), { recursive: true });
|
|
95
|
+
await mkdir(path.join(absoluteOut, "vite-client", "plugins"), { recursive: true });
|
|
96
|
+
await mkdir(path.join(absoluteOut, "vite-client", "middleware"), { recursive: true });
|
|
93
97
|
const runtimeConfig = await readRuntimeConfig(absoluteRoot, absoluteOut);
|
|
94
98
|
const routeRules = createRouteRules(runtimeConfig);
|
|
95
99
|
const componentFiles = await discoverAppVueFiles(absoluteRoot, "components");
|
|
@@ -99,9 +103,9 @@ export async function buildProject(appRoot, outDir = path.join(appRoot, ".resux"
|
|
|
99
103
|
?? await optionalFile(path.join(absoluteRoot, "app", "app.vue"));
|
|
100
104
|
const errorFile = await optionalFile(path.join(absoluteRoot, "error.vue"))
|
|
101
105
|
?? await optionalFile(path.join(absoluteRoot, "app", "error.vue"));
|
|
102
|
-
const pluginFiles = await
|
|
103
|
-
const middlewareFiles = await
|
|
104
|
-
const serverMiddlewareFiles = await
|
|
106
|
+
const pluginFiles = await discoverSupportTsFiles(absoluteRoot, "plugins");
|
|
107
|
+
const middlewareFiles = await discoverSupportTsFiles(absoluteRoot, "middleware");
|
|
108
|
+
const serverMiddlewareFiles = await discoverSupportTsFiles(absoluteRoot, path.join("server", "middleware"));
|
|
105
109
|
const serverHandlerFiles = [
|
|
106
110
|
...await discoverServerFiles(path.join(absoluteRoot, "server", "api"), "/api"),
|
|
107
111
|
...await discoverServerFiles(path.join(absoluteRoot, "server", "routes"), "")
|
|
@@ -133,10 +137,20 @@ export async function buildProject(appRoot, outDir = path.join(appRoot, ".resux"
|
|
|
133
137
|
const app = appFile ? compiledByFile.get(appFile) : undefined;
|
|
134
138
|
const error = errorFile ? compiledByFile.get(errorFile) : undefined;
|
|
135
139
|
for (const plugin of plugins) {
|
|
136
|
-
|
|
140
|
+
if (plugin.mode !== "client") {
|
|
141
|
+
await writeFile(path.join(absoluteOut, "server", "resux-plugins", `${plugin.id}.mjs`), plugin.serverSource, "utf8");
|
|
142
|
+
}
|
|
143
|
+
if (plugin.mode !== "server") {
|
|
144
|
+
await writeFile(path.join(absoluteOut, "vite-client", "plugins", `${plugin.id}.mjs`), plugin.clientSource, "utf8");
|
|
145
|
+
}
|
|
137
146
|
}
|
|
138
147
|
for (const entry of middleware) {
|
|
139
|
-
|
|
148
|
+
if (entry.mode !== "client") {
|
|
149
|
+
await writeFile(path.join(absoluteOut, "server", "resux-middleware", `${entry.id}.mjs`), entry.serverSource, "utf8");
|
|
150
|
+
}
|
|
151
|
+
if (entry.mode !== "server") {
|
|
152
|
+
await writeFile(path.join(absoluteOut, "vite-client", "middleware", `${entry.id}.mjs`), entry.clientSource, "utf8");
|
|
153
|
+
}
|
|
140
154
|
}
|
|
141
155
|
for (const entry of serverMiddleware) {
|
|
142
156
|
await writeFile(path.join(absoluteOut, "server", "request-middleware", `${entry.id}.mjs`), entry.source, "utf8");
|
|
@@ -152,7 +166,7 @@ export async function buildProject(appRoot, outDir = path.join(appRoot, ".resux"
|
|
|
152
166
|
await writeViteClientRuntime(absoluteOut);
|
|
153
167
|
}
|
|
154
168
|
else {
|
|
155
|
-
await buildClientAssets(absoluteRoot, absoluteOut, components, vueIslands);
|
|
169
|
+
await buildClientAssets(absoluteRoot, absoluteOut, components, plugins, middleware, vueIslands);
|
|
156
170
|
}
|
|
157
171
|
await writeFile(path.join(absoluteOut, "server", "manifest.mjs"), createServerManifestSource(routes, components, layouts, plugins, middleware, serverMiddleware, serverHandlers, vueIslands, app, error, runtimeConfig, buildOptions), "utf8");
|
|
158
172
|
if (buildOptions.server === "bundle") {
|
|
@@ -163,7 +177,7 @@ export async function buildProject(appRoot, outDir = path.join(appRoot, ".resux"
|
|
|
163
177
|
components: components.map(({ id, name, file, handlers, styles, styleScopeId, meta }) => ({ id, name, file, handlers, styles, styleScopeId, meta })),
|
|
164
178
|
layouts: layouts.map(({ id, name, file, styles, styleScopeId }) => ({ id, name, file, styles, styleScopeId })),
|
|
165
179
|
plugins: plugins.map(({ id, file, mode }) => ({ id, file, mode })),
|
|
166
|
-
middleware: middleware.map(({ id, name, file, global }) => ({ id, name, file, global })),
|
|
180
|
+
middleware: middleware.map(({ id, name, file, global, mode }) => ({ id, name, file, global, mode })),
|
|
167
181
|
serverMiddleware: serverMiddleware.map(({ id, file }) => ({ id, file })),
|
|
168
182
|
serverHandlers: serverHandlers.map(({ id, path: routePath, file, params }) => ({ id, path: routePath, file, params })),
|
|
169
183
|
vueIslands,
|
|
@@ -229,7 +243,7 @@ async function buildServerBundle(root, outDir) {
|
|
|
229
243
|
throw new Error(`Vite server build failed. Ensure vite is installed. ${message}`);
|
|
230
244
|
}
|
|
231
245
|
}
|
|
232
|
-
async function buildClientAssets(root, outDir, components, vueIslands) {
|
|
246
|
+
async function buildClientAssets(root, outDir, components, plugins, middleware, vueIslands) {
|
|
233
247
|
const inputRoot = path.join(outDir, "vite-client");
|
|
234
248
|
const runtimeInput = await writeViteClientRuntime(outDir);
|
|
235
249
|
const input = {
|
|
@@ -238,6 +252,12 @@ async function buildClientAssets(root, outDir, components, vueIslands) {
|
|
|
238
252
|
for (const component of components) {
|
|
239
253
|
input[`handlers/${component.id}`] = path.join(inputRoot, "handlers", `${component.id}.mjs`);
|
|
240
254
|
}
|
|
255
|
+
for (const plugin of plugins.filter((entry) => entry.mode !== "server")) {
|
|
256
|
+
input[`plugins/${plugin.id}`] = path.join(inputRoot, "plugins", `${plugin.id}.mjs`);
|
|
257
|
+
}
|
|
258
|
+
for (const entry of middleware.filter((candidate) => candidate.mode !== "server")) {
|
|
259
|
+
input[`middleware/${entry.id}`] = path.join(inputRoot, "middleware", `${entry.id}.mjs`);
|
|
260
|
+
}
|
|
241
261
|
for (const island of vueIslands) {
|
|
242
262
|
input[`vue-islands/${island.name}`] = path.join(inputRoot, "vue-islands", `${island.name}.mjs`);
|
|
243
263
|
}
|
|
@@ -1090,9 +1110,13 @@ function relativeImportPath(fromDir, file) {
|
|
|
1090
1110
|
}
|
|
1091
1111
|
function createServerManifestSource(routes, components, layouts, plugins, middleware, serverMiddleware, serverHandlers, vueIslands, app, error, runtimeConfig = {}, buildOptions = {}) {
|
|
1092
1112
|
const importSuffix = buildOptions.vite === "dev" ? `?t=${Date.now()}` : "";
|
|
1113
|
+
const serverPlugins = plugins.filter((entry) => entry.mode !== "client");
|
|
1114
|
+
const clientPlugins = plugins.filter((entry) => entry.mode !== "server");
|
|
1115
|
+
const serverRouteMiddleware = middleware.filter((entry) => entry.mode !== "client");
|
|
1116
|
+
const clientRouteMiddleware = middleware.filter((entry) => entry.mode !== "server");
|
|
1093
1117
|
const imports = components.map((component) => `import ${component.id} from "./${component.id}.mjs${importSuffix}";`).join("\n");
|
|
1094
|
-
const pluginImports =
|
|
1095
|
-
const middlewareImports =
|
|
1118
|
+
const pluginImports = serverPlugins.map((plugin) => `import ${plugin.id} from "./resux-plugins/${plugin.id}.mjs${importSuffix}";`).join("\n");
|
|
1119
|
+
const middlewareImports = serverRouteMiddleware.map((entry) => `import ${entry.id} from "./resux-middleware/${entry.id}.mjs${importSuffix}";`).join("\n");
|
|
1096
1120
|
const serverMiddlewareImports = serverMiddleware.map((entry) => `import ${entry.id} from "./request-middleware/${entry.id}.mjs${importSuffix}";`).join("\n");
|
|
1097
1121
|
const handlerImports = serverHandlers.map((handler) => `import ${handler.id} from "./handlers/${handler.id}.mjs${importSuffix}";`).join("\n");
|
|
1098
1122
|
const componentEntries = components.map((component) => `${JSON.stringify(component.name)}: ${component.id}`).join(",\n");
|
|
@@ -1103,17 +1127,35 @@ function createServerManifestSource(routes, components, layouts, plugins, middle
|
|
|
1103
1127
|
const vueIslandEntries = vueIslands
|
|
1104
1128
|
.map((island) => `${JSON.stringify(island.name)}: ${JSON.stringify(`/__resux/vue-islands/${island.name}.mjs`)}`)
|
|
1105
1129
|
.join(",\n");
|
|
1106
|
-
const pluginEntries =
|
|
1107
|
-
.filter((plugin) => plugin.mode !== "client")
|
|
1130
|
+
const pluginEntries = serverPlugins
|
|
1108
1131
|
.map((plugin) => plugin.id)
|
|
1109
1132
|
.join(",\n");
|
|
1110
|
-
const
|
|
1111
|
-
.map((
|
|
1112
|
-
id: ${JSON.stringify(
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1133
|
+
const clientPluginEntries = clientPlugins
|
|
1134
|
+
.map((plugin) => `{
|
|
1135
|
+
id: ${JSON.stringify(plugin.id)},
|
|
1136
|
+
file: ${JSON.stringify(normalizePath(plugin.file))},
|
|
1137
|
+
mode: ${JSON.stringify(plugin.mode)},
|
|
1138
|
+
src: ${JSON.stringify(`/__resux/plugins/${plugin.id}.mjs`)}
|
|
1139
|
+
}`)
|
|
1140
|
+
.join(",\n");
|
|
1141
|
+
const middlewareEntries = serverRouteMiddleware
|
|
1142
|
+
.map((entry) => `{
|
|
1143
|
+
id: ${JSON.stringify(entry.id)},
|
|
1144
|
+
name: ${JSON.stringify(entry.name)},
|
|
1145
|
+
file: ${JSON.stringify(normalizePath(entry.file))},
|
|
1146
|
+
global: ${JSON.stringify(entry.global)},
|
|
1147
|
+
mode: ${JSON.stringify(entry.mode)},
|
|
1148
|
+
handler: ${entry.id}
|
|
1149
|
+
}`)
|
|
1150
|
+
.join(",\n");
|
|
1151
|
+
const clientMiddlewareEntries = clientRouteMiddleware
|
|
1152
|
+
.map((entry) => `{
|
|
1153
|
+
id: ${JSON.stringify(entry.id)},
|
|
1154
|
+
name: ${JSON.stringify(entry.name)},
|
|
1155
|
+
file: ${JSON.stringify(normalizePath(entry.file))},
|
|
1156
|
+
global: ${JSON.stringify(entry.global)},
|
|
1157
|
+
mode: ${JSON.stringify(entry.mode)},
|
|
1158
|
+
src: ${JSON.stringify(`/__resux/middleware/${entry.id}.mjs`)}
|
|
1117
1159
|
}`)
|
|
1118
1160
|
.join(",\n");
|
|
1119
1161
|
const serverMiddlewareEntries = serverMiddleware
|
|
@@ -1175,12 +1217,18 @@ ${vueIslandEntries}
|
|
|
1175
1217
|
export const resuxPlugins = [
|
|
1176
1218
|
${pluginEntries}
|
|
1177
1219
|
];
|
|
1178
|
-
export const
|
|
1179
|
-
${
|
|
1180
|
-
];
|
|
1181
|
-
export const
|
|
1182
|
-
${
|
|
1183
|
-
];
|
|
1220
|
+
export const clientPlugins = [
|
|
1221
|
+
${clientPluginEntries}
|
|
1222
|
+
];
|
|
1223
|
+
export const middleware = [
|
|
1224
|
+
${middlewareEntries}
|
|
1225
|
+
];
|
|
1226
|
+
export const clientMiddleware = [
|
|
1227
|
+
${clientMiddlewareEntries}
|
|
1228
|
+
];
|
|
1229
|
+
export const serverMiddleware = [
|
|
1230
|
+
${serverMiddlewareEntries}
|
|
1231
|
+
];
|
|
1184
1232
|
export const serverHandlers = [
|
|
1185
1233
|
${serverHandlerEntries}
|
|
1186
1234
|
];
|
|
@@ -1338,24 +1386,41 @@ function isRedirectObject(value) {
|
|
|
1338
1386
|
function layoutNameFromFile(file) {
|
|
1339
1387
|
return kebabCase(path.basename(file, ".vue"));
|
|
1340
1388
|
}
|
|
1389
|
+
function supportFileMode(file) {
|
|
1390
|
+
if (file.includes(".client.")) {
|
|
1391
|
+
return "client";
|
|
1392
|
+
}
|
|
1393
|
+
if (file.includes(".server.")) {
|
|
1394
|
+
return "server";
|
|
1395
|
+
}
|
|
1396
|
+
return "all";
|
|
1397
|
+
}
|
|
1398
|
+
function supportFileBaseName(file) {
|
|
1399
|
+
const extensionStripped = path.basename(file).replace(/\.[cm]?[tj]s$/, "");
|
|
1400
|
+
return extensionStripped.replace(/(?:\.global|\.client|\.server)+$/, "");
|
|
1401
|
+
}
|
|
1341
1402
|
function compilePluginFile(file, index) {
|
|
1342
|
-
const mode =
|
|
1403
|
+
const mode = supportFileMode(file);
|
|
1343
1404
|
return {
|
|
1344
1405
|
id: `p${index}`,
|
|
1345
1406
|
file,
|
|
1346
1407
|
mode,
|
|
1347
|
-
|
|
1408
|
+
serverSource: transpileSupportModule(file, `import { defineResuxPlugin } from "resuxjs/runtime";\n`),
|
|
1409
|
+
clientSource: transpileSupportModule(file, `import { defineResuxPlugin } from "/__resux/runtime-client.mjs";\n`)
|
|
1348
1410
|
};
|
|
1349
1411
|
}
|
|
1350
1412
|
function compileMiddlewareFile(file, index) {
|
|
1351
|
-
const baseName =
|
|
1352
|
-
const
|
|
1413
|
+
const baseName = supportFileBaseName(file);
|
|
1414
|
+
const mode = supportFileMode(file);
|
|
1415
|
+
const global = /\.global(?:\.(?:client|server))?\.[cm]?[tj]s$/.test(file);
|
|
1353
1416
|
return {
|
|
1354
1417
|
id: `w${index}`,
|
|
1355
1418
|
name: kebabCase(baseName),
|
|
1356
1419
|
file,
|
|
1420
|
+
mode,
|
|
1357
1421
|
global,
|
|
1358
|
-
|
|
1422
|
+
serverSource: transpileSupportModule(file, `import { defineResuxRouteMiddleware, navigateTo, abortNavigation } from "resuxjs/runtime";\n`),
|
|
1423
|
+
clientSource: transpileSupportModule(file, `import { defineResuxRouteMiddleware, defineClientRouteRedirect as navigateTo, defineClientRouteAbort as abortNavigation } from "/__resux/runtime-client.mjs";\n`)
|
|
1359
1424
|
};
|
|
1360
1425
|
}
|
|
1361
1426
|
function compileServerMiddlewareFile(file, index) {
|
|
@@ -1672,34 +1737,40 @@ async function discoverAppVueFiles(root, dirName) {
|
|
|
1672
1737
|
...await discoverVueFiles(path.join(root, "app", dirName))
|
|
1673
1738
|
]);
|
|
1674
1739
|
}
|
|
1675
|
-
async function
|
|
1740
|
+
async function discoverSupportTsFiles(root, dirName) {
|
|
1676
1741
|
const dirs = [path.join(root, dirName), path.join(root, "app", dirName)];
|
|
1677
1742
|
const files = [];
|
|
1678
1743
|
for (const dir of dirs) {
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
if (indexFile) {
|
|
1693
|
-
files.push(indexFile);
|
|
1694
|
-
}
|
|
1695
|
-
}
|
|
1744
|
+
files.push(...await discoverSupportTsFilesInDir(dir));
|
|
1745
|
+
}
|
|
1746
|
+
return unique(files).sort((left, right) => normalizePath(left).localeCompare(normalizePath(right)));
|
|
1747
|
+
}
|
|
1748
|
+
async function discoverSupportTsFilesInDir(dir) {
|
|
1749
|
+
try {
|
|
1750
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
1751
|
+
const files = [];
|
|
1752
|
+
for (const entry of entries) {
|
|
1753
|
+
const fullPath = path.join(dir, entry.name);
|
|
1754
|
+
if (entry.isDirectory()) {
|
|
1755
|
+
files.push(...await discoverSupportTsFilesInDir(fullPath));
|
|
1756
|
+
continue;
|
|
1696
1757
|
}
|
|
1758
|
+
if (!entry.isFile()) {
|
|
1759
|
+
continue;
|
|
1760
|
+
}
|
|
1761
|
+
if (!/\.[cm]?[tj]s$/.test(entry.name)) {
|
|
1762
|
+
continue;
|
|
1763
|
+
}
|
|
1764
|
+
if (/\.d\.[cm]?[tj]s$/.test(entry.name)) {
|
|
1765
|
+
continue;
|
|
1766
|
+
}
|
|
1767
|
+
files.push(fullPath);
|
|
1697
1768
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1769
|
+
return files;
|
|
1770
|
+
}
|
|
1771
|
+
catch {
|
|
1772
|
+
return [];
|
|
1701
1773
|
}
|
|
1702
|
-
return unique(files).sort();
|
|
1703
1774
|
}
|
|
1704
1775
|
async function discoverServerFiles(dir, routePrefix) {
|
|
1705
1776
|
try {
|