-
-
Notifications
You must be signed in to change notification settings - Fork 108
no-custom-classname: perf optimization #338
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
Merged
francoismassart
merged 3 commits into
francoismassart:master
from
XantreDev:no-custom-classname-perf-improvements
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,17 @@ | |
| const fg = require('fast-glob'); | ||
| const fs = require('fs'); | ||
| const postcss = require('postcss'); | ||
| const removeDuplicatesFromArray = require('./removeDuplicatesFromArray'); | ||
|
|
||
| let previousGlobsResults = []; | ||
| const classRegexp = /\.([^\.\,\s\n\:\(\)\[\]\'~\+\>\*\\]*)/gim; | ||
|
|
||
| let lastUpdate = null; | ||
| let classnamesFromFiles = []; | ||
|
|
||
| /** | ||
| * @type {Map<string, number} | ||
| */ | ||
| const prevEditedTimestamp = new Map() | ||
|
|
||
| /** | ||
| * Read CSS files and extract classnames | ||
| * @param {Array} patterns Glob patterns to locate files | ||
|
|
@@ -17,27 +22,56 @@ let classnamesFromFiles = []; | |
| */ | ||
| const generateClassnamesListSync = (patterns, refreshRate = 5_000) => { | ||
| const now = new Date().getTime(); | ||
| const files = fg.sync(patterns, { suppressErrors: true }); | ||
| const newGlobs = previousGlobsResults.flat().join(',') != files.flat().join(','); | ||
| const expired = lastUpdate === null || now - lastUpdate > refreshRate; | ||
| if (newGlobs || expired) { | ||
| previousGlobsResults = files; | ||
| lastUpdate = now; | ||
| let detectedClassnames = []; | ||
| for (const file of files) { | ||
| const data = fs.readFileSync(file, 'utf-8'); | ||
| const root = postcss.parse(data); | ||
| root.walkRules((rule) => { | ||
| const regexp = /\.([^\.\,\s\n\:\(\)\[\]\'~\+\>\*\\]*)/gim; | ||
| const matches = [...rule.selector.matchAll(regexp)]; | ||
| const classnames = matches.map((arr) => arr[1]); | ||
| detectedClassnames.push(...classnames); | ||
| }); | ||
| detectedClassnames = removeDuplicatesFromArray(detectedClassnames); | ||
|
|
||
| if (!expired) { | ||
| return classnamesFromFiles; | ||
| } | ||
| const files = fg.sync(patterns, { suppressErrors: true, stats: true }); | ||
| lastUpdate = now; | ||
|
|
||
| /** | ||
| * @type {Set<string} | ||
| */ | ||
| const detectedClassnames = new Set(); | ||
| /** | ||
| * @type {Set<string>} | ||
| */ | ||
| const filesSet = new Set(); | ||
| for (const { path: file, stats } of files) { | ||
| filesSet.add(file); | ||
| if (!stats) {} | ||
| // file is not changed -> we do need to do extra work | ||
| else if (prevEditedTimestamp.get(file) === stats.mtimeMs) { | ||
| continue; | ||
| } else { | ||
| prevEditedTimestamp.set(file, stats.mtimeMs); | ||
| } | ||
| classnamesFromFiles = detectedClassnames; | ||
| const data = fs.readFileSync(file, 'utf-8'); | ||
| const root = postcss.parse(data); | ||
| root.walkRules((rule) => { | ||
| for (const match of rule.selector.matchAll(classRegexp)) { | ||
| detectedClassnames.add(match[1]); | ||
| } | ||
| }); | ||
| } | ||
| return classnamesFromFiles; | ||
| // avoiding memory leak | ||
| { | ||
| /** | ||
| * @type {string[]} | ||
| */ | ||
| const keysToDelete = [] | ||
| for (const cachedFilePath of prevEditedTimestamp.keys()) { | ||
| if (!filesSet.has(cachedFilePath)) { | ||
| keysToDelete.push(cachedFilePath); | ||
| } | ||
| } | ||
| for (const key of keysToDelete) { | ||
| prevEditedTimestamp.delete(key); | ||
| } | ||
| } | ||
| classnamesFromFiles = [...detectedClassnames]; | ||
| return classnamesFromFiles | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ends up being an empty array since we hit the |
||
| }; | ||
|
|
||
| module.exports = generateClassnamesListSync; | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@XantreDev @francoismassart This seems to have introduced a regression if the linter takes longer than
cssFilesRefreshRateto complete.When iterating through the css files to parse for class names, if the file is unchanged we skip it, meaning that none of the classnames in any of the CSS files are honored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Detected class names need to be also cached
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snyamathi Should it resolve the problem? #341