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

Standardise lazy requires #4729

Merged
merged 4 commits into from May 3, 2020
Merged

Standardise lazy requires #4729

merged 4 commits into from May 3, 2020

Conversation

jeddy3
Copy link
Member

@jeddy3 jeddy3 commented May 1, 2020

Which issue, if any, is this issue related to?

#2454

Is there anything in the PR that needs further explanation?

In this pull request, I've standardised how we lazy/dynamic-require:

  • formatters
  • rules
  • syntaxes

Previously we were using lots of different workarounds to get the require-time down. I'm now only using import-lazy.

This lays the foundation for being able to bundle code, closing #2454 and #3935.

This pull request doesn't improve the require-time much, because we'd already optimised all that we could without bundling (using the various workarounds). Require-time does seem more consistent, though, normally coming in at the 120 mark.

This pull request does, however, improve the time taken to run stylelint when the auto syntax is used because all the syntaxes are now lazy-required.

Testing locally with

{
  "code": "a { color: #fff; }", // or sass equivalent
  "config": "{ rules: { "color-hex-length": "long" } }"
}

On master:

// no syntax
node test.js  0.44s user 0.14s system 0.651 total

// no syntax, sass file
node test.js  0.43s user 0.10s system 0.787 total

// syntax: “css”
node test.js  0.17s user 0.05s system 0.294 total

This branch:

// no syntax
node test.js  0.16s user 0.04s system 0.232 total

// no syntax, sass file
node test.js  0.19s user 0.04s system 0.221 total

// syntax: “css”
node test.js  0.16s user 0.04s system 0.200 total

Using the auto syntax is almost comparable to using the syntax option in this branch, whereas it's significantly slower in master.

This improvement is reflected in the test suite which now runs in 72s (down from 92s).

The import-lazy library has two patterns:

I'm using the former for formatters and rules, and the latter for syntaxes. This will allow us to bundle syntaxes separately in the future.

As a consequence, it exposed a shortcoming in how we were excluding files from TypeScript. Previously all the rule files were ignored because they were excluded and dynamically required using require("rules" + rulename), which TypeScript couldn't resolve. As we are now using a lazy-require pattern it can resolve, we have to explicitly mark each rule file with a // @ts-nocheck. This is arguably cleaner as we were previously relying on a quirk of the TypeScript's module resolution. The include and exclude TypeScript config options are cleaner too now.

Copy link
Member

@hudochenkov hudochenkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

@@ -0,0 +1,5 @@
declare module 'import-lazy' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be moved to types/global.d.ts?

Copy link
Member

@m-allanson m-allanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, looks good!

@jeddy3 jeddy3 merged commit 160c6ae into master May 3, 2020
@jeddy3 jeddy3 deleted the standardise-lazy-requires branch May 3, 2020 08:07
@jeddy3
Copy link
Member Author

jeddy3 commented May 3, 2020

  • Fixed: improved performance when auto syntax is used (#4729).

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.

None yet

3 participants