@netlify/zip-it-and-ship-it 4.29.4 → 5.1.0

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 (36) hide show
  1. package/dist/feature_flags.js +1 -0
  2. package/dist/main.d.ts +0 -1
  3. package/dist/main.js +0 -1
  4. package/dist/manifest.js +2 -2
  5. package/dist/runtimes/node/bundlers/esbuild/bundler.d.ts +1 -0
  6. package/dist/runtimes/node/bundlers/esbuild/bundler.js +4 -0
  7. package/dist/runtimes/node/bundlers/esbuild/index.js +2 -1
  8. package/dist/runtimes/node/bundlers/esbuild/plugin_in_source_config.d.ts +5 -0
  9. package/dist/runtimes/node/bundlers/esbuild/plugin_in_source_config.js +19 -0
  10. package/dist/runtimes/node/bundlers/index.d.ts +1 -0
  11. package/dist/runtimes/node/bundlers/nft/es_modules.js +10 -9
  12. package/dist/runtimes/node/bundlers/nft/transpile.js +1 -0
  13. package/dist/runtimes/node/bundlers/zisi/traverse.js +1 -3
  14. package/dist/runtimes/node/in_source_config/index.d.ts +14 -0
  15. package/dist/runtimes/node/in_source_config/index.js +54 -0
  16. package/dist/runtimes/node/in_source_config/properties/schedule.d.ts +6 -0
  17. package/dist/runtimes/node/in_source_config/properties/schedule.js +12 -0
  18. package/dist/runtimes/node/index.js +7 -1
  19. package/dist/runtimes/node/parser/exports.d.ts +4 -0
  20. package/dist/runtimes/node/parser/exports.js +61 -0
  21. package/dist/runtimes/node/parser/helpers.d.ts +7 -0
  22. package/dist/runtimes/node/parser/helpers.js +38 -0
  23. package/dist/runtimes/node/parser/imports.d.ts +6 -0
  24. package/dist/runtimes/node/parser/imports.js +60 -0
  25. package/dist/runtimes/node/parser/index.d.ts +2 -1
  26. package/dist/runtimes/node/parser/index.js +36 -1
  27. package/dist/runtimes/node/utils/zip.js +2 -1
  28. package/dist/runtimes/runtime.d.ts +2 -0
  29. package/dist/utils/format_result.d.ts +1 -0
  30. package/dist/utils/format_result.js +2 -1
  31. package/dist/utils/fs.d.ts +2 -1
  32. package/dist/utils/fs.js +13 -1
  33. package/dist/zip.d.ts +4 -1
  34. package/package.json +5 -4
  35. package/dist/utils/polyfills.d.ts +0 -1
  36. package/dist/utils/polyfills.js +0 -6
@@ -7,6 +7,7 @@ const FLAGS = {
7
7
  buildRustSource: Boolean(process_1.env.NETLIFY_EXPERIMENTAL_BUILD_RUST_SOURCE),
8
8
  defaultEsModulesToEsbuild: Boolean(process_1.env.NETLIFY_EXPERIMENTAL_DEFAULT_ES_MODULES_TO_ESBUILD),
9
9
  nftTranspile: false,
10
+ parseISC: false,
10
11
  parseWithEsbuild: false,
11
12
  traceWithNft: false,
12
13
  };
package/dist/main.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import './utils/polyfills';
2
1
  import { Config } from './config';
3
2
  import { FeatureFlags } from './feature_flags';
4
3
  import { RuntimeName } from './runtimes/runtime';
package/dist/main.js CHANGED
@@ -22,7 +22,6 @@ var __rest = (this && this.__rest) || function (s, e) {
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.zipFunctions = exports.zipFunction = exports.listFunctionsFiles = exports.listFunctions = void 0;
24
24
  const path_1 = require("path");
25
- require("./utils/polyfills");
26
25
  const feature_flags_1 = require("./feature_flags");
27
26
  const runtimes_1 = require("./runtimes");
28
27
  const fs_1 = require("./utils/fs");
package/dist/manifest.js CHANGED
@@ -25,11 +25,11 @@ const createManifest = ({ functions, path }) => __awaiter(void 0, void 0, void 0
25
25
  yield (0, fs_1.writeFile)(path, JSON.stringify(payload));
26
26
  });
27
27
  exports.createManifest = createManifest;
28
- const formatFunctionForManifest = ({ config, mainFile, name, path, runtime }) => ({
28
+ const formatFunctionForManifest = ({ mainFile, name, path, runtime, schedule }) => ({
29
29
  mainFile,
30
30
  name,
31
31
  path: (0, path_1.resolve)(path),
32
32
  runtime,
33
- schedule: config.schedule,
33
+ schedule,
34
34
  });
35
35
  //# sourceMappingURL=manifest.js.map
@@ -14,6 +14,7 @@ declare const bundleJsFile: ({ additionalModulePaths, basePath, config, external
14
14
  bundlePaths: Map<string, string>;
15
15
  cleanTempFiles: () => Promise<void>;
16
16
  inputs: string[];
17
+ iscImporterPaths: Set<string>;
17
18
  nativeNodeModules: {};
18
19
  nodeModulesWithDynamicImports: string[];
19
20
  warnings: import("@netlify/esbuild").Message[];
@@ -16,6 +16,7 @@ const tmp_promise_1 = require("tmp-promise");
16
16
  const fs_1 = require("../../../../utils/fs");
17
17
  const bundler_target_1 = require("./bundler_target");
18
18
  const plugin_dynamic_imports_1 = require("./plugin_dynamic_imports");
19
+ const plugin_in_source_config_1 = require("./plugin_in_source_config");
19
20
  const plugin_native_modules_1 = require("./plugin_native_modules");
20
21
  const plugin_node_builtin_1 = require("./plugin_node_builtin");
21
22
  // Maximum number of log messages that an esbuild instance will produce. This
@@ -44,6 +45,7 @@ const bundleJsFile = function ({ additionalModulePaths, basePath, config, extern
44
45
  // format) to be included in the bundle in order to make a dynamic import
45
46
  // work at runtime.
46
47
  const dynamicImportsIncludedPaths = new Set();
48
+ const iscImporterPaths = new Set();
47
49
  // The list of esbuild plugins to enable for this build.
48
50
  const plugins = [
49
51
  (0, plugin_node_builtin_1.getNodeBuiltinPlugin)(),
@@ -55,6 +57,7 @@ const bundleJsFile = function ({ additionalModulePaths, basePath, config, extern
55
57
  processImports: config.processDynamicNodeImports !== false,
56
58
  srcDir,
57
59
  }),
60
+ (0, plugin_in_source_config_1.getISCImportFinderPlugin)({ importerPaths: iscImporterPaths }),
58
61
  ];
59
62
  // The version of ECMAScript to use as the build target. This will determine
60
63
  // whether certain features are transpiled down or left untransformed.
@@ -92,6 +95,7 @@ const bundleJsFile = function ({ additionalModulePaths, basePath, config, extern
92
95
  bundlePaths,
93
96
  cleanTempFiles,
94
97
  inputs,
98
+ iscImporterPaths,
95
99
  nativeNodeModules,
96
100
  nodeModulesWithDynamicImports: [...nodeModulesWithDynamicImports],
97
101
  warnings,
@@ -39,7 +39,7 @@ const getExternalAndIgnoredModules = ({ config, srcDir }) => __awaiter(void 0, v
39
39
  });
40
40
  const bundle = ({ basePath, config = {}, extension, featureFlags, filename, mainFile, name, pluginsModulesPath, repositoryRoot, runtime, srcDir, srcPath, stat, }) => __awaiter(void 0, void 0, void 0, function* () {
41
41
  const { externalModules, ignoredModules } = yield getExternalAndIgnoredModules({ config, srcDir });
42
- const { additionalPaths, bundlePaths, cleanTempFiles, inputs, nativeNodeModules = {}, nodeModulesWithDynamicImports, warnings, } = yield (0, bundler_1.bundleJsFile)({
42
+ const { additionalPaths, bundlePaths, cleanTempFiles, inputs, iscImporterPaths, nativeNodeModules = {}, nodeModulesWithDynamicImports, warnings, } = yield (0, bundler_1.bundleJsFile)({
43
43
  additionalModulePaths: pluginsModulesPath ? [pluginsModulesPath] : [],
44
44
  basePath,
45
45
  config,
@@ -81,6 +81,7 @@ const bundle = ({ basePath, config = {}, extension, featureFlags, filename, main
81
81
  basePath: functionBasePath,
82
82
  bundlerWarnings,
83
83
  inputs,
84
+ iscImporterPaths,
84
85
  mainFile: normalizedMainFile,
85
86
  nativeNodeModules,
86
87
  nodeModulesWithDynamicImports,
@@ -0,0 +1,5 @@
1
+ import type { Plugin } from '@netlify/esbuild';
2
+ declare const getISCImportFinderPlugin: ({ importerPaths }: {
3
+ importerPaths: Set<string>;
4
+ }) => Plugin;
5
+ export { getISCImportFinderPlugin };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getISCImportFinderPlugin = void 0;
4
+ const in_source_config_1 = require("../../in_source_config");
5
+ // An esbuild plugin that collects the paths of any files that import the
6
+ // in-source configuration module.
7
+ const getISCImportFinderPlugin = ({ importerPaths }) => ({
8
+ name: 'find-isc-imports',
9
+ setup(build) {
10
+ const filter = new RegExp(`^${in_source_config_1.IN_SOURCE_CONFIG_MODULE}$`);
11
+ build.onResolve({ filter }, (args) => {
12
+ importerPaths.add(args.importer);
13
+ // eslint-disable-next-line unicorn/no-useless-undefined
14
+ return undefined;
15
+ });
16
+ },
17
+ });
18
+ exports.getISCImportFinderPlugin = getISCImportFinderPlugin;
19
+ //# sourceMappingURL=plugin_in_source_config.js.map
@@ -19,6 +19,7 @@ declare type BundleFunction = (args: {
19
19
  bundlerWarnings?: BundlerWarning[];
20
20
  cleanupFunction?: CleanupFunction;
21
21
  inputs: string[];
22
+ iscImporterPaths?: Set<string>;
22
23
  mainFile: string;
23
24
  nativeNodeModules?: NativeNodeModules;
24
25
  nodeModulesWithDynamicImports?: string[];
@@ -27,7 +27,7 @@ const patchESMPackage = (path, fsCache) => __awaiter(void 0, void 0, void 0, fun
27
27
  const patchedPackageJson = Object.assign(Object.assign({}, packageJson), { type: 'commonjs' });
28
28
  return JSON.stringify(patchedPackageJson);
29
29
  });
30
- const shouldTranspile = (path, cache, reasons) => {
30
+ const shouldTranspile = (path, cache, esmPaths, reasons) => {
31
31
  if (cache.has(path)) {
32
32
  return cache.get(path);
33
33
  }
@@ -39,32 +39,33 @@ const shouldTranspile = (path, cache, reasons) => {
39
39
  return false;
40
40
  }
41
41
  const { parents } = reason;
42
- // If the path doesn't have any parents, it's safe to transpile.
42
+ // If the path is an entrypoint, we transpile it only if it's an ESM file.
43
43
  if (parents.size === 0) {
44
- cache.set(path, true);
45
- return true;
44
+ const isESM = esmPaths.has(path);
45
+ cache.set(path, isESM);
46
+ return isESM;
46
47
  }
47
48
  // The path should be transpiled if every parent will also be transpiled, or
48
49
  // if there is no parent.
49
- const shouldTranspilePath = [...parents].every((parentPath) => shouldTranspile(parentPath, cache, reasons));
50
+ const shouldTranspilePath = [...parents].every((parentPath) => shouldTranspile(parentPath, cache, esmPaths, reasons));
50
51
  cache.set(path, shouldTranspilePath);
51
52
  return shouldTranspilePath;
52
53
  };
53
54
  const transpileESM = ({ basePath, config, esmPaths, fsCache, reasons, }) => __awaiter(void 0, void 0, void 0, function* () {
54
55
  const cache = new Map();
55
- const paths = [...esmPaths].filter((path) => shouldTranspile(path, cache, reasons));
56
- const pathsSet = new Set(paths);
56
+ const pathsToTranspile = [...esmPaths].filter((path) => shouldTranspile(path, cache, esmPaths, reasons));
57
+ const pathsToTranspileSet = new Set(pathsToTranspile);
57
58
  const packageJsonPaths = [...reasons.entries()]
58
59
  .filter(([path, reason]) => {
59
60
  if ((0, path_1.basename)(path) !== 'package.json') {
60
61
  return false;
61
62
  }
62
- const needsPatch = [...reason.parents].some((parentPath) => pathsSet.has(parentPath));
63
+ const needsPatch = [...reason.parents].some((parentPath) => pathsToTranspileSet.has(parentPath));
63
64
  return needsPatch;
64
65
  })
65
66
  .map(([path]) => (basePath ? (0, path_1.resolve)(basePath, path) : (0, path_1.resolve)(path)));
66
67
  const rewrites = yield getPatchedESMPackages(packageJsonPaths, fsCache);
67
- yield Promise.all(paths.map((path) => __awaiter(void 0, void 0, void 0, function* () {
68
+ yield Promise.all(pathsToTranspile.map((path) => __awaiter(void 0, void 0, void 0, function* () {
68
69
  const absolutePath = basePath ? (0, path_1.resolve)(basePath, path) : (0, path_1.resolve)(path);
69
70
  const transpiled = yield (0, transpile_1.transpile)(absolutePath, config);
70
71
  rewrites.set(absolutePath, transpiled);
@@ -22,6 +22,7 @@ const transpile = (path, config) => __awaiter(void 0, void 0, void 0, function*
22
22
  format: 'cjs',
23
23
  logLevel: 'error',
24
24
  platform: 'node',
25
+ sourcemap: Boolean(config.nodeSourcemap),
25
26
  target: [nodeTarget],
26
27
  write: false,
27
28
  });
@@ -71,9 +71,7 @@ const getNestedModules = function ({ modulePath, state, packageJson, pluginsModu
71
71
  return __awaiter(this, void 0, void 0, function* () {
72
72
  const dependencies = (0, nested_1.getNestedDependencies)(packageJson);
73
73
  const depsPaths = yield Promise.all(dependencies.map((dependency) => getDependencyPathsForDependency({ dependency, basedir: modulePath, state, packageJson, pluginsModulesPath })));
74
- // TODO: switch to Array.flat() once we drop support for Node.js < 11.0.0
75
- // eslint-disable-next-line unicorn/prefer-spread
76
- return [].concat(...depsPaths);
74
+ return depsPaths.flat();
77
75
  });
78
76
  };
79
77
  //# sourceMappingURL=traverse.js.map
@@ -0,0 +1,14 @@
1
+ import { ArgumentPlaceholder, Expression, SpreadElement, JSXNamespacedName } from '@babel/types';
2
+ import { parse as parseSchedule } from './properties/schedule';
3
+ declare const IN_SOURCE_CONFIG_MODULE = "@netlify/functions";
4
+ declare type ISCValues = Partial<ReturnType<typeof parseSchedule>>;
5
+ declare const findISCDeclarationsInPaths: (sourcePaths: string[]) => Promise<Partial<{
6
+ schedule: string | undefined;
7
+ }>>;
8
+ declare type ISCHandlerArg = ArgumentPlaceholder | Expression | SpreadElement | JSXNamespacedName;
9
+ interface ISCExport {
10
+ local: string;
11
+ args: ISCHandlerArg[];
12
+ }
13
+ export { findISCDeclarationsInPaths, IN_SOURCE_CONFIG_MODULE };
14
+ export type { ISCExport, ISCHandlerArg, ISCValues };
@@ -0,0 +1,54 @@
1
+ "use strict";
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.IN_SOURCE_CONFIG_MODULE = exports.findISCDeclarationsInPaths = void 0;
13
+ const non_nullable_1 = require("../../../utils/non_nullable");
14
+ const parser_1 = require("../parser");
15
+ const exports_1 = require("../parser/exports");
16
+ const imports_1 = require("../parser/imports");
17
+ const schedule_1 = require("./properties/schedule");
18
+ const IN_SOURCE_CONFIG_MODULE = '@netlify/functions';
19
+ exports.IN_SOURCE_CONFIG_MODULE = IN_SOURCE_CONFIG_MODULE;
20
+ // Parses a JS/TS file and looks for in-source config declarations. It returns
21
+ // an array of all declarations found, with `property` indicating the name of
22
+ // the property and `data` its value.
23
+ const findISCDeclarationsInPath = (sourcePath) => __awaiter(void 0, void 0, void 0, function* () {
24
+ const ast = yield (0, parser_1.safelyParseFile)(sourcePath);
25
+ if (ast === null) {
26
+ return {};
27
+ }
28
+ const imports = ast.body.map((node) => (0, imports_1.getImports)(node, IN_SOURCE_CONFIG_MODULE)).flat();
29
+ const exports = (0, exports_1.getMainExport)(ast.body);
30
+ const iscExports = exports
31
+ .map(({ args, local: exportName }) => {
32
+ const matchingImport = imports.find(({ local: importName }) => importName === exportName);
33
+ if (matchingImport === undefined) {
34
+ return null;
35
+ }
36
+ switch (matchingImport.imported) {
37
+ case 'schedule':
38
+ return (0, schedule_1.parse)({ args });
39
+ default:
40
+ // no-op
41
+ }
42
+ return null;
43
+ })
44
+ .filter(non_nullable_1.nonNullable);
45
+ const mergedExports = iscExports.reduce((acc, obj) => (Object.assign(Object.assign({}, acc), obj)), {});
46
+ return mergedExports;
47
+ });
48
+ const findISCDeclarationsInPaths = (sourcePaths) => __awaiter(void 0, void 0, void 0, function* () {
49
+ const paths = yield Promise.all(sourcePaths.map(findISCDeclarationsInPath));
50
+ const mergedValues = paths.reduce((acc, obj) => (Object.assign(Object.assign({}, acc), obj)), {});
51
+ return mergedValues;
52
+ });
53
+ exports.findISCDeclarationsInPaths = findISCDeclarationsInPaths;
54
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ import type { ISCHandlerArg } from '..';
2
+ export declare const parse: ({ args }: {
3
+ args: ISCHandlerArg[];
4
+ }) => {
5
+ schedule: string | undefined;
6
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = void 0;
4
+ const parse = ({ args }) => {
5
+ const [expression] = args;
6
+ const schedule = expression.type === 'StringLiteral' ? expression.value : undefined;
7
+ return {
8
+ schedule,
9
+ };
10
+ };
11
+ exports.parse = parse;
12
+ //# sourceMappingURL=schedule.js.map
@@ -27,6 +27,7 @@ const path_1 = require("path");
27
27
  const cp_file_1 = __importDefault(require("cp-file"));
28
28
  const bundlers_1 = require("./bundlers");
29
29
  const finder_1 = require("./finder");
30
+ const in_source_config_1 = require("./in_source_config");
30
31
  const detect_es_module_1 = require("./utils/detect_es_module");
31
32
  const plugin_modules_path_1 = require("./utils/plugin_modules_path");
32
33
  const zip_1 = require("./utils/zip");
@@ -73,7 +74,7 @@ const zipFunction = function ({ archiveFormat, basePath, config = {}, destFolder
73
74
  yield (0, cp_file_1.default)(srcPath, destPath);
74
75
  return { config, path: destPath };
75
76
  }
76
- const { aliases = new Map(), cleanupFunction, basePath: finalBasePath, bundlerWarnings, inputs, mainFile: finalMainFile = mainFile, nativeNodeModules, nodeModulesWithDynamicImports, rewrites, srcFiles, } = yield bundler.bundle({
77
+ const { aliases = new Map(), cleanupFunction, basePath: finalBasePath, bundlerWarnings, inputs, iscImporterPaths = new Set(), mainFile: finalMainFile = mainFile, nativeNodeModules, nodeModulesWithDynamicImports, rewrites, srcFiles, } = yield bundler.bundle({
77
78
  basePath,
78
79
  config,
79
80
  extension,
@@ -88,6 +89,10 @@ const zipFunction = function ({ archiveFormat, basePath, config = {}, destFolder
88
89
  srcPath,
89
90
  stat,
90
91
  });
92
+ let inSourceConfig = {};
93
+ if (featureFlags.parseISC) {
94
+ inSourceConfig = yield (0, in_source_config_1.findISCDeclarationsInPaths)([...iscImporterPaths]);
95
+ }
91
96
  (0, plugin_modules_path_1.createAliases)(srcFiles, pluginsModulesPath, aliases, finalBasePath);
92
97
  const zipPath = yield (0, zip_1.zipNodeJs)({
93
98
  aliases,
@@ -106,6 +111,7 @@ const zipFunction = function ({ archiveFormat, basePath, config = {}, destFolder
106
111
  bundlerWarnings,
107
112
  config,
108
113
  inputs,
114
+ inSourceConfig,
109
115
  nativeNodeModules,
110
116
  nodeModulesWithDynamicImports,
111
117
  path: zipPath,
@@ -0,0 +1,4 @@
1
+ import { Statement } from '@babel/types';
2
+ import type { ISCExport } from '../in_source_config';
3
+ declare const getMainExport: (nodes: Statement[]) => ISCExport[];
4
+ export { getMainExport };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMainExport = void 0;
4
+ const helpers_1 = require("./helpers");
5
+ // Finds the main handler export in an AST.
6
+ const getMainExport = (nodes) => {
7
+ let handlerExport = [];
8
+ nodes.find((node) => {
9
+ const esmExports = getMainExportFromESM(node);
10
+ if (esmExports.length !== 0) {
11
+ handlerExport = esmExports;
12
+ return true;
13
+ }
14
+ const cjsExports = getMainExportFromCJS(node);
15
+ if (cjsExports.length !== 0) {
16
+ handlerExport = cjsExports;
17
+ return true;
18
+ }
19
+ return false;
20
+ });
21
+ return handlerExport;
22
+ };
23
+ exports.getMainExport = getMainExport;
24
+ // Finds the main handler export in a CJS AST.
25
+ const getMainExportFromCJS = (node) => {
26
+ const handlerPath = ['module', 'exports', 'handler'];
27
+ if (!(0, helpers_1.isModuleExports)(node, handlerPath) || node.expression.right.type !== 'CallExpression') {
28
+ return [];
29
+ }
30
+ return getExportsFromCallExpression(node.expression.right);
31
+ };
32
+ // Finds the main handler export in an ESM AST.
33
+ // eslint-disable-next-line complexity
34
+ const getMainExportFromESM = (node) => {
35
+ var _a;
36
+ if (node.type !== 'ExportNamedDeclaration' || node.exportKind !== 'value') {
37
+ return [];
38
+ }
39
+ const { declaration } = node;
40
+ if (!declaration || declaration.type !== 'VariableDeclaration') {
41
+ return [];
42
+ }
43
+ const handlerDeclaration = declaration.declarations.find((childDeclaration) => {
44
+ const { id, type } = childDeclaration;
45
+ return type === 'VariableDeclarator' && id.type === 'Identifier' && id.name === 'handler';
46
+ });
47
+ if (((_a = handlerDeclaration === null || handlerDeclaration === void 0 ? void 0 : handlerDeclaration.init) === null || _a === void 0 ? void 0 : _a.type) !== 'CallExpression') {
48
+ return [];
49
+ }
50
+ const exports = getExportsFromCallExpression(handlerDeclaration.init);
51
+ return exports;
52
+ };
53
+ const getExportsFromCallExpression = (node) => {
54
+ const { arguments: args, callee } = node;
55
+ if (callee.type !== 'Identifier') {
56
+ return [];
57
+ }
58
+ const exports = [{ local: callee.name, args }];
59
+ return exports;
60
+ };
61
+ //# sourceMappingURL=exports.js.map
@@ -0,0 +1,7 @@
1
+ import { AssignmentExpression, Expression, ExpressionStatement, ImportDeclaration, Statement } from '@babel/types';
2
+ declare const isImport: (node: Statement, importPath: string) => node is ImportDeclaration;
3
+ declare const isModuleExports: (node: Statement, dotExpression?: string[]) => node is ExpressionStatement & {
4
+ expression: AssignmentExpression;
5
+ };
6
+ declare const isRequire: (node: Expression | undefined | null, requirePath: string) => boolean;
7
+ export { isImport, isModuleExports, isRequire };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isRequire = exports.isModuleExports = exports.isImport = void 0;
4
+ // eslint-disable-next-line complexity
5
+ const isDotExpression = (node, expression) => {
6
+ if (node.type !== 'MemberExpression') {
7
+ return false;
8
+ }
9
+ const object = expression.slice(0, -1);
10
+ const [property] = expression.slice(-1);
11
+ if (node.property.type !== 'Identifier' || node.property.name !== property) {
12
+ return false;
13
+ }
14
+ if (object.length > 1) {
15
+ return isDotExpression(node.object, object);
16
+ }
17
+ return node.object.type === 'Identifier' && object[0] === node.object.name && property === node.property.name;
18
+ };
19
+ const isImport = (node, importPath) => node.type === 'ImportDeclaration' && node.source.value === importPath;
20
+ exports.isImport = isImport;
21
+ const isModuleExports = (node, dotExpression = ['module', 'exports']) => node.type === 'ExpressionStatement' &&
22
+ node.expression.type === 'AssignmentExpression' &&
23
+ node.expression.left.type === 'MemberExpression' &&
24
+ isDotExpression(node.expression.left, dotExpression);
25
+ exports.isModuleExports = isModuleExports;
26
+ const isRequire = (node, requirePath) => {
27
+ var _a;
28
+ if (!node || node.type !== 'CallExpression') {
29
+ return false;
30
+ }
31
+ const { arguments: args, callee } = node;
32
+ const isRequiredModule = ((_a = args[0]) === null || _a === void 0 ? void 0 : _a.type) === 'StringLiteral' && isRequirePath(args[0], requirePath);
33
+ return isRequireCall(callee) && isRequiredModule;
34
+ };
35
+ exports.isRequire = isRequire;
36
+ const isRequireCall = (node) => node.type === 'Identifier' && node.name === 'require';
37
+ const isRequirePath = (node, path) => node.type === 'StringLiteral' && node.value === path;
38
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1,6 @@
1
+ import { Statement } from '@babel/types';
2
+ declare const getImports: (node: Statement, importPath: string) => {
3
+ imported: string;
4
+ local: string;
5
+ }[];
6
+ export { getImports };
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getImports = void 0;
4
+ const non_nullable_1 = require("../../../utils/non_nullable");
5
+ const helpers_1 = require("./helpers");
6
+ // Finds import/require statements of a given path in an AST.
7
+ const getImports = (node, importPath) => {
8
+ const esmImports = getImportsFromESM(node, importPath);
9
+ if (esmImports.length !== 0) {
10
+ return esmImports;
11
+ }
12
+ const cjsImports = getImportsFromCJS(node, importPath);
13
+ return cjsImports;
14
+ };
15
+ exports.getImports = getImports;
16
+ // Finds import/require statements of a given path in a CJS AST.
17
+ const getImportsFromCJS = (node, importPath) => {
18
+ if (node.type !== 'VariableDeclaration') {
19
+ return [];
20
+ }
21
+ const { declarations } = node;
22
+ const requireDeclaration = declarations.find((declaration) => declaration.type === 'VariableDeclarator' && (0, helpers_1.isRequire)(declaration.init, importPath));
23
+ if (requireDeclaration === undefined || requireDeclaration.id.type !== 'ObjectPattern') {
24
+ return [];
25
+ }
26
+ const imports = requireDeclaration.id.properties
27
+ .map((property) => {
28
+ if (property.type !== 'ObjectProperty') {
29
+ return;
30
+ }
31
+ const { key, value } = property;
32
+ if (key.type !== 'Identifier' || value.type !== 'Identifier') {
33
+ return;
34
+ }
35
+ return { imported: key.name, local: value.name };
36
+ })
37
+ .filter(non_nullable_1.nonNullable);
38
+ return imports;
39
+ };
40
+ // Finds import/require statements of a given path in an ESM AST.
41
+ const getImportsFromESM = (node, importPath) => {
42
+ if (!(0, helpers_1.isImport)(node, importPath)) {
43
+ return [];
44
+ }
45
+ const { specifiers } = node;
46
+ const imports = specifiers
47
+ .map((specifier) => {
48
+ if (specifier.type !== 'ImportSpecifier') {
49
+ return;
50
+ }
51
+ const { imported, local } = specifier;
52
+ if (imported.type !== 'Identifier' || local.type !== 'Identifier') {
53
+ return;
54
+ }
55
+ return { imported: imported.name, local: local.name };
56
+ })
57
+ .filter(non_nullable_1.nonNullable);
58
+ return imports;
59
+ };
60
+ //# sourceMappingURL=imports.js.map
@@ -6,4 +6,5 @@ declare const parseExpression: ({ basePath, expression: rawExpression, resolveDi
6
6
  includedPathsGlob: string | null | undefined;
7
7
  type: string;
8
8
  } | undefined;
9
- export { parseExpression };
9
+ declare const safelyParseFile: (path: string) => Promise<import("@babel/types").Program | null>;
10
+ export { parseExpression, safelyParseFile };
@@ -1,8 +1,19 @@
1
1
  "use strict";
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseExpression = void 0;
12
+ exports.safelyParseFile = exports.parseExpression = void 0;
13
+ /* eslint-disable max-lines */
4
14
  const path_1 = require("path");
5
15
  const parser_1 = require("@babel/parser");
16
+ const fs_1 = require("../../../utils/fs");
6
17
  const non_nullable_1 = require("../../../utils/non_nullable");
7
18
  const GLOB_WILDCARD = '**';
8
19
  // Transforms an array of glob nodes into a glob string including an absolute
@@ -75,6 +86,29 @@ const parseExpression = ({ basePath, expression: rawExpression, resolveDir, }) =
75
86
  }
76
87
  };
77
88
  exports.parseExpression = parseExpression;
89
+ // Parses a JS/TS file and returns the resulting AST.
90
+ const parseFile = (path) => __awaiter(void 0, void 0, void 0, function* () {
91
+ const code = yield (0, fs_1.readFile)(path, 'utf8');
92
+ const ast = (0, parser_1.parse)(code, {
93
+ plugins: ['typescript'],
94
+ sourceType: 'module',
95
+ });
96
+ return ast.program;
97
+ });
98
+ // Attemps to parse a JS/TS file at the given path, returning its AST if
99
+ // successful, or `null` if not.
100
+ const safelyParseFile = (path) => __awaiter(void 0, void 0, void 0, function* () {
101
+ if (!path) {
102
+ return null;
103
+ }
104
+ try {
105
+ return yield parseFile(path);
106
+ }
107
+ catch (error) {
108
+ return null;
109
+ }
110
+ });
111
+ exports.safelyParseFile = safelyParseFile;
78
112
  // Parses a `require()` and returns a glob string with an absolute path.
79
113
  const parseRequire = ({ basePath, expression, resolveDir, }) => {
80
114
  const { arguments: args = [] } = expression;
@@ -146,4 +180,5 @@ const validateGlobNodes = (globNodes) => {
146
180
  const hasStaticHead = globNodes[0] !== GLOB_WILDCARD;
147
181
  return hasStrings && hasStaticHead;
148
182
  };
183
+ /* eslint-enable max-lines */
149
184
  //# sourceMappingURL=index.js.map
@@ -36,6 +36,7 @@ const make_dir_1 = __importDefault(require("make-dir"));
36
36
  const p_map_1 = __importDefault(require("p-map"));
37
37
  const unixify_1 = __importDefault(require("unixify"));
38
38
  const archive_1 = require("../../../archive");
39
+ const fs_2 = require("../../../utils/fs");
39
40
  const pStat = (0, util_1.promisify)(fs_1.default.stat);
40
41
  const pWriteFile = (0, util_1.promisify)(fs_1.default.writeFile);
41
42
  // Taken from https://www.npmjs.com/package/cpy.
@@ -67,7 +68,7 @@ const createDirectory = function ({ aliases = new Map(), basePath, destFolder, e
67
68
  });
68
69
  const absoluteDestPath = (0, path_1.join)(functionFolder, normalizedDestPath);
69
70
  if (rewrites.has(srcFile)) {
70
- return pWriteFile(absoluteDestPath, rewrites.get(srcFile));
71
+ return (0, fs_2.mkdirAndWriteFile)(absoluteDestPath, rewrites.get(srcFile));
71
72
  }
72
73
  return (0, cp_file_1.default)(srcFile, absoluteDestPath);
73
74
  }, { concurrency: COPY_FILE_CONCURRENCY });
@@ -4,6 +4,7 @@ import { FeatureFlags } from '../feature_flags';
4
4
  import { FunctionSource, SourceFile } from '../function';
5
5
  import { FsCache } from '../utils/fs';
6
6
  import type { NodeBundlerName } from './node';
7
+ import type { ISCValues } from './node/in_source_config';
7
8
  declare type RuntimeName = 'go' | 'js' | 'rs';
8
9
  declare type FindFunctionsInPathsFunction = (args: {
9
10
  featureFlags: FeatureFlags;
@@ -22,6 +23,7 @@ interface ZipFunctionResult {
22
23
  bundlerWarnings?: object[];
23
24
  config: FunctionConfig;
24
25
  inputs?: string[];
26
+ inSourceConfig?: ISCValues;
25
27
  nativeNodeModules?: object;
26
28
  nodeModulesWithDynamicImports?: string[];
27
29
  path: string;
@@ -2,6 +2,7 @@ import { FunctionArchive } from '../function';
2
2
  import { RuntimeName } from '../runtimes/runtime';
3
3
  declare type FunctionResult = Omit<FunctionArchive, 'runtime'> & {
4
4
  runtime: RuntimeName;
5
+ schedule?: string;
5
6
  };
6
7
  declare const formatZipResult: (archive: FunctionArchive) => FunctionResult;
7
8
  export { formatZipResult };
@@ -4,7 +4,8 @@ exports.formatZipResult = void 0;
4
4
  const remove_undefined_1 = require("./remove_undefined");
5
5
  // Takes the result of zipping a function and formats it for output.
6
6
  const formatZipResult = (archive) => {
7
- const functionResult = Object.assign(Object.assign({}, archive), { runtime: archive.runtime.name });
7
+ var _a, _b, _c;
8
+ const functionResult = Object.assign(Object.assign({}, archive), { inSourceConfig: undefined, runtime: archive.runtime.name, schedule: (_b = (_a = archive.inSourceConfig) === null || _a === void 0 ? void 0 : _a.schedule) !== null && _b !== void 0 ? _b : (_c = archive === null || archive === void 0 ? void 0 : archive.config) === null || _c === void 0 ? void 0 : _c.schedule });
8
9
  return (0, remove_undefined_1.removeUndefined)(functionResult);
9
10
  };
10
11
  exports.formatZipResult = formatZipResult;
@@ -17,5 +17,6 @@ declare const safeUnlink: (path: string) => Promise<void>;
17
17
  declare const listFunctionsDirectories: (srcFolders: string[]) => Promise<string[]>;
18
18
  declare const listFunctionsDirectory: (srcFolder: string) => Promise<string[]>;
19
19
  declare const resolveFunctionsDirectories: (input: string | string[]) => string[];
20
- export { cachedLstat, cachedReaddir, cachedReadFile, pLstat as lstat, getPathWithExtension, listFunctionsDirectories, listFunctionsDirectory, resolveFunctionsDirectories, safeUnlink, pStat as stat, pWriteFile as writeFile, pReadFile as readFile, };
20
+ declare const mkdirAndWriteFile: typeof pWriteFile;
21
+ export { cachedLstat, cachedReaddir, cachedReadFile, pLstat as lstat, getPathWithExtension, listFunctionsDirectories, listFunctionsDirectory, resolveFunctionsDirectories, safeUnlink, pStat as stat, pWriteFile as writeFile, pReadFile as readFile, mkdirAndWriteFile, };
21
22
  export type { FsCache };
package/dist/utils/fs.js CHANGED
@@ -8,11 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.readFile = exports.writeFile = exports.stat = exports.safeUnlink = exports.resolveFunctionsDirectories = exports.listFunctionsDirectory = exports.listFunctionsDirectories = exports.getPathWithExtension = exports.lstat = exports.cachedReadFile = exports.cachedReaddir = exports.cachedLstat = void 0;
15
+ exports.mkdirAndWriteFile = exports.readFile = exports.writeFile = exports.stat = exports.safeUnlink = exports.resolveFunctionsDirectories = exports.listFunctionsDirectory = exports.listFunctionsDirectories = exports.getPathWithExtension = exports.lstat = exports.cachedReadFile = exports.cachedReaddir = exports.cachedLstat = void 0;
13
16
  const fs_1 = require("fs");
14
17
  const path_1 = require("path");
15
18
  const util_1 = require("util");
19
+ const make_dir_1 = __importDefault(require("make-dir"));
16
20
  const non_nullable_1 = require("./non_nullable");
17
21
  const pLstat = (0, util_1.promisify)(fs_1.lstat);
18
22
  exports.lstat = pLstat;
@@ -93,4 +97,12 @@ const resolveFunctionsDirectories = (input) => {
93
97
  return absoluteDirectories;
94
98
  };
95
99
  exports.resolveFunctionsDirectories = resolveFunctionsDirectories;
100
+ const mkdirAndWriteFile = (path, ...params) => __awaiter(void 0, void 0, void 0, function* () {
101
+ if (typeof path === 'string') {
102
+ const directory = (0, path_1.dirname)(path);
103
+ yield (0, make_dir_1.default)(directory);
104
+ }
105
+ return pWriteFile(path, ...params);
106
+ });
107
+ exports.mkdirAndWriteFile = mkdirAndWriteFile;
96
108
  //# sourceMappingURL=fs.js.map
package/dist/zip.d.ts CHANGED
@@ -12,6 +12,9 @@ declare type ZipFunctionsOptions = ZipFunctionOptions & {
12
12
  manifest?: string;
13
13
  parallelLimit?: number;
14
14
  };
15
- declare const zipFunctions: (relativeSrcFolders: string | string[], destFolder: string, { archiveFormat, basePath, config, featureFlags: inputFeatureFlags, manifest, parallelLimit, repositoryRoot, }?: ZipFunctionsOptions) => Promise<import("./utils/format_result").FunctionResult[]>;
15
+ declare const zipFunctions: (relativeSrcFolders: string | string[], destFolder: string, { archiveFormat, basePath, config, featureFlags: inputFeatureFlags, manifest, parallelLimit, repositoryRoot, }?: ZipFunctionsOptions) => Promise<(Omit<import("./function").FunctionArchive, "runtime"> & {
16
+ runtime: import("./runtimes/runtime").RuntimeName;
17
+ schedule?: string | undefined;
18
+ })[]>;
16
19
  declare const zipFunction: (relativeSrcPath: string, destFolder: string, { archiveFormat, basePath, config: inputConfig, featureFlags: inputFeatureFlags, repositoryRoot, }?: ZipFunctionOptions) => Promise<import("./utils/format_result").FunctionResult | undefined>;
17
20
  export { zipFunction, zipFunctions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/zip-it-and-ship-it",
3
- "version": "4.29.4",
3
+ "version": "5.1.0",
4
4
  "description": "Zip it and ship it",
5
5
  "main": "./dist/main.js",
6
6
  "bin": {
@@ -61,7 +61,6 @@
61
61
  "@netlify/esbuild": "^0.13.6",
62
62
  "@vercel/nft": "^0.17.0",
63
63
  "archiver": "^5.3.0",
64
- "array-flat-polyfill": "^1.0.1",
65
64
  "common-path-prefix": "^3.0.0",
66
65
  "cp-file": "^9.0.0",
67
66
  "del": "^6.0.0",
@@ -94,7 +93,7 @@
94
93
  },
95
94
  "devDependencies": {
96
95
  "@babel/types": "^7.15.6",
97
- "@netlify/eslint-config-node": "^3.3.6",
96
+ "@netlify/eslint-config-node": "^3.3.8",
98
97
  "@types/archiver": "^5.1.1",
99
98
  "@types/end-of-stream": "^1.4.1",
100
99
  "@types/resolve": "^1.20.1",
@@ -109,13 +108,15 @@
109
108
  "husky": "^4.3.8",
110
109
  "npm-run-all": "^4.1.5",
111
110
  "nyc": "^15.0.0",
111
+ "p-every": "^2.0.0",
112
112
  "sinon": "^12.0.0",
113
113
  "sort-on": "^4.1.1",
114
+ "source-map-support": "^0.5.20",
114
115
  "throat": "^6.0.1",
115
116
  "typescript": "^4.4.3"
116
117
  },
117
118
  "engines": {
118
- "node": ">=10.18.0"
119
+ "node": "^12.20.0 || ^14.14.0 || >=16.0.0"
119
120
  },
120
121
  "ava": {
121
122
  "files": [
@@ -1 +0,0 @@
1
- import 'array-flat-polyfill';
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- // Patches `Array.flat()` and `Array.flatMap()`
4
- // TODO: remove after dropping Node <12 support
5
- require("array-flat-polyfill");
6
- //# sourceMappingURL=polyfills.js.map