-
-
Notifications
You must be signed in to change notification settings - Fork 356
[StimulusBundle] Skip mapping .ts controller if .js version is available #2702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -70,13 +70,22 @@ private function loadCustomControllers(): array | |||
|
||||
$controllersMap = []; | ||||
foreach ($finder as $file) { | ||||
// Skip .ts controller if .js version is available | ||||
if ('ts' === $file->getExtension() && file_exists(substr($file->getRealPath(), 0, -2).'js')) { | ||||
continue; | ||||
} | ||||
|
||||
$name = $file->getRelativePathname(); | ||||
// use regex to extract 'controller'-postfix including extension | ||||
preg_match(self::FILENAME_REGEX, $name, $matches); | ||||
$name = str_replace(['_'.$matches[1], '-'.$matches[1]], '', $name); | ||||
$name = str_replace(['_', '/', '\\'], ['-', '--', '--'], $name); | ||||
|
||||
$asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath()); | ||||
if (!$asset) { | ||||
throw new \RuntimeException(\sprintf('Could not find an asset mapper path that points to the "%s" controller.', $name)); | ||||
} | ||||
Comment on lines
+85
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove this... as there is no other situation it could happen right ? Or let's add a test if possible There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can happen anytime there are
|
||||
|
||||
$content = file_get_contents($asset->sourcePath); | ||||
$isLazy = preg_match('/\/\*\s*stimulusFetch:\s*\'lazy\'\s*\*\//i', $content); | ||||
|
||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// excluded-controller.js | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// other-controller.js | ||
// @ts-ignore | ||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seen the code just belove, what about we sort files
by name
ascending (.js
would be before.ts
)That way:
would only register the first one in the controller map, sa they would all have "foo-bar" as name.
We could
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a similar idea but later found out that controllers should be overridable. There is a test which checks for this feature:
ux/src/StimulusBundle/tests/AssetMapper/ControllersMapGeneratorTest.php
Line 115 in 61819ae
So skipping previously mapped controllers with the same name (
if (isset($controllersMap[$name])) continue;
) breaks that test since it doesn't allow controllers to be overridden.And since controllers can be overridden, we probably don't want to
sortByName()
as that may break the override order given in the controller paths.For example, given the following
config/packages/stimulus.yaml
:Then
controllers_a/hello-controller.js
would overridecontrollers_z/hello-controller.js
. But if we callsortByName()
thencontrollers_z/hello-controller.js
would overridecontrollers_a/hello-controller.js
. So devs could no longer specify controller override order instimulus.yaml
.When you say:
Are you referring to the
file_exists
call? I haven't looked at the PHP source code but according to this page: The file_exists doesn’t actually open the file. It just checks the file system to see if the file or directory is there where you specified. I'm assumingfile_exists
queries the filesystem instead of trying to allocate a file descriptor/read the file, so it shouldn't lock the file or otherwise consume resources. Also theloadCustomControllers
function is called once, either when runningconsole asset-map:compile
or when building the dev/test cache, so any performance impact should be negligible.Another nice thing about
if ($file->getExtension() === 'ts' && file_exists())
solution is the behaviour is explicit and localized to those few lines, so theloadCustomControllers
function is easier to understand and refactor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the solution
if ($file->getExtension() === 'ts' && file_exists())
which is more explicit than asortByName()
that works "by miracle" (I mean, without showing the intention behind)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you say:
Ah, I think I know what you mean now, i.e. skipping previously mapped controllers would bypass
ux/src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php
Line 80 in 61819ae
The
file_exists()
solution will also avoid opening files unecessarily.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ol you guys focused on the TS thing, but i was talking more generally of names vs path, seen the str_replace two lines later.
Nevermind, i'll open a PR after this one :)