-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Resolve imports from CSS file #15010
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
Changes from 5 commits
f65d58f
2c33aad
927c6a0
c577fd2
ecca617
b293ac5
0001eb2
bfc08aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { globby } from 'globby' | ||
| import { globby, isGitIgnored } from 'globby' | ||
| import fs from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import postcss from 'postcss' | ||
| import atImport from 'postcss-import' | ||
| import { formatNodes } from './codemods/format-nodes' | ||
| import { sortBuckets } from './codemods/sort-buckets' | ||
| import { help } from './commands/help' | ||
|
|
@@ -67,9 +68,7 @@ async function run() { | |
|
|
||
| // Discover CSS files in case no files were provided | ||
| if (files.length === 0) { | ||
| info( | ||
| 'No input stylesheets provided. Searching for CSS files in the current directory and its subdirectories…', | ||
| ) | ||
| info('Searching for CSS files in the current directory and its subdirectories…') | ||
|
|
||
| files = await globby(['**/*.css'], { | ||
| absolute: true, | ||
|
|
@@ -80,19 +79,44 @@ async function run() { | |
| // Ensure we are only dealing with CSS files | ||
| files = files.filter((file) => file.endsWith('.css')) | ||
|
|
||
| // Analyze the stylesheets | ||
| let loadResults = await Promise.allSettled(files.map((filepath) => Stylesheet.load(filepath))) | ||
| // Load the stylesheets and their imports | ||
| let sheetsByFile = new Map<string, Stylesheet>() | ||
| let isIgnored = await isGitIgnored() | ||
| let queue = files.slice() | ||
| while (queue.length > 0) { | ||
| let file = queue.shift()! | ||
|
|
||
| // Load and parse all stylesheets | ||
| for (let result of loadResults) { | ||
| if (result.status === 'rejected') { | ||
| error(`${result.reason}`) | ||
| // Already handled | ||
| if (sheetsByFile.has(file)) continue | ||
|
|
||
| // We don't want to process ignored files (like node_modules) | ||
| if (isIgnored(file)) continue | ||
|
|
||
| let sheet = await Stylesheet.load(file).catch((e) => { | ||
| error(`${e}`) | ||
| return null | ||
| }) | ||
| if (!sheet) continue | ||
|
|
||
| // Track the sheet by its file | ||
| sheetsByFile.set(file, sheet) | ||
|
|
||
| // We process the stylesheet which will also process its imports and | ||
| // inline everything. We still want to handle the imports separately, so | ||
| // we just use the postcss-import messages to find the imported files. | ||
| // | ||
| // We can't use the `sheet.root` directly because this will mutate the | ||
| // `sheet.root` | ||
| let processed = await postcss().use(atImport()).process(sheet.root.toString(), { from: file }) | ||
|
||
|
|
||
| for (let msg of processed.messages) { | ||
| if (msg.type === 'dependency' && msg.plugin === 'postcss-import') { | ||
| queue.push(msg.file) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let stylesheets = loadResults | ||
| .filter((result) => result.status === 'fulfilled') | ||
| .map((result) => result.value) | ||
| let stylesheets = Array.from(sheetsByFile.values()) | ||
|
|
||
| // Analyze the stylesheets | ||
| try { | ||
|
|
||
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.
When you don't pass any files, then we used globby with the
gitignore: trueoption, so I wanted to add that here as well.I added an explicitly ignored CSS file to the integration tests to make sure we don't touch it.