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
137 lines (125 loc) · 4.16 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
import { dirname, normalize } from 'path'
import type { FunctionConfig } from '../../../../config.js'
import { getPathWithExtension } from '../../../../utils/fs.js'
import { nonNullable } from '../../../../utils/non_nullable.js'
import { getBasePath } from '../../utils/base_path.js'
import type { BundleFunction } from '../index.js'
import { bundleJsFile } from './bundler.js'
import { getExternalAndIgnoredModulesFromSpecialCases } from './special_cases.js'
import { getSrcFiles } from './src_files.js'
const getFunctionBasePath = ({
basePathFromConfig,
mainFile,
repositoryRoot,
supportingSrcFiles,
}: {
basePathFromConfig?: string
mainFile: string
repositoryRoot?: string
supportingSrcFiles: string[]
}) => {
// If there is a base path defined in the config, we use that. To account for
// paths outside of `basePathFromConfig` but inside `repositoryRoot`, we use
// the common path prefix between the two.
if (basePathFromConfig !== undefined) {
return getBasePath([basePathFromConfig, repositoryRoot].filter(nonNullable))
}
// If not, the base path is the common path prefix between all the supporting
// files and the main file.
const dirnames = [...supportingSrcFiles, mainFile].map((filePath) => normalize(dirname(filePath)))
return getBasePath(dirnames)
}
// Convenience method for retrieving external and ignored modules from
// different places and merging them together.
const getExternalAndIgnoredModules = async ({ config, srcDir }: { config: FunctionConfig; srcDir: string }) => {
const { externalNodeModules: externalModulesFromConfig = [], ignoredNodeModules: ignoredModulesFromConfig = [] } =
config
const { externalModules: externalModulesFromSpecialCases, ignoredModules: ignoredModulesFromSpecialCases } =
await getExternalAndIgnoredModulesFromSpecialCases({ srcDir })
const externalModules = [...new Set([...externalModulesFromConfig, ...externalModulesFromSpecialCases])]
const ignoredModules = [...ignoredModulesFromConfig, ...ignoredModulesFromSpecialCases]
return { externalModules, ignoredModules }
}
const bundle: BundleFunction = async ({
basePath,
config = {},
extension,
featureFlags,
filename,
mainFile,
name,
pluginsModulesPath,
repositoryRoot,
runtime,
srcDir,
srcPath,
stat,
}) => {
const { externalModules, ignoredModules } = await getExternalAndIgnoredModules({ config, srcDir })
const {
additionalPaths,
bundlePaths,
cleanTempFiles,
inputs,
moduleFormat,
nativeNodeModules = {},
nodeModulesWithDynamicImports,
warnings,
} = await bundleJsFile({
additionalModulePaths: pluginsModulesPath ? [pluginsModulesPath] : [],
basePath,
config,
externalModules,
featureFlags,
ignoredModules,
name,
srcDir,
srcFile: mainFile,
})
const bundlerWarnings = warnings.length === 0 ? undefined : warnings
const { srcFiles, includedFiles } = await getSrcFiles({
basePath,
config: {
...config,
externalNodeModules: [...externalModules, ...Object.keys(nativeNodeModules)],
includedFiles: [...(config.includedFiles || []), ...additionalPaths],
includedFilesBasePath: config.includedFilesBasePath || basePath,
},
extension,
featureFlags,
filename,
mainFile,
name,
pluginsModulesPath,
runtime,
srcDir,
srcPath,
stat,
})
// We want to remove `mainFile` from `srcFiles` because it represents the
// path of the original, pre-bundling function file. We'll add the actual
// bundled file further below.
const supportingSrcFiles = srcFiles.filter((path) => path !== mainFile)
const normalizedMainFile = getPathWithExtension(mainFile, '.js')
const functionBasePath = getFunctionBasePath({
basePathFromConfig: basePath,
mainFile,
repositoryRoot,
supportingSrcFiles,
})
return {
aliases: bundlePaths,
cleanupFunction: cleanTempFiles,
basePath: functionBasePath,
bundlerWarnings,
includedFiles,
inputs,
mainFile: normalizedMainFile,
moduleFormat,
nativeNodeModules,
nodeModulesWithDynamicImports,
srcFiles: [...supportingSrcFiles, ...bundlePaths.keys()],
}
}
const bundler = { bundle, getSrcFiles }
export default bundler