Skip to content
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

Add precacheImportVariables option for faster dynamic import builds #15

Merged
merged 4 commits into from
Sep 19, 2016

Conversation

exogen
Copy link

@exogen exogen commented Sep 18, 2016

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 a precacheImportVariables 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:

  • Before: 27 render attempts in 33 minutes
  • After: 1 render attempt in 5 minutes

@dstevensio
Copy link

👍 I like what I see

@jeffrifwald
Copy link

LGTM

@exogen exogen merged commit 33e0191 into master Sep 19, 2016
@exogen exogen deleted the visit-evaluate-static branch September 19, 2016 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants