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
/
index.js
161 lines (143 loc) · 4.51 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
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
const { dirname, basename, normalize } = require('path')
const findUp = require('find-up')
const { not: notJunk } = require('junk')
const precinct = require('precinct')
const { listImports } = require('../runtimes/node/list_imports')
const { getPackageJson } = require('./package_json')
const { resolvePathPreserveSymlinks } = require('./resolve')
const { getExternalAndIgnoredModulesFromSpecialCases } = require('./special_cases')
const {
getDependencyPathsForDependency,
getDependencyNamesAndPathsForDependencies,
getDependencyNamesAndPathsForDependency,
getNewCache,
} = require('./traverse')
const { getTreeFiles } = require('./tree_files')
const { shouldTreeShake } = require('./tree_shake')
const AUTO_PLUGINS_DIR = '.netlify/plugins/'
const getPluginsModulesPath = (srcDir) => findUp(`${AUTO_PLUGINS_DIR}node_modules`, { cwd: srcDir, type: 'directory' })
// 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 listFilesUsingLegacyBundler = async function ({
featureFlags,
srcPath,
mainFile,
srcDir,
stat,
pluginsModulesPath,
}) {
const [treeFiles, depFiles] = await Promise.all([
getTreeFiles(srcPath, stat),
getDependencies(mainFile, srcDir, pluginsModulesPath, featureFlags),
])
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()
return filteredFiles
}
// 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, pluginsModulesPath, featureFlags) {
const packageJson = await getPackageJson(srcDir)
const state = getNewCache()
try {
return await getFileDependencies({ featureFlags, path: mainFile, packageJson, pluginsModulesPath, state })
} catch (error) {
error.message = `In file "${mainFile}"\n${error.message}`
throw error
}
}
const getFileDependencies = async function ({
featureFlags,
path,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
}) {
if (state.localFiles.has(path)) {
return []
}
state.localFiles.add(path)
const basedir = dirname(path)
const dependencies = featureFlags.parseWithEsbuild
? await listImports({ path })
: precinct.paperwork(path, { includeCore: false })
const depsPaths = await Promise.all(
dependencies.filter(Boolean).map((dependency) =>
getImportDependencies({
dependency,
basedir,
featureFlags,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
}),
),
)
// TODO: switch to Array.flat() once we drop support for Node.js < 11.0.0
// eslint-disable-next-line unicorn/prefer-spread
return [].concat(...depsPaths)
}
const getImportDependencies = function ({
dependency,
basedir,
featureFlags,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
}) {
const shouldTreeShakeNext = treeShakeNext || isNextOnNetlify(dependency)
if (shouldTreeShake(dependency, shouldTreeShakeNext)) {
return getTreeShakedDependencies({
dependency,
basedir,
featureFlags,
packageJson,
pluginsModulesPath,
state,
treeShakeNext: shouldTreeShakeNext,
})
}
return getDependencyPathsForDependency({ dependency, basedir, state, packageJson, pluginsModulesPath })
}
const isNextOnNetlify = function (dependency) {
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,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
}) {
const path = await resolvePathPreserveSymlinks(dependency, [basedir, pluginsModulesPath].filter(Boolean))
const depsPath = await getFileDependencies({
featureFlags,
path,
packageJson,
pluginsModulesPath,
state,
treeShakeNext,
})
return [path, ...depsPath]
}
module.exports = {
getDependencyPathsForDependency,
getDependencyNamesAndPathsForDependencies,
getDependencyNamesAndPathsForDependency,
getExternalAndIgnoredModulesFromSpecialCases,
getPluginsModulesPath,
listFilesUsingLegacyBundler,
}