ModuleOptions
interface ModuleOptions {
filePath?: string;
generateScopedName?: (
localName: string,
filePath: string,
pattern: string,
hashLength?: number,
) => string | Promise<string>;
hashLength?: number;
naming?: ModuleCaseTransformEnum;
pattern?: string;
scoped?: boolean | ModuleScopeEnumOptions;
}
filePath?: string;
generateScopedName?: (
localName: string,
filePath: string,
pattern: string,
hashLength?: number,
) => string | Promise<string>;
hashLength?: number;
naming?: ModuleCaseTransformEnum;
pattern?: string;
scoped?: boolean | ModuleScopeEnumOptions;
}
Index
Properties
Properties
Optionalfile Path
filePath?: string
Module output file path. it is used to generate the scoped name. if not provided, options.src will be used
Optionalgenerate Scoped Name
generateScopedName?: (
localName: string,
filePath: string,
pattern: string,
hashLength?: number,
) => string | Promise<string>
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
hashLength?: number
Generated scope hash length. the default is 5
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
Optionalpattern
pattern?: string
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
}
Optionalscoped
Use local scope vs global scope
CSS module parser options