@netlify/zip-it-and-ship-it 4.23.4 → 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 +18 -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
@@ -0,0 +1,8 @@
1
+ /// <reference types="node" />
2
+ export function startZip(destPath: any): {
3
+ archive: any;
4
+ output: import("fs").WriteStream;
5
+ };
6
+ export function addZipFile(archive: any, file: any, name: any, stat: any): void;
7
+ export function addZipContent(archive: any, content: any, name: any): void;
8
+ export function endZip(archive: any, output: any): Promise<void>;
@@ -0,0 +1,39 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { createWriteStream } = require('fs');
11
+ const { promisify } = require('util');
12
+ const archiver = require('archiver');
13
+ const endOfStream = require('end-of-stream');
14
+ const pEndOfStream = promisify(endOfStream);
15
+ // Start zipping files
16
+ const startZip = function (destPath) {
17
+ const output = createWriteStream(destPath);
18
+ const archive = archiver('zip', { level: ZIP_LEVEL });
19
+ archive.pipe(output);
20
+ return { archive, output };
21
+ };
22
+ const ZIP_LEVEL = 9;
23
+ // Add new file to zip
24
+ const addZipFile = function (archive, file, name, stat) {
25
+ // Ensure sha256 stability regardless of mtime
26
+ archive.file(file, { name, mode: stat.mode, date: new Date(0), stats: stat });
27
+ };
28
+ // Add new file content to zip
29
+ const addZipContent = function (archive, content, name) {
30
+ archive.append(content, { name, date: new Date(0) });
31
+ };
32
+ // End zipping files
33
+ const endZip = function (archive, output) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ archive.finalize();
36
+ yield pEndOfStream(output);
37
+ });
38
+ };
39
+ module.exports = { startZip, addZipFile, addZipContent, endZip };
package/dist/bin.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/bin.js ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env node
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ const { exit } = require('process');
23
+ const yargs = require('yargs');
24
+ const zipIt = require('./main');
25
+ const { ARCHIVE_FORMAT_NONE, ARCHIVE_FORMAT_ZIP } = require('./utils/consts');
26
+ // CLI entry point
27
+ const runCli = function () {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const _a = parseArgs(), { destFolder, srcFolder } = _a, options = __rest(_a, ["destFolder", "srcFolder"]);
30
+ try {
31
+ const zipped = yield zipIt.zipFunctions(srcFolder, destFolder, options);
32
+ console.log(JSON.stringify(zipped, null, 2));
33
+ }
34
+ catch (error) {
35
+ console.error(error.toString());
36
+ exit(1);
37
+ }
38
+ });
39
+ };
40
+ const parseArgs = function () {
41
+ return yargs.command('* <srcFolder> <destFolder>').options(OPTIONS).usage(USAGE).strict().parse();
42
+ };
43
+ const OPTIONS = {
44
+ 'archive-format': {
45
+ string: true,
46
+ choices: [ARCHIVE_FORMAT_NONE, ARCHIVE_FORMAT_ZIP],
47
+ default: ARCHIVE_FORMAT_ZIP,
48
+ describe: 'Format of the archive created for each function',
49
+ },
50
+ config: {
51
+ default: {},
52
+ describe: 'An object matching glob-like expressions to objects containing configuration properties. Whenever a function name matches one of the expressions, it inherits the configuration properties',
53
+ },
54
+ manifest: {
55
+ string: true,
56
+ describe: 'If a manifest file is to be created, specifies its path',
57
+ },
58
+ 'parallel-limit': {
59
+ number: true,
60
+ describe: 'Maximum number of Functions to bundle at the same time',
61
+ },
62
+ };
63
+ const USAGE = `$0 [OPTIONS...] FUNCTIONS_DIRECTORY OUTPUT_DIRECTORY
64
+
65
+ Zip all function files inside FUNCTIONS_DIRECTORY so that they can be uploaded
66
+ to AWS Lambda.`;
67
+ runCli();
@@ -0,0 +1,4 @@
1
+ export function getConfigForFunction({ config, func }: {
2
+ config: any;
3
+ func: any;
4
+ }): any;
package/dist/config.js ADDED
@@ -0,0 +1,26 @@
1
+ const mergeOptions = require('merge-options');
2
+ const minimatch = require('minimatch');
3
+ const getConfigForFunction = ({ config, func }) => {
4
+ if (!config) {
5
+ return {};
6
+ }
7
+ // It's safe to mutate the array because it's local to this function.
8
+ const matches = Object.keys(config)
9
+ .filter((expression) => minimatch(func.name, expression))
10
+ .map((expression) => {
11
+ const wildcardCount = [...expression].filter((char) => char === '*').length;
12
+ // The weight increases with the number of hardcoded (i.e. non-wildcard)
13
+ // characters — e.g. "netlify" has a higher weight than "net*". We do a
14
+ // subtraction of 1 if there is at least one wildcard character, so that
15
+ // "netlify" has a higher weight than "netlify*".
16
+ const weight = expression.length - wildcardCount - (wildcardCount === 0 ? 0 : 1);
17
+ return {
18
+ expression,
19
+ weight,
20
+ };
21
+ })
22
+ .sort(({ weight: weightA }, { weight: weightB }) => weightA - weightB)
23
+ .map(({ expression }) => config[expression]);
24
+ return mergeOptions.apply({ concatArrays: true, ignoreUndefined: true }, matches);
25
+ };
26
+ module.exports = { getConfigForFunction };
@@ -0,0 +1,12 @@
1
+ export namespace FLAGS {
2
+ const buildGoSource: boolean;
3
+ const buildRustSource: boolean;
4
+ const defaultEsModulesToEsbuild: boolean;
5
+ const parseWithEsbuild: boolean;
6
+ }
7
+ export function getFlags(input?: {}, flags?: {
8
+ buildGoSource: boolean;
9
+ buildRustSource: boolean;
10
+ defaultEsModulesToEsbuild: boolean;
11
+ parseWithEsbuild: boolean;
12
+ }): {};
@@ -0,0 +1,10 @@
1
+ const { env } = require('process');
2
+ // List of supported flags and their default value.
3
+ const FLAGS = {
4
+ buildGoSource: Boolean(env.NETLIFY_EXPERIMENTAL_BUILD_GO_SOURCE),
5
+ buildRustSource: Boolean(env.NETLIFY_EXPERIMENTAL_BUILD_RUST_SOURCE),
6
+ defaultEsModulesToEsbuild: Boolean(env.NETLIFY_EXPERIMENTAL_DEFAULT_ES_MODULES_TO_ESBUILD),
7
+ parseWithEsbuild: false,
8
+ };
9
+ const getFlags = (input = {}, flags = FLAGS) => Object.entries(flags).reduce((result, [key, defaultValue]) => (Object.assign(Object.assign({}, result), { [key]: input[key] === undefined ? defaultValue : input[key] })), {});
10
+ module.exports = { FLAGS, getFlags };
package/dist/main.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { zipFunctions } from "./zip";
2
+ import { zipFunction } from "./zip";
3
+ export function listFunctions(relativeSrcFolders: any, { featureFlags: inputFeatureFlags }?: {
4
+ featureFlags: any;
5
+ }): Promise<{
6
+ name: any;
7
+ mainFile: any;
8
+ runtime: any;
9
+ extension: any;
10
+ }[]>;
11
+ export function listFunctionsFiles(relativeSrcFolders: any, { config, featureFlags: inputFeatureFlags }?: {
12
+ config: any;
13
+ featureFlags: any;
14
+ }): Promise<any[]>;
15
+ export { zipFunctions, zipFunction };
package/dist/main.js ADDED
@@ -0,0 +1,80 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { extname } = require('path');
11
+ require('./utils/polyfills');
12
+ const { getFlags } = require('./feature_flags');
13
+ const { getPluginsModulesPath } = require('./node_dependencies');
14
+ const { getFunctionsFromPaths } = require('./runtimes');
15
+ const { listFunctionsDirectories, resolveFunctionsDirectories } = require('./utils/fs');
16
+ const { zipFunction, zipFunctions } = require('./zip');
17
+ // List all Netlify Functions main entry files for a specific directory
18
+ const listFunctions = function (relativeSrcFolders, { featureFlags: inputFeatureFlags } = {}) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ const featureFlags = getFlags(inputFeatureFlags);
21
+ const srcFolders = resolveFunctionsDirectories(relativeSrcFolders);
22
+ const paths = yield listFunctionsDirectories(srcFolders);
23
+ const functions = yield getFunctionsFromPaths(paths, { featureFlags });
24
+ const listedFunctions = [...functions.values()].map(getListedFunction);
25
+ return listedFunctions;
26
+ });
27
+ };
28
+ // List all Netlify Functions files for a specific directory
29
+ const listFunctionsFiles = function (relativeSrcFolders, { config, featureFlags: inputFeatureFlags } = {}) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const featureFlags = getFlags(inputFeatureFlags);
32
+ const srcFolders = resolveFunctionsDirectories(relativeSrcFolders);
33
+ const paths = yield listFunctionsDirectories(srcFolders);
34
+ const [functions, pluginsModulesPath] = yield Promise.all([
35
+ getFunctionsFromPaths(paths, { config, featureFlags }),
36
+ getPluginsModulesPath(srcFolders[0]),
37
+ ]);
38
+ const listedFunctionsFiles = yield Promise.all([...functions.values()].map((func) => getListedFunctionFiles(func, { featureFlags, pluginsModulesPath })));
39
+ // TODO: switch to Array.flat() once we drop support for Node.js < 11.0.0
40
+ // eslint-disable-next-line unicorn/prefer-spread
41
+ return [].concat(...listedFunctionsFiles);
42
+ });
43
+ };
44
+ const getListedFunction = function ({ runtime, name, mainFile, extension }) {
45
+ return { name, mainFile, runtime: runtime.name, extension };
46
+ };
47
+ const getListedFunctionFiles = function ({ config, runtime, name, stat, mainFile, extension, srcPath, srcDir }, { featureFlags, pluginsModulesPath }) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const srcFiles = yield getSrcFiles({
50
+ featureFlags,
51
+ runtime,
52
+ stat,
53
+ mainFile,
54
+ extension,
55
+ srcPath,
56
+ srcDir,
57
+ pluginsModulesPath,
58
+ config,
59
+ });
60
+ return srcFiles.map((srcFile) => ({ srcFile, name, mainFile, runtime: runtime.name, extension: extname(srcFile) }));
61
+ });
62
+ };
63
+ const getSrcFiles = function ({ bundler, config, featureFlags, runtime, stat, mainFile, extension, srcPath, srcDir, pluginsModulesPath, }) {
64
+ const { getSrcFiles: getRuntimeSrcFiles } = runtime;
65
+ if (extension === '.zip' || typeof getRuntimeSrcFiles !== 'function') {
66
+ return [srcPath];
67
+ }
68
+ return getRuntimeSrcFiles({
69
+ bundler,
70
+ config,
71
+ extension,
72
+ featureFlags,
73
+ srcPath,
74
+ mainFile,
75
+ srcDir,
76
+ stat,
77
+ pluginsModulesPath,
78
+ });
79
+ };
80
+ module.exports = { zipFunctions, zipFunction, listFunctions, listFunctionsFiles };
@@ -0,0 +1,4 @@
1
+ export function createManifest({ functions, path }: {
2
+ functions: any;
3
+ path: any;
4
+ }): Promise<void>;
@@ -0,0 +1,25 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { resolve } = require('path');
11
+ const { arch, platform } = require('process');
12
+ const { writeFile } = require('./utils/fs');
13
+ const MANIFEST_VERSION = 1;
14
+ const createManifest = ({ functions, path }) => __awaiter(this, void 0, void 0, function* () {
15
+ const formattedFunctions = functions.map(formatFunction);
16
+ const payload = {
17
+ functions: formattedFunctions,
18
+ system: { arch, platform },
19
+ timestamp: Date.now(),
20
+ version: MANIFEST_VERSION,
21
+ };
22
+ yield writeFile(path, JSON.stringify(payload));
23
+ });
24
+ const formatFunction = ({ mainFile, name, path, runtime }) => ({ mainFile, name, path: resolve(path), runtime });
25
+ module.exports = { createManifest };
@@ -0,0 +1,15 @@
1
+ import { getDependencyPathsForDependency } from "./traverse";
2
+ import { getDependencyNamesAndPathsForDependencies } from "./traverse";
3
+ import { getDependencyNamesAndPathsForDependency } from "./traverse";
4
+ import { getExternalAndIgnoredModulesFromSpecialCases } from "./special_cases";
5
+ export function getPluginsModulesPath(srcDir: any): Promise<string>;
6
+ export function listFilesUsingLegacyBundler({ featureFlags, srcPath, mainFile, name, srcDir, stat, pluginsModulesPath, }: {
7
+ featureFlags: any;
8
+ srcPath: any;
9
+ mainFile: any;
10
+ name: any;
11
+ srcDir: any;
12
+ stat: any;
13
+ pluginsModulesPath: any;
14
+ }): Promise<string[]>;
15
+ export { getDependencyPathsForDependency, getDependencyNamesAndPathsForDependencies, getDependencyNamesAndPathsForDependency, getExternalAndIgnoredModulesFromSpecialCases };
@@ -0,0 +1,132 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const { dirname, basename, normalize } = require('path');
11
+ const findUp = require('find-up');
12
+ const { not: notJunk } = require('junk');
13
+ const precinct = require('precinct');
14
+ const { listImports } = require('../runtimes/node/list_imports');
15
+ const { getPackageJson } = require('./package_json');
16
+ const { resolvePathPreserveSymlinks } = require('./resolve');
17
+ const { getExternalAndIgnoredModulesFromSpecialCases } = require('./special_cases');
18
+ const { getDependencyPathsForDependency, getDependencyNamesAndPathsForDependencies, getDependencyNamesAndPathsForDependency, getNewCache, } = require('./traverse');
19
+ const { getTreeFiles } = require('./tree_files');
20
+ const { shouldTreeShake } = require('./tree_shake');
21
+ const AUTO_PLUGINS_DIR = '.netlify/plugins/';
22
+ const getPluginsModulesPath = (srcDir) => findUp(`${AUTO_PLUGINS_DIR}node_modules`, { cwd: srcDir, type: 'directory' });
23
+ // Retrieve the paths to the Node.js files to zip.
24
+ // We only include the files actually needed by the function because AWS Lambda
25
+ // has a size limit for the zipped file. It also makes cold starts faster.
26
+ const listFilesUsingLegacyBundler = function ({ featureFlags, srcPath, mainFile, name, srcDir, stat, pluginsModulesPath, }) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const [treeFiles, depFiles] = yield Promise.all([
29
+ getTreeFiles(srcPath, stat),
30
+ getDependencies({ featureFlags, functionName: name, mainFile, pluginsModulesPath, srcDir }),
31
+ ]);
32
+ const files = [...treeFiles, ...depFiles].map(normalize);
33
+ const uniqueFiles = [...new Set(files)];
34
+ // We sort so that the archive's checksum is deterministic.
35
+ // Mutating is fine since `Array.filter()` returns a shallow copy
36
+ const filteredFiles = uniqueFiles.filter(isNotJunk).sort();
37
+ return filteredFiles;
38
+ });
39
+ };
40
+ // Remove temporary files like *~, *.swp, etc.
41
+ const isNotJunk = function (file) {
42
+ return notJunk(basename(file));
43
+ };
44
+ // Retrieve all the files recursively required by a Node.js file
45
+ const getDependencies = function ({ featureFlags, functionName, mainFile, pluginsModulesPath, srcDir }) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const packageJson = yield getPackageJson(srcDir);
48
+ const state = getNewCache();
49
+ try {
50
+ return yield getFileDependencies({
51
+ featureFlags,
52
+ functionName,
53
+ path: mainFile,
54
+ packageJson,
55
+ pluginsModulesPath,
56
+ state,
57
+ });
58
+ }
59
+ catch (error) {
60
+ error.message = `In file "${mainFile}"\n${error.message}`;
61
+ throw error;
62
+ }
63
+ });
64
+ };
65
+ const getFileDependencies = function ({ featureFlags, functionName, path, packageJson, pluginsModulesPath, state, treeShakeNext, }) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ if (state.localFiles.has(path)) {
68
+ return [];
69
+ }
70
+ state.localFiles.add(path);
71
+ const basedir = dirname(path);
72
+ const dependencies = featureFlags.parseWithEsbuild
73
+ ? yield listImports({ functionName, path })
74
+ : precinct.paperwork(path, { includeCore: false });
75
+ const depsPaths = yield Promise.all(dependencies.filter(Boolean).map((dependency) => getImportDependencies({
76
+ dependency,
77
+ basedir,
78
+ featureFlags,
79
+ functionName,
80
+ packageJson,
81
+ pluginsModulesPath,
82
+ state,
83
+ treeShakeNext,
84
+ })));
85
+ // TODO: switch to Array.flat() once we drop support for Node.js < 11.0.0
86
+ // eslint-disable-next-line unicorn/prefer-spread
87
+ return [].concat(...depsPaths);
88
+ });
89
+ };
90
+ const getImportDependencies = function ({ dependency, basedir, featureFlags, functionName, packageJson, pluginsModulesPath, state, treeShakeNext, }) {
91
+ const shouldTreeShakeNext = treeShakeNext || isNextOnNetlify(dependency);
92
+ if (shouldTreeShake(dependency, shouldTreeShakeNext)) {
93
+ return getTreeShakedDependencies({
94
+ dependency,
95
+ basedir,
96
+ featureFlags,
97
+ functionName,
98
+ packageJson,
99
+ pluginsModulesPath,
100
+ state,
101
+ treeShakeNext: shouldTreeShakeNext,
102
+ });
103
+ }
104
+ return getDependencyPathsForDependency({ dependency, basedir, state, packageJson, pluginsModulesPath });
105
+ };
106
+ const isNextOnNetlify = function (dependency) {
107
+ return basename(dependency, '.js') === 'renderNextPage';
108
+ };
109
+ // When a file requires another one, we apply the top-level logic recursively
110
+ const getTreeShakedDependencies = function ({ dependency, basedir, featureFlags, functionName, packageJson, pluginsModulesPath, state, treeShakeNext, }) {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ const path = yield resolvePathPreserveSymlinks(dependency, [basedir, pluginsModulesPath].filter(Boolean));
113
+ const depsPath = yield getFileDependencies({
114
+ featureFlags,
115
+ functionName,
116
+ path,
117
+ packageJson,
118
+ pluginsModulesPath,
119
+ state,
120
+ treeShakeNext,
121
+ });
122
+ return [path, ...depsPath];
123
+ });
124
+ };
125
+ module.exports = {
126
+ getDependencyPathsForDependency,
127
+ getDependencyNamesAndPathsForDependencies,
128
+ getDependencyNamesAndPathsForDependency,
129
+ getExternalAndIgnoredModulesFromSpecialCases,
130
+ getPluginsModulesPath,
131
+ listFilesUsingLegacyBundler,
132
+ };
@@ -0,0 +1 @@
1
+ export function getModuleName(dependency: any): any;
@@ -0,0 +1,10 @@
1
+ const requirePackageName = require('require-package-name');
2
+ // Windows path normalization
3
+ const BACKSLASH_REGEXP = /\\/g;
4
+ // When doing require("moduleName/file/path"), only keep `moduleName`
5
+ const getModuleName = function (dependency) {
6
+ const dependencyA = dependency.replace(BACKSLASH_REGEXP, '/');
7
+ const moduleName = requirePackageName(dependencyA);
8
+ return moduleName;
9
+ };
10
+ module.exports = { getModuleName };
@@ -0,0 +1,10 @@
1
+ export function getNestedDependencies({ dependencies, peerDependencies, optionalDependencies }: {
2
+ dependencies?: {};
3
+ peerDependencies?: {};
4
+ optionalDependencies?: {};
5
+ }): string[];
6
+ export function handleModuleNotFound({ error, moduleName, packageJson }: {
7
+ error: any;
8
+ moduleName: any;
9
+ packageJson: any;
10
+ }): any[];
@@ -1,25 +1,21 @@
1
- const { valid: validVersion, validRange, satisfies, gte: greaterThanEqual, ltr: lessThanRange } = require('semver')
2
-
1
+ const { valid: validVersion, validRange, satisfies, gte: greaterThanEqual, ltr: lessThanRange } = require('semver');
3
2
  // Apply the Node.js module logic recursively on its own dependencies, using
4
3
  // the `package.json` `dependencies`, `peerDependencies` and
5
4
  // `optionalDependencies` keys
6
5
  const getNestedDependencies = function ({ dependencies = {}, peerDependencies = {}, optionalDependencies = {} }) {
7
- return [
8
- ...Object.keys(dependencies),
9
- ...Object.keys(peerDependencies).filter(shouldIncludePeerDependency),
10
- ...Object.keys(optionalDependencies),
11
- ]
12
- }
13
-
6
+ return [
7
+ ...Object.keys(dependencies),
8
+ ...Object.keys(peerDependencies).filter(shouldIncludePeerDependency),
9
+ ...Object.keys(optionalDependencies),
10
+ ];
11
+ };
14
12
  // Workaround for https://github.com/netlify/zip-it-and-ship-it/issues/73
15
13
  // TODO: remove this after adding proper modules exclusion as outlined in
16
14
  // https://github.com/netlify/zip-it-and-ship-it/issues/68
17
15
  const shouldIncludePeerDependency = function (name) {
18
- return !EXCLUDED_PEER_DEPENDENCIES.has(name)
19
- }
20
-
21
- const EXCLUDED_PEER_DEPENDENCIES = new Set(['@prisma/cli', 'prisma2', 'prisma'])
22
-
16
+ return !EXCLUDED_PEER_DEPENDENCIES.has(name);
17
+ };
18
+ const EXCLUDED_PEER_DEPENDENCIES = new Set(['@prisma/cli', 'prisma2', 'prisma']);
23
19
  // Modules can be required conditionally (inside an `if` or `try`/`catch` block).
24
20
  // When a `require()` statement is found but the module is not found, it is
25
21
  // possible that that block either always evaluates to:
@@ -43,52 +39,35 @@ const EXCLUDED_PEER_DEPENDENCIES = new Set(['@prisma/cli', 'prisma2', 'prisma'])
43
39
  // - are not reported when missing
44
40
  // - are included in module dependencies
45
41
  const handleModuleNotFound = function ({ error, moduleName, packageJson }) {
46
- if (
47
- error.code === 'MODULE_NOT_FOUND' &&
48
- (isOptionalModule(moduleName, packageJson) || isExternalCrittersModule(moduleName, packageJson))
49
- ) {
50
- return []
51
- }
52
-
53
- throw error
54
- }
55
-
56
- const isOptionalModule = function (
57
- moduleName,
58
- { optionalDependencies = {}, peerDependenciesMeta = {}, peerDependencies = {} },
59
- ) {
60
- return (
61
- optionalDependencies[moduleName] !== undefined ||
62
- (peerDependenciesMeta[moduleName] &&
63
- peerDependenciesMeta[moduleName].optional &&
64
- peerDependencies[moduleName] !== undefined)
65
- )
66
- }
67
-
68
- const MIN_NEXT_VERSION = '10.0.4'
69
-
70
- const satisfiesRange = (range) =>
71
- validRange(range) && (satisfies(MIN_NEXT_VERSION, range) || lessThanRange(MIN_NEXT_VERSION, range))
72
-
42
+ if (error.code === 'MODULE_NOT_FOUND' &&
43
+ (isOptionalModule(moduleName, packageJson) || isExternalCrittersModule(moduleName, packageJson))) {
44
+ return [];
45
+ }
46
+ throw error;
47
+ };
48
+ const isOptionalModule = function (moduleName, { optionalDependencies = {}, peerDependenciesMeta = {}, peerDependencies = {} }) {
49
+ return (optionalDependencies[moduleName] !== undefined ||
50
+ (peerDependenciesMeta[moduleName] &&
51
+ peerDependenciesMeta[moduleName].optional &&
52
+ peerDependencies[moduleName] !== undefined));
53
+ };
54
+ const MIN_NEXT_VERSION = '10.0.4';
55
+ const satisfiesRange = (range) => validRange(range) && (satisfies(MIN_NEXT_VERSION, range) || lessThanRange(MIN_NEXT_VERSION, range));
73
56
  // 'critters' is used only in Next.js >= 10.0.4 when enabling an experimental option and has to be installed manually
74
57
  // we ignore it if it's missing
75
58
  const isExternalCrittersModule = function (moduleName, { dependencies = {}, devDependencies = {} }) {
76
- if (moduleName !== 'critters') {
77
- return false
78
- }
79
- const nextVersion = dependencies.next || devDependencies.next
80
-
81
- if (nextVersion === 'latest') {
82
- return true
83
- }
84
-
85
- // can the declared Next.js version resolve to >=10.0.4 ?
86
- // test exact versions
87
- if (validVersion(nextVersion)) {
88
- return greaterThanEqual(nextVersion, MIN_NEXT_VERSION)
89
- }
90
-
91
- return satisfiesRange(nextVersion)
92
- }
93
-
94
- module.exports = { getNestedDependencies, handleModuleNotFound }
59
+ if (moduleName !== 'critters') {
60
+ return false;
61
+ }
62
+ const nextVersion = dependencies.next || devDependencies.next;
63
+ if (nextVersion === 'latest') {
64
+ return true;
65
+ }
66
+ // can the declared Next.js version resolve to >=10.0.4 ?
67
+ // test exact versions
68
+ if (validVersion(nextVersion)) {
69
+ return greaterThanEqual(nextVersion, MIN_NEXT_VERSION);
70
+ }
71
+ return satisfiesRange(nextVersion);
72
+ };
73
+ module.exports = { getNestedDependencies, handleModuleNotFound };
@@ -0,0 +1 @@
1
+ export function getPackageJson(srcDir: any): Promise<any>;
@@ -0,0 +1,29 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const pkgDir = require('pkg-dir');
11
+ // Retrieve the `package.json` of a specific project or module
12
+ const getPackageJson = function (srcDir) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ const packageRoot = yield pkgDir(srcDir);
15
+ if (packageRoot === undefined) {
16
+ return {};
17
+ }
18
+ const packageJsonPath = `${packageRoot}/package.json`;
19
+ try {
20
+ // The path depends on the user's build, i.e. must be dynamic
21
+ // eslint-disable-next-line import/no-dynamic-require, node/global-require
22
+ return require(packageJsonPath);
23
+ }
24
+ catch (error) {
25
+ throw new Error(`${packageJsonPath} is invalid JSON: ${error.message}`);
26
+ }
27
+ });
28
+ };
29
+ module.exports = { getPackageJson };
@@ -0,0 +1 @@
1
+ export function getPublishedFiles(modulePath: any): Promise<string[]>;