skovy / typed-scss-modules

🎁 Generate type definitions (.d.ts) for CSS Modules using SCSS
https://skovy.dev/generating-typescript-definitions-for-css-modules-using-sass/
MIT License
638 stars 67 forks source link
cli css-modules generator sass scss scss-modules typescript

🎁 typed-scss-modules

npm version

Generate TypeScript definitions (.d.ts) files for CSS Modules that are written in SCSS (.scss). Check out this post to learn more about the rationale and inspiration behind this package.

Example

For example, given the following SCSS:

@import "variables";

.text {
  color: $blue;

  &-highlighted {
    color: $yellow;
  }
}

The following type definitions will be generated:

export declare const text: string;
export declare const textHighlighted: string;

Basic Usage

Install and run as a devDependency:

yarn add -D typed-scss-modules
yarn typed-scss-modules src

Or, install globally:

yarn global add typed-scss-modules
typed-scss-modules src

Or, with npm:

npm install -D typed-scss-modules
npx typed-scss-modules src

CLI Options

For all possible commands, run typed-scss-modules --help.

The only required argument is the directory where all SCSS files are located. Running typed-scss-modules src will search for all files matching src/**/*.scss. This can be overridden by providing a glob pattern instead of a directory. For example, typed-scss-modules src/*.scss

--watch (-w)

Watch for files that get added or are changed and generate the corresponding type definitions.

--ignoreInitial

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.

--ignore

A pattern or an array of glob patterns to exclude files that match and avoid generating type definitions.

--includePaths (-i)

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.

--implementation

--aliases (-a)

An object of aliases to map to their corresponding paths. This example will replace any @import '~alias' with @import 'src/core/variables'.

--aliasPrefixes (-p)

An object of prefix strings to replace with their corresponding paths. This example will replace any @import '~bootstrap/lib/bootstrap' with @import 'node_modules/bootstrap/lib/bootstrap'. This matches the common use-case for importing scss files from node_modules when sass-loader will be used with webpack to compile the project.

--nameFormat (-n)

The class naming format to use when converting the classes to type definitions.

--listDifferent (-l)

List any type definition files that are different than those that would be generated. If any are different, exit with a status code 1.

--exportType (-e)

The export type to use when generating type definitions.

named

Given the following SCSS:

.text {
  color: blue;

  &-highlighted {
    color: yellow;
  }
}

The following type definitions will be generated:

export declare const text: string;
export declare const textHighlighted: string;

default

Given the following SCSS:

.text {
  color: blue;

  &-highlighted {
    color: yellow;
  }
}

The following type definitions will be generated:

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.

--exportTypeName

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:

export type ClassesType = keyof Styles;

--exportTypeInterface

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:

export type IStyles = {
  // ...
};

--quoteType (-q)

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 ("). If Prettier is installed and configured in the project, it will be used and is likely to override the effect of this setting.

--updateStaleOnly (-u)

Overwrite generated files only if the source file has more recent changes. This can be useful if you want to avoid extraneous file updates, which can cause watcher processes to trigger unnecessarily (e.g. tsc --watch). This is done by first checking if the generated file was modified more recently than the source file, and secondly by comparing the existing file contents to the generated file contents.

Caveat: If a generated type definition file is updated manually, it won't be re-generated until the corresponding scss file is also updated.

--logLevel (-L)

Sets verbosity level of console output.

verbose

Print all messages

error

Print only errors

info

Print only some messages

silent

Print nothing

--banner

Will prepend a string to the top of your output files

// This is an example banner
export type Styles = {
  // ...
};

--outputFolder (-o)

Set a relative folder to output the generated type definitions. Instead of writing the type definitions directly next to each SCSS module (sibling file), it will write to the output folder with the same path.

It will use the relative path to the SCSS module from where this tool is executed. This same path (including any directories) will be constructed in the output folder. This is important for this to work properly with TypeScript.

Important: for this to work as expected the tsconfig.json needs to have rootDirs added with the same output folder. This will allow TypeScript to pick up these type definitions and map them to the actual SCSS modules.

{
  "compilerOptions": {
    "rootDirs": [".", "__generated__"]
  }
}

--additionalData (-d)

Prepend the provided SCSS code before each file. This is useful for injecting globals into every file, such as adding an import to load global variables for each file.

Config options

All options above are also supported as a configuration file in the root of the project. The following configuration file names are supported:

The file can provide either a named config export or a default export.

// Example of a named export with some of the options sets.
export const config = {
  banner: "// customer banner",
  exportType: "default",
  exportTypeName: "TheClasses",
  logLevel: "error",
};

// Example of a default export with some of the options sets.
export default {
  banner: "// customer banner",
  exportType: "default",
  exportTypeName: "TheClasses",
  logLevel: "error",
};

Note: the configuration options are the same as the CLI options without the leading dashes (--). Only the full option name is supported (not aliases) in the configuration file.

CLI options will take precedence over configuration file options.

In addition to all CLI options, the following are options only available with the configuration file:

importer

Define a single custom SASS importer or an array of SASS importers. This should only be necessary if custom SASS importers are already being used in the build process. This is used internally to implement aliases and aliasPrefixes.

Refer to lib/sass/importer.ts for more details and the node-sass and sass importer type definitions.

Examples

For examples of how this tool can be used and configured, see the examples directory:

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Janeene Beeforth
Janeene Beeforth

πŸ› πŸ’» πŸ“–
Eric Ferreira
Eric Ferreira

πŸ’» πŸ“–
Luis Lopes
Luis Lopes

πŸ’»
Josh Wedekind
Josh Wedekind

πŸ’» πŸ“– ⚠️
Jared Gesser
Jared Gesser

πŸ€”
RaphaΓ«l L
RaphaΓ«l L

πŸ’» πŸ€”
Nick Perez
Nick Perez

πŸ› πŸ’»
Even Alander
Even Alander

πŸ’» ⚠️ πŸ€”
Katie Foster
Katie Foster

πŸ’» ⚠️ πŸ“–
Carlos Aguilera
Carlos Aguilera

πŸ’»
Craig McCown
Craig McCown

πŸ€” πŸ’» ⚠️ πŸ“–
Guillaume Vagner
Guillaume Vagner

πŸ’» ⚠️ πŸ›
Sam Magura
Sam Magura

πŸ’» ⚠️
Mike Gregory
Mike Gregory

πŸ› πŸ’» ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

Alternatives

This package was heavily influenced on typed-css-modules which generates TypeScript definitions (.d.ts) files for CSS Modules that are written in CSS (.css).

This package is currently used as a CLI. There are also packages that generate types as a webpack loader.