8000 [AssetMapper] Split ImportmapManager into 2 · symfony/symfony@da648b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit da648b1

Browse files
weaverryanfabpot
authored andcommitted
[AssetMapper] Split ImportmapManager into 2
1 parent 5929aa1 commit da648b1

14 files changed

+1091
-946
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Symfony\Component\AssetMapper\Factory\MappedAssetFactory;
3232
use Symfony\Component\AssetMapper\ImportMap\ImportMapAuditor;
3333
use Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader;
34+
use Symfony\Component\AssetMapper\ImportMap\ImportMapGenerator;
3435
use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
3536
use Symfony\Component\AssetMapper\ImportMap\ImportMapRenderer;
3637
use Symfony\Component\AssetMapper\ImportMap\ImportMapUpdateChecker;
@@ -101,7 +102,7 @@
101102
->args([
102103
service('asset_mapper.public_assets_path_resolver'),
103104
service('asset_mapper'),
104-
service('asset_mapper.importmap.manager'),
105+
service('asset_mapper.importmap.generator'),
105106
service('filesystem'),
106107
param('kernel.project_dir'),
107108
abstract_arg('public directory name'),
@@ -137,7 +138,7 @@
137138

138139
->set('asset_mapper.compiler.javascript_import_path_compiler', JavaScriptImportPathCompiler::class)
139140
->args([
140-
service('asset_mapper.importmap.manager'),
141+
service('asset_mapper.importmap.config_reader'),
141142
abstract_arg('missing import mode'),
142143
service('logger'),
143144
])
@@ -153,13 +154,19 @@
153154
->set('asset_mapper.importmap.manager', ImportMapManager::class)
154155
->args([
155156
service('asset_mapper'),
156-
service('asset_mapper.public_assets_path_resolver'),
157157
service('asset_mapper.importmap.config_reader'),
158158
service('asset_mapper.importmap.remote_package_downloader'),
159159
service('asset_mapper.importmap.resolver'),
160160
])
161161
->alias(ImportMapManager::class, 'asset_mapper.importmap.manager')
162162

163+
->set('asset_mapper.importmap.generator', ImportMapGenerator::class)
164+
->args([
165+
service('asset_mapper'),
166+
service('asset_mapper.public_assets_path_resolver'),
167+
service('asset_mapper.importmap.config_reader'),
168+
])
169+
163170
->set('asset_mapper.importmap.remote_package_storage', RemotePackageStorage::class)
164171
->args([
165172
abstract_arg('vendor directory'),
@@ -183,7 +190,7 @@
183190

184191
->set('asset_mapper.importmap.renderer', ImportMapRenderer::class)
185192
->args([
186-
service('asset_mapper.importmap.manager'),
193+
service('asset_mapper.importmap.generator'),
187194
service('assets.packages')->nullOnInvalid(),
188195
param('kernel.charset'),
189196
abstract_arg('polyfill URL'),
@@ -205,7 +212,6 @@
205212
->set('asset_mapper.importmap.command.require', ImportMapRequireCommand::class)
206213
->args([
207214
service('asset_mapper.importmap.manager'),
208-
param('kernel.project_dir'),
209215
service('asset_mapper.importmap.version_checker'),
210216
])
211217
->tag('console.command')

src/Symfony/Component/AssetMapper/Command/AssetMapperCompileCommand.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\AssetMapper\AssetMapper;
1515
use Symfony\Component\AssetMapper\AssetMapperInterface;
1616
use Symfony\Component\AssetMapper\Event\PreAssetsCompileEvent;
17-
use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
17+
use Symfony\Component\AssetMapper\ImportMap\ImportMapGenerator;
1818
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
1919
use Symfony\Component\Console\Attribute\AsCommand;
2020
use Symfony\Component\Console\Command\Command;
@@ -38,7 +38,7 @@ final class AssetMapperCompileCommand extends Command
3838
public function __construct(
3939
private readonly PublicAssetsPathResolverInterface $publicAssetsPathResolver,
4040
private readonly AssetMapperInterface $assetMapper,
41-
private readonly ImportMapManager $importMapManager,
41+
private readonly ImportMapGenerator $importMapGenerator,
4242
private readonly Filesystem $filesystem,
4343
private readonly string $projectDir,
4444
private readonly string $publicDirName,
@@ -81,12 +81,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8181
$manifestPath = $outputDir.'/'.AssetMapper::MANIFEST_FILE_NAME;
8282
$files[] = $manifestPath;
8383

84-
$importMapPath = $outputDir.'/'.ImportMapManager::IMPORT_MAP_CACHE_FILENAME;
84+
$importMapPath = $outputDir.'/'.ImportMapGenerator::IMPORT_MAP_CACHE_FILENAME;
8585
$files[] = $importMapPath;
8686

8787
$entrypointFilePaths = [];
88-
foreach ($this->importMapManager->getEntrypointNames() as $entrypointName) {
89-
$dumpedEntrypointPath = $outputDir.'/'.sprintf(ImportMapManager::ENTRYPOINT_CACHE_FILENAME_PATTERN, $entrypointName);
88+
foreach ($this->importMapGenerator->getEntrypointNames() as $entrypointName) {
89+
$dumpedEntrypointPath = $outputDir.'/'.sprintf(ImportMapGenerator::ENTRYPOINT_CACHE_FILENAME_PATTERN, $entrypointName);
9090
$files[] = $dumpedEntrypointPath;
9191
$entrypointFilePaths[$entrypointName] = $dumpedEntrypointPath;
9292
}
@@ -105,12 +105,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
105105
$this->filesystem->dumpFile($manifestPath, json_encode($manifest, \JSON_PRETTY_PRINT));
106106
$io->comment(sprintf('Manifest written to <info>%s</info>', $this->shortenPath($manifestPath)));
107107

108-
$this->filesystem->dumpFile($importMapPath, json_encode($this->importMapManager->getRawImportMapData(), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG));
108+
$this->filesystem->dumpFile($importMapPath, json_encode($this->importMapGenerator->getRawImportMapData(), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG));
109109
$io->comment(sprintf('Import map data written to <info>%s</info>.', $this->shortenPath($importMapPath)));
110110

111-
$entrypointNames = $this->importMapManager->getEntrypointNames();
111+
$entrypointNames = $this->importMapGenerator->getEntrypointNames();
112112
foreach ($entrypointFilePaths as $entrypointName => $path) {
113-
$this->filesystem->dumpFile($path, json_encode($this->importMapManager->getEntrypointMetadata($entrypointName), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG));
113+
$this->filesystem->dumpFile($path, json_encode($this->importMapGenerator->findEagerEntrypointImports($entrypointName), \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG));
114114
}
115115
$styledEntrypointNames = array_map(fn (string $entrypointName) => sprintf('<info>%s</>', $entrypointName), $entrypointNames);
116116
$io->comment(sprintf('Entrypoint metadata written for <comment>%d</> entrypoints (%s).', \count($entrypointNames), implode(', ', $styledEntrypointNames)));

src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ final class ImportMapRequireCommand extends Command
3333

3434
public function __construct(
3535
private readonly ImportMapManager $importMapManager,
36-
private readonly string $projectDir,
3736
private readonly ImportMapVersionChecker $importMapVersionChecker,
3837
) {
3938
parent::__construct();

src/Symfony/Component/AssetMapper/Compiler/JavaScriptImportPathCompiler.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\AssetMapper\AssetMapperInterface;
1616
use Symfony\Component\AssetMapper\Exception\CircularAssetsException;
1717
use Symfony\Component\AssetMapper\Exception\RuntimeException;
18-
use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
18+
use Symfony\Component\AssetMapper\ImportMap\ImportMapConfigReader;
1919
use Symfony\Component\AssetMapper\ImportMap\JavaScriptImport;
2020
use Symfony\Component\AssetMapper\MappedAsset;
2121

@@ -32,7 +32,7 @@ final class JavaScriptImportPathCompiler implements AssetCompilerInterface
3232
private const IMPORT_PATTERN = '/(?:import\s*(?:(?:\*\s*as\s+\w+|[\w\s{},*]+)\s*from\s*)?|\bimport\()\s*[\'"`](\.\/[^\'"`]+|(\.\.\/)*[^\'"`]+)[\'"`]\s*[;\)]?/m';
3333

3434
public function __construct(
35-
private readonly ImportMapManager $importMapManager,
35+
private readonly ImportMapConfigReader $importMapConfigReader,
3636
private readonly string $missingImportMode = self::MISSING_IMPORT_WARN,
3737
private readonly ?LoggerInterface $logger = null,
3838
) {
@@ -139,7 +139,7 @@ private function isCommentedOut(mixed $offsetStart, string $fullContent): bool
139139

140140
private function findAssetForBareImport(string $importedModule, AssetMapperInterface $assetMapper): ?MappedAsset
141141
{
142-
if (!$importMapEntry = $this->importMapManager->findRootImportMapEntry($importedModule)) {
142+
if (!$importMapEntry = $this->importMapConfigReader->findRootImportMapEntry($importedModule)) {
143143
// don't warn on missing non-relative (bare) imports: these could be valid URLs
144144

145145
return null;

src/Symfony/Component/AssetMapper/ImportMap/ImportMapConfigReader.php

+7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public function writeEntries(ImportMapEntries $entries): void
130130
EOF);
131131
}
132132

133+
public function findRootImportMapEntry(string $moduleName): ?ImportMapEntry
134+
{
135+
$entries = $this->getEntries();
136+
137+
return $entries->has($moduleName) ? $entries->get($moduleName) : null;
138+
}
139+
133140
public function createRemoteEntry(string $importName, ImportMapType $type, string $version, string $packageModuleSpecifier, bool $isEntrypoint): ImportMapEntry
134141
{
135142
$path = $this->remotePackageStorage->getDownloadPath($packageModuleSpecifier, $type);

0 commit comments

Comments
 (0)
0