Skip to content

Commit 63282d6

Browse files
committed
[StimulusBundle] Skip mapping .ts controller if .js version is available
1 parent 61819ae commit 63282d6

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php

+9
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,22 @@ private function loadCustomControllers(): array
7070

7171
$controllersMap = [];
7272
foreach ($finder as $file) {
73+
// Skip .ts controller if .js version is available
74+
if ('ts' === $file->getExtension() && file_exists(substr($file->getRealPath(), 0, -2).'js')) {
75+
continue;
76+
}
77+
7378
$name = $file->getRelativePathname();
7479
// use regex to extract 'controller'-postfix including extension
7580
preg_match(self::FILENAME_REGEX, $name, $matches);
7681
$name = str_replace(['_'.$matches[1], '-'.$matches[1]], '', $name);
7782
$name = str_replace(['_', '/', '\\'], ['-', '--', '--'], $name);
7883

7984
$asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath());
85+
if (!$asset) {
86+
throw new \RuntimeException(\sprintf('Could not find an asset mapper path that points to the "%s" controller.', $name));
87+
}
88+
8089
$content = file_get_contents($asset->sourcePath);
8190
$isLazy = preg_match('/\/\*\s*stimulusFetch:\s*\'lazy\'\s*\*\//i', $content);
8291

src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function testGetControllersMap()
3434
$logicalPath = 'fake-vendor/ux-package1/package-controller-second.js';
3535
} elseif (str_ends_with($path, 'package-hello-controller.js')) {
3636
$logicalPath = 'fake-vendor/ux-package2/package-hello-controller.js';
37+
} elseif (str_ends_with($path, 'other-controller.ts') || str_ends_with($path, 'excluded-controller.js')) {
38+
return null;
3739
} else {
3840
// replace windows slashes
3941
$path = str_replace('\\', '/', $path);
@@ -75,15 +77,18 @@ public function testGetControllersMap()
7577
$autoImportLocator,
7678
);
7779

80+
$this->expectException(\RuntimeException::class);
81+
$this->expectExceptionMessage('Could not find an asset mapper path that points to the "excluded" controller.');
7882
$map = $generator->getControllersMap();
7983
// + 3 controller.json UX controllers
8084
// - 1 controllers.json UX controller is disabled
81-
// + 10 custom controllers (1 file is not a controller & 1 is overridden)
82-
$this->assertCount(12, $map);
85+
// + 11 custom controllers (1 file is not a controller, 1 is overridden)
86+
$this->assertCount(13, $map);
8387
$packageNames = array_keys($map);
8488
sort($packageNames);
8589
$this->assertSame([
8690
'bye',
91+
'excluded',
8792
'fake-vendor--ux-package1--controller-second',
8893
'fake-vendor--ux-package2--hello-controller',
8994
'hello',

src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function testFullApplicationLoad()
6363
// 2x from UX packages, which are enabled in controllers.json
6464
'/assets/fake-vendor/ux-package1/package-controller-second.js',
6565
'/assets/fake-vendor/ux-package2/package-hello-controller.js',
66-
// 3x from more-controllers
66+
// 4x from more-controllers
67+
'/assets/more-controllers/excluded-controller.js',
6768
'/assets/more-controllers/hello-controller.js',
6869
'/assets/more-controllers/minified-controller.js',
6970
'/assets/more-controllers/other-controller.js',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// excluded-controller.js
2+
import { Controller } from '@hotwired/stimulus';
3+
4+
/* stimulusFetch: 'lazy' */
5+
export default class extends Controller {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// other-controller.js
2+
// @ts-ignore
3+
import { Controller } from '@hotwired/stimulus';
4+
5+
/* stimulusFetch: 'lazy' */
6+
export default class extends Controller {
7+
}

0 commit comments

Comments
 (0)