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
/
dependencies.js
290 lines (240 loc) · 9.34 KB
/
dependencies.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const { dirname, basename, normalize } = require('path')
const glob = require('glob')
const { not: notJunk } = require('junk')
const pkgDir = require('pkg-dir')
const precinct = require('precinct')
const requirePackageName = require('require-package-name')
const promisify = require('util.promisify')
const { resolvePathPreserveSymlinks, resolvePackage } = require('./resolve')
const pGlob = promisify(glob)
// 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.
const listNodeFiles = async function(srcPath, mainFile, srcDir, stat) {
const [treeFiles, depFiles] = await Promise.all([getTreeFiles(srcPath, stat), getDependencies(mainFile, srcDir)])
const files = [...treeFiles, ...depFiles].map(normalize)
const uniqueFiles = [...new Set(files)]
// We sort so that the archive's checksum is deterministic.
const filteredFiles = uniqueFiles.filter(isNotJunk).sort()
return filteredFiles
}
// When using a directory, we include all its descendants except `node_modules`
const getTreeFiles = function(srcPath, stat) {
if (!stat.isDirectory()) {
return [srcPath]
}
return pGlob(`${srcPath}/**`, {
ignore: `${srcPath}/**/node_modules/**`,
nodir: true,
absolute: true
})
}
// Remove temporary files like *~, *.swp, etc.
const isNotJunk = function(file) {
return notJunk(basename(file))
}
// Retrieve all the files recursively required by a Node.js file
const getDependencies = async function(mainFile, srcDir) {
const packageRoot = await pkgDir(srcDir)
const packageJson = getPackageJson(packageRoot)
const state = { localFiles: [], modulePaths: [] }
try {
return await getFileDependencies(mainFile, packageJson, state)
} catch (error) {
error.message = `In file "${mainFile}": ${error.message}`
throw error
}
}
const getPackageJson = function(packageRoot) {
if (packageRoot === undefined) {
return {}
}
const packageJsonPath = `${packageRoot}/package.json`
try {
return require(packageJsonPath)
} catch (error) {
throw new Error(`${packageJsonPath} is invalid JSON: ${error.message}`)
}
}
const getFileDependencies = async function(path, packageJson, state) {
if (state.localFiles.includes(path)) {
return []
}
state.localFiles.push(path)
const basedir = dirname(path)
// This parses JavaScript in `path` to retrieve all the `require()` statements
// TODO: `precinct.paperwork()` uses `fs.readFileSync()` under the hood,
// but should use `fs.readFile()` instead
const dependencies = precinct.paperwork(path, { includeCore: false })
const depsPaths = await Promise.all(
dependencies.map(dependency => getImportDependencies(dependency, basedir, packageJson, state))
)
return [].concat(...depsPaths)
}
// `require()` statements can be either `require('moduleName')` or
// `require(path)`
const getImportDependencies = function(dependency, basedir, packageJson, state) {
if (LOCAL_IMPORT_REGEXP.test(dependency)) {
return getLocalImportDependencies(dependency, basedir, packageJson, state)
}
return getModuleDependencies(dependency, basedir, state, packageJson)
}
const LOCAL_IMPORT_REGEXP = /^(\.|\/)/
// When a file requires another one, we apply the top-level logic recursively
const getLocalImportDependencies = async function(dependency, basedir, packageJson, state) {
const dependencyPath = await resolvePathPreserveSymlinks(dependency, basedir)
const depsPath = await getFileDependencies(dependencyPath, packageJson, state)
return [dependencyPath, ...depsPath]
}
// When a file requires a module, we find its path inside `node_modules` and
// use all its published files. We also recurse on the module's dependencies.
const getModuleDependencies = async function(dependency, basedir, state, packageJson) {
const moduleName = getModuleName(dependency)
// Happens when doing require("@scope") (not "@scope/name") or other oddities
// Ignore those.
if (moduleName === null) {
return []
}
try {
return await getModuleNameDependencies(moduleName, basedir, state)
} catch (error) {
return handleModuleNotFound({ error, moduleName, packageJson })
}
}
// When doing require("moduleName/file/path"), only keep `moduleName`
const getModuleName = function(dependency) {
const dependencyA = dependency.replace(BACKSLASH_REGEXP, '/')
const moduleName = requirePackageName(dependencyA)
return moduleName
}
// Windows path normalization
const BACKSLASH_REGEXP = /\\/g
const getModuleNameDependencies = async function(moduleName, basedir, state) {
if (isExcludedModule(moduleName)) {
return []
}
// Find the Node.js module directory path
const packagePath = await resolvePackage(moduleName, basedir)
if (packagePath === undefined) {
return []
}
const modulePath = dirname(packagePath)
if (state.modulePaths.includes(modulePath)) {
return []
}
state.modulePaths.push(modulePath)
const pkg = require(packagePath)
const [publishedFiles, sideFiles, depsPaths] = await Promise.all([
getPublishedFiles(modulePath),
getSideFiles(modulePath, moduleName),
getNestedModules(modulePath, state, pkg)
])
return [...publishedFiles, ...sideFiles, ...depsPaths]
}
const isExcludedModule = function(moduleName) {
return EXCLUDED_MODULES.includes(moduleName) || moduleName.startsWith('@types/')
}
const EXCLUDED_MODULES = ['aws-sdk']
// Some modules generate source files on `postinstall` that are not located
// inside the module's directory itself.
const getSideFiles = function(modulePath, moduleName) {
const sideFiles = SIDE_FILES[moduleName]
if (sideFiles === undefined) {
return []
}
return getPublishedFiles(`${modulePath}/${sideFiles}`)
}
const SIDE_FILES = {
'@prisma/client': '../../.prisma'
}
// We use all the files published by the Node.js except some that are not needed
const getPublishedFiles = async function(modulePath) {
const ignore = getIgnoredFiles(modulePath)
const publishedFiles = await pGlob(`${modulePath}/**`, {
ignore,
nodir: true,
absolute: true,
dot: true
})
return publishedFiles
}
const getIgnoredFiles = function(modulePath) {
return IGNORED_FILES.map(ignoreFile => `${modulePath}/${ignoreFile}`)
}
// To make the zip archive smaller, we remove those.
const IGNORED_FILES = [
'node_modules/**',
'.npmignore',
'package-lock.json',
'yarn.lock',
'*.log',
'*.lock',
'*~',
'*.map',
'*.ts',
'*.patch'
]
// Apply the Node.js module logic recursively on its own dependencies, using
// the `package.json` `dependencies`, `peerDependencies` and
// `optionalDependencies` keys
const getNestedModules = async function(modulePath, state, pkg) {
const dependencies = getNestedDependencies(pkg)
const depsPaths = await Promise.all(
dependencies.map(dependency => getModuleDependencies(dependency, modulePath, state, pkg))
)
return [].concat(...depsPaths)
}
const getNestedDependencies = function({ dependencies = {}, peerDependencies = {}, optionalDependencies = {} }) {
return [
...Object.keys(dependencies),
...Object.keys(peerDependencies).filter(shouldIncludePeerDependency),
...Object.keys(optionalDependencies)
]
}
// Workaround for https://github.com/netlify/zip-it-and-ship-it/issues/73
// TODO: remove this after adding proper modules exclusion as outlined in
// https://github.com/netlify/zip-it-and-ship-it/issues/68
const shouldIncludePeerDependency = function(name) {
return !EXCLUDED_PEER_DEPENDENCIES.includes(name)
}
const EXCLUDED_PEER_DEPENDENCIES = ['@prisma/cli', 'prisma2']
// Modules can be required conditionally (inside an `if` or `try`/`catch` block).
// When a `require()` statement is found but the module is not found, it is
// possible that that block either always evaluates to:
// - `false`: in which case, we should not bundle the dependency
// - `true`: in which case, we should report the dependency as missing
// Those conditional modules might be:
// - present in the `package.json` `dependencies`
// - present in the `package.json` `optionalDependencies`
// - present in the `package.json` `peerDependencies`
// - not present in the `package.json`, if the module author wants its users
// to explicitly install it as an optional dependency.
// The current implementation:
// - when parsing `require()` statements inside function files, always consider
// conditional modules to be included, i.e. report them if not found.
// This is because our current parsing logic does not know whether a
// `require()` is conditional or not.
// - when parsing module dependencies, ignore `require()` statements if not
// present in the `package.json` `*dependencies`. I.e. user must manually
// install them if the module is used.
// `optionalDependencies`:
// - are not reported when missing
// - are included in module dependencies
const handleModuleNotFound = function({ error, moduleName, packageJson }) {
if (error.code === 'MODULE_NOT_FOUND' && isOptionalModule(moduleName, packageJson)) {
return []
}
throw error
}
const isOptionalModule = function(
moduleName,
{ optionalDependencies = {}, peerDependenciesMeta = {}, peerDependencies = {} }
) {
return (
optionalDependencies[moduleName] !== undefined ||
(peerDependenciesMeta[moduleName] &&
peerDependenciesMeta[moduleName].optional &&
peerDependencies[moduleName] !== undefined)
)
}
module.exports = { listNodeFiles }