vite-plugin-files-server 1.0.2 → 1.1.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/dist/index.js DELETED
@@ -1,393 +0,0 @@
1
- // src/index.ts
2
- import fs from "fs";
3
- import path from "path";
4
-
5
- // src/temp.html?raw
6
- var temp_default = `<!DOCTYPE html>\r
7
- <html>\r
8
- <head>\r
9
- <meta charset="UTF-8" />\r
10
- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />\r
11
- <title>listing directory {{url}}</title>\r
12
- <style>\r
13
- /* body {\r
14
- font-family: system-ui;\r
15
- padding: 2rem;\r
16
- max-width: 800px;\r
17
- margin: 0 auto;\r
18
- }\r
19
- h2 {\r
20
- border-bottom: 1px solid #eee;\r
21
- padding-bottom: 10px;\r
22
- }\r
23
- table {\r
24
- width: 100%;\r
25
- border-collapse: collapse;\r
26
- }\r
27
- td {\r
28
- padding: 8px;\r
29
- border-bottom: 1px solid #f5f5f5;\r
30
- }\r
31
- a {\r
32
- text-decoration: none;\r
33
- color: #646cff;\r
34
- }\r
35
- a:hover {\r
36
- text-decoration: underline;\r
37
- }\r
38
- ul,\r
39
- li {\r
40
- list-style: none;\r
41
- margin: 0;\r
42
- padding: 0;\r
43
- } */\r
44
- * {\r
45
- margin: 0;\r
46
- padding: 0;\r
47
- outline: 0;\r
48
- }\r
49
- \r
50
- body {\r
51
- padding: 80px 100px;\r
52
- font: 13px 'Helvetica Neue', 'Lucida Grande', 'Arial';\r
53
- background: #ece9e9 -webkit-gradient(linear, 0% 0%, 0% 100%, from(#fff), to(#ece9e9));\r
54
- background: #ece9e9 -moz-linear-gradient(top, #fff, #ece9e9);\r
55
- background-repeat: no-repeat;\r
56
- color: #555;\r
57
- -webkit-font-smoothing: antialiased;\r
58
- }\r
59
- h1,\r
60
- h2,\r
61
- h3 {\r
62
- padding-left: 10px;\r
63
- font-size: 22px;\r
64
- color: #343434;\r
65
- }\r
66
- h1 em,\r
67
- h2 em {\r
68
- padding: 0 5px;\r
69
- font-weight: normal;\r
70
- }\r
71
- h1 {\r
72
- font-size: 60px;\r
73
- }\r
74
- h2 {\r
75
- margin-top: 10px;\r
76
- }\r
77
- h3 {\r
78
- margin: 5px 0 10px 0;\r
79
- padding-bottom: 5px;\r
80
- border-bottom: 1px solid #eee;\r
81
- font-size: 18px;\r
82
- }\r
83
- ul li {\r
84
- list-style: none;\r
85
- }\r
86
- ul li:hover {\r
87
- cursor: pointer;\r
88
- color: #2e2e2e;\r
89
- }\r
90
- ul li .path {\r
91
- padding-left: 5px;\r
92
- font-weight: bold;\r
93
- }\r
94
- ul li .line {\r
95
- padding-right: 5px;\r
96
- font-style: italic;\r
97
- }\r
98
- ul li:first-child .path {\r
99
- padding-left: 0;\r
100
- }\r
101
- p {\r
102
- line-height: 1.5;\r
103
- }\r
104
- a {\r
105
- color: #555;\r
106
- text-decoration: none;\r
107
- }\r
108
- a:hover {\r
109
- color: #303030;\r
110
- }\r
111
- #stacktrace {\r
112
- margin-top: 15px;\r
113
- }\r
114
- .directory h1 {\r
115
- margin-bottom: 15px;\r
116
- font-size: 18px;\r
117
- }\r
118
- ul#files {\r
119
- width: 100%;\r
120
- height: 100%;\r
121
- overflow: hidden;\r
122
- }\r
123
- ul#files li {\r
124
- float: left;\r
125
- width: 30%;\r
126
- line-height: 25px;\r
127
- margin: 1px;\r
128
- }\r
129
- ul#files li a {\r
130
- display: block;\r
131
- height: 25px;\r
132
- border: 1px solid transparent;\r
133
- -webkit-border-radius: 5px;\r
134
- -moz-border-radius: 5px;\r
135
- border-radius: 5px;\r
136
- overflow: hidden;\r
137
- white-space: nowrap;\r
138
- }\r
139
- ul#files li a:focus,\r
140
- ul#files li a:hover {\r
141
- background: rgba(255, 255, 255, 0.65);\r
142
- border: 1px solid #ececec;\r
143
- }\r
144
- ul#files li a.highlight {\r
145
- -webkit-transition: background 0.4s ease-in-out;\r
146
- background: #ffff4f;\r
147
- border-color: #e9dc51;\r
148
- }\r
149
- #search {\r
150
- display: block;\r
151
- position: fixed;\r
152
- top: 20px;\r
153
- right: 20px;\r
154
- width: 90px;\r
155
- -webkit-transition: width ease 0.2s, opacity ease 0.4s;\r
156
- -moz-transition: width ease 0.2s, opacity ease 0.4s;\r
157
- -webkit-border-radius: 32px;\r
158
- -moz-border-radius: 32px;\r
159
- -webkit-box-shadow: inset 0px 0px 3px rgba(0, 0, 0, 0.25), inset 0px 1px 3px rgba(0, 0, 0, 0.7),\r
160
- 0px 1px 0px rgba(255, 255, 255, 0.03);\r
161
- -moz-box-shadow: inset 0px 0px 3px rgba(0, 0, 0, 0.25), inset 0px 1px 3px rgba(0, 0, 0, 0.7),\r
162
- 0px 1px 0px rgba(255, 255, 255, 0.03);\r
163
- -webkit-font-smoothing: antialiased;\r
164
- text-align: left;\r
165
- font: 13px 'Helvetica Neue', Arial, sans-serif;\r
166
- padding: 4px 10px;\r
167
- border: none;\r
168
- background: transparent;\r
169
- margin-bottom: 0;\r
170
- outline: none;\r
171
- opacity: 0.7;\r
172
- color: #888;\r
173
- }\r
174
- #search:focus {\r
175
- width: 120px;\r
176
- opacity: 1;\r
177
- }\r
178
- \r
179
- /*views*/\r
180
- #files span {\r
181
- display: inline-block;\r
182
- overflow: hidden;\r
183
- text-overflow: ellipsis;\r
184
- text-indent: 10px;\r
185
- }\r
186
- #files .name {\r
187
- background-repeat: no-repeat;\r
188
- }\r
189
- #files .icon .name {\r
190
- text-indent: 28px;\r
191
- }\r
192
- \r
193
- /*tiles*/\r
194
- .view-tiles .name {\r
195
- width: 100%;\r
196
- background-position: 8px 5px;\r
197
- }\r
198
- .view-tiles .size,\r
199
- .view-tiles .date {\r
200
- display: none;\r
201
- }\r
202
- \r
203
- /*details*/\r
204
- ul#files.view-details li {\r
205
- float: none;\r
206
- display: block;\r
207
- width: 90%;\r
208
- }\r
209
- ul#files.view-details li.header {\r
210
- height: 25px;\r
211
- background: #000;\r
212
- color: #fff;\r
213
- font-weight: bold;\r
214
- }\r
215
- .view-details .header {\r
216
- border-radius: 5px;\r
217
- }\r
218
- .view-details .name {\r
219
- width: 60%;\r
220
- background-position: 8px 5px;\r
221
- }\r
222
- .view-details .size {\r
223
- width: 10%;\r
224
- }\r
225
- .view-details .date {\r
226
- width: 30%;\r
227
- }\r
228
- .view-details .size,\r
229
- .view-details .date {\r
230
- text-align: right;\r
231
- direction: rtl;\r
232
- }\r
233
- \r
234
- /*mobile*/\r
235
- @media (max-width: 768px) {\r
236
- body {\r
237
- font-size: 13px;\r
238
- line-height: 16px;\r
239
- padding: 0;\r
240
- }\r
241
- #search {\r
242
- position: static;\r
243
- width: 100%;\r
244
- font-size: 2em;\r
245
- line-height: 1.8em;\r
246
- text-indent: 10px;\r
247
- border: 0;\r
248
- border-radius: 0;\r
249
- padding: 10px 0;\r
250
- margin: 0;\r
251
- }\r
252
- #search:focus {\r
253
- width: 100%;\r
254
- border: 0;\r
255
- opacity: 1;\r
256
- }\r
257
- .directory h1 {\r
258
- font-size: 2em;\r
259
- line-height: 1.5em;\r
260
- color: #fff;\r
261
- background: #000;\r
262
- padding: 15px 10px;\r
263
- margin: 0;\r
264
- }\r
265
- ul#files {\r
266
- border-top: 1px solid #cacaca;\r
267
- }\r
268
- ul#files li {\r
269
- float: none;\r
270
- width: auto !important;\r
271
- display: block;\r
272
- border-bottom: 1px solid #cacaca;\r
273
- font-size: 2em;\r
274
- line-height: 1.2em;\r
275
- text-indent: 0;\r
276
- margin: 0;\r
277
- }\r
278
- ul#files li:nth-child(odd) {\r
279
- background: #e0e0e0;\r
280
- }\r
281
- ul#files li a {\r
282
- height: auto;\r
283
- border: 0;\r
284
- border-radius: 0;\r
285
- padding: 15px 10px;\r
286
- }\r
287
- ul#files li a:focus,\r
288
- ul#files li a:hover {\r
289
- border: 0;\r
290
- }\r
291
- #files .header,\r
292
- #files .size,\r
293
- #files .date {\r
294
- display: none !important;\r
295
- }\r
296
- #files .name {\r
297
- float: none;\r
298
- display: inline-block;\r
299
- width: 100%;\r
300
- text-indent: 0;\r
301
- background-position: 0 50%;\r
302
- }\r
303
- #files .icon .name {\r
304
- text-indent: 41px;\r
305
- }\r
306
- }\r
307
- #files .icon-directory .name {\r
308
- background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC/xhBQAAAWtQTFRFAAAA/PPQ9Nhc2q402qQ12qs2/PTX2pg12p81+/LM89NE9dto2q82+/fp2rM22qY39d6U+/bo2qo2/frx/vz32q812qs12qE279SU8c4w9NZP+/LK//367s9y7s925cp0/vzw9t92//342po2/vz25s1579B6+OSO2bQ0/v799NyT8tE79dld8Msm+OrC/vzx79KA2IYs7s6I9d6R4cJe9+OF/PLI/fry79OF/v30//328tWB89RJ8c9p8c0u9eCf//7+9txs6sts5Mdr+++5+u2z/vrv+/fq6cFz8dBs8tA57cpq+OaU9uGs27Y8//799NdX/PbY9uB89unJ//z14sNf+emh+emk+vDc+uys9+OL8dJy89NH+eic8tN5+OaV+OWR9N2n9dtl9t529+KF9+GB9Nue9NdU8tR/9t5y89qW9dpj89iO89eG/vvu2pQ12Y4z/vzy2Ict/vvv48dr/vzz4sNg///+2Igty3PqwQAAAAF0Uk5TAEDm2GYAAACtSURBVBjTY2AgA2iYlJWVhfohBPg0yx38y92dS0pKVOVBAqIi6sb2vsWWpfrFeTI8QAEhYQEta28nCwM1OVleZqCAmKCEkUdwYWmhQnFeOStQgL9cySqkNNDHVJGbiY0FKCCuYuYSGRsV5KgjxcXIARRQNncNj09JTgqw0ZbkZAcK5LuFJaRmZqfHeNnpSucDBQoiEtOycnIz4qI9bfUKQA6pKKqAgqIKQyK8BgAZ5yfODmnHrQAAAABJRU5ErkJggg==);\r
309
- }\r
310
- #files .icon-text-html .name {\r
311
- background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHtSURBVDjLjZM9T9tQFIYpQ5eOMBKlW6eWIQipa8RfQKQghEAKqZgKFQgmFn5AWyVDCipVQZC2EqBWlEqdO2RCpAssQBRsx1+1ndix8wFvfW6wcUhQsfTI0j33PD7n+N4uAF2E+/S5RFwG/8Njl24/LyCIOI6j1+v1y0ajgU64cSSTybdBSVAwSMmmacKyLB/DMKBpGkRRZBJBEJBKpXyJl/yABLTBtm1Uq1X2JsrlMnRdhyRJTFCpVEAfSafTTUlQoFs1luxBAkoolUqQZbmtJTYTT/AoHInOfpcwtVtkwcSBgrkDGYph+60oisIq4Xm+VfB0+U/P0Lvj3NwPGfHPTcHMvoyFXwpe7UmQtAqTUCU0D1VVbwTPVk5jY19Fe3ZfQny7CE51WJDXqpjeEUHr45ki9rIqa4dmQiJfMLItGEs/FcQ2ucbRmdnSYy5vYWyLx/w3EaMfLmBaDpMQvuDJ65PY8Dpnz3wpYmLtApzcrIAqmfrEgdZH1grY/a36w6Xz0DKD8ES25/niYS6+wWE8mWfByY8cXmYEJFYLkHUHtVqNQcltAvoLD3v7o/FUHsNvzlnwxfsCEukC/ho3yUHaBN5Buo17Ojtyl+DqrnvQgUtfcC0ZcAdkUeA+ye7eMru9AUGIJPe4zh509UP/AAfNypi8oj/mAAAAAElFTkSuQmCC);\r
312
- }\r
313
- </style>\r
314
- </head>\r
315
- <body class="directory">\r
316
- <div class="wrapper">\r
317
- <h1>{{headItem}}</h1>\r
318
- <ul id="files" class="view-tiles">\r
319
- {{listItems}}\r
320
- </ul>\r
321
- </div>\r
322
- </body>\r
323
- </html>\r
324
- `;
325
-
326
- // src/index.ts
327
- function fileServerPlugin(options = {}) {
328
- const { enable = true } = options;
329
- return {
330
- name: "vite-plugin-files-server",
331
- // 必须唯一
332
- // 核心钩子:配置开发服务器
333
- configureServer(server) {
334
- if (!enable) return;
335
- server.middlewares.use((req, res, next) => {
336
- const url = req.url ? decodeURIComponent(req.url.split("?")[0]) : "/";
337
- if (url.startsWith("/@") || url.includes("vite")) {
338
- return next();
339
- }
340
- const projectRoot = server.config.root;
341
- const fullPath = path.join(projectRoot, url);
342
- try {
343
- if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
344
- if (fs.existsSync(path.join(fullPath, "index.html"))) {
345
- return next();
346
- }
347
- let urlSplitArray = url.split("/");
348
- if (urlSplitArray[urlSplitArray.length - 1] == "") urlSplitArray = urlSplitArray.slice(0, -1);
349
- let headItem = '<a href="/">\u{1F3E0}\uFE0E</a> / ';
350
- urlSplitArray.forEach((p) => {
351
- p && (headItem += `<a href="/${p}">${p}</a> / `);
352
- });
353
- const files = fs.readdirSync(fullPath);
354
- const listItems = files.map((file) => {
355
- const filePath = path.join(fullPath, file);
356
- const isDir = fs.statSync(filePath).isDirectory();
357
- const href = path.join(url, file).replace(/\\/g, "/");
358
- const item = `
359
- <li>
360
- <a href="${href}" class="icon ${isDir ? "icon-directory" : "icon-html icon-text-html"}">
361
- <span class="name">${file}</span>
362
- </a>
363
- </li>`;
364
- return item;
365
- }).join("");
366
- urlSplitArray = urlSplitArray.slice(0, -1);
367
- let backUrl = urlSplitArray.join("/");
368
- const backItem = `
369
- <li>
370
- <a href="${backUrl}" class="icon icon-directory"><span class="name">..</span></a>
371
- </li>`;
372
- let fullList = urlSplitArray.length > 1 ? backItem + listItems : listItems;
373
- const html = temp_default.replace(/{{title}}/g, `Index of ${url}`).replace(/{{headItem}}/g, headItem).replace(/{{url}}/g, url).replace(/{{listItems}}/g, fullList);
374
- res.setHeader("Content-Type", "text/html");
375
- res.end(html);
376
- return;
377
- } else if (!fs.existsSync(fullPath)) {
378
- const html = "path not found";
379
- res.setHeader("Content-Type", "text/html");
380
- res.end(html);
381
- return;
382
- }
383
- } catch (e) {
384
- console.error("File Server Plugin Error:", e);
385
- }
386
- next();
387
- });
388
- }
389
- };
390
- }
391
- export {
392
- fileServerPlugin as default
393
- };