-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnuxt.js
68 lines (56 loc) · 1.46 KB
/
nuxt.js
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
57
58
59
60
61
62
63
64
65
66
67
68
import { Nuxt, Builder, Generator } from "nuxt";
import request from "request-promise-native";
import Koa from "koa";
import serveStatic from "koa-static";
import baseConfig from "./fixture/nuxt.config";
jest.setTimeout(60000);
process.env.PORT = process.env.PORT || 5060;
process.env.NODE_ENV = "production";
const url = path => `http://localhost:${process.env.PORT}${path}`;
const get = path => request(url(path));
let nuxt;
let generator;
let server;
const serve = (isStatic = false) => {
const app = new Koa();
app.use(
!isStatic
? ctx => {
ctx.status = 200;
ctx.respond = false;
ctx.req.ctx = ctx;
nuxt.render(ctx.req, ctx.res);
}
: serveStatic(generator.distPath)
);
server = app.listen(process.env.PORT);
};
const commonBefore = async (config = {}) => {
const mergedConfig = {
...baseConfig,
...config
};
// Build a fresh nuxt
nuxt = new Nuxt(mergedConfig);
const builder = new Builder(nuxt);
await builder.build();
serve();
};
const generate = async (config = {}) => {
const mergedConfig = {
...baseConfig,
...config
};
// Build a fresh nuxt
nuxt = new Nuxt(mergedConfig);
const builder = new Builder(nuxt);
generator = new Generator(nuxt, builder);
await generator.generate();
serve(true);
};
const commonAfter = async () => {
// Close all opened resources
server.close();
await nuxt.close();
};
export { get, commonBefore, commonAfter, generate };