TransformOptions
beautify?: boolean;
computeCalcExpression?: boolean;
computeShorthand?: boolean;
computeTransform?: boolean;
convertColor?: any;
cwd?: string;
expandIfSyntax?: boolean;
expandNestingRules?: boolean;
filePath?: string;
generateScopedName?: (
localName: string,
filePath: string,
pattern: string,
hashLength?: number,
) => string | Promise<string>;
hashLength?: number;
indent?: string;
inlineCssVariables?: boolean;
inputSourceMap?: string | SourceMapObject;
lenient?: boolean;
load?: (
url: string | { absolute: string; relative: string },
currentDirectory?: string,
responseType?: boolean | ResponseType,
) => Promise<
string
| ArrayBuffer
| ReadableStream<Uint8Array<ArrayBufferLike>>,
>;
minify?: boolean;
module?:
| boolean
| ModuleSyncOptions
| ModuleCaseTransformEnum
| ModuleScopeEnumOptions;
naming?: ModuleCaseTransformEnum;
nestingRules?: boolean;
newLine?: string;
output?: string;
parseColor?: boolean;
pass?: number;
pattern?: string;
preserveLicense?: boolean;
removeCharset?: boolean;
removeComments?: boolean;
removeDuplicateDeclarations?: string
| boolean
| string[];
removeEmpty?: boolean;
removePrefix?: boolean;
resolve?: (
url: string,
currentUrl: string,
currentWorkingDirectory?: string,
) => { absolute: string; relative: string };
resolveImport?: boolean;
resolveUrls?: boolean;
scoped?: boolean | ModuleScopeEnumOptions;
signal?: AbortSignal;
source?: SourceFile | null;
sourcemap?: boolean | "inline";
sourcesMap?: Map<number, SourceFile>;
src?: string;
validation?: boolean | ValidationLevel;
visitor?:
| GenericVisitorAstNodeSyncHandlerMap<T>
| GenericVisitorAstNodeHandlerMap<T>
| VisitorNodeMap
| VisitorNodeMap[];
withParents?: boolean;
}
Hierarchy (View Summary)
- ParserOptions
- RenderOptions
- TransformOptions
Index
Properties
Properties
Optionalbeautify
Pretty print css
const result = await transform(css, {beautify: true});
Optionalcompute Calc Expression
Compute css math functions
Optionalcompute Shorthand
Compute shorthand properties
Optionalcompute Transform
Compute css transform functions
Optionalconvert Color
Convert color to the specified color space. 'true' will convert to HEX
Optional Internalcwd
Current working directory
Optionalexpand If Syntax
Experimental, convert css if() function into legacy syntax.
Optionalexpand Nesting Rules
Expand nested rules
Optionalfile Path
Module output file path. it is used to generate the scoped name. if not provided, options.src will be used
Optionalgenerate Scoped Name
localName: string,
filePath: string,
pattern: string,
hashLength?: number,
) => string | Promise<string>
Function to generate scoped name
Type Declaration
-
- (
localName: string,
filePath: string,
pattern: string,
hashLength?: number,
): string | Promise<string> -
Parameters
localName: stringfilePath: stringpattern: stringOptionalhashLength: number
Returns string | Promise<string>
- (
Optionalhash Length
Generated scope hash length. the default is 5
Optionalindent
Indention string
Optionalinline Css Variables
Inline css variables
Optionalinput Source Map
Input source map
Optionallenient
Lenient validation. retain nodes that failed validation
Optionalload
url: string | { absolute: string; relative: string },
currentDirectory?: string,
responseType?: boolean | ResponseType,
) => Promise<
string
| ArrayBuffer
| ReadableStream<Uint8Array<ArrayBufferLike>>,
>
Custom URL and file loader.
Optionalminify
Enable minification
Optionalmodule
CSS modules options
Optionalnaming
optional. function change the case of the scoped name and the class mapping
- ModuleCaseTransformEnum.IgnoreCase: do not change case
- ModuleCaseTransformEnum.CamelCase: camelCase ParseResult.mapping key name
- ModuleCaseTransformEnum.CamelCaseOnly: camelCase ParseResult.mapping key name and the scoped class name
- ModuleCaseTransformEnum.DashCase: dashCase ParseResult.mapping key name
- ModuleCaseTransformEnum.DashCaseOnly: dashCase ParseResult.mapping key name and the scoped class name
Optionalnesting Rules
Generate nested rules
Optionalnew Line
New line string
Optional Internaloutput
Output file. Used for url resolution
Optionalparse Color
Parse color tokens
Optionalpass
Define minification passes.
Optionalpattern
The pattern used to generate scoped names. the supported placeholders are:
- name: the file base name without the extension
- hash: the file path hash
- local: the local name
- path: the file path
- folder: the folder name
- ext: the file extension
the pattern can optionally have a maximum number of characters:
pattern: '[local:2]-[hash:5]'
the hash pattern can take an algorithm, a maximum number of characters or both:
pattern: '[local]-[hash:base64:5]'
or
pattern: '[local]-[hash:5]'
or
pattern: '[local]-[hash:sha1]'
supported hash algorithms are:
- base64
- hex
- base64url
- sha1
- sha256
- sha384
- sha512
import {transform, ModuleCaseTransformEnum} from '@tbela99/css-parser';
import type {TransformResult} from '@tbela99/css-parser';
css = `
:local(.className) {
background: red;
color: yellow;
}
:local(.subClass) {
composes: className;
background: blue;
}
`;
let result: TransformResult = await transform(css, {
beautify:true,
module: {
pattern: '[local]-[hash:sha256]'
}
});
console.log(result.code);
generated css
.className-b629f {
background: red;
color: #ff0
}
.subClass-a0c35 {
background: blue
}
Optionalpreserve License
Preserve license comments. License comments are comments that start with '/*!'
Optionalremove Charset
Remove at-rule charset
Optionalremove Comments
Remove comments
Optionalremove Duplicate Declarations
Remove duplicate declarations from the same rule. if passed as a string array, duplicated declarations are removed, except for those in the array
import {transform} from '@tbela99/css-parser';
const css = `
.table {
width: 100%;
width: calc(100% + 40px);
margin-left: 20px;
margin-left: min(100% , 20px)
}
`;
const result = await transform(css, {
beautify: true,
validation: true,
removeDuplicateDeclarations: ['width']
}
);
console.log(result.code);
Optionalremove Empty
Remove empty ast nodes
Optionalremove Prefix
Remove css prefix
Optionalresolve
url: string,
currentUrl: string,
currentWorkingDirectory?: string,
) => { absolute: string; relative: string }
Url and path resolver
Optionalresolve Import
Resolve import
Optionalresolve Urls
Resolve urls
Optionalscoped
Use local scope vs global scope
Optionalsignal
Abort signal
Example: abort after 10 seconds
const result = await parse(cssString, {
signal: AbortSignal.timeout(10000)
});
Optional Internalsource
Source file to be used for sourcemap
Optionalsourcemap
Include sourcemap in the ast. Sourcemap info is always generated
Optional Internalsources Map
Source file to be used for sourcemap
Optionalsrc
Source file to be used for sourcemap
Optionalvalidation
Enable CSS validation. Using ValidationLevel as value is deprecated and will be removed
see ValidationLevel
Optionalvisitor
| GenericVisitorAstNodeSyncHandlerMap<T>
| GenericVisitorAstNodeHandlerMap<T>
| VisitorNodeMap
| VisitorNodeMap[]
Node visitor VisitorNodeMap[]
Optionalwith Parents
Render the node along with its parents
Transform options