Repository navigation
Переводит сборку стилей на lightningcss #1377
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| const fs = require('fs') | ||
| const os = require('os') | ||
| const path = require('path') | ||
|
|
||
| const { cssTargets, styleEntries, bundleStyle } = require('../css.js') | ||
|
|
||
| const bundleSource = (css) => { | ||
| const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'doka-css-')) | ||
| const filename = path.join(dir, 'index.css') | ||
| fs.writeFileSync(filename, css) | ||
|
|
||
| try { | ||
| return bundleStyle(filename).toString() | ||
| } finally { | ||
| fs.rmSync(dir, { recursive: true, force: true }) | ||
| } | ||
| } | ||
|
|
||
| describe('сборка стилей', () => { | ||
| it('собирает @import в один файл', () => { | ||
| const result = bundleStyle(path.join('src/styles', styleEntries[0])).toString() | ||
|
|
||
| expect(result).not.toContain('@import') | ||
| expect(result.length).toBeGreaterThan(0) | ||
| }) | ||
|
|
||
| // Ради этого и уезжали с csso: его css-tree не знал современного синтаксиса | ||
| // и ронял всю сборку, а Vite гонит через ту же цепочку стили демок. | ||
| it.each([ | ||
| ['@container', '@container (width > 400px) { .a { color: red } }'], | ||
| ['@scope', '@scope (.a) to (.b) { .c { color: red } }'], | ||
| ['вложенные слои', '@layer framework.base { .a { color: red } }'], | ||
| ['вложенность', '.a { color: red; &.b { color: blue } }'], | ||
| ])('не падает на %s', (_, css) => { | ||
| expect(() => bundleSource(css)).not.toThrow() | ||
| }) | ||
|
|
||
| it('оставляет @container в выдаче', () => { | ||
| expect(bundleSource('@container (width > 400px) { .a { color: red } }')).toContain('@container') | ||
| }) | ||
|
|
||
| // Префиксы ставит lightningcss по targets, отдельного autoprefixer больше нет. | ||
| it('ставит вендорные префиксы по targets', () => { | ||
| expect(bundleSource('.a { user-select: none }')).toContain('-webkit-user-select') | ||
| }) | ||
|
|
||
| it('вычисляет targets из browserslist проекта', () => { | ||
| expect(Object.keys(cssTargets).length).toBeGreaterThan(0) | ||
| }) | ||
| }) |
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| const browserslist = require('browserslist') | ||
| const { bundle, browserslistToTargets } = require('lightningcss') | ||
|
|
||
| const { browserslist: browsers } = require('../package.json') | ||
|
|
||
| // Единственный источник правды по браузерам — поле browserslist в package.json. | ||
| // По нему lightningcss решает и какие вендорные префиксы ставить, и какой | ||
| // синтаксис компилировать: раньше первым занимался autoprefixer, вторым — | ||
| // postcss-media-minmax, и каждый жил по своим правилам. | ||
| const cssTargets = browserslistToTargets(browserslist(browsers)) | ||
|
|
||
| // Точки входа: всё остальное подключается к ним через @import, их же | ||
| // lightningcss и собирает в один файл. | ||
| const styleEntries = ['index.css', 'index.sc.css', 'dark-theme.css'] | ||
|
|
||
| const bundleStyle = (filename) => bundle({ filename, minify: true, targets: cssTargets }).code | ||
|
|
||
| module.exports = { | ||
| cssTargets, | ||
| styleEntries, | ||
| bundleStyle, | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
А зачем тогда Vite?
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.
Валидно, надо будет оставить только его и для локальной и для продакшен сборки. В следующих сериях =)