-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
feat: support postcss sugarss #2436
Changes from all commits
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,3 +1,12 @@ | ||
module.exports = { | ||
plugins: [require('postcss-nested')] | ||
module.exports = (api) => { | ||
if (/\.sss$/.test(api.file)) { | ||
return { | ||
parser: 'sugarss', | ||
plugins: [require('postcss-simple-vars'), require('postcss-nested')] | ||
} | ||
} | ||
|
||
return { | ||
plugins: [require('postcss-nested')] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$black: #000 | ||
|
||
.sugarss | ||
border: thick double $black |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,7 +75,7 @@ export interface CSSModulesOptions { | |
| null | ||
} | ||
|
||
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|postcss)($|\\?)` | ||
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|postcss|sss)($|\\?)` | ||
const cssLangRE = new RegExp(cssLangs) | ||
const cssModuleRE = new RegExp(`\\.module${cssLangs}`) | ||
const directRequestRE = /(\?|&)direct\b/ | ||
|
@@ -507,7 +507,7 @@ async function compileCSS( | |
// crawl them in order to register watch dependencies. | ||
const needInlineImport = code.includes('@import') | ||
const hasUrl = cssUrlRE.test(code) || cssImageSetRE.test(code) | ||
const postcssConfig = await resolvePostcssConfig(config) | ||
const postcssConfig = await resolvePostcssConfig(id, config) | ||
const lang = id.match(cssLangRE)?.[1] | ||
|
||
// 1. plain css that needs no processing | ||
|
@@ -664,38 +664,47 @@ async function compileCSS( | |
interface PostCSSConfigResult { | ||
options: Postcss.ProcessOptions | ||
plugins: Postcss.Plugin[] | ||
file?: string | ||
} | ||
|
||
let cachedPostcssConfig: PostCSSConfigResult | null | undefined | ||
let cachedPostcssConfigPath: string | undefined | null | ||
|
||
async function resolvePostcssConfig( | ||
file: string, | ||
config: ResolvedConfig | ||
): Promise<PostCSSConfigResult | null> { | ||
if (cachedPostcssConfig !== undefined) { | ||
return cachedPostcssConfig | ||
} | ||
|
||
// inline postcss config via vite config | ||
const inlineOptions = config.css?.postcss | ||
|
||
if (isObject(inlineOptions)) { | ||
const result = { | ||
options: { ...inlineOptions }, | ||
plugins: inlineOptions.plugins || [] | ||
} | ||
delete result.options.plugins | ||
return (cachedPostcssConfig = result) | ||
|
||
return result | ||
} | ||
|
||
if (cachedPostcssConfigPath === null) { | ||
return null | ||
} | ||
|
||
try { | ||
const searchPath = | ||
typeof inlineOptions === 'string' ? inlineOptions : config.root | ||
// @ts-ignore | ||
return (cachedPostcssConfig = await postcssrc({}, searchPath)) | ||
cachedPostcssConfigPath ?? | ||
(typeof inlineOptions === 'string' ? inlineOptions : config.root) | ||
|
||
const ctx = { file, env: config.mode } | ||
const result = (await postcssrc(ctx, searchPath)) as PostCSSConfigResult | ||
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 is obviously very inefficient - I think a better way to do this is have two cached & resolved config values - one for SugarSS and one for normal CSS. 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. We can cache the resolved config with a hash of the 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. Another option is to provide a |
||
cachedPostcssConfigPath = result.file | ||
|
||
return result | ||
} catch (e) { | ||
if (!/No PostCSS Config found/.test(e.message)) { | ||
throw e | ||
} | ||
return (cachedPostcssConfig = null) | ||
return (cachedPostcssConfigPath = null) | ||
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. thought (dx): I'm a bit confused by this line cachedPostcssConfigPath = null
return null |
||
} | ||
} | ||
|
||
|
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.
nitpick (non-blocking): I would prefer an initial object at the start of the function body and return it at the end of the function
Then in between you can add dynamically properties and extend it
So something like
May need to be extended with some type-safety or so