-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.js
More file actions
56 lines (50 loc) · 1.82 KB
/
Copy pathvite.js
File metadata and controls
56 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const Koa = require("koa");
const app = new Koa();
const Router = require("koa-router");
const fs = require("fs");
const path = require("path");
const compilerSfc = require("vue/compiler-sfc");
const { rewriteImport, handleScript, handleTemplate, handleStyle } = require("./util");
const router = new Router();
router
.get("/", (ctx) => {
ctx.type = "text/html";
ctx.body = fs.readFileSync(path.resolve(__dirname, "./index.html"));
})
.get(/\.js$/, (ctx) => {
const url = ctx.url;
console.log(url);
const jsPath = path.join(__dirname, url);
ctx.type = "text/javascript";
const file = fs.readFileSync(jsPath, "utf8");
ctx.body = rewriteImport(file);
})
.get(/^(\/@modules\/)/, (ctx) => {
const url = ctx.url;
ctx.type = "text/javascript";
const filePrefix = path.resolve(__dirname, "node_modules", url.replace("/@modules/", ""));
const module = require(filePrefix + "/package.json").module;
const file = fs.readFileSync(filePrefix + "/" + module, "utf-8");
ctx.body = rewriteImport(file);
})
.get(/\.vue$/, (ctx) => {
const { query, url } = ctx.request;
const queryType = query.type || "script";
const vuePath = path.join(__dirname, url.split("?")[0]);
const res = compilerSfc.parse(fs.readFileSync(vuePath, "utf8"));
const { script, template, styles } = res.descriptor;
const map = {
script: () => handleScript(script, styles, url),
template: () => handleTemplate(template),
style: () => handleStyle(styles, query),
};
const body = map[queryType]();
ctx.type = "application/javascript";
ctx.body = body;
})
.get(/\.(jpeg|png|jpg)$/, (ctx) => {
const url = ctx.url;
ctx.body = fs.readFileSync("src" + url);
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(8080, () => console.log("server start"));