Skip to content

Commit cec166c

Browse files
authored
fix: Address main fields incorrectly inputted (#385)
* fix: Address main fields incorrectly inputted * Add return-type hint * Improve function name
1 parent 05697ac commit cec166c

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

aws_lambda_builders/workflows/nodejs_npm_esbuild/esbuild.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def run(self, args, cwd=None):
102102

103103

104104
# The esbuild API flags are broken up into three forms (https://esbuild.github.io/api/):
105+
# Multi-word arguments are expected to be passed down using snake case e.g. entry_points
105106
# Boolean types (--minify)
106107
SUPPORTED_ESBUILD_APIS_BOOLEAN = [
107108
"minify",
@@ -112,7 +113,7 @@ def run(self, args, cwd=None):
112113
SUPPORTED_ESBUILD_APIS_SINGLE_VALUE = [
113114
"target",
114115
"format",
115-
"main-fields",
116+
"main_fields",
116117
]
117118

118119
# Multi-value types (--external:axios --external:aws-sdk)
@@ -236,7 +237,7 @@ def _get_boolean_args(self) -> List[str]:
236237
args = []
237238
for param in SUPPORTED_ESBUILD_APIS_BOOLEAN:
238239
if param in self._bundler_config and self._bundler_config[param] is True:
239-
args.append(f"--{param}")
240+
args.append(f"--{self._convert_snake_to_kebab_case(param)}")
240241
return args
241242

242243
def _get_single_value_args(self) -> List[str]:
@@ -250,7 +251,7 @@ def _get_single_value_args(self) -> List[str]:
250251
for param in SUPPORTED_ESBUILD_APIS_SINGLE_VALUE:
251252
if param in self._bundler_config:
252253
value = self._bundler_config.get(param)
253-
args.append(f"--{param}={value}")
254+
args.append(f"--{self._convert_snake_to_kebab_case(param)}={value}")
254255
return args
255256

256257
def _get_multi_value_args(self) -> List[str]:
@@ -267,7 +268,7 @@ def _get_multi_value_args(self) -> List[str]:
267268
if not isinstance(values, list):
268269
raise EsbuildCommandError(f"Invalid type for property {param}, must be a dict.")
269270
for param_item in values:
270-
args.append(f"--{param}:{param_item}")
271+
args.append(f"--{self._convert_snake_to_kebab_case(param)}:{param_item}")
271272
return args
272273

273274
def _get_explicit_file_type(self, entry_point, entry_path):
@@ -296,3 +297,14 @@ def _get_explicit_file_type(self, entry_point, entry_path):
296297
return entry_point + ext
297298

298299
raise ActionFailedError("entry point {} does not exist".format(entry_path))
300+
301+
@staticmethod
302+
def _convert_snake_to_kebab_case(arg: str) -> str:
303+
"""
304+
The configuration properties passed down to Lambda Builders are done so using snake case
305+
e.g. "main_fields" but esbuild expects them using kebab-case "main-fields"
306+
307+
:rtype: str
308+
:return: mutated string to match the esbuild argument format
309+
"""
310+
return arg.replace("_", "-")

tests/unit/workflows/nodejs_npm_esbuild/test_esbuild.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_builds_args_from_config(self, osutils_mock):
200200
"target": "node14",
201201
"loader": [".proto=text", ".json=js"],
202202
"external": ["aws-sdk", "axios"],
203-
"main-fields": "module,main",
203+
"main_fields": "module,main",
204204
}
205205

206206
args = (
@@ -246,3 +246,14 @@ def test_combined_builder_with_dependencies(self, osutils_mock):
246246
"--loader:.json=js",
247247
],
248248
)
249+
250+
@parameterized.expand(
251+
[
252+
("main_fields", "main-fields"),
253+
("entry_points", "entry-points"),
254+
("main-fields", "main-fields"),
255+
("bundle", "bundle"),
256+
]
257+
)
258+
def test_convert_snake_case_to_kebab_case(self, field, expected):
259+
self.assertEqual(EsbuildCommandBuilder._convert_snake_to_kebab_case(field), expected)

0 commit comments

Comments
 (0)