-
Notifications
You must be signed in to change notification settings - Fork 6
/
outputLambdas.ts
82 lines (65 loc) · 2.1 KB
/
outputLambdas.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
import fs from 'fs'
import path from 'path'
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import { afix } from 'afix'
import { hashContent } from '@presta/utils'
import { create } from '../config'
import { Env } from '../constants'
import { outputLambdas, slugify } from '../outputLambdas'
const test = suite('presta - outputLambdas')
test('outputLambdas', async () => {
const fixture = afix({
slug: ['slug.js', `module.exports = { route: '/:slug' }`],
fallback: ['fallback.js', `module.exports = { route: '/:slug?' }`],
error: ['error.js', `module.exports = { route: '/:slug?'`],
})
const config = create(
Env.DEVELOPMENT,
{
_: [path.join(fixture.root, '*.js')],
output: path.join(fixture.root, 'output'),
},
{}
)
const [slug, fallback] = outputLambdas(
[fixture.files.slug.path, fixture.files.fallback.path, fixture.files.error.path],
config
)
assert.equal(slug.route, `/:slug`)
assert.ok(slug.dest.includes(`slug.js`))
assert.equal(fallback.route, `/:slug?`)
assert.ok(fallback.dest.includes(`fallback.js`))
})
test('outputLambdas - hashed in prod', async () => {
const fixture = afix({
slug: ['slug.js', `module.exports = { route: '/:slug' }`],
})
const config = create(
Env.PRODUCTION,
{
_: [path.join(fixture.root, '*.js')],
output: path.join(fixture.root, 'output'),
},
{}
)
const [slug] = outputLambdas([fixture.files.slug.path], config)
const hash = hashContent(fixture.files.slug.content)
assert.equal(slug.route, `/:slug`)
assert.ok(slug.dest.includes(`slug-${hash}.js`))
assert.ok(fs.existsSync(slug.dest))
})
test('slugify', async () => {
const cwd = process.cwd()
const fixture = afix({
basic: ['file.js', ``],
nested: ['pages/file.js', ``],
complex: ['pages/file-something.page.js', ``],
})
process.chdir(fixture.root)
assert.equal(slugify(fixture.files.basic.path), 'file')
assert.equal(slugify(fixture.files.nested.path), 'pages-file')
assert.equal(slugify(fixture.files.complex.path), 'pages-file-something-page')
process.chdir(cwd)
})
test.run()