@netlify/zip-it-and-ship-it 4.23.3 → 4.23.5-setup-typescript-4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/dist/archive.d.ts +8 -0
  2. package/dist/archive.js +39 -0
  3. package/dist/bin.d.ts +2 -0
  4. package/dist/bin.js +67 -0
  5. package/dist/config.d.ts +4 -0
  6. package/dist/config.js +26 -0
  7. package/dist/feature_flags.d.ts +12 -0
  8. package/dist/feature_flags.js +10 -0
  9. package/dist/main.d.ts +15 -0
  10. package/dist/main.js +80 -0
  11. package/dist/manifest.d.ts +4 -0
  12. package/dist/manifest.js +25 -0
  13. package/dist/node_dependencies/index.d.ts +15 -0
  14. package/dist/node_dependencies/index.js +132 -0
  15. package/dist/node_dependencies/module.d.ts +1 -0
  16. package/dist/node_dependencies/module.js +10 -0
  17. package/dist/node_dependencies/nested.d.ts +10 -0
  18. package/{src → dist}/node_dependencies/nested.js +39 -60
  19. package/dist/node_dependencies/package_json.d.ts +1 -0
  20. package/dist/node_dependencies/package_json.js +29 -0
  21. package/dist/node_dependencies/published.d.ts +1 -0
  22. package/dist/node_dependencies/published.js +42 -0
  23. package/dist/node_dependencies/resolve.d.ts +2 -0
  24. package/dist/node_dependencies/resolve.js +114 -0
  25. package/dist/node_dependencies/side_files.d.ts +1 -0
  26. package/dist/node_dependencies/side_files.js +14 -0
  27. package/dist/node_dependencies/special_cases.d.ts +6 -0
  28. package/dist/node_dependencies/special_cases.js +44 -0
  29. package/dist/node_dependencies/traverse.d.ts +39 -0
  30. package/dist/node_dependencies/traverse.js +120 -0
  31. package/dist/node_dependencies/tree_files.d.ts +3 -0
  32. package/dist/node_dependencies/tree_files.js +32 -0
  33. package/dist/node_dependencies/tree_shake.d.ts +1 -0
  34. package/{src → dist}/node_dependencies/tree_shake.js +8 -12
  35. package/dist/runtimes/detect_runtime.d.ts +4 -0
  36. package/dist/runtimes/detect_runtime.js +27 -0
  37. package/dist/runtimes/go/builder.d.ts +12 -0
  38. package/dist/runtimes/go/builder.js +39 -0
  39. package/dist/runtimes/go/index.d.ts +18 -0
  40. package/dist/runtimes/go/index.js +78 -0
  41. package/dist/runtimes/index.d.ts +9 -0
  42. package/dist/runtimes/index.js +87 -0
  43. package/dist/runtimes/node/base_path.d.ts +1 -0
  44. package/dist/runtimes/node/base_path.js +8 -0
  45. package/dist/runtimes/node/bundler.d.ts +20 -0
  46. package/dist/runtimes/node/bundler.js +135 -0
  47. package/dist/runtimes/node/bundler_target.d.ts +1 -0
  48. package/dist/runtimes/node/bundler_target.js +16 -0
  49. package/dist/runtimes/node/detect_es_module.d.ts +3 -0
  50. package/dist/runtimes/node/detect_es_module.js +28 -0
  51. package/dist/runtimes/node/dynamic_imports/parser.d.ts +8 -0
  52. package/dist/runtimes/node/dynamic_imports/parser.js +135 -0
  53. package/dist/runtimes/node/dynamic_imports/plugin.d.ts +10 -0
  54. package/dist/runtimes/node/dynamic_imports/plugin.js +102 -0
  55. package/dist/runtimes/node/finder.d.ts +12 -0
  56. package/dist/runtimes/node/finder.js +82 -0
  57. package/dist/runtimes/node/index.d.ts +16 -0
  58. package/dist/runtimes/node/index.js +118 -0
  59. package/dist/runtimes/node/list_imports.d.ts +4 -0
  60. package/dist/runtimes/node/list_imports.js +68 -0
  61. package/dist/runtimes/node/native_modules/detector.d.ts +7 -0
  62. package/dist/runtimes/node/native_modules/detector.js +14 -0
  63. package/dist/runtimes/node/native_modules/plugin.d.ts +4 -0
  64. package/dist/runtimes/node/native_modules/plugin.js +65 -0
  65. package/dist/runtimes/node/src_files.d.ts +20 -0
  66. package/dist/runtimes/node/src_files.js +84 -0
  67. package/dist/runtimes/node/zip_esbuild.d.ts +22 -0
  68. package/dist/runtimes/node/zip_esbuild.js +95 -0
  69. package/dist/runtimes/node/zip_zisi.d.ts +20 -0
  70. package/dist/runtimes/node/zip_zisi.js +42 -0
  71. package/dist/runtimes/rust/builder.d.ts +9 -0
  72. package/dist/runtimes/rust/builder.js +100 -0
  73. package/dist/runtimes/rust/constants.d.ts +2 -0
  74. package/dist/runtimes/rust/constants.js +3 -0
  75. package/dist/runtimes/rust/index.d.ts +20 -0
  76. package/dist/runtimes/rust/index.js +101 -0
  77. package/dist/utils/consts.d.ts +8 -0
  78. package/dist/utils/consts.js +10 -0
  79. package/dist/utils/fs.d.ts +14 -0
  80. package/dist/utils/fs.js +86 -0
  81. package/dist/utils/polyfills.d.ts +1 -0
  82. package/{src → dist}/utils/polyfills.js +1 -1
  83. package/dist/utils/remove_falsy.d.ts +1 -0
  84. package/dist/utils/remove_falsy.js +9 -0
  85. package/dist/utils/shell.d.ts +2 -0
  86. package/dist/utils/shell.js +2 -0
  87. package/dist/zip.d.ts +15 -0
  88. package/dist/zip.js +116 -0
  89. package/dist/zip_binary.d.ts +7 -0
  90. package/dist/zip_binary.js +20 -0
  91. package/dist/zip_node.d.ts +4 -0
  92. package/dist/zip_node.js +143 -0
  93. package/package.json +19 -13
  94. package/src/archive.js +0 -36
  95. package/src/bin.js +0 -53
  96. package/src/config.js +0 -32
  97. package/src/feature_flags.js +0 -20
  98. package/src/main.js +0 -91
  99. package/src/manifest.js +0 -22
  100. package/src/node_dependencies/index.js +0 -161
  101. package/src/node_dependencies/module.js +0 -13
  102. package/src/node_dependencies/package_json.js +0 -21
  103. package/src/node_dependencies/published.js +0 -37
  104. package/src/node_dependencies/resolve.js +0 -108
  105. package/src/node_dependencies/side_files.js +0 -18
  106. package/src/node_dependencies/special_cases.js +0 -42
  107. package/src/node_dependencies/traverse.js +0 -147
  108. package/src/node_dependencies/tree_files.js +0 -20
  109. package/src/runtimes/detect_runtime.js +0 -20
  110. package/src/runtimes/go/builder.js +0 -37
  111. package/src/runtimes/go/index.js +0 -89
  112. package/src/runtimes/index.js +0 -96
  113. package/src/runtimes/node/base_path.js +0 -11
  114. package/src/runtimes/node/bundler.js +0 -151
  115. package/src/runtimes/node/bundler_target.js +0 -22
  116. package/src/runtimes/node/detect_es_module.js +0 -24
  117. package/src/runtimes/node/dynamic_imports/parser.js +0 -162
  118. package/src/runtimes/node/dynamic_imports/plugin.js +0 -116
  119. package/src/runtimes/node/finder.js +0 -93
  120. package/src/runtimes/node/index.js +0 -122
  121. package/src/runtimes/node/list_imports.js +0 -59
  122. package/src/runtimes/node/native_modules/detector.js +0 -21
  123. package/src/runtimes/node/native_modules/plugin.js +0 -77
  124. package/src/runtimes/node/src_files.js +0 -99
  125. package/src/runtimes/node/zip_esbuild.js +0 -119
  126. package/src/runtimes/node/zip_zisi.js +0 -50
  127. package/src/runtimes/rust/builder.js +0 -114
  128. package/src/runtimes/rust/constants.js +0 -4
  129. package/src/runtimes/rust/index.js +0 -112
  130. package/src/utils/consts.js +0 -10
  131. package/src/utils/fs.js +0 -88
  132. package/src/utils/remove_falsy.js +0 -12
  133. package/src/utils/shell.js +0 -3
  134. package/src/zip.js +0 -161
  135. package/src/zip_binary.js +0 -11
  136. package/src/zip_node.js +0 -179
@@ -1,151 +0,0 @@
1
- const { basename, dirname, extname, resolve, join } = require('path')
2
-
3
- const esbuild = require('@netlify/esbuild')
4
- const { tmpName } = require('tmp-promise')
5
-
6
- const { RUNTIME_JS } = require('../../utils/consts')
7
- const { getPathWithExtension, safeUnlink } = require('../../utils/fs')
8
-
9
- const { getBundlerTarget } = require('./bundler_target')
10
- const { getDynamicImportsPlugin } = require('./dynamic_imports/plugin')
11
- const { getNativeModulesPlugin } = require('./native_modules/plugin')
12
-
13
- // Maximum number of log messages that an esbuild instance will produce. This
14
- // limit is important to avoid out-of-memory errors due to too much data being
15
- // sent in the Go<>Node IPC channel.
16
- const ESBUILD_LOG_LIMIT = 10
17
-
18
- // When resolving imports with no extension (e.g. require('./foo')), these are
19
- // the extensions that esbuild will look for, in this order.
20
- const RESOLVE_EXTENSIONS = ['.js', '.jsx', '.mjs', '.cjs', '.ts', '.json']
21
-
22
- // eslint-disable-next-line max-statements
23
- const bundleJsFile = async function ({
24
- additionalModulePaths,
25
- basePath,
26
- config,
27
- externalModules = [],
28
- ignoredModules = [],
29
- name,
30
- srcDir,
31
- srcFile,
32
- }) {
33
- // We use a temporary directory as the destination for esbuild files to avoid
34
- // any naming conflicts with files generated by other functions.
35
- const targetDirectory = await tmpName()
36
-
37
- // De-duping external and ignored modules.
38
- const external = [...new Set([...externalModules, ...ignoredModules])]
39
-
40
- // To be populated by the native modules plugin with the names, versions and
41
- // paths of any Node modules with native dependencies.
42
- const nativeNodeModules = {}
43
-
44
- // To be populated by the dynamic imports plugin with the names of the Node
45
- // modules that include imports with dynamic expressions.
46
- const nodeModulesWithDynamicImports = new Set()
47
-
48
- // To be populated by the dynamic imports plugin with any paths (in a glob
49
- // format) to be included in the bundle in order to make a dynamic import
50
- // work at runtime.
51
- const dynamicImportsIncludedPaths = new Set()
52
-
53
- // The list of esbuild plugins to enable for this build.
54
- const plugins = [
55
- getNativeModulesPlugin(nativeNodeModules),
56
- getDynamicImportsPlugin({
57
- basePath,
58
- includedPaths: dynamicImportsIncludedPaths,
59
- moduleNames: nodeModulesWithDynamicImports,
60
- processImports: config.processDynamicNodeImports !== false,
61
- srcDir,
62
- }),
63
- ]
64
-
65
- // The version of ECMAScript to use as the build target. This will determine
66
- // whether certain features are transpiled down or left untransformed.
67
- const nodeTarget = getBundlerTarget(config.nodeVersion)
68
-
69
- // esbuild will format `sources` relative to the sourcemap file, which lives
70
- // in `destFolder`. We use `sourceRoot` to establish that relation. They are
71
- // URLs, not paths, so even on Windows they should use forward slashes.
72
- const sourceRoot = targetDirectory.replace(/\\/g, '/')
73
-
74
- try {
75
- const { metafile, warnings } = await esbuild.build({
76
- bundle: true,
77
- entryPoints: [srcFile],
78
- external,
79
- logLevel: 'warning',
80
- logLimit: ESBUILD_LOG_LIMIT,
81
- metafile: true,
82
- nodePaths: additionalModulePaths,
83
- outdir: targetDirectory,
84
- platform: 'node',
85
- plugins,
86
- resolveExtensions: RESOLVE_EXTENSIONS,
87
- sourcemap: Boolean(config.nodeSourcemap),
88
- sourceRoot,
89
- target: [nodeTarget],
90
- })
91
- const bundlePaths = getBundlePaths({
92
- destFolder: targetDirectory,
93
- outputs: metafile.outputs,
94
- srcFile,
95
- })
96
- const inputs = Object.keys(metafile.inputs).map((path) => resolve(path))
97
- const cleanTempFiles = getCleanupFunction([...bundlePaths.keys()])
98
-
99
- return {
100
- additionalPaths: [...dynamicImportsIncludedPaths],
101
- bundlePaths,
102
- cleanTempFiles,
103
- inputs,
104
- nativeNodeModules,
105
- nodeModulesWithDynamicImports: [...nodeModulesWithDynamicImports],
106
- warnings,
107
- }
108
- } catch (error) {
109
- error.customErrorInfo = { type: 'functionsBundling', location: { functionName: name, runtime: RUNTIME_JS } }
110
-
111
- throw error
112
- }
113
- }
114
-
115
- // Takes the `outputs` object produced by esbuild and returns a Map with the
116
- // absolute paths of the generated files as keys, and the paths that those
117
- // files should take in the generated bundle as values. This is compatible
118
- // with the `aliases` format used upstream.
119
- const getBundlePaths = ({ destFolder, outputs, srcFile }) => {
120
- const bundleFilename = `${basename(srcFile, extname(srcFile))}.js`
121
- const mainFileDirectory = dirname(srcFile)
122
- const bundlePaths = new Map()
123
-
124
- // The paths returned by esbuild are relative to the current directory, which
125
- // is a problem on Windows if the target directory is in a different drive
126
- // letter. To get around that, instead of using `path.resolve`, we compute
127
- // the absolute path by joining `destFolder` with the `basename` of each
128
- // entry of the `outputs` object.
129
- Object.entries(outputs).forEach(([path, output]) => {
130
- const filename = basename(path)
131
- const extension = extname(path)
132
- const absolutePath = join(destFolder, filename)
133
-
134
- if (output.entryPoint && basename(output.entryPoint) === basename(srcFile)) {
135
- // Ensuring the main file has a `.js` extension.
136
- const normalizedSrcFile = getPathWithExtension(srcFile, '.js')
137
-
138
- bundlePaths.set(absolutePath, normalizedSrcFile)
139
- } else if (extension === '.js' || filename === `${bundleFilename}.map`) {
140
- bundlePaths.set(absolutePath, join(mainFileDirectory, filename))
141
- }
142
- })
143
-
144
- return bundlePaths
145
- }
146
-
147
- const getCleanupFunction = (paths) => async () => {
148
- await Promise.all(paths.filter(Boolean).map(safeUnlink))
149
- }
150
-
151
- module.exports = { bundleJsFile, ESBUILD_LOG_LIMIT }
@@ -1,22 +0,0 @@
1
- const DEFAULT_VERSION = 'node12'
2
-
3
- const versionMap = {
4
- '8.x': 'node8',
5
- '10.x': 'node10',
6
- '12.x': 'node12',
7
- '14.x': 'node14',
8
- }
9
-
10
- const getBundlerTarget = (suppliedVersion) => {
11
- const version = normalizeVersion(suppliedVersion)
12
-
13
- return versionMap[version] || DEFAULT_VERSION
14
- }
15
-
16
- const normalizeVersion = (version) => {
17
- const match = version && version.match(/^nodejs(.*)$/)
18
-
19
- return match ? match[1] : version
20
- }
21
-
22
- module.exports = { getBundlerTarget }
@@ -1,24 +0,0 @@
1
- const { readFile } = require('fs')
2
- const { promisify } = require('util')
3
-
4
- const pReadFile = promisify(readFile)
5
-
6
- const { init, parse } = require('es-module-lexer')
7
-
8
- const detectEsModule = async ({ mainFile }) => {
9
- if (!mainFile) {
10
- return false
11
- }
12
-
13
- try {
14
- const [mainFileContents] = await Promise.all([pReadFile(mainFile, 'utf8'), init])
15
- const [imports, exports] = parse(mainFileContents)
16
-
17
- return imports.length !== 0 || exports.length !== 0
18
- } catch {
19
- // If there are any problems with init or parsing, assume it's not an ES module
20
- return false
21
- }
22
- }
23
-
24
- module.exports = { detectEsModule }
@@ -1,162 +0,0 @@
1
- const { join, relative, resolve } = require('path')
2
-
3
- const babel = require('@babel/parser')
4
-
5
- const GLOB_WILDCARD = '**'
6
-
7
- // Transforms an array of glob nodes into a glob string including an absolute
8
- // path.
9
- //
10
- // Example: ["./files/", "*", ".json"] => "/home/ntl/files/*.json"
11
- const getAbsoluteGlob = ({ basePath, globNodes, resolveDir }) => {
12
- if (!validateGlobNodes(globNodes)) {
13
- return null
14
- }
15
-
16
- const globStarIndex = globNodes.indexOf(GLOB_WILDCARD)
17
- const [staticPath, dynamicPath] =
18
- globStarIndex === -1
19
- ? [globNodes.join(''), '']
20
- : [globNodes.slice(0, globStarIndex).join(''), globNodes.slice(globStarIndex).join('')]
21
- const absoluteStaticPath = resolve(resolveDir, staticPath)
22
- const relativeStaticPath = relative(basePath, absoluteStaticPath)
23
- const absoluteGlob = join(relativeStaticPath, dynamicPath)
24
-
25
- return absoluteGlob
26
- }
27
-
28
- // Returns GLOB_WILDCARD for AST nodes that are accepted as part of a dynamic
29
- // expression and convertable to a wildcard character. This determines whether
30
- // we convert an expression or leave it alone. For example:
31
- //
32
- // - `./files/${someVariable}`: Convert `someVariable` to GLOB_WILDCARD
33
- // - `./files/${[some, array]}`: Don't convert expression
34
- //
35
- // The following AST nodes are converted to a wildcard:
36
- //
37
- // - CallExpression: `someFunction()`
38
- // - ConditionalExpression: `someCond ? someValue : otherValue`
39
- // - Identifier: `someVariable`
40
- // - MemberExpression: `someArray[index]` or `someObject.property`
41
- const getWildcardFromASTNode = (node) => {
42
- switch (node.type) {
43
- case 'CallExpression':
44
- case 'ConditionalExpression':
45
- case 'Identifier':
46
- case 'MemberExpression':
47
- return GLOB_WILDCARD
48
-
49
- default:
50
- throw new Error('Expression member not supported')
51
- }
52
- }
53
-
54
- // Tries to parse an expression, returning an object with:
55
- // - `includedPathsGlob`: A glob with the files to be included in the bundle
56
- // - `type`: The expression type (e.g. "require", "import")
57
- const parseExpression = ({ basePath, expression: rawExpression, resolveDir }) => {
58
- const { program } = babel.parse(rawExpression, {
59
- sourceType: 'module',
60
- })
61
- const [topLevelExpression] = program.body
62
- const { expression } = topLevelExpression
63
-
64
- if (expression.type === 'CallExpression' && expression.callee.name === 'require') {
65
- try {
66
- const includedPathsGlob = parseRequire({ basePath, expression, resolveDir })
67
-
68
- return {
69
- includedPathsGlob,
70
- type: expression.callee.name,
71
- }
72
- } catch (_) {
73
- // no-op
74
- }
75
- }
76
- }
77
-
78
- // Parses a `require()` and returns a glob string with an absolute path.
79
- const parseRequire = ({ basePath, expression, resolveDir }) => {
80
- const { arguments: args = [] } = expression
81
- const argType = args.length === 0 ? null : args[0].type
82
-
83
- if (argType === 'BinaryExpression') {
84
- try {
85
- const globNodes = parseBinaryExpression(args[0])
86
-
87
- return getAbsoluteGlob({ basePath, globNodes, resolveDir })
88
- } catch (_) {
89
- // no-op
90
- }
91
- }
92
-
93
- if (argType === 'TemplateLiteral') {
94
- const globNodes = parseTemplateLiteral(args[0])
95
-
96
- return getAbsoluteGlob({ basePath, globNodes, resolveDir })
97
- }
98
- }
99
-
100
- // Transforms a binary expression AST node into an array of glob nodes, where
101
- // static parts will be left untouched and identifiers will be replaced by
102
- // `GLOB_WILDCARD`.
103
- //
104
- // Example: './files/' + lang + '.json' => ["./files/", "**", ".json"]
105
- const parseBinaryExpression = (expression) => {
106
- const { left, operator, right } = expression
107
-
108
- if (operator !== '+') {
109
- throw new Error('Expression operator not supported')
110
- }
111
-
112
- const operands = [left, right].flatMap((operand) => {
113
- switch (operand.type) {
114
- case 'BinaryExpression':
115
- return parseBinaryExpression(operand)
116
-
117
- case 'StringLiteral':
118
- return operand.value
119
-
120
- default:
121
- return getWildcardFromASTNode(operand)
122
- }
123
- })
124
-
125
- return operands
126
- }
127
-
128
- // Transforms a template literal AST node into an array of glob nodes, where
129
- // static parts will be left untouched and identifiers will be replaced by
130
- // `GLOB_WILDCARD`.
131
- //
132
- // Example: `./files/${lang}.json` => ["./files/", "**", ".json"]
133
- const parseTemplateLiteral = (expression) => {
134
- const { expressions, quasis } = expression
135
- const parts = [...expressions, ...quasis].sort((partA, partB) => partA.start - partB.start)
136
- const globNodes = parts.map((part) => {
137
- switch (part.type) {
138
- case 'TemplateElement':
139
- return part.value.cooked
140
-
141
- default:
142
- return getWildcardFromASTNode(part)
143
- }
144
- })
145
-
146
- return globNodes.filter(Boolean)
147
- }
148
-
149
- // For our purposes, we consider a glob to be valid if all the nodes are
150
- // strings and the first node is static (i.e. not a wildcard character).
151
- const validateGlobNodes = (globNodes) => {
152
- if (!globNodes) {
153
- return false
154
- }
155
-
156
- const hasStrings = globNodes.every((node) => typeof node === 'string')
157
- const hasStaticHead = globNodes[0] !== GLOB_WILDCARD
158
-
159
- return hasStrings && hasStaticHead
160
- }
161
-
162
- module.exports = { parseExpression }
@@ -1,116 +0,0 @@
1
- const { basename, join, relative } = require('path')
2
-
3
- const findUp = require('find-up')
4
- const readPackageJson = require('read-package-json-fast')
5
- const unixify = require('unixify')
6
-
7
- const { parseExpression } = require('./parser')
8
-
9
- // This plugin intercepts module imports using dynamic expressions and does a
10
- // couple of things with them. First of all, it figures out whether the call
11
- // is being made from within a Node module, and if so it adds the name of the
12
- // module to `moduleNames`, so that we can warn the user of potential runtime
13
- // issues. Secondly, it parses the dynamic expressions and tries to include in
14
- // the bundle all the files that are possibly needed to make the import work at
15
- // runtime. This is not always possible, but we do our best.
16
- const getDynamicImportsPlugin = ({ basePath, includedPaths, moduleNames, processImports, srcDir }) => ({
17
- name: 'dynamic-imports',
18
- setup(build) {
19
- const cache = new Map()
20
-
21
- // eslint-disable-next-line complexity
22
- build.onDynamicImport({}, async (args) => {
23
- const { expression, resolveDir } = args
24
-
25
- // Don't attempt to parse the expression if the base path isn't defined,
26
- // since we won't be able to generate the globs for the included paths.
27
- // Also don't parse the expression if we're not interested in processing
28
- // the dynamic import expressions.
29
- if (basePath && processImports) {
30
- const { includedPathsGlob, type: expressionType } = parseExpression({ basePath, expression, resolveDir }) || {}
31
-
32
- if (includedPathsGlob) {
33
- // The parser has found a glob of paths that should be included in the
34
- // bundle to make this import work, so we add it to `includedPaths`.
35
- includedPaths.add(includedPathsGlob)
36
-
37
- // Create the shim that will handle the import at runtime.
38
- const contents = getShimContents({ expressionType, resolveDir, srcDir })
39
-
40
- // This is the only branch where we actually solve a dynamic import.
41
- // eslint-disable-next-line max-depth
42
- if (contents) {
43
- return {
44
- contents,
45
- }
46
- }
47
- }
48
- }
49
-
50
- // If we're here, it means we weren't able to solve the dynamic import.
51
- // We add it to the list of modules with dynamic imports, which allows
52
- // consumers like Netlify Build or CLI to advise users on how to proceed.
53
- await registerModuleWithDynamicImports({ cache, moduleNames, resolveDir, srcDir })
54
- })
55
- },
56
- })
57
-
58
- const getPackageName = async ({ resolveDir, srcDir }) => {
59
- const packageJsonPath = await findUp(
60
- async (directory) => {
61
- // We stop traversing if we're about to leave the boundaries of the
62
- // function directory or any node_modules directory.
63
- if (directory === srcDir || basename(directory) === 'node_modules') {
64
- return findUp.stop
65
- }
66
-
67
- const path = join(directory, 'package.json')
68
- const hasPackageJson = await findUp.exists(path)
69
-
70
- return hasPackageJson ? path : false
71
- },
72
- { cwd: resolveDir },
73
- )
74
-
75
- if (packageJsonPath !== undefined) {
76
- const { name } = await readPackageJson(packageJsonPath)
77
-
78
- return name
79
- }
80
- }
81
-
82
- const getPackageNameCached = ({ cache, resolveDir, srcDir }) => {
83
- if (!cache.has(resolveDir)) {
84
- cache.set(resolveDir, getPackageName({ resolveDir, srcDir }))
85
- }
86
-
87
- return cache.get(resolveDir)
88
- }
89
-
90
- const getShimContents = ({ expressionType, resolveDir, srcDir }) => {
91
- // The shim needs to modify the path of the import, since originally it was
92
- // relative to wherever the importer sat in the file tree (i.e. anywhere in
93
- // the user space or inside `node_modules`), but at runtime paths must be
94
- // relative to the main bundle file, since esbuild will flatten everything
95
- // into a single file.
96
- const relativeResolveDir = relative(srcDir, resolveDir)
97
- const requireArg = relativeResolveDir ? `\`./${unixify(relativeResolveDir)}/$\{args}\`` : 'args'
98
-
99
- if (expressionType === 'require') {
100
- return `module.exports = args => require(${requireArg})`
101
- }
102
- }
103
-
104
- const registerModuleWithDynamicImports = async ({ cache, moduleNames, resolveDir, srcDir }) => {
105
- try {
106
- const packageName = await getPackageNameCached({ cache, resolveDir, srcDir })
107
-
108
- if (packageName !== undefined) {
109
- moduleNames.add(packageName)
110
- }
111
- } catch (_) {
112
- // no-op
113
- }
114
- }
115
-
116
- module.exports = { getDynamicImportsPlugin }
@@ -1,93 +0,0 @@
1
- const { lstat } = require('fs')
2
- const { join, dirname, basename, extname } = require('path')
3
- const { promisify } = require('util')
4
-
5
- const locatePath = require('locate-path')
6
-
7
- const pLstat = promisify(lstat)
8
-
9
- // List of extensions that this runtime will look for, in order of precedence.
10
- const allowedExtensions = ['.js', '.zip', '.cjs', '.mjs', '.ts']
11
-
12
- // Sorting function, compatible with the callback of Array.sort, which sorts
13
- // entries by extension according to their position in `allowedExtensions`.
14
- // It places extensions with a higher precedence last in the array, so that
15
- // they "win" when the array is flattened into a Map.
16
- const sortByExtension = (fA, fB) => {
17
- const indexA = allowedExtensions.indexOf(fA.extension)
18
- const indexB = allowedExtensions.indexOf(fB.extension)
19
-
20
- return indexB - indexA
21
- }
22
-
23
- const findFunctionsInPaths = async function ({ paths }) {
24
- const functions = await Promise.all(paths.map(getFunctionAtPath))
25
-
26
- // It's fine to mutate the array since its scope is local to this function.
27
- const sortedFunctions = functions.filter(Boolean).sort((fA, fB) => {
28
- // We first sort the functions array to put directories first. This is so
29
- // that `{name}/{name}.js` takes precedence over `{name}.js`.
30
- const directorySort = Number(fA.stat.isDirectory()) - Number(fB.stat.isDirectory())
31
-
32
- if (directorySort !== 0) {
33
- return directorySort
34
- }
35
-
36
- // If the functions have the same name, we sort them according to the order
37
- // defined in `allowedExtensions`.
38
- if (fA.name === fB.name) {
39
- return sortByExtension(fA, fB)
40
- }
41
-
42
- return 0
43
- })
44
-
45
- return sortedFunctions
46
- }
47
-
48
- const getFunctionAtPath = async function (srcPath) {
49
- const filename = basename(srcPath)
50
-
51
- if (filename === 'node_modules') {
52
- return
53
- }
54
-
55
- const stat = await pLstat(srcPath)
56
- const mainFile = await getMainFile(srcPath, filename, stat)
57
-
58
- if (mainFile === undefined) {
59
- return
60
- }
61
-
62
- const extension = extname(srcPath)
63
- const srcDir = stat.isDirectory() ? srcPath : dirname(srcPath)
64
- const name = basename(srcPath, extname(srcPath))
65
-
66
- return { extension, mainFile, name, srcDir, srcPath, stat }
67
- }
68
-
69
- // Each `srcPath` can also be a directory with an `index` file or a file using
70
- // the same filename as its directory.
71
- const getMainFile = function (srcPath, filename, stat) {
72
- if (stat.isDirectory()) {
73
- return locatePath(
74
- [
75
- join(srcPath, `${filename}.js`),
76
- join(srcPath, 'index.js'),
77
- join(srcPath, `${filename}.mjs`),
78
- join(srcPath, 'index.mjs'),
79
- join(srcPath, `${filename}.ts`),
80
- join(srcPath, 'index.ts'),
81
- ],
82
- { type: 'file' },
83
- )
84
- }
85
-
86
- const extension = extname(srcPath)
87
-
88
- if (allowedExtensions.includes(extension)) {
89
- return srcPath
90
- }
91
- }
92
-
93
- module.exports = { findFunctionsInPaths, getFunctionAtPath }
@@ -1,122 +0,0 @@
1
- const { join } = require('path')
2
-
3
- const cpFile = require('cp-file')
4
-
5
- const { JS_BUNDLER_ESBUILD, JS_BUNDLER_ESBUILD_ZISI, JS_BUNDLER_ZISI, RUNTIME_JS } = require('../../utils/consts')
6
-
7
- const { detectEsModule } = require('./detect_es_module')
8
- const { findFunctionsInPaths } = require('./finder')
9
- const { getSrcFiles } = require('./src_files')
10
- const { zipEsbuild } = require('./zip_esbuild')
11
- const { zipZisi } = require('./zip_zisi')
12
-
13
- // We use ZISI as the default bundler, except for certain extensions, for which
14
- // esbuild is the only option.
15
- const getDefaultBundler = async ({ extension, mainFile, featureFlags = {} }) => {
16
- if (['.mjs', '.ts'].includes(extension)) {
17
- return JS_BUNDLER_ESBUILD
18
- }
19
-
20
- if (featureFlags.defaultEsModulesToEsbuild) {
21
- const isEsModule = await detectEsModule({ mainFile })
22
-
23
- if (isEsModule) {
24
- return JS_BUNDLER_ESBUILD
25
- }
26
- }
27
-
28
- return JS_BUNDLER_ZISI
29
- }
30
-
31
- // A proxy for the `getSrcFiles` function which adds a default `bundler` using
32
- // the `getDefaultBundler` function.
33
- const getSrcFilesWithBundler = async (parameters) => {
34
- const bundler = parameters.config.nodeBundler || (await getDefaultBundler({ extension: parameters.extension }))
35
-
36
- return getSrcFiles({ ...parameters, bundler })
37
- }
38
-
39
- const zipFunction = async function ({
40
- archiveFormat,
41
- basePath,
42
- config = {},
43
- destFolder,
44
- extension,
45
- featureFlags,
46
- filename,
47
- mainFile,
48
- name,
49
- pluginsModulesPath,
50
- srcDir,
51
- srcPath,
52
- stat,
53
- }) {
54
- const bundler = config.nodeBundler || (await getDefaultBundler({ extension, mainFile, featureFlags }))
55
- // If the file is a zip, we assume the function is bundled and ready to go.
56
- // We simply copy it to the destination path with no further processing.
57
- if (extension === '.zip') {
58
- const destPath = join(destFolder, filename)
59
- await cpFile(srcPath, destPath)
60
- return { config, path: destPath }
61
- }
62
-
63
- if (bundler === JS_BUNDLER_ZISI) {
64
- return zipZisi({
65
- archiveFormat,
66
- basePath,
67
- config,
68
- destFolder,
69
- extension,
70
- featureFlags,
71
- filename,
72
- mainFile,
73
- pluginsModulesPath,
74
- srcDir,
75
- srcPath,
76
- stat,
77
- })
78
- }
79
-
80
- return zipEsbuild({
81
- archiveFormat,
82
- basePath,
83
- config,
84
- destFolder,
85
- extension,
86
- filename,
87
- mainFile,
88
- name,
89
- pluginsModulesPath,
90
- srcDir,
91
- srcPath,
92
- stat,
93
- })
94
- }
95
-
96
- const zipWithFunctionWithFallback = async ({ config = {}, ...parameters }) => {
97
- // If a specific JS bundler version is specified, we'll use it.
98
- if (config.nodeBundler !== JS_BUNDLER_ESBUILD_ZISI) {
99
- return zipFunction({ ...parameters, config })
100
- }
101
-
102
- // Otherwise, we'll try to bundle with esbuild and, if that fails, fallback
103
- // to zisi.
104
- try {
105
- return await zipFunction({ ...parameters, config: { ...config, nodeBundler: JS_BUNDLER_ESBUILD } })
106
- } catch (esbuildError) {
107
- try {
108
- const data = await zipFunction({ ...parameters, config: { ...config, nodeBundler: JS_BUNDLER_ZISI } })
109
-
110
- return { ...data, bundlerErrors: esbuildError.errors }
111
- } catch (zisiError) {
112
- throw esbuildError
113
- }
114
- }
115
- }
116
-
117
- module.exports = {
118
- findFunctionsInPaths,
119
- getSrcFiles: getSrcFilesWithBundler,
120
- name: RUNTIME_JS,
121
- zipFunction: zipWithFunctionWithFallback,
122
- }