-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Enable source maps if either Sourcemap: true or --enable-source-maps is provided #4062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1dc156a
24112f7
6528ad0
b16e6cc
25095ef
e5a017d
6d83423
ecdaedb
12540c3
3798147
6267ffa
6f4c2e9
7f4022b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -222,6 +222,9 @@ def run(self): | |
| if is_sam_template: | ||
| SamApiProvider.check_implicit_api_resource_ids(self.stacks) | ||
|
|
||
| # modify the stack resources to support source maps | ||
| self._enable_source_maps() | ||
|
|
||
| try: | ||
| builder = ApplicationBuilder( | ||
| self.get_resources_to_build(), | ||
|
|
@@ -265,6 +268,7 @@ def run(self): | |
| stack, self._stack_name, self.build_dir, modified_template, build_result | ||
| ) | ||
| modified_template = nested_stack_manager.generate_auto_dependency_layer_stack() | ||
|
|
||
| move_template(stack.location, output_template_path, modified_template) | ||
|
|
||
| click.secho("\nBuild Succeeded", fg="green") | ||
|
|
@@ -307,6 +311,108 @@ def run(self): | |
| wrapped_from = deep_wrap if deep_wrap else ex.__class__.__name__ | ||
| raise UserException(str(ex), wrapped_from=wrapped_from) from ex | ||
|
|
||
| def _enable_source_maps(self): | ||
| """ | ||
| Appends ``NODE_OPTIONS: --enable-source-maps``, if Sourcemap is set to true | ||
| and sets Sourcemap to true if ``NODE_OPTIONS: --enable-source-maps`` is provided. | ||
| """ | ||
| using_source_maps = False | ||
| invalid_node_option = False | ||
|
|
||
| for stack in self.stacks: | ||
| for name, resource in stack.resources.items(): | ||
| metadata = resource.get("Metadata", {}) | ||
| if metadata.get("BuildMethod", "") != "esbuild": | ||
| continue | ||
|
|
||
| node_option_set = self._is_node_option_set(resource) | ||
|
|
||
| # check if Sourcemap is provided and append --enable-source-map if not set | ||
| build_properties = metadata.get("BuildProperties", {}) | ||
| source_map = build_properties.get("Sourcemap", None) | ||
|
|
||
| if source_map and not node_option_set: | ||
| LOG.info( | ||
| "\nSourcemap set without --enable-source-maps, adding" | ||
| " --enable-source-maps to function %s NODE_OPTIONS", | ||
| name, | ||
| ) | ||
|
|
||
| resource.setdefault("Properties", {}) | ||
| resource["Properties"].setdefault("Environment", {}) | ||
| resource["Properties"]["Environment"].setdefault("Variables", {}) | ||
| existing_options = resource["Properties"]["Environment"]["Variables"].setdefault("NODE_OPTIONS", "") | ||
|
|
||
| # make sure the NODE_OPTIONS is a string | ||
| if not isinstance(existing_options, str): | ||
| invalid_node_option = True | ||
| else: | ||
| resource["Properties"]["Environment"]["Variables"]["NODE_OPTIONS"] = " ".join( | ||
| [existing_options, "--enable-source-maps"] | ||
| ) | ||
|
|
||
| using_source_maps = True | ||
|
|
||
| # check if --enable-source-map is provided and append Sourcemap: true if it is not set | ||
| if source_map is None and node_option_set: | ||
| LOG.info( | ||
| "\n--enable-source-maps set without Sourcemap, adding Sourcemap to" | ||
| " Metadata BuildProperties for %s", | ||
| name, | ||
| ) | ||
|
|
||
| resource.setdefault("Metadata", {}) | ||
| resource["Metadata"].setdefault("BuildProperties", {}) | ||
| resource["Metadata"]["BuildProperties"]["Sourcemap"] = True | ||
|
|
||
| using_source_maps = True | ||
|
|
||
| if using_source_maps: | ||
| self._warn_using_source_maps() | ||
|
|
||
| if invalid_node_option: | ||
| self._warn_invalid_node_options() | ||
|
|
||
| @staticmethod | ||
| def _is_node_option_set(resource: Dict) -> bool: | ||
| """ | ||
| Checks if the template has NODE_OPTIONS --enable-source-maps set | ||
|
|
||
| Parameters | ||
| ---------- | ||
| resource : Dict | ||
| The resource dictionary to lookup if --enable-source-maps is set | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| True if --enable-source-maps is set, otherwise false | ||
| """ | ||
| try: | ||
| node_options = resource["Properties"]["Environment"]["Variables"]["NODE_OPTIONS"] | ||
|
|
||
| return "--enable-source-maps" in node_options.split() | ||
| except (KeyError, AttributeError): | ||
| return False | ||
|
|
||
| @staticmethod | ||
| def _warn_invalid_node_options(): | ||
| click.secho( | ||
| "\nNODE_OPTIONS is not a string! As a result, the NODE_OPTIONS environment variable will " | ||
| "not be set correctly, please make sure it is a string. " | ||
| "Visit https://nodejs.org/api/cli.html#node_optionsoptions for more details.\n", | ||
| fg="yellow", | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _warn_using_source_maps(): | ||
| click.secho( | ||
| "\nYou are using source maps, note that this comes with a performance hit!" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If they try to remove just the env var it will still be set next time if the source maps are still set in the metadata, right? Should we clarify that it needs to be removed from both spots?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, I'll update it to reflect this change |
||
| " Set Sourcemap to false and remove" | ||
| " NODE_OPTIONS: --enable-source-maps to disable source maps.\n", | ||
| fg="yellow", | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def gen_success_msg(artifacts_dir: str, output_template_path: str, is_default_build_dir: bool) -> str: | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the logic here it doesn't seem to have special handling to the scenario where node_option_set is true and the build_properties.get("Sourcemap") is an explicit value False, which I feel we might want to handle differently from it being None? Open to any reason for this self conflict input should not be a concern
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, you're correct that it does not handle the case where NODE_OPTIONS is set and Sourcemap is false. In this case, no source map is generated, so the environment variable to enable source maps will not do anything even though it is set. The only potential concern the performance hit with source maps, I'm not quite sure if the performance is degraded with a source map file, or if its just solely by using the enable source map environment variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure of the performance concerns here, but to @qingchm 's question is there a change in behavior from before this PR and after? If there is a conflict, we can raise that as LOG warning that there is a conflict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(not @qingchm answering) Before this PR, setting Sourcemaps to false would effectively disable source map usage since no map file is generated, even if the env variable was provided, this was expected behaviour. The behaviour remains the same after the PR. The
Sourcemap: falseoption is basically just a way to opt out of source maps if it was defined at the Global level.