@netlify/zip-it-and-ship-it 5.10.0 → 5.10.1

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.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.findISCDeclarationsInPath = exports.IN_SOURCE_CONFIG_MODULE = void 0;
4
4
  const non_nullable_js_1 = require("../../../utils/non_nullable.js");
5
+ const bindings_js_1 = require("../parser/bindings.js");
5
6
  const exports_js_1 = require("../parser/exports.js");
6
7
  const imports_js_1 = require("../parser/imports.js");
7
8
  const index_js_1 = require("../parser/index.js");
@@ -16,7 +17,8 @@ const findISCDeclarationsInPath = async (sourcePath) => {
16
17
  return {};
17
18
  }
18
19
  const imports = ast.body.flatMap((node) => (0, imports_js_1.getImports)(node, exports.IN_SOURCE_CONFIG_MODULE));
19
- const mainExports = (0, exports_js_1.getMainExport)(ast.body);
20
+ const getAllBindings = (0, bindings_js_1.createBindingsMethod)(ast.body);
21
+ const mainExports = (0, exports_js_1.getMainExport)(ast.body, getAllBindings);
20
22
  const iscExports = mainExports
21
23
  .map(({ args, local: exportName }) => {
22
24
  const matchingImport = imports.find(({ local: importName }) => importName === exportName);
@@ -25,7 +27,7 @@ const findISCDeclarationsInPath = async (sourcePath) => {
25
27
  }
26
28
  switch (matchingImport.imported) {
27
29
  case 'schedule':
28
- return (0, schedule_js_1.parse)({ args });
30
+ return (0, schedule_js_1.parse)({ args }, getAllBindings);
29
31
  default:
30
32
  // no-op
31
33
  }
@@ -1,6 +1,7 @@
1
+ import type { BindingMethod } from '../../parser/bindings.js';
1
2
  import type { ISCHandlerArg } from '../index.js';
2
3
  export declare const parse: ({ args }: {
3
4
  args: ISCHandlerArg[];
4
- }) => {
5
+ }, getAllBindings: BindingMethod) => {
5
6
  schedule: string | undefined;
6
7
  };
@@ -1,8 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parse = void 0;
4
- const parse = ({ args }) => {
5
- const [expression] = args;
4
+ const parse = ({ args }, getAllBindings) => {
5
+ let [expression] = args;
6
+ if (expression.type === 'Identifier') {
7
+ const binding = getAllBindings().get(expression.name);
8
+ if (binding) {
9
+ expression = binding;
10
+ }
11
+ }
6
12
  const schedule = expression.type === 'StringLiteral' ? expression.value : undefined;
7
13
  return {
8
14
  schedule,
@@ -0,0 +1,5 @@
1
+ import type { Expression, Statement } from '@babel/types';
2
+ declare type Bindings = Map<string, Expression>;
3
+ export declare type BindingMethod = () => Bindings;
4
+ export declare const createBindingsMethod: (nodes: Statement[]) => BindingMethod;
5
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createBindingsMethod = void 0;
4
+ const getBindingFromVariableDeclaration = function (node, bindings) {
5
+ node.declarations.forEach((declaration) => {
6
+ if (declaration.id.type === 'Identifier' && declaration.init) {
7
+ bindings.set(declaration.id.name, declaration.init);
8
+ }
9
+ });
10
+ };
11
+ // eslint-disable-next-line complexity
12
+ const getBindingsFromNode = function (node, bindings) {
13
+ var _a;
14
+ if (node.type === 'VariableDeclaration') {
15
+ // A variable was created, so create it and store the potential value
16
+ getBindingFromVariableDeclaration(node, bindings);
17
+ }
18
+ else if (node.type === 'ExpressionStatement' &&
19
+ node.expression.type === 'AssignmentExpression' &&
20
+ node.expression.left.type === 'Identifier') {
21
+ // The variable was reassigned, so let's store the new value
22
+ bindings.set(node.expression.left.name, node.expression.right);
23
+ }
24
+ else if (node.type === 'ExportNamedDeclaration' && ((_a = node.declaration) === null || _a === void 0 ? void 0 : _a.type) === 'VariableDeclaration') {
25
+ // A `export const|let ...` creates a binding that can later be referenced again
26
+ getBindingFromVariableDeclaration(node.declaration, bindings);
27
+ }
28
+ };
29
+ /**
30
+ * Goes through all relevant nodes and creates a map from binding name to assigned value/expression
31
+ */
32
+ const getAllBindings = function (nodes) {
33
+ const bindings = new Map();
34
+ nodes.forEach((node) => {
35
+ getBindingsFromNode(node, bindings);
36
+ });
37
+ return bindings;
38
+ };
39
+ const createBindingsMethod = function (nodes) {
40
+ // memoize the result for these nodes
41
+ let result;
42
+ return () => {
43
+ if (!result) {
44
+ result = getAllBindings(nodes);
45
+ }
46
+ return result;
47
+ };
48
+ };
49
+ exports.createBindingsMethod = createBindingsMethod;
50
+ //# sourceMappingURL=bindings.js.map
@@ -1,3 +1,4 @@
1
- import { Statement } from '@babel/types';
1
+ import type { Statement } from '@babel/types';
2
2
  import type { ISCExport } from '../in_source_config/index.js';
3
- export declare const getMainExport: (nodes: Statement[]) => ISCExport[];
3
+ import type { BindingMethod } from './bindings.js';
4
+ export declare const getMainExport: (nodes: Statement[], getAllBindings: BindingMethod) => ISCExport[];
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getMainExport = void 0;
4
4
  const helpers_js_1 = require("./helpers.js");
5
5
  // Finds the main handler export in an AST.
6
- const getMainExport = (nodes) => {
6
+ const getMainExport = (nodes, getAllBindings) => {
7
7
  let handlerExport = [];
8
8
  nodes.find((node) => {
9
- const esmExports = getMainExportFromESM(node);
9
+ const esmExports = getMainExportFromESM(node, getAllBindings);
10
10
  if (esmExports.length !== 0) {
11
11
  handlerExport = esmExports;
12
12
  return true;
@@ -28,34 +28,58 @@ const getMainExportFromCJS = (node) => {
28
28
  ['exports', 'handler'],
29
29
  ];
30
30
  return handlerPaths.flatMap((handlerPath) => {
31
- if (!(0, helpers_js_1.isModuleExports)(node, handlerPath) || node.expression.right.type !== 'CallExpression') {
31
+ if (!(0, helpers_js_1.isModuleExports)(node, handlerPath)) {
32
32
  return [];
33
33
  }
34
- return getExportsFromCallExpression(node.expression.right);
34
+ return getExportsFromExpression(node.expression.right);
35
35
  });
36
36
  };
37
37
  // Finds the main handler export in an ESM AST.
38
- // eslint-disable-next-line complexity
39
- const getMainExportFromESM = (node) => {
40
- var _a;
38
+ const getMainExportFromESM = (node, getAllBindings) => {
41
39
  if (node.type !== 'ExportNamedDeclaration' || node.exportKind !== 'value') {
42
40
  return [];
43
41
  }
44
- const { declaration } = node;
45
- if (!declaration || declaration.type !== 'VariableDeclaration') {
42
+ const { declaration, specifiers } = node;
43
+ if ((specifiers === null || specifiers === void 0 ? void 0 : specifiers.length) > 0) {
44
+ return getExportsFromBindings(specifiers, getAllBindings);
45
+ }
46
+ if ((declaration === null || declaration === void 0 ? void 0 : declaration.type) !== 'VariableDeclaration') {
46
47
  return [];
47
48
  }
48
49
  const handlerDeclaration = declaration.declarations.find((childDeclaration) => {
49
50
  const { id, type } = childDeclaration;
50
51
  return type === 'VariableDeclarator' && id.type === 'Identifier' && id.name === 'handler';
51
52
  });
52
- if (((_a = handlerDeclaration === null || handlerDeclaration === void 0 ? void 0 : handlerDeclaration.init) === null || _a === void 0 ? void 0 : _a.type) !== 'CallExpression') {
53
+ const exports = getExportsFromExpression(handlerDeclaration === null || handlerDeclaration === void 0 ? void 0 : handlerDeclaration.init);
54
+ return exports;
55
+ };
56
+ // Check if the Node is an ExportSpecifier that has a named export called `handler`
57
+ // either with Identifier `export { handler }`
58
+ // or with StringLiteral `export { x as "handler" }`
59
+ const isHandlerExport = (node) => {
60
+ const { type, exported } = node;
61
+ return (type === 'ExportSpecifier' &&
62
+ ((exported.type === 'Identifier' && exported.name === 'handler') ||
63
+ (exported.type === 'StringLiteral' && exported.value === 'handler')));
64
+ };
65
+ // Tries to resolve the export from a binding (variable)
66
+ // for example `let handler; handler = () => {}; export { handler }` would
67
+ // resolve correctly to the handler function
68
+ const getExportsFromBindings = (specifiers, getAllBindings) => {
69
+ const specifier = specifiers.find(isHandlerExport);
70
+ if (!specifier) {
53
71
  return [];
54
72
  }
55
- const exports = getExportsFromCallExpression(handlerDeclaration.init);
73
+ const binding = getAllBindings().get(specifier.local.name);
74
+ const exports = getExportsFromExpression(binding);
56
75
  return exports;
57
76
  };
58
- const getExportsFromCallExpression = (node) => {
77
+ const getExportsFromExpression = (node) => {
78
+ // We're only interested in expressions representing function calls, because
79
+ // the ISC patterns we implement at the moment are all helper functions.
80
+ if ((node === null || node === void 0 ? void 0 : node.type) !== 'CallExpression') {
81
+ return [];
82
+ }
59
83
  const { arguments: args, callee } = node;
60
84
  if (callee.type !== 'Identifier') {
61
85
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/zip-it-and-ship-it",
3
- "version": "5.10.0",
3
+ "version": "5.10.1",
4
4
  "description": "Zip it and ship it",
5
5
  "main": "./dist/main.js",
6
6
  "bin": {