This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
src_files.ts
209 lines (191 loc) · 5.56 KB
/
src_files.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint-disable max-lines */
import { dirname, basename, normalize } from 'path'
import { not as notJunk } from 'junk'
import precinct from 'precinct'
import { FeatureFlags } from '../../../../feature_flags.js'
import { nonNullable } from '../../../../utils/non_nullable.js'
import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js'
import { getPackageJson, PackageJson } from '../../utils/package_json.js'
import { getNewCache, TraversalCache } from '../../utils/traversal_cache.js'
import type { GetSrcFilesFunction } from '../index.js'
import { listImports } from './list_imports.js'
import { resolvePathPreserveSymlinks } from './resolve.js'
import { getDependencyPathsForDependency } from './traverse.js'
import { getTreeFiles } from './tree_files.js'
import { shouldTreeShake } from './tree_shake.js'
// Retrieve the paths to the Node.js files to zip.
// We only include the files actually needed by the function because AWS Lambda
// has a size limit for the zipped file. It also makes cold starts faster.
export const getSrcFiles: GetSrcFilesFunction = async function ({
config,
featureFlags,
mainFile,
name,
pluginsModulesPath,
srcDir,
srcPath,
stat,
}) {
const { includedFiles = [], includedFilesBasePath } = config
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath,
)
const [treeFiles, depFiles] = await Promise.all([
getTreeFiles(srcPath, stat),
getDependencies({ featureFlags, functionName: name, mainFile, pluginsModulesPath, srcDir }),
])
const files = [...treeFiles, ...depFiles].map(normalize)
const uniqueFiles = [...new Set(files)]
// We sort so that the archive's checksum is deterministic.
// Mutating is fine since `Array.filter()` returns a shallow copy
const filteredFiles = uniqueFiles.filter(isNotJunk).sort()
const srcFiles = filterExcludedPaths(filteredFiles, excludePatterns)
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)
return { srcFiles: [...srcFiles, ...includedPaths], includedFiles: includedPaths }
}
// Remove temporary files like *~, *.swp, etc.
const isNotJunk = function (file: string) {
return notJunk(basename(file))
}
// Retrieve all the files recursively required by a Node.js file
const getDependencies = async function ({
featureFlags,
functionName,
mainFile,
pluginsModulesPath,
srcDir,
}: {
featureFlags: FeatureFlags
functionName: string
mainFile: string
pluginsModulesPath?: string
srcDir: string
}) {
const packageJson = await getPackageJson(srcDir)
const state = getNewCache()
try {
return await getFileDependencies({
featureFlags,
functionName,
path: mainFile,
packageJson,
pluginsModulesPath,
state,
})
} catch (error) {
error.message = `In file "${mainFile}"\n${error.message}`
throw error
}
}
const getFileDependencies = async function ({
featureFlags,
functionName,
path,
packageJson,
pluginsModulesPath,
state,
treeShakeNext = false,
}: {
featureFlags: FeatureFlags
functionName: string
path: string
packageJson: PackageJson
pluginsModulesPath?: string
state: TraversalCache
treeShakeNext?: boolean
}): Promise<string[]> {
if (state.localFiles.has(path)) {
return []
}
state.localFiles.add(path)
const basedir = dirname(path)
const dependencies = featureFlags.parseWithEsbuild
? await listImports({ functionName, path })
: await precinct.paperwork(path, { includeCore: false })
const depsPaths = await Promise.all(
dependencies.filter(nonNullable).map((dependency) =>
getImportDependencies({
dependency,
basedir,
featureFlags,
functionName,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
}),
),
)
return depsPaths.flat()
}
const getImportDependencies = function ({
dependency,
basedir,
featureFlags,
functionName,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
}: {
dependency: string
basedir: string
featureFlags: FeatureFlags
functionName: string
packageJson: PackageJson
pluginsModulesPath?: string
state: TraversalCache
treeShakeNext: boolean
}): Promise<string[]> {
const shouldTreeShakeNext = treeShakeNext || isNextOnNetlify(dependency)
if (shouldTreeShake(dependency, shouldTreeShakeNext)) {
return getTreeShakedDependencies({
dependency,
basedir,
featureFlags,
functionName,
packageJson,
pluginsModulesPath,
state,
treeShakeNext: shouldTreeShakeNext,
})
}
return getDependencyPathsForDependency({ dependency, basedir, state, packageJson, pluginsModulesPath })
}
const isNextOnNetlify = function (dependency: string) {
return basename(dependency, '.js') === 'renderNextPage'
}
// When a file requires another one, we apply the top-level logic recursively
const getTreeShakedDependencies = async function ({
dependency,
basedir,
featureFlags,
functionName,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
}: {
dependency: string
basedir: string
featureFlags: FeatureFlags
functionName: string
packageJson: PackageJson
pluginsModulesPath?: string
state: TraversalCache
treeShakeNext: boolean
}) {
const path = await resolvePathPreserveSymlinks(dependency, [basedir, pluginsModulesPath].filter(nonNullable))
const depsPath = await getFileDependencies({
featureFlags,
functionName,
path,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
})
return [path, ...depsPath]
}
/* eslint-enable max-lines */