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
/
zip_esbuild.js
120 lines (108 loc) · 3.78 KB
/
zip_esbuild.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
const { dirname, normalize } = require('path')
const { getExternalAndIgnoredModulesFromSpecialCases } = require('../../node_dependencies')
const { JS_BUNDLER_ESBUILD } = require('../../utils/consts')
const { getPathWithExtension } = require('../../utils/fs')
const { zipNodeJs } = require('../../zip_node')
const { getBasePath } = require('./base_path')
const { bundleJsFile } = require('./bundler')
const { getSrcFilesAndExternalModules } = require('./src_files')
const getFunctionBasePath = ({ basePathFromConfig, mainFile, supportingSrcFiles }) => {
// If there is a base path defined in the config, we use that.
if (basePathFromConfig !== undefined) {
return basePathFromConfig
}
// 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 }) => {
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 zipEsbuild = async ({
archiveFormat,
basePath,
config = {},
destFolder,
extension,
filename,
mainFile,
name,
pluginsModulesPath,
srcDir,
srcPath,
stat,
}) => {
const { externalModules, ignoredModules } = await getExternalAndIgnoredModules({ config, srcDir })
const {
additionalPaths,
bundlePaths,
cleanTempFiles,
inputs,
nativeNodeModules = {},
nodeModulesWithDynamicImports,
warnings,
} = await bundleJsFile({
additionalModulePaths: pluginsModulesPath ? [pluginsModulesPath] : [],
basePath,
config,
destFilename: filename,
externalModules,
ignoredModules,
name,
srcDir,
srcFile: mainFile,
})
const bundlerWarnings = warnings.length === 0 ? undefined : warnings
const { paths: srcFiles } = await getSrcFilesAndExternalModules({
externalNodeModules: [...externalModules, ...Object.keys(nativeNodeModules)],
bundler: JS_BUNDLER_ESBUILD,
includedFiles: [...(config.includedFiles || []), ...additionalPaths],
includedFilesBasePath: config.includedFilesBasePath || basePath,
mainFile,
name,
srcPath,
srcDir,
pluginsModulesPath,
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, supportingSrcFiles })
try {
const path = await zipNodeJs({
aliases: bundlePaths,
archiveFormat,
basePath: functionBasePath,
destFolder,
extension,
filename,
mainFile: normalizedMainFile,
pluginsModulesPath,
srcFiles: [...supportingSrcFiles, ...bundlePaths.keys()],
})
return {
bundler: JS_BUNDLER_ESBUILD,
bundlerWarnings,
config,
inputs,
nativeNodeModules,
nodeModulesWithDynamicImports,
path,
}
} finally {
await cleanTempFiles()
}
}
module.exports = { zipEsbuild }