Skip to content

Commit 373d501

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

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,23 @@ private function loadCustomControllers(): array
7070

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

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

src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php

Lines changed: 2 additions & 0 deletions
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')) {
38+
return null;
3739
} else {
3840
// replace windows slashes
3941
$path = str_replace('\\', '/', $path);
Lines changed: 7 additions & 0 deletions
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)