-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
123 lines (110 loc) · 3.11 KB
/
index.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
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
123
const {
createLambda,
download,
getNodeVersion,
getSpawnOptions
} = require('@vercel/build-utils') // eslint-disable-line import/no-extraneous-dependencies
const path = require('path')
const {
getLauncherFiles,
getMountPoint,
globAndPrefix
} = require('./lib/utils')
const { getConfig } = require('./lib/config')
const { npmBuild } = require('./lib/npm')
exports.config = {
maxLambdaSize: '10mb'
}
exports.build = async ({
files,
entrypoint,
workPath,
config: rawConfig,
meta = {}
}) => {
const mountpoint = getMountPoint(entrypoint)
const entrypointDir = path.join(workPath, mountpoint)
await download(files, workPath, meta)
process.chdir(entrypointDir)
const config = getConfig(rawConfig)
const nodeVersion = await getNodeVersion(entrypointDir, null, config)
const spawnOpts = getSpawnOptions(meta, nodeVersion)
const prodDependencies = await npmBuild(
config,
entrypointDir,
spawnOpts,
meta
)
const launcherFiles = getLauncherFiles()
const staticFiles = await globAndPrefix(entrypointDir, 'static')
const applicationFiles = await globAndPrefix(entrypointDir, '__sapper__')
const includeFilesArray = await Promise.all(
config.include.map((path) => globAndPrefix(entrypointDir, path))
)
const includeFiles = includeFilesArray.reduce(
(all, files) => ({
...all,
...files
}),
{}
)
// To avoid a sirv error, add 1 file from the static folder
const sirvFix = !Object.keys(includeFiles).find((p) => p.startsWith('static'))
if (sirvFix) {
const first = Object.keys(staticFiles)[0]
if (first) {
includeFiles[first] = staticFiles[first]
}
console.log('vercel-sapper includeFiles', Object.keys(includeFiles))
}
// Use the system-installed version of `node` when running via `vercel dev`
const runtime = meta.isDev ? 'nodejs' : nodeVersion.runtime
const memory = config.memory || undefined
const lambda = await createLambda({
files: {
...includeFiles,
...launcherFiles,
...prodDependencies,
...applicationFiles
},
handler: 'launcher.launcher',
runtime,
...(memory ? { memory } : {})
}).catch((e) => {
console.error('createLambda.error', e)
console.error('createLambda.config', config)
})
const output = {
...serve(staticFiles, 'static/', ''),
...serve(
applicationFiles,
'__sapper__/build/service-worker.js',
'service-worker.js'
),
...serve(applicationFiles, '__sapper__/build/client', 'client'),
index: lambda
}
const routes = [
{
src: '/client/.+\\.(css|js|map)',
headers: { 'cache-control': 'public,max-age=31536000,immutable' },
continue: true
},
{
src: '/service-worker.js',
headers: { 'cache-control': 'public,max-age=0,must-revalidate' },
continue: true
},
{ handle: 'filesystem' },
{ src: '/(.*)', dest: '/' }
]
return { output, routes }
}
function serve(arr, filePath, routePath) {
return Object.keys(arr)
.filter((path) => path.startsWith(filePath))
.reduce((obj, key) => {
obj[key.replace(filePath, routePath)] = arr[key]
return obj
}, {})
}