Skip to content

Commit da0e8b8

Browse files
[StimulusBundle] Add support for typescript controllers
1 parent 8fe391d commit da0e8b8

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

src/StimulusBundle/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.php-cs-fixer.cache
22
.phpunit.result.cache
33
composer.lock
4+
var/
45
vendor/
56
tests/fixtures/var

src/StimulusBundle/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.x
4+
5+
- Added Typescript controllers support
6+
37
## 2.13.2
48

59
- Revert "Change JavaScript package to `type: module`"

src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
*/
2929
class ControllersMapGenerator
3030
{
31+
private const FILENAME_REGEX = '/^.*[-_](controller\.[jt]s)$/';
32+
3133
public function __construct(
3234
private AssetMapperInterface $assetMapper,
3335
private UxPackageReader $uxPackageReader,
@@ -66,12 +68,14 @@ private function loadCustomControllers(): array
6668
$finder = new Finder();
6769
$finder->in($this->controllerPaths)
6870
->files()
69-
->name('/^.*[-_]controller\.js$/');
71+
->name(self::FILENAME_REGEX);
7072

7173
$controllersMap = [];
7274
foreach ($finder as $file) {
7375
$name = $file->getRelativePathname();
74-
$name = str_replace(['_controller.js', '-controller.js'], '', $name);
76+
// use regex to extract 'controller'-postfix including extension
77+
preg_match(self::FILENAME_REGEX, $name, $matches);
78+
$name = str_replace(['_'.$matches[1], '-'.$matches[1]], '', $name);
7579
$name = str_replace(['_', '/'], ['-', '--'], $name);
7680

7781
$asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath());

src/StimulusBundle/tests/AssetMapper/ControllerMapGeneratorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public function testGetControllersMap()
7373
$map = $generator->getControllersMap();
7474
// + 3 controller.json UX controllers
7575
// - 1 controllers.json UX controller is disabled
76-
// + 8 custom controllers (1 file is not a controller & 1 is overridden)
77-
$this->assertCount(10, $map);
76+
// + 9 custom controllers (1 file is not a controller & 1 is overridden)
77+
$this->assertCount(11, $map);
7878
$packageNames = array_keys($map);
7979
sort($packageNames);
8080
$this->assertSame([
@@ -88,6 +88,7 @@ public function testGetControllersMap()
8888
'subdir--deeper',
8989
'subdir--deeper-with-dashes',
9090
'subdir--deeper-with-underscores',
91+
'typescript',
9192
], $packageNames);
9293

9394
$controllerSecond = $map['fake-vendor--ux-package1--controller-second'];

src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ public function testFullApplicationLoad()
5252
$this->assertSame([
5353
// 1x import from loader.js (which is aliased to @symfony/stimulus-bundle via importmap)
5454
'/assets/@symfony/stimulus-bundle/controllers.js',
55-
// 6x from "controllers" (hello is overridden)
55+
// 7x from "controllers" (hello is overridden)
5656
'/assets/controllers/bye_controller.js',
5757
'/assets/controllers/hello-with-dashes-controller.js',
5858
'/assets/controllers/hello_with_underscores-controller.js',
5959
'/assets/controllers/subdir/deeper-controller.js',
6060
'/assets/controllers/subdir/deeper-with-dashes-controller.js',
6161
'/assets/controllers/subdir/deeper_with_underscores-controller.js',
62+
'/assets/controllers/typescript-controller.ts',
6263
// 2x from UX packages, which are enabled in controllers.json
6364
'/assets/fake-vendor/ux-package1/package-controller-second.js',
6465
'/assets/fake-vendor/ux-package2/package-hello-controller.js',
@@ -92,7 +93,7 @@ public function testFullApplicationLoad()
9293
$preLoadHrefs = $crawler->filter('link[rel="modulepreload"]')->each(function ($link) {
9394
return $link->attr('href');
9495
});
95-
$this->assertCount(11, $preLoadHrefs);
96+
$this->assertCount(12, $preLoadHrefs);
9697
sort($preLoadHrefs);
9798
$this->assertStringStartsWith('/assets/@symfony/stimulus-bundle/controllers-', $preLoadHrefs[0]);
9899
$this->assertStringStartsWith('/assets/@symfony/stimulus-bundle/loader-', $preLoadHrefs[1]);
@@ -101,9 +102,10 @@ public function testFullApplicationLoad()
101102
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-controller-', $preLoadHrefs[5]);
102103
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-with-dashes-controller-', $preLoadHrefs[6]);
103104
$this->assertStringStartsWith('/assets/controllers/subdir/deeper_with_underscores-controller-', $preLoadHrefs[7]);
104-
$this->assertStringStartsWith('/assets/fake-vendor/ux-package2/package-hello-controller-', $preLoadHrefs[8]);
105-
$this->assertStringStartsWith('/assets/more-controllers/hello-controller-', $preLoadHrefs[9]);
106-
$this->assertStringStartsWith('/assets/vendor/@hotwired/stimulus/stimulus.index', $preLoadHrefs[10]);
105+
$this->assertStringStartsWith('/assets/controllers/typescript-controller-', $preLoadHrefs[8]);
106+
$this->assertStringStartsWith('/assets/fake-vendor/ux-package2/package-hello-controller-', $preLoadHrefs[9]);
107+
$this->assertStringStartsWith('/assets/more-controllers/hello-controller-', $preLoadHrefs[10]);
108+
$this->assertStringStartsWith('/assets/vendor/@hotwired/stimulus/stimulus.index', $preLoadHrefs[11]);
107109
} else {
108110
// legacy
109111
$this->assertSame([
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// typescript-controller.js
2+
// @ts-ignore
3+
import { Controller } from '@hotwired/stimulus';
4+
5+
export default class extends Controller {
6+
}

0 commit comments

Comments
 (0)