Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transformer-directive): get raw string in directive, support slas…
…h & hash (#3790) Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
7 changed files
with
159 additions
and
128 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
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
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,71 @@ | ||
import { toArray } from '@unocss/core' | ||
import type { UnoGenerator } from '@unocss/core' | ||
import type { CssNode, List, ListItem } from 'css-tree' | ||
import { parse, walk } from 'css-tree' | ||
import type MagicString from 'magic-string' | ||
import { hasThemeFn as hasThemeFunction } from '@unocss/rule-utils' | ||
import { handleScreen } from './screen' | ||
import { handleApply } from './apply' | ||
import { handleFunction } from './functions' | ||
import type { TransformerDirectivesContext, TransformerDirectivesOptions } from './types' | ||
|
||
export async function transformDirectives( | ||
code: MagicString, | ||
uno: UnoGenerator, | ||
options: TransformerDirectivesOptions, | ||
filename?: string, | ||
originalCode?: string, | ||
offset?: number, | ||
) { | ||
let { applyVariable } = options | ||
const varStyle = options.varStyle | ||
if (applyVariable === undefined) { | ||
if (varStyle !== undefined) | ||
applyVariable = varStyle ? [`${varStyle}apply`] : [] | ||
applyVariable = ['--at-apply', '--uno-apply', '--uno'] | ||
} | ||
applyVariable = toArray(applyVariable || []) | ||
|
||
const hasApply = code.original.includes('@apply') || applyVariable.some(s => code.original.includes(s)) | ||
const hasScreen = code.original.includes('@screen') | ||
const hasThemeFn = hasThemeFunction(code.original) | ||
|
||
if (!hasApply && !hasThemeFn && !hasScreen) | ||
return | ||
|
||
const ast = parse(originalCode || code.original, { | ||
parseCustomProperty: true, | ||
parseAtrulePrelude: false, | ||
positions: true, | ||
filename, | ||
}) | ||
|
||
if (ast.type !== 'StyleSheet') | ||
return | ||
|
||
const stack: Promise<void>[] = [] | ||
|
||
const ctx: TransformerDirectivesContext = { | ||
options, | ||
applyVariable, | ||
uno, | ||
code, | ||
filename, | ||
offset, | ||
} | ||
|
||
const processNode = async (node: CssNode, _item: ListItem<CssNode>, _list: List<CssNode>) => { | ||
if (hasScreen && node.type === 'Atrule') | ||
handleScreen(ctx, node) | ||
|
||
if (node.type === 'Function') | ||
handleFunction(ctx, node) | ||
|
||
if (hasApply && node.type === 'Rule') | ||
await handleApply(ctx, node) | ||
} | ||
|
||
walk(ast, (...args) => stack.push(processNode(...args))) | ||
|
||
await Promise.all(stack) | ||
} |
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,41 @@ | ||
import type { SourceCodeTransformer, UnoGenerator } from '@unocss/core' | ||
import type MagicString from 'magic-string' | ||
|
||
export interface TransformerDirectivesOptions { | ||
enforce?: SourceCodeTransformer['enforce'] | ||
|
||
/** | ||
* Throw an error if utils or themes are not found. | ||
* | ||
* @default true | ||
*/ | ||
throwOnMissing?: boolean | ||
|
||
/** | ||
* Treat CSS custom properties as @apply directives for CSS syntax compatibility. | ||
* | ||
* Pass `false` to disable. | ||
* | ||
* @default ['--at-apply', '--uno-apply', '--uno'] | ||
*/ | ||
applyVariable?: false | string | string[] | ||
|
||
/** | ||
* Treat CSS custom properties as directives for CSS syntax compatibility. | ||
* | ||
* Pass `false` to disable, or a string to use as a prefix. | ||
* | ||
* @deprecated use `applyVariable` to specify the full var name instead. | ||
* @default '--at-' | ||
*/ | ||
varStyle?: false | string | ||
} | ||
|
||
export interface TransformerDirectivesContext { | ||
code: MagicString | ||
uno: UnoGenerator | ||
options: TransformerDirectivesOptions | ||
applyVariable: string[] | ||
offset?: number | ||
filename?: string | ||
} |
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