12
12
LOG = logging .getLogger (__name__ )
13
13
14
14
EXTERNAL_KEY = "external"
15
+ # minimum esbuild version required to use "--external"
16
+ MINIMUM_VERSION_FOR_EXTERNAL = "0.14.13"
15
17
16
18
17
19
class EsbuildBundleAction (BaseAction ):
@@ -27,40 +29,36 @@ class EsbuildBundleAction(BaseAction):
27
29
28
30
def __init__ (
29
31
self ,
30
- scratch_dir : str ,
31
- artifacts_dir : str ,
32
+ working_directory : str ,
33
+ output_directory : str ,
32
34
bundler_config : Dict [str , Any ],
33
35
osutils : OSUtils ,
34
36
subprocess_esbuild : SubprocessEsbuild ,
35
37
manifest : str ,
36
38
skip_deps = False ,
37
39
):
38
40
"""
39
- :type scratch_dir: str
40
- :param scratch_dir: an existing (writable) directory for temporary files
41
-
42
- :type artifacts_dir: str
43
- :param artifacts_dir: an existing (writable) directory where to store the output.
41
+ Parameters
42
+ ----------
43
+ working_directory : str
44
+ directory where esbuild is executed
45
+ output_directory : str
46
+ an existing (writable) directory where to store the output.
44
47
Note that the actual result will be in the 'package' subdirectory here.
45
-
46
- :type osutils: aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils
47
- :param osutils: An instance of OS Utilities for file manipulation
48
-
49
- :type subprocess_esbuild: aws_lambda_builders.workflows.nodejs_npm_esbuild.esbuild.SubprocessEsbuild
50
- :param subprocess_esbuild: An instance of the Esbuild process wrapper
51
-
52
- :type skip_deps: bool
53
- :param skip_deps: if dependencies should be omitted from bundling
54
-
55
- :type bundler_config: Dict[str,Any]
56
- :param bundler_config: the bundler configuration
57
-
58
- :type manifest: str
59
- :param manifest: path to package.json file contents to read
48
+ bundler_config : Dict[str, Any]
49
+ the bundle configuration
50
+ osutils : OSUtils
51
+ An instance of OS Utilities for file manipulation
52
+ subprocess_esbuild : SubprocessEsbuild
53
+ An instance of the Esbuild process wrapper
54
+ manifest : str
55
+ path to package.json file contents to read
56
+ skip_deps : bool, optional
57
+ if dependencies should be omitted from bundling, by default False
60
58
"""
61
59
super (EsbuildBundleAction , self ).__init__ ()
62
- self ._scratch_dir = scratch_dir
63
- self ._artifacts_dir = artifacts_dir
60
+ self ._working_directory = working_directory
61
+ self ._output_directory = output_directory
64
62
self ._bundler_config = bundler_config
65
63
self ._osutils = osutils
66
64
self ._subprocess_esbuild = subprocess_esbuild
@@ -71,13 +69,21 @@ def execute(self) -> None:
71
69
"""
72
70
Runs the action.
73
71
74
- :raises lambda_builders.actions.ActionFailedError: when esbuild packaging fails
72
+ Raises
73
+ ------
74
+ ActionFailedError
75
+ when esbuild packaging fails
75
76
"""
76
77
esbuild_command = EsbuildCommandBuilder (
77
- self ._scratch_dir , self ._artifacts_dir , self ._bundler_config , self ._osutils , self ._manifest
78
+ self ._working_directory , self ._output_directory , self ._bundler_config , self ._osutils , self ._manifest
78
79
)
79
80
80
81
if self ._should_bundle_deps_externally ():
82
+ check_minimum_esbuild_version (
83
+ minimum_version_required = MINIMUM_VERSION_FOR_EXTERNAL ,
84
+ working_directory = self ._working_directory ,
85
+ subprocess_esbuild = self ._subprocess_esbuild ,
86
+ )
81
87
esbuild_command .build_with_no_dependencies ()
82
88
if EXTERNAL_KEY in self ._bundler_config :
83
89
# Already marking everything as external,
@@ -89,7 +95,7 @@ def execute(self) -> None:
89
95
)
90
96
91
97
try :
92
- self ._subprocess_esbuild .run (args , cwd = self ._scratch_dir )
98
+ self ._subprocess_esbuild .run (args , cwd = self ._working_directory )
93
99
except EsbuildExecutionError as ex :
94
100
raise ActionFailedError (str (ex ))
95
101
@@ -103,65 +109,62 @@ def _should_bundle_deps_externally(self) -> bool:
103
109
return self ._skip_deps or "./node_modules/*" in self ._bundler_config .get (EXTERNAL_KEY , [])
104
110
105
111
106
- class EsbuildCheckVersionAction ( BaseAction ):
107
- """
108
- A Lambda Builder Action that verifies that esbuild is a version supported by sam accelerate
112
+ def check_minimum_esbuild_version (
113
+ minimum_version_required : str , working_directory : str , subprocess_esbuild : SubprocessEsbuild
114
+ ):
109
115
"""
116
+ Checks esbuild version against a minimum version required.
110
117
111
- NAME = "EsbuildCheckVersion"
112
- DESCRIPTION = "Checking esbuild version"
113
- PURPOSE = Purpose .COMPILE_SOURCE
118
+ Parameters
119
+ ----------
120
+ minimum_version_required: str
121
+ minimum esbuild version required for check to pass
114
122
115
- MIN_VERSION = "0.14.13"
123
+ working_directory: str
124
+ directory where esbuild is executed
116
125
117
- def __init__ (self , scratch_dir , subprocess_esbuild ):
118
- """
119
- :type scratch_dir: str
120
- :param scratch_dir: temporary directory where esbuild is executed
126
+ subprocess_esbuild: aws_lambda_builders.workflows.nodejs_npm_esbuild.esbuild.SubprocessEsbuild
127
+ An instance of the Esbuild process wrapper
121
128
122
- :type subprocess_esbuild: aws_lambda_builders.workflows.nodejs_npm_esbuild.esbuild.SubprocessEsbuild
123
- :param subprocess_esbuild: An instance of the Esbuild process wrapper
124
- """
125
- super (). __init__ ()
126
- self . scratch_dir = scratch_dir
127
- self . subprocess_esbuild = subprocess_esbuild
129
+ Raises
130
+ ----------
131
+ lambda_builders.actions.ActionFailedError
132
+ when esbuild version checking fails
133
+ """
134
+ args = [ "--version" ]
128
135
129
- def execute (self ):
130
- """
131
- Runs the action.
136
+ try :
137
+ version = subprocess_esbuild .run (args , cwd = working_directory )
138
+ except EsbuildExecutionError as ex :
139
+ raise ActionFailedError (str (ex ))
132
140
133
- :raises lambda_builders.actions.ActionFailedError: when esbuild version checking fails
134
- """
135
- args = ["--version" ]
141
+ LOG .debug ("Found esbuild with version: %s" , version )
136
142
137
- try :
138
- version = self .subprocess_esbuild .run (args , cwd = self .scratch_dir )
139
- except EsbuildExecutionError as ex :
140
- raise ActionFailedError (str (ex ))
143
+ try :
144
+ check_version = _get_version_tuple (minimum_version_required )
145
+ esbuild_version = _get_version_tuple (version )
141
146
142
- LOG .debug ("Found esbuild with version: %s" , version )
147
+ if esbuild_version < check_version :
148
+ raise ActionFailedError (
149
+ f"Unsupported esbuild version. To use a dependency layer, the esbuild version must be at "
150
+ f"least { minimum_version_required } . Version found: { version } "
151
+ )
152
+ except (TypeError , ValueError ) as ex :
153
+ raise ActionFailedError (f"Unable to parse esbuild version: { str (ex )} " )
143
154
144
- try :
145
- check_version = EsbuildCheckVersionAction ._get_version_tuple (self .MIN_VERSION )
146
- esbuild_version = EsbuildCheckVersionAction ._get_version_tuple (version )
147
-
148
- if esbuild_version < check_version :
149
- raise ActionFailedError (
150
- f"Unsupported esbuild version. To use a dependency layer, the esbuild version must be at "
151
- f"least { self .MIN_VERSION } . Version found: { version } "
152
- )
153
- except (TypeError , ValueError ) as ex :
154
- raise ActionFailedError (f"Unable to parse esbuild version: { str (ex )} " )
155
-
156
- @staticmethod
157
- def _get_version_tuple (version_string ):
158
- """
159
- Get an integer tuple representation of the version for comparison
160
155
161
- :type version_string: str
162
- :param version_string: string containing the esbuild version
156
+ def _get_version_tuple (version_string : str ):
157
+ """
158
+ Get an integer tuple representation of the version for comparison
163
159
164
- :rtype: tuple
165
- :return: version tuple used for comparison
166
- """
167
- return tuple (map (int , version_string .split ("." )))
160
+ Parameters
161
+ ----------
162
+ version_string: str
163
+ string containing the esbuild version
164
+
165
+ Returns
166
+ ----------
167
+ tuple
168
+ version tuple used for comparison
169
+ """
170
+ return tuple (map (int , version_string .split ("." )))
0 commit comments