-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(app): normalize subpath imports to install base npm package #6380
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -1009,12 +1009,11 @@ def _get_frontend_packages(self, imports: dict[str, set[ImportVar]]): | |
| dependencies = constants.PackageJson.DEPENDENCIES | ||
| dev_dependencies = constants.PackageJson.DEV_DEPENDENCIES | ||
| page_imports = { | ||
| i | ||
| for i, tags in imports.items() | ||
| if i not in dependencies | ||
| and i not in dev_dependencies | ||
| and not any(i.startswith(prefix) for prefix in ["/", "$/", "."]) | ||
| and i != "" | ||
| package_name | ||
| for import_name, tags in imports.items() | ||
| if (package_name := self._get_frontend_package_name(import_name)) | ||
| and package_name not in dependencies | ||
| and package_name not in dev_dependencies | ||
| and any(tag.install for tag in tags) | ||
| } | ||
| pinned = {i.rpartition("@")[0] for i in page_imports if "@" in i} | ||
|
|
@@ -1032,6 +1031,48 @@ def _get_frontend_packages(self, imports: dict[str, set[ImportVar]]): | |
| page_imports.update(filtered_frontend_packages) | ||
| js_runtimes.install_frontend_packages(page_imports, get_config()) | ||
|
|
||
| @staticmethod | ||
| def _get_frontend_package_name(import_name: str) -> str | None: | ||
| """Resolve the npm package name to install for a library import path. | ||
|
|
||
| Args: | ||
| import_name: The import path key used in component imports. | ||
|
|
||
| Returns: | ||
| The package name that should be installed, including pinned version when | ||
| available, or None when the import does not represent an installable npm package. | ||
| """ | ||
| if import_name == "" or any( | ||
| import_name.startswith(prefix) for prefix in ("/", "$/", ".") | ||
| ): | ||
| return None | ||
| if import_name.startswith(("https://", "http://")): | ||
| return import_name | ||
|
|
||
| library_name = format.format_library_name(import_name) | ||
| if library_name.startswith("@"): | ||
| scope, slash, package_and_path = library_name.partition("/") | ||
| package_name = ( | ||
| f"{scope}/{package_and_path.split('/', maxsplit=1)[0]}" | ||
| if slash and package_and_path | ||
| else library_name | ||
| ) | ||
| else: | ||
| package_name = library_name.split("/", maxsplit=1)[0] | ||
|
|
||
| if import_name.startswith(f"{library_name}@"): | ||
| version_and_maybe_subpath = import_name[len(library_name) + 1 :] | ||
| version, slash, _ = version_and_maybe_subpath.partition("/") | ||
| if slash and ":" not in version: | ||
| return f"{package_name}@{version}" | ||
| if package_name == library_name: | ||
| return import_name | ||
| return f"{package_name}@{version_and_maybe_subpath}" | ||
|
|
||
| if package_name == library_name: | ||
| return import_name | ||
|
Comment on lines
+1072
to
+1073
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.
When an import carries both a version pin and a subpath, e.g. The condition is meant to short-circuit when there is no subpath to strip, but it does not account for |
||
| return package_name | ||
|
|
||
| def _app_root(self, app_wrappers: dict[tuple[int, str], Component]) -> Component: | ||
| for component in tuple(app_wrappers.values()): | ||
| app_wrappers.update(component._get_all_app_wrap_components()) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.