Support Symfony's Stimulus Bridge#431
Open
marcmorente wants to merge 17 commits intomarcoroth:mainfrom
Open
Conversation
This prevents the package from being ignored during package analysis.
Add analyzeStimulusBridgeLazyLoading() method to parse startStimulusApp() calls with require.context using the @symfony/stimulus-bridge/lazy-controller-loader! prefix pattern.
Add fixture project with bootstrap.js and controller demonstrating the Symfony Stimulus Bridge lazy loading pattern.
Add integration test verifying controllers are detected with stimulus-loading-lazy load mode when using the Symfony Stimulus Bridge.
This package is used by Symfony projects with AssetMapper to bootstrap Stimulus applications. Adding it to helperPackages enables detection of controllers index files that import from this package.
Detects if a project uses Symfony AssetMapper by checking for the presence of importmap.php or config/packages/stimulus.yaml files. Uses lazy initialization with existsSync for efficient repeated access.
…Index
- Added hasStimulusBundleImport to detect imports from @symfony/stimulus-bundle
- Updated isStimulusControllersIndex to return true for files with
@symfony/stimulus-bundle import when project uses AssetMapper
This allows detection of AssetMapper bootstrap files like:
import { startStimulusApp } from '@symfony/stimulus-bundle'
startStimulusApp()
AssetMapper uses a simpler pattern than Webpack Encore: - Webpack: startStimulusApp(require.context(...)) - AssetMapper: startStimulusApp() This change: - Detects AssetMapper projects via Project.isAssetMapper - Handles startStimulusApp() calls without context arguments - Defaults controller root to 'controllers/' subdirectory Also removed unused fileExists import and redundant hasStimulusBundle check.
This fixture tests AssetMapper support with: - assets/bootstrap.js: Uses startStimulusApp() without context (AssetMapper pattern) - assets/controllers/hello_controller.js: Sample Stimulus controller The fixture is set up to use a mock @symfony/stimulus-bundle package since we can't install the real package in test fixtures.
The symfony-asset-mapper fixture needs to import from @symfony/stimulus-bundle but can't use the real package. This change creates a minimal mock package that exports the startStimulusApp function needed for testing. The mock package is created at: test/fixtures/symfony-asset-mapper/node_modules/@symfony/stimulus-bundle/ With: - package.json with name, version, and main entry point - loader.js exporting startStimulusApp function
Tests that: - AssetMapper project is correctly detected - Controllers index file (bootstrap.js) is found - The hello controller is registered with stimulus-loading-lazy mode - Controller root is set to assets/controllers
…plicationFileImport The original code had a subtle bug where undefined === undefined evaluates to true, causing all files to incorrectly return true for hasResolvedStimulusApplicationFileImport when applicationFile was not defined. This fix adds an early return when applicationFile is undefined.
The AssetMapper detection previously required importmap.php or config/packages/stimulus.yaml files. However, projects using @symfony/stimulus-bundle should be detected even without these files present. This change: - Adds check for hasStimulusBundleImport to the early return guard - Updates the AssetMapper branch to trigger when hasStimulusBundle is true - Allows detection of AssetMapper projects that only import @symfony/stimulus-bundle
Owner
|
Thank you so much @marcmorente! I haven't forgotten about this and I'm going to take a look! 🙏🏼 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves #67
Problem
Stimulus controllers in Symfony projects were not being detected by stimulus-parser, causing several issues in stimulus-lsp:
data-controller="..."Root Cause
Symfony supports two different ways to integrate Stimulus, and the parser needed to support both:
Pattern 1:
@symfony/stimulus-bridge(Webpack Encore)Pattern 2:
@symfony/stimulus-bundle(AssetMapper)The parser originally only supported Pattern 1. Pattern 2 uses a simpler
startStimulusApp()call without therequire.context()wrapper, which went unrecognized.Solution
Added comprehensive support for both Symfony Stimulus integration patterns:
For
@symfony/stimulus-bridge:@symfony/stimulus-bridgeas a helper packagestartStimulusApp()calls withrequire.context()using the@symfony/stimulus-bridge/lazy-controller-loader!prefixstimulus-loading-lazyload modeFor
@symfony/stimulus-bundle(AssetMapper):@symfony/stimulus-bundleas a helper packageimportmap.phporconfig/packages/stimulus.yamlstartStimulusApp()calls without context argumentscontrollers/subdirectoryResult
After this fix, stimulus-lsp correctly:
Key Differences at a Glance
startStimulusApp(require.context(...))startStimulusApp()./controllers./controllers