TypeScript type definition generator for LESS CSS Modules
npm install @qiniu/typed-less-modules




Generate TypeScript definitions (.d.ts) files for CSS Modules that are written in LESS (.less).
typed-less-modules 用于将 .less 转换为对应的 .d.ts TypeScript 类型声明文件。
For example, given the following LESS:
``less
@import "variables";
.text {
color: @blue;
&-highlighted {
color: @yellow;
}
}
`
The following type definitions will be generated:
`typescript`
export const text: string;
export const textHighlighted: string;
Run with npm package runner:
`bash`
npx tlm src
Or, install globally:
`bash`
yarn global add typed-less-modules
tlm src
Or, install and run as a devDependency:
`bash`
yarn add -D typed-less-modules
yarn tlm src
For all possible commands, run tlm --help.
The only required argument is the directory where all LESS files are located (config.pattern). Running tlm src will search for all files matching src/*/.less. This can be overridden by providing a glob pattern instead of a directory. For example, tlm src/*.less
- Type: booleanfalse
- Default: tlm src --watch
- Example:
Watch for files that get added or are changed and generate the corresponding type definitions.
- Type: booleanfalse
- Default: tlm src --watch --ignoreInitial
- Example:
Skips the initial build when passing the watch flag. Use this when running concurrently with another watch, but the initial build should happen first. You would run without watch first, then start off the concurrent runs after.
- Type: string[][]
- Default: tlm src --watch --ignore "**/secret.less"
- Example:
A pattern or an array of glob patterns to exclude files that match and avoid generating type definitions.
- Type: string[][]
- Default: tlm src --includePaths src/core
- Example:
An array of paths to look in to attempt to resolve your @import declarations. This example will search the src/core directory when resolving imports.
- Type: object{}
- Default: tlm src --aliases.~some-alias src/core/variables
- Example:
An object of aliases to map to their corresponding paths. This example will replace any @import '~alias' with @import 'src/core/variables'.
- Type: "camel" | "kebab" | "param" | "dashes" | "none""camel"
- Default: tlm src --nameFormat camel
- Example:
The class naming format to use when converting the classes to type definitions.
- camel: convert all class names to camel-case, e.g. App-Logo => appLogo.App-Logo
- kebab/param: convert all class names to kebab/param case, e.g. => app-logo (all lower case with '-' separators).App
- dashes: only convert class names containing dashes to camel-case, leave others alone, e.g. => App, App-Logo => appLogo. Matches the webpack css-loader camelCase 'dashesOnly' option.--exportType default
- none: do not modify the given class names (you should use when using --nameFormat none as any classes with a - in them are invalid as normal variable names).--nameFormat none --exportType default
Note: If you are using create-react-app v2.x and have NOT ejected, matches the class names that are generated in CRA's webpack's config.
- Type: booleanfalse
- Default: tlm src --listDifferent
- Example:
List any type definition files that are different than those that would be generated. If any are different, exit with a status code 1.
- Type: "named" | "default""named"
- Default: tlm src --exportType default
- Example:
The export type to use when generating type definitions.
#### named
Given the following LESS:
`less
.text {
color: blue;
&-highlighted {
color: yellow;
}
}
`
The following type definitions will be generated:
`typescript`
export const text: string;
export const textHighlighted: string;
#### default
Given the following LESS:
`less
.text {
color: blue;
&-highlighted {
color: yellow;
}
}
`
The following type definitions will be generated:
`typescript
export type Styles = {
text: string;
textHighlighted: string;
};
export type ClassNames = keyof Styles;
declare const styles: Styles;
export default styles;
`
This export type is useful when using kebab (param) cased class names since variables with a - are not valid variables and will produce invalid types or when a class name is a TypeScript keyword (eg: while or delete). Additionally, the Styles and ClassNames types are exported which can be useful for properly typing variables, functions, etc. when working with dynamic class names.
- Type: string"ClassNames"
- Default: tlm src --exportType default --exportTypeName ClassesType
- Example:
Customize the type name exported in the generated file when --exportType is set to "default".
Only default exports are affected by this command. This example will change the export type line to:
`typescript`
export type ClassesType = keyof Styles;
- Type: string"Styles"
- Default: tlm src --exportType default --exportTypeInterface IStyles
- Example:
Customize the interface name exported in the generated file when --exportType is set to "default".
Only default exports are affected by this command. This example will change the export interface line to:
`typescript`
export type IStyles = {
// ...
};
- Type: "single" | "double""single"
- Default: tlm src --exportType default --quoteType double
- Example:
Specify a quote type to match your TypeScript configuration. Only default exports are affected by this command. This example will wrap class names with double quotes (").
- Type: "verbose" | "error" | "info" | "silent""verbose"
- Default: tlm src --logLevel error
- Example:
Sets verbosity level of console output.
- Type: stringtlm.config.js
- Default: tlm --config ./path/to/tlm.config.js
- Example:
指定配置文件的路径,配置文件可代替所有的命令行参数,默认读取 process.cwd() + tlm.config.js 文件。
`js
// tlm.config.js
const path = require("path");
module.exports = {
pattern: "./src/*/.m.less",
watch: true,
// ...
// 上述所有配置均可用
aliases: {
// 映射至多路径
"~": [
path.resolve(__dirname, "node_modules"),
path.resolve(__dirname, "src")
],
// 映射至单路径
"@": path.resolve(__dirname, "some-dir"),
// 自定义映射规则
"abc-module"(filePath) {
return filePath.replace("abc-module", "xxx-path");
}
},
// less.render options 参数
lessRenderOptions: {
javascriptEnabled: true
}
};
`
#### verbose
Print all messages
#### error
Print only errors
#### info
Print only some messages
#### silent
Print nothing
For examples, see the examples` directory:
- Basic Example
- Default Export Example
This package was forked from typed-scss-modules.
This package is currently used as a CLI. There are also packages that generate types as a webpack loader.