Lazy controllers for user-land controllers #19
Merged
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.
Hi!
This allows users to make their own controllers "lazy" by adding a special comment above their controllers:
When you do this, your controller (and its dependencies) are not included in the main
app.js
built file. Instead, they are split into their own chunk/file. This file is loaded asynchronously the moment that the first element appears on the page for this controller (e.g.<div data-controller="hello">
). This follows up on #15 (and only the last commit is new to this PR). The/* stimulusFetch: 'lazy' */
is inspired by Webpack's/* webpackMode: 'lazy' */
comments and uses the same mechanism to parse them.To activate this, you only need to process your controllers through a new loader:
That looks a bit ugly, but this is code that we'll give users via the recipe anyways. I've tested this in a real app and it works beautifully ❤️ .
Cheers!