-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Speed up dev prefix generation for long file paths (#1466)
- Loading branch information
Showing
6 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@vanilla-extract/css': patch | ||
--- | ||
|
||
Speed up dev prefix generation for long file paths |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { getDebugFileName } from './getDebugFileName'; | ||
|
||
const testCases = [ | ||
{ | ||
name: 'longPath', | ||
input: | ||
'node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]_re_ctndskkf4y74v2qksfjwfz6ezy/node_modules/braid-design-system/dist/lib/components/ButtonDir/Button.css.mjs', | ||
expected: 'Button', | ||
}, | ||
{ | ||
name: 'emojiPath', | ||
input: 'node_modules/my_package/dist/👨👩👦Test🎉Dir👨🚀/Test🎉File👨🚀.css.ts', | ||
expected: 'Test🎉File👨🚀', | ||
}, | ||
{ | ||
name: 'loneSurrogates', | ||
input: 'node_modules/my_package/dist/Test\uD801Dir/Test\uDC01File.css.ts', | ||
expected: 'Test\uDC01File', | ||
}, | ||
{ | ||
name: 'noExtension', | ||
input: 'node_modules/my_package/dist/TestDir/TestFile', | ||
expected: '', | ||
}, | ||
{ | ||
name: 'singleFileSparator', | ||
input: 'src/Button.css.ts', | ||
expected: 'Button', | ||
}, | ||
{ | ||
name: 'noDir', | ||
input: 'myFile.css.ts', | ||
expected: 'myFile', | ||
}, | ||
]; | ||
|
||
describe('getDebugFileName', () => { | ||
testCases.forEach(({ name, input, expected }) => { | ||
it(name, () => { | ||
const result = getDebugFileName(input); | ||
|
||
expect(result).toStrictEqual(expected); | ||
}); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { LRUCache } from 'lru-cache'; | ||
|
||
const getLastSlashBeforeIndex = (path: string, index: number) => { | ||
let pathIndex = index - 1; | ||
|
||
while (pathIndex >= 0) { | ||
if (path[pathIndex] === '/') { | ||
return pathIndex; | ||
} | ||
|
||
pathIndex--; | ||
} | ||
|
||
return -1; | ||
}; | ||
|
||
/** | ||
* Assumptions: | ||
* - The path is always normalized to use posix file separators (/) (see `addFileScope`) | ||
* - The path is always relative to the project root, i.e. there will never be a leading slash (see `addFileScope`) | ||
* - As long as `.css` is there, we have a valid `.css.*` file path, because otherwise there wouldn't | ||
* be a file scope to begin with | ||
* | ||
* The LRU cache we use can't cache undefined/null values, so we opt to return an empty string, | ||
* rather than using a custom Symbol or something similar. | ||
*/ | ||
const _getDebugFileName = (path: string): string => { | ||
let file: string; | ||
|
||
const lastIndexOfDotCss = path.lastIndexOf('.css'); | ||
|
||
if (lastIndexOfDotCss === -1) { | ||
return ''; | ||
} | ||
|
||
const lastSlashIndex = getLastSlashBeforeIndex(path, lastIndexOfDotCss); | ||
file = path.slice(lastSlashIndex + 1, lastIndexOfDotCss); | ||
|
||
// There are no slashes, therefore theres no directory to extract | ||
if (lastSlashIndex === -1) { | ||
return file; | ||
} | ||
|
||
let secondLastSlashIndex = getLastSlashBeforeIndex(path, lastSlashIndex - 1); | ||
// If secondLastSlashIndex is -1, it means that the path looks like `directory/file.css.ts`, | ||
// in which case dir will still be sliced starting at 0, which is what we want | ||
const dir = path.slice(secondLastSlashIndex + 1, lastSlashIndex); | ||
|
||
const debugFileName = file !== 'index' ? file : dir; | ||
|
||
return debugFileName; | ||
}; | ||
|
||
const memoizedGetDebugFileName = () => { | ||
const cache = new LRUCache<string, string>({ | ||
max: 500, | ||
}); | ||
|
||
return (path: string) => { | ||
const cachedResult = cache.get(path); | ||
|
||
if (cachedResult) { | ||
return cachedResult; | ||
} | ||
|
||
const result = _getDebugFileName(path); | ||
cache.set(path, result); | ||
|
||
return result; | ||
}; | ||
}; | ||
|
||
export const getDebugFileName = memoizedGetDebugFileName(); |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.