Add precacheImportVariables option for faster dynamic import builds #15
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.
Since we can't know what values Stylus variables will have at runtime until we actually render, we can't resolve dynamic imports (those with variables) ahead of time. That means encountering a dynamic import requires that we bail out, resolve the import with webpack, then try rendering again. For large import trees with many dynamic imports, the result can be many render attempts and a slow build.
This adds a new option,
precacheImportVariables
. If variables used in imports tend to be one of a few values, like Walmart's$tenant
variable, then we can just guess ahead of time that we'll need to resolve certain paths before rendering. In many cases this can cut the build back down to a single render attempt.Note that variables can still have any value at runtime regardless of
precacheImportVariables
, but supplying aprecacheImportVariables
value that results in a "cache hit" will make things faster.To make this possible in a reasonable way, this switches import detection from a simple regular expression back to parsing each file and visiting the AST.
Results on one Walmart app: