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.ts
166 lines (146 loc) · 4.76 KB
/
index.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
import { basename, dirname, join, normalize, resolve } from 'path'
import { nodeFileTrace } from '@vercel/nft'
import resolveDependency from '@vercel/nft/out/resolve-dependency.js'
import type { FunctionConfig } from '../../../../config.js'
import { FeatureFlags } from '../../../../feature_flags.js'
import { cachedReadFile, FsCache } from '../../../../utils/fs.js'
import { minimatch } from '../../../../utils/matching.js'
import { getBasePath } from '../../utils/base_path.js'
import { filterExcludedPaths, getPathsOfIncludedFiles } from '../../utils/included_files.js'
import type { GetSrcFilesFunction, BundleFunction } from '../index.js'
import { processESM } from './es_modules.js'
// Paths that will be excluded from the tracing process.
const ignore = ['node_modules/aws-sdk/**']
const appearsToBeModuleName = (name: string) => !name.startsWith('.')
const bundle: BundleFunction = async ({
basePath,
config,
featureFlags,
mainFile,
name,
pluginsModulesPath,
repositoryRoot = basePath,
}) => {
const { includedFiles = [], includedFilesBasePath } = config
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath || basePath,
)
const {
moduleFormat,
paths: dependencyPaths,
rewrites,
} = await traceFilesAndTranspile({
basePath: repositoryRoot,
config,
featureFlags,
mainFile,
pluginsModulesPath,
name,
})
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)
const filteredIncludedPaths = [...filterExcludedPaths(dependencyPaths, excludePatterns), ...includedPaths]
const dirnames = filteredIncludedPaths.map((filePath) => normalize(dirname(filePath))).sort()
// Sorting the array to make the checksum deterministic.
const srcFiles = [...filteredIncludedPaths].sort()
return {
basePath: getBasePath(dirnames),
includedFiles: includedPaths,
inputs: dependencyPaths,
mainFile,
moduleFormat,
rewrites,
srcFiles,
}
}
const ignoreFunction = (path: string) => {
const shouldIgnore = ignore.some((expression) => minimatch(path, expression))
return shouldIgnore
}
const traceFilesAndTranspile = async function ({
basePath,
config,
featureFlags,
mainFile,
pluginsModulesPath,
name,
}: {
basePath?: string
config: FunctionConfig
featureFlags: FeatureFlags
mainFile: string
pluginsModulesPath?: string
name: string
}) {
const fsCache: FsCache = {}
const {
fileList: dependencyPaths,
esmFileList,
reasons,
} = await nodeFileTrace([mainFile], {
base: basePath,
ignore: ignoreFunction,
readFile: async (path: string) => {
try {
const source = (await cachedReadFile(fsCache, path, 'utf8')) as string
return source
} catch (error) {
if (error.code === 'ENOENT' || error.code === 'EISDIR') {
return null
}
throw error
}
},
resolve: async (specifier, parent, ...args) => {
try {
return await resolveDependency(specifier, parent, ...args)
} catch (error) {
// If we get a `MODULE_NOT_FOUND` error for what appears to be a module
// name, we try to resolve it a second time using `pluginsModulesPath`
// as the base directory.
if (error.code === 'MODULE_NOT_FOUND' && pluginsModulesPath && appearsToBeModuleName(specifier)) {
const newParent = join(pluginsModulesPath, basename(parent))
return await resolveDependency(specifier, newParent, ...args)
}
throw error
}
},
})
const normalizedDependencyPaths = [...dependencyPaths].map((path) =>
basePath ? resolve(basePath, path) : resolve(path),
)
const { moduleFormat, rewrites } = await processESM({
basePath,
config,
esmPaths: esmFileList,
featureFlags,
fsCache,
mainFile,
reasons,
name,
})
return {
moduleFormat,
paths: normalizedDependencyPaths,
rewrites,
}
}
const getSrcFiles: GetSrcFilesFunction = async function ({ basePath, config, mainFile }) {
const { includedFiles = [], includedFilesBasePath } = config
const { excludePatterns, paths: includedFilePaths } = await getPathsOfIncludedFiles(
includedFiles,
includedFilesBasePath,
)
const { fileList: dependencyPaths } = await nodeFileTrace([mainFile], { base: basePath, ignore: ignoreFunction })
const normalizedDependencyPaths = [...dependencyPaths].map((path) =>
basePath ? resolve(basePath, path) : resolve(path),
)
const srcFiles = filterExcludedPaths(normalizedDependencyPaths, excludePatterns)
const includedPaths = filterExcludedPaths(includedFilePaths, excludePatterns)
return {
srcFiles: [...srcFiles, ...includedPaths],
includedFiles: includedPaths,
}
}
const bundler = { bundle, getSrcFiles }
export default bundler