-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
122 lines (99 loc) · 2.74 KB
/
index.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import fs from 'fs'
import path from 'path'
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import { afix } from 'afix'
import { build as esbuild } from 'esbuild'
import { ManifestDynamicFile } from 'presta'
import createPlugin, { vercelConfig, onPostBuild } from '../index'
const test = suite('@presta/adapter-vercel')
test('onPostBuild', async () => {
const cwd = process.cwd()
const fixture = afix({
static: ['build/static/index.html', 'html'],
})
process.chdir(fixture.root)
const output = path.join(fixture.root, 'build')
onPostBuild({
output,
staticOutput: path.join(output, 'static'),
functionsOutput: path.join(output, 'functions'),
manifest: { files: [] },
})
assert.ok(fs.existsSync(path.join(fixture.root, './.output/static/index.html')))
process.chdir(cwd)
})
test('createPlugin', async () => {
const cwd = process.cwd()
const fixture = afix({})
process.chdir(fixture.root)
let plan = 0
await createPlugin()(
// @ts-ignore
{},
{
onPostBuild(fn) {
plan++
},
}
)
assert.equal(plan, 1)
assert.equal(JSON.parse(fs.readFileSync(path.join(fixture.root, 'vercel.json'), 'utf8')), vercelConfig)
process.chdir(cwd)
})
test('generateRoutes', async () => {
const cwd = process.cwd()
const fixture = afix({
index: [
'build/functions/home.js',
`export const route = '/';
export function handler () {
return ''
}`,
],
page: [
'build/functions/page.js',
`export const route = '*';
export function handler () {
return ''
}`,
],
})
process.chdir(fixture.root)
const prestaOutput = path.join(fixture.root, 'build')
const dynamicFiles: ManifestDynamicFile[] = [
{
type: 'dynamic',
src: 'src',
dest: path.join(prestaOutput, 'functions/home.js'),
route: '/',
},
{
type: 'dynamic',
src: 'src',
dest: path.join(prestaOutput, 'functions/page.js'),
route: '*',
},
]
const { generateRoutes } = require('proxyquire')('../index', {
esbuild: {
async build(config: any) {
await esbuild({
...config,
external: ['@presta/adapter-vercel'],
})
},
},
})
await generateRoutes(prestaOutput, dynamicFiles)
const routesManifestPath = path.join(process.cwd(), './.output/routes-manifest.json')
const routesManifest = JSON.parse(fs.readFileSync(routesManifestPath, 'utf8'))
assert.equal(
routesManifest.dynamicRoutes.map((r: any) => r.page),
['/index', '/page']
)
assert.ok(fs.existsSync(path.join(process.cwd(), './.output/server/pages/index.js')))
assert.ok(fs.existsSync(path.join(process.cwd(), './.output/server/pages/page.js')))
process.chdir(cwd)
})
test.run()