@hybridly/vite 0.0.1-alpha.13 → 0.0.1-alpha.15
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/dist/index.cjs +50 -42
- package/dist/index.mjs +50 -42
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -16,14 +16,15 @@ const path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
|
16
16
|
const makeDebugger__default = /*#__PURE__*/_interopDefaultLegacy(makeDebugger);
|
|
17
17
|
const fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
18
18
|
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
19
|
+
const ROUTING_PLUGIN_NAME = "vite:hybridly:routing";
|
|
20
|
+
const ROUTING_HMR_UPDATE_ROUTING = "hybridly:routing:update";
|
|
21
|
+
const ROUTING_HMR_QUERY_UPDATE_ROUTING = "hybridly:routing:pls-update";
|
|
22
|
+
const ROUTING_VIRTUAL_MODULE_ID = "virtual:hybridly/router";
|
|
23
|
+
const RESOLVED_ROUTING_VIRTUAL_MODULE_ID = `\0${ROUTING_VIRTUAL_MODULE_ID}`;
|
|
23
24
|
const LAYOUT_PLUGIN_NAME = "vite:hybridly:layout";
|
|
24
25
|
|
|
25
26
|
const debug = {
|
|
26
|
-
router: makeDebugger__default(
|
|
27
|
+
router: makeDebugger__default(ROUTING_PLUGIN_NAME),
|
|
27
28
|
layout: makeDebugger__default(LAYOUT_PLUGIN_NAME)
|
|
28
29
|
};
|
|
29
30
|
|
|
@@ -64,7 +65,8 @@ const layout = (options = {}) => {
|
|
|
64
65
|
`, "").trim();
|
|
65
66
|
debug.layout(`Resolved layouts "${layouts.join(", ")}":`, {
|
|
66
67
|
sourceFile: id,
|
|
67
|
-
layouts
|
|
68
|
+
layouts,
|
|
69
|
+
imports
|
|
68
70
|
});
|
|
69
71
|
return `
|
|
70
72
|
<script${isTypeScript ? ' lang="ts"' : ""}>
|
|
@@ -78,45 +80,45 @@ const layout = (options = {}) => {
|
|
|
78
80
|
};
|
|
79
81
|
};
|
|
80
82
|
|
|
81
|
-
function getClientCode(
|
|
83
|
+
function getClientCode(routing) {
|
|
82
84
|
return `
|
|
83
85
|
if (typeof window !== 'undefined') {
|
|
84
86
|
window.hybridly = {
|
|
85
|
-
|
|
87
|
+
routing: ${JSON.stringify(routing)}
|
|
86
88
|
}
|
|
87
|
-
|
|
89
|
+
|
|
88
90
|
if (import.meta.hot) {
|
|
89
|
-
import.meta.hot.on('${
|
|
90
|
-
window.dispatchEvent(
|
|
91
|
-
new CustomEvent('hybridly:routes', { detail: routes })
|
|
92
|
-
)
|
|
91
|
+
import.meta.hot.on('${ROUTING_HMR_UPDATE_ROUTING}', (routing) => {
|
|
92
|
+
window.dispatchEvent(new CustomEvent('hybridly:routing', { detail: routing }))
|
|
93
93
|
})
|
|
94
|
+
|
|
95
|
+
import.meta.hot.send('${ROUTING_HMR_QUERY_UPDATE_ROUTING}')
|
|
94
96
|
}
|
|
95
97
|
}
|
|
96
98
|
`;
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
const shell = node_util.promisify(node_child_process.exec);
|
|
100
|
-
async function
|
|
102
|
+
async function fetchRoutingFromArtisan(options) {
|
|
101
103
|
try {
|
|
102
104
|
const php = options.php ?? "php";
|
|
103
105
|
const result = await shell(`${php} artisan hybridly:routes`);
|
|
104
|
-
const
|
|
105
|
-
write(options,
|
|
106
|
-
return
|
|
106
|
+
const routing = JSON.parse(result.stdout);
|
|
107
|
+
write(options, routing);
|
|
108
|
+
return routing;
|
|
107
109
|
} catch {
|
|
108
110
|
}
|
|
109
111
|
}
|
|
110
112
|
|
|
111
113
|
const write = throttleDebounce.debounce(1e3, writeDefinitions, { atBegin: true });
|
|
112
|
-
async function writeDefinitions(options,
|
|
113
|
-
|
|
114
|
-
if (options.dts === false || !
|
|
114
|
+
async function writeDefinitions(options, routing) {
|
|
115
|
+
routing ?? (routing = await fetchRoutingFromArtisan(options));
|
|
116
|
+
if (options.dts === false || !routing) {
|
|
115
117
|
return;
|
|
116
118
|
}
|
|
117
|
-
debug.router("Writing types for
|
|
119
|
+
debug.router("Writing types for routing:", routing);
|
|
118
120
|
const target = path__default.resolve(options.dts ?? "resources/types/routes.d.ts");
|
|
119
|
-
const routes = Object.fromEntries(Object.entries(
|
|
121
|
+
const routes = Object.fromEntries(Object.entries(routing.routes).map(([key, route]) => {
|
|
120
122
|
const bindings = route.bindings ? Object.fromEntries(Object.entries(route.bindings).map(([key2]) => [key2, "__key_placeholder__"])) : void 0;
|
|
121
123
|
return [key, {
|
|
122
124
|
...route.uri ? { uri: route.uri } : {},
|
|
@@ -125,7 +127,7 @@ async function writeDefinitions(options, collection) {
|
|
|
125
127
|
...route.bindings ? { bindings } : {}
|
|
126
128
|
}];
|
|
127
129
|
}));
|
|
128
|
-
const definitions = generateDefinitions().replace("__URL__",
|
|
130
|
+
const definitions = generateDefinitions().replace("__URL__", routing?.url ?? "").replace("__ROUTES__", JSON.stringify(routes).replaceAll('"__key_placeholder__"', "any"));
|
|
129
131
|
fs__default.mkdirSync(path__default.dirname(target), { recursive: true });
|
|
130
132
|
fs__default.writeFileSync(target, definitions, { encoding: "utf-8" });
|
|
131
133
|
}
|
|
@@ -135,7 +137,7 @@ function generateDefinitions() {
|
|
|
135
137
|
// Modifications will be discarded
|
|
136
138
|
// It is recommended to add it in your .gitignore
|
|
137
139
|
|
|
138
|
-
declare module 'hybridly
|
|
140
|
+
declare module 'hybridly' {
|
|
139
141
|
export interface GlobalRouteCollection {
|
|
140
142
|
url: '__URL__'
|
|
141
143
|
routes: __ROUTES__
|
|
@@ -154,37 +156,43 @@ const router = (options = {}) => {
|
|
|
154
156
|
],
|
|
155
157
|
...options
|
|
156
158
|
};
|
|
157
|
-
let
|
|
159
|
+
let routingBeforeUpdate;
|
|
160
|
+
async function sendRoutingUpdate(server, force = false) {
|
|
161
|
+
const routing = await fetchRoutingFromArtisan(resolved) ?? routingBeforeUpdate;
|
|
162
|
+
if (force || JSON.stringify(routing) !== JSON.stringify(routingBeforeUpdate)) {
|
|
163
|
+
debug.router("Updating routes via HMR:", routing);
|
|
164
|
+
server.ws.send({
|
|
165
|
+
type: "custom",
|
|
166
|
+
event: ROUTING_HMR_UPDATE_ROUTING,
|
|
167
|
+
data: routing
|
|
168
|
+
});
|
|
169
|
+
write(resolved);
|
|
170
|
+
routingBeforeUpdate = routing;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
158
173
|
return {
|
|
159
|
-
name:
|
|
174
|
+
name: ROUTING_PLUGIN_NAME,
|
|
160
175
|
configureServer(server) {
|
|
161
176
|
write(resolved);
|
|
177
|
+
server.ws.on(ROUTING_HMR_QUERY_UPDATE_ROUTING, () => {
|
|
178
|
+
sendRoutingUpdate(server, true);
|
|
179
|
+
});
|
|
162
180
|
server.watcher.on("change", async (path) => {
|
|
163
181
|
if (!resolved.watch.some((regex) => regex.test(path))) {
|
|
164
182
|
return;
|
|
165
183
|
}
|
|
166
|
-
|
|
167
|
-
if (routes && JSON.stringify(routes) !== JSON.stringify(previousRoutes)) {
|
|
168
|
-
debug.router("Updating routes via HMR:", routes);
|
|
169
|
-
server.ws.send({
|
|
170
|
-
type: "custom",
|
|
171
|
-
event: ROUTER_HMR_UPDATE_ROUTE,
|
|
172
|
-
data: routes
|
|
173
|
-
});
|
|
174
|
-
write(resolved);
|
|
175
|
-
previousRoutes = routes;
|
|
176
|
-
}
|
|
184
|
+
sendRoutingUpdate(server);
|
|
177
185
|
});
|
|
178
186
|
},
|
|
179
187
|
resolveId(id) {
|
|
180
|
-
if (id ===
|
|
181
|
-
return
|
|
188
|
+
if (id === ROUTING_VIRTUAL_MODULE_ID) {
|
|
189
|
+
return RESOLVED_ROUTING_VIRTUAL_MODULE_ID;
|
|
182
190
|
}
|
|
183
191
|
},
|
|
184
192
|
async load(id) {
|
|
185
|
-
if (id ===
|
|
186
|
-
const
|
|
187
|
-
return getClientCode(
|
|
193
|
+
if (id === RESOLVED_ROUTING_VIRTUAL_MODULE_ID) {
|
|
194
|
+
const routing = await fetchRoutingFromArtisan(resolved);
|
|
195
|
+
return getClientCode(routing);
|
|
188
196
|
}
|
|
189
197
|
},
|
|
190
198
|
async handleHotUpdate(ctx) {
|
package/dist/index.mjs
CHANGED
|
@@ -6,14 +6,15 @@ import { debounce } from 'throttle-debounce';
|
|
|
6
6
|
import { exec } from 'node:child_process';
|
|
7
7
|
import { promisify } from 'node:util';
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
9
|
+
const ROUTING_PLUGIN_NAME = "vite:hybridly:routing";
|
|
10
|
+
const ROUTING_HMR_UPDATE_ROUTING = "hybridly:routing:update";
|
|
11
|
+
const ROUTING_HMR_QUERY_UPDATE_ROUTING = "hybridly:routing:pls-update";
|
|
12
|
+
const ROUTING_VIRTUAL_MODULE_ID = "virtual:hybridly/router";
|
|
13
|
+
const RESOLVED_ROUTING_VIRTUAL_MODULE_ID = `\0${ROUTING_VIRTUAL_MODULE_ID}`;
|
|
13
14
|
const LAYOUT_PLUGIN_NAME = "vite:hybridly:layout";
|
|
14
15
|
|
|
15
16
|
const debug = {
|
|
16
|
-
router: makeDebugger(
|
|
17
|
+
router: makeDebugger(ROUTING_PLUGIN_NAME),
|
|
17
18
|
layout: makeDebugger(LAYOUT_PLUGIN_NAME)
|
|
18
19
|
};
|
|
19
20
|
|
|
@@ -54,7 +55,8 @@ const layout = (options = {}) => {
|
|
|
54
55
|
`, "").trim();
|
|
55
56
|
debug.layout(`Resolved layouts "${layouts.join(", ")}":`, {
|
|
56
57
|
sourceFile: id,
|
|
57
|
-
layouts
|
|
58
|
+
layouts,
|
|
59
|
+
imports
|
|
58
60
|
});
|
|
59
61
|
return `
|
|
60
62
|
<script${isTypeScript ? ' lang="ts"' : ""}>
|
|
@@ -68,45 +70,45 @@ const layout = (options = {}) => {
|
|
|
68
70
|
};
|
|
69
71
|
};
|
|
70
72
|
|
|
71
|
-
function getClientCode(
|
|
73
|
+
function getClientCode(routing) {
|
|
72
74
|
return `
|
|
73
75
|
if (typeof window !== 'undefined') {
|
|
74
76
|
window.hybridly = {
|
|
75
|
-
|
|
77
|
+
routing: ${JSON.stringify(routing)}
|
|
76
78
|
}
|
|
77
|
-
|
|
79
|
+
|
|
78
80
|
if (import.meta.hot) {
|
|
79
|
-
import.meta.hot.on('${
|
|
80
|
-
window.dispatchEvent(
|
|
81
|
-
new CustomEvent('hybridly:routes', { detail: routes })
|
|
82
|
-
)
|
|
81
|
+
import.meta.hot.on('${ROUTING_HMR_UPDATE_ROUTING}', (routing) => {
|
|
82
|
+
window.dispatchEvent(new CustomEvent('hybridly:routing', { detail: routing }))
|
|
83
83
|
})
|
|
84
|
+
|
|
85
|
+
import.meta.hot.send('${ROUTING_HMR_QUERY_UPDATE_ROUTING}')
|
|
84
86
|
}
|
|
85
87
|
}
|
|
86
88
|
`;
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
const shell = promisify(exec);
|
|
90
|
-
async function
|
|
92
|
+
async function fetchRoutingFromArtisan(options) {
|
|
91
93
|
try {
|
|
92
94
|
const php = options.php ?? "php";
|
|
93
95
|
const result = await shell(`${php} artisan hybridly:routes`);
|
|
94
|
-
const
|
|
95
|
-
write(options,
|
|
96
|
-
return
|
|
96
|
+
const routing = JSON.parse(result.stdout);
|
|
97
|
+
write(options, routing);
|
|
98
|
+
return routing;
|
|
97
99
|
} catch {
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
|
|
101
103
|
const write = debounce(1e3, writeDefinitions, { atBegin: true });
|
|
102
|
-
async function writeDefinitions(options,
|
|
103
|
-
|
|
104
|
-
if (options.dts === false || !
|
|
104
|
+
async function writeDefinitions(options, routing) {
|
|
105
|
+
routing ?? (routing = await fetchRoutingFromArtisan(options));
|
|
106
|
+
if (options.dts === false || !routing) {
|
|
105
107
|
return;
|
|
106
108
|
}
|
|
107
|
-
debug.router("Writing types for
|
|
109
|
+
debug.router("Writing types for routing:", routing);
|
|
108
110
|
const target = path.resolve(options.dts ?? "resources/types/routes.d.ts");
|
|
109
|
-
const routes = Object.fromEntries(Object.entries(
|
|
111
|
+
const routes = Object.fromEntries(Object.entries(routing.routes).map(([key, route]) => {
|
|
110
112
|
const bindings = route.bindings ? Object.fromEntries(Object.entries(route.bindings).map(([key2]) => [key2, "__key_placeholder__"])) : void 0;
|
|
111
113
|
return [key, {
|
|
112
114
|
...route.uri ? { uri: route.uri } : {},
|
|
@@ -115,7 +117,7 @@ async function writeDefinitions(options, collection) {
|
|
|
115
117
|
...route.bindings ? { bindings } : {}
|
|
116
118
|
}];
|
|
117
119
|
}));
|
|
118
|
-
const definitions = generateDefinitions().replace("__URL__",
|
|
120
|
+
const definitions = generateDefinitions().replace("__URL__", routing?.url ?? "").replace("__ROUTES__", JSON.stringify(routes).replaceAll('"__key_placeholder__"', "any"));
|
|
119
121
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
120
122
|
fs.writeFileSync(target, definitions, { encoding: "utf-8" });
|
|
121
123
|
}
|
|
@@ -125,7 +127,7 @@ function generateDefinitions() {
|
|
|
125
127
|
// Modifications will be discarded
|
|
126
128
|
// It is recommended to add it in your .gitignore
|
|
127
129
|
|
|
128
|
-
declare module 'hybridly
|
|
130
|
+
declare module 'hybridly' {
|
|
129
131
|
export interface GlobalRouteCollection {
|
|
130
132
|
url: '__URL__'
|
|
131
133
|
routes: __ROUTES__
|
|
@@ -144,37 +146,43 @@ const router = (options = {}) => {
|
|
|
144
146
|
],
|
|
145
147
|
...options
|
|
146
148
|
};
|
|
147
|
-
let
|
|
149
|
+
let routingBeforeUpdate;
|
|
150
|
+
async function sendRoutingUpdate(server, force = false) {
|
|
151
|
+
const routing = await fetchRoutingFromArtisan(resolved) ?? routingBeforeUpdate;
|
|
152
|
+
if (force || JSON.stringify(routing) !== JSON.stringify(routingBeforeUpdate)) {
|
|
153
|
+
debug.router("Updating routes via HMR:", routing);
|
|
154
|
+
server.ws.send({
|
|
155
|
+
type: "custom",
|
|
156
|
+
event: ROUTING_HMR_UPDATE_ROUTING,
|
|
157
|
+
data: routing
|
|
158
|
+
});
|
|
159
|
+
write(resolved);
|
|
160
|
+
routingBeforeUpdate = routing;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
148
163
|
return {
|
|
149
|
-
name:
|
|
164
|
+
name: ROUTING_PLUGIN_NAME,
|
|
150
165
|
configureServer(server) {
|
|
151
166
|
write(resolved);
|
|
167
|
+
server.ws.on(ROUTING_HMR_QUERY_UPDATE_ROUTING, () => {
|
|
168
|
+
sendRoutingUpdate(server, true);
|
|
169
|
+
});
|
|
152
170
|
server.watcher.on("change", async (path) => {
|
|
153
171
|
if (!resolved.watch.some((regex) => regex.test(path))) {
|
|
154
172
|
return;
|
|
155
173
|
}
|
|
156
|
-
|
|
157
|
-
if (routes && JSON.stringify(routes) !== JSON.stringify(previousRoutes)) {
|
|
158
|
-
debug.router("Updating routes via HMR:", routes);
|
|
159
|
-
server.ws.send({
|
|
160
|
-
type: "custom",
|
|
161
|
-
event: ROUTER_HMR_UPDATE_ROUTE,
|
|
162
|
-
data: routes
|
|
163
|
-
});
|
|
164
|
-
write(resolved);
|
|
165
|
-
previousRoutes = routes;
|
|
166
|
-
}
|
|
174
|
+
sendRoutingUpdate(server);
|
|
167
175
|
});
|
|
168
176
|
},
|
|
169
177
|
resolveId(id) {
|
|
170
|
-
if (id ===
|
|
171
|
-
return
|
|
178
|
+
if (id === ROUTING_VIRTUAL_MODULE_ID) {
|
|
179
|
+
return RESOLVED_ROUTING_VIRTUAL_MODULE_ID;
|
|
172
180
|
}
|
|
173
181
|
},
|
|
174
182
|
async load(id) {
|
|
175
|
-
if (id ===
|
|
176
|
-
const
|
|
177
|
-
return getClientCode(
|
|
183
|
+
if (id === RESOLVED_ROUTING_VIRTUAL_MODULE_ID) {
|
|
184
|
+
const routing = await fetchRoutingFromArtisan(resolved);
|
|
185
|
+
return getClientCode(routing);
|
|
178
186
|
}
|
|
179
187
|
},
|
|
180
188
|
async handleHotUpdate(ctx) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hybridly/vite",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.15",
|
|
4
4
|
"description": "A solution to develop server-driven, client-rendered applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hybridly",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"vue": "^3.2.33"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
42
|
+
"throttle-debounce": "^5.0.0",
|
|
43
|
+
"@hybridly/core": "0.0.1-alpha.15"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"rollup": "^3.4.0",
|