-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[AssetMapper] Add a "package specifier" to importmap in case import name != package+path #52024
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
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 |
---|---|---|
|
@@ -28,8 +28,8 @@ final class JavaScriptImportPathCompiler implements AssetCompilerInterface | |
{ | ||
use AssetCompilerPathResolverTrait; | ||
|
||
// https://regex101.com/r/5Q38tj/1 | ||
private const IMPORT_PATTERN = '/(?:import\s+(?:(?:\*\s+as\s+\w+|[\w\s{},*]+)\s+from\s+)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m'; | ||
// https://regex101.com/r/fquriB/1 | ||
private const IMPORT_PATTERN = '/(?:import\s*(?:(?:\*\s*as\s+\w+|[\w\s{},*]+)\s*from\s*)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m'; | ||
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. Covered with a test |
||
|
||
public function __construct( | ||
private readonly ImportMapManager $importMapManager, | ||
|
@@ -145,12 +145,11 @@ private function findAssetForBareImport(string $importedModule, AssetMapperInter | |
return null; | ||
} | ||
|
||
// remote entries have no MappedAsset | ||
if ($importMapEntry->isRemotePackage()) { | ||
return null; | ||
if ($asset = $assetMapper->getAsset($importMapEntry->path)) { | ||
return $asset; | ||
} | ||
|
||
return $assetMapper->getAsset($importMapEntry->path); | ||
return $assetMapper->getAssetFromSourcePath($importMapEntry->path); | ||
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. e.g. |
||
} | ||
|
||
private function findAssetForRelativeImport(string $importedModule, MappedAsset $asset, AssetMapperInterface $assetMapper): ?MappedAsset | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\AssetMapper\Exception; | ||
|
||
class LogicException extends \LogicException implements ExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,10 +35,6 @@ public function audit(): array | |
{ | ||
$entries = $this->configReader->getEntries(); | ||
|
||
if (!$entries) { | ||
return []; | ||
} | ||
|
||
/** @var array<string, array<string, ImportMapPackageAudit>> $installed */ | ||
$packageAudits = []; | ||
|
||
|
@@ -51,14 +47,19 @@ public function audit(): array | |
} | ||
$version = $entry->version; | ||
|
||
$installed[$entry->importName] ??= []; | ||
$installed[$entry->importName][] = $version; | ||
$packageName = $entry->getPackageName(); | ||
$installed[$packageName] ??= []; | ||
$installed[$packageName][] = $version; | ||
|
||
$packageVersion = $entry->importName.($version ? '@'.$version : ''); | ||
$packageAudits[$packageVersion] ??= new ImportMapPackageAudit($entry->importName, $version); | ||
$packageVersion = $packageName.'@'.$version; | ||
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. Bug fix - e.g. if you have Also, |
||
$packageAudits[$packageVersion] ??= new ImportMapPackageAudit($packageName, $version); | ||
$affectsQuery[] = $packageVersion; | ||
} | ||
|
||
if (!$affectsQuery) { | ||
return []; | ||
} | ||
|
||
// @see https://docs.github.com/en/rest/security-advisories/global-advisories?apiVersion=2022-11-28#list-global-security-advisories | ||
$response = $this->httpClient->request('GET', self::AUDIT_URL, [ | ||
'query' => ['affects' => implode(',', $affectsQuery)], | ||
|
@@ -81,7 +82,7 @@ public function audit(): array | |
if (!$version || !$this->versionMatches($version, $vulnerability['vulnerable_version_range'] ?? '>= *')) { | ||
continue; | ||
} | ||
$packageAudits[$package.($version ? '@'.$version : '')] = $packageAudits[$package.($version ? '@'.$version : '')]->withVulnerability( | ||
$packageAudits[$package.'@'.$version] = $packageAudits[$package.'@'.$version]->withVulnerability( | ||
new ImportMapPackageAuditVulnerability( | ||
$advisory['ghsa_id'], | ||
$advisory['cve_id'], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,10 @@ class ImportMapConfigReader | |
{ | ||
private ImportMapEntries $rootImportMapEntries; | ||
|
||
public function __construct(private readonly string $importMapConfigPath) | ||
{ | ||
public function __construct( | ||
private readonly string $importMapConfigPath, | ||
private readonly RemotePackageStorage $remotePackageStorage, | ||
) { | ||
} | ||
|
||
public function getEntries(): ImportMapEntries | ||
|
@@ -38,7 +40,7 @@ public function getEntries(): ImportMapEntries | |
|
||
$entries = new ImportMapEntries(); | ||
foreach ($importMapConfig ?? [] as $importName => $data) { | ||
$validKeys = ['path', 'version', 'type', 'entrypoint', 'url']; | ||
$validKeys = ['path', 'version', 'type', 'entrypoint', 'url', 'package_specifier']; | ||
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. Supports the rare-use case that you require a package (e.g. |
||
if ($invalidKeys = array_diff(array_keys($data), $validKeys)) { | ||
throw new \InvalidArgumentException(sprintf('The following keys are not valid for the importmap entry "%s": "%s". Valid keys are: "%s".', $importName, implode('", "', $invalidKeys), implode('", "', $validKeys))); | ||
} | ||
|
@@ -49,36 +51,33 @@ public function getEntries(): ImportMapEntries | |
} | ||
|
||
$type = isset($data['type']) ? ImportMapType::tryFrom($data['type']) : ImportMapType::JS; | ||
$isEntry = $data['entrypoint'] ?? false; | ||
$isEntrypoint = $data['entrypoint'] ?? false; | ||
|
||
if (isset($data['path'])) { | ||
if (isset($data['version'])) { | ||
throw new RuntimeException(sprintf('The importmap entry "%s" cannot have both a "path" and "version" option.', $importName)); | ||
} | ||
if (isset($data['package_specifier'])) { | ||
throw new RuntimeException(sprintf('The importmap entry "%s" cannot have both a "path" and "package_specifier" option.', $importName)); | ||
} | ||
|
||
$entries->add(ImportMapEntry::createLocal($importName, $type, $data['path'], $isEntrypoint)); | ||
|
||
if ($isEntry && ImportMapType::JS !== $type) { | ||
throw new RuntimeException(sprintf('The "entrypoint" option can only be used with the "js" type. Found "%s" in importmap.php for key "%s".', $importName, $type->value)); | ||
continue; | ||
} | ||
|
||
$path = $data['path'] ?? null; | ||
$version = $data['version'] ?? null; | ||
if (null === $version && ($data['url'] ?? null)) { | ||
// BC layer for 6.3->6.4 | ||
$version = $this->extractVersionFromLegacyUrl($data['url']); | ||
} | ||
if (null === $version && null === $path) { | ||
|
||
if (null === $version) { | ||
throw new RuntimeException(sprintf('The importmap entry "%s" must have either a "path" or "version" option.', $importName)); | ||
} | ||
if (null !== $version && null !== $path) { | ||
throw new RuntimeException(sprintf('The importmap entry "%s" cannot have both a "path" and "version" option.', $importName)); | ||
} | ||
|
||
[$packageName, $filePath] = self::splitPackageNameAndFilePath($importName); | ||
|
||
$entries->add(new ImportMapEntry( | ||
$importName, | ||
path: $path, | ||
version: $version, | ||
type: $type, | ||
isEntrypoint: $isEntry, | ||
packageName: $packageName, | ||
filePath: $filePath, | ||
)); | ||
$packageModuleSpecifier = $data['package_specifier'] ?? $importName; | ||
$entries->add($this->createRemoteEntry($importName, $type, $version, $packageModuleSpecifier, $isEntrypoint)); | ||
} | ||
|
||
return $this->rootImportMapEntries = $entries; | ||
|
@@ -91,19 +90,21 @@ public function writeEntries(ImportMapEntries $entries): void | |
$importMapConfig = []; | ||
foreach ($entries as $entry) { | ||
$config = []; | ||
if ($entry->path) { | ||
$path = $entry->path; | ||
$config['path'] = $path; | ||
} | ||
if ($entry->version) { | ||
if ($entry->isRemotePackage()) { | ||
$config['version'] = $entry->version; | ||
if ($entry->packageModuleSpecifier !== $entry->importName) { | ||
$config['package_specifier'] = $entry->packageModuleSpecifier; | ||
} | ||
} else { | ||
$config['path'] = $entry->path; | ||
} | ||
if (ImportMapType::JS !== $entry->type) { | ||
$config['type'] = $entry->type->value; | ||
} | ||
if ($entry->isEntrypoint) { | ||
$config['entrypoint'] = true; | ||
} | ||
|
||
$importMapConfig[$entry->importName] = $config; | ||
} | ||
|
||
|
@@ -129,6 +130,13 @@ public function writeEntries(ImportMapEntries $entries): void | |
EOF); | ||
} | ||
|
||
public function createRemoteEntry(string $importName, ImportMapType $type, string $version, string $packageModuleSpecifier, bool $isEntrypoint): ImportMapEntry | ||
{ | ||
$path = $this->remotePackageStorage->getDownloadPath($packageModuleSpecifier, $type); | ||
|
||
return ImportMapEntry::createRemote($importName, $type, $path, $version, $packageModuleSpecifier, $isEntrypoint); | ||
} | ||
|
||
public function getRootDirectory(): string | ||
{ | ||
return \dirname($this->importMapConfigPath); | ||
|
@@ -148,18 +156,4 @@ private function extractVersionFromLegacyUrl(string $url): ?string | |
|
||
return substr($url, $lastAt + 1, $nextSlash - $lastAt - 1); | ||
} | ||
|
||
public static function splitPackageNameAndFilePath(string $packageName): array | ||
{ | ||
$filePath = ''; | ||
$i = strpos($packageName, '/'); | ||
|
||
if ($i && (!str_starts_with($packageName, '@') || $i = strpos($packageName, '/', $i + 1))) { | ||
// @vendor/package/filepath or package/filepath | ||
$filePath = substr($packageName, $i); | ||
$packageName = substr($packageName, 0, $i); | ||
} | ||
|
||
return [$packageName, $filePath]; | ||
} | ||
} |
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.
Check is done in
ImportMapManager
with a nice error