Skip to content

Commit d6ff6c1

Browse files
committed
[AssetMapper] Fix JavaScript compiler create self-referencing imports
1 parent c2cc75d commit d6ff6c1

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
9292
return $fullImportString;
9393
}
9494

95+
// Ignore self-referencing import
96+
if ($dependentAsset->logicalPath === $asset->logicalPath) {
97+
return $fullImportString;
98+
}
99+
95100
// List as a JavaScript import.
96101
// This will cause the asset to be included in the importmap (for relative imports)
97102
// and will be used to generate the preloads in the importmap.

src/Symfony/Component/AssetMapper/Tests/Compiler/JavaScriptImportPathCompilerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,34 @@ public function testCompileHandlesCircularBareImportAssets()
574574
$this->assertSame($popperAsset->logicalPath, $bootstrapAsset->getJavaScriptImports()[0]->assetLogicalPath);
575575
}
576576

577+
public function testCompileIgnoresSelfReferencingBareImportAssets()
578+
{
579+
$bootstrapAsset = new MappedAsset('foo.js', 'foo.js', 'foo.js');
580+
581+
$importMapConfigReader = $this->createMock(ImportMapConfigReader::class);
582+
$importMapConfigReader->expects($this->once())
583+
->method('findRootImportMapEntry')
584+
->with('foobar')
585+
->willReturn(ImportMapEntry::createRemote('foobar', ImportMapType::JS, 'foo.js', '1.2.3', 'foobar', false));
586+
$importMapConfigReader->expects($this->any())
587+
->method('convertPathToFilesystemPath')
588+
->with('foo.js')
589+
->willReturn('foo.js');
590+
591+
$assetMapper = $this->createMock(AssetMapperInterface::class);
592+
$assetMapper->expects($this->once())
593+
->method('getAssetFromSourcePath')
594+
->with('foo.js')
595+
->willReturn($bootstrapAsset);
596+
597+
$compiler = new JavaScriptImportPathCompiler($importMapConfigReader);
598+
$input = 'import { foo } from "foobar";';
599+
$compiler->compile($input, $bootstrapAsset, $assetMapper);
600+
$this->assertCount(0, $bootstrapAsset->getDependencies());
601+
$this->assertCount(0, $bootstrapAsset->getFileDependencies());
602+
$this->assertCount(0, $bootstrapAsset->getJavaScriptImports());
603+
}
604+
577605
/**
578606
* @dataProvider provideMissingImportModeTests
579607
*/

0 commit comments

Comments
 (0)