1.0.1 • Published 7 years ago

css-to-ts v1.0.1

Weekly downloads
2,613
License
MIT
Repository
github
Last release
7 years ago

css-to-ts

Compiles css files to importable TypeScript files.

Installation

npm install css-to-ts -g

Global installation is not necessary. You can install this package with:

npm install css-to-ts

and use it with npm-scripts.

Features

  • Takes css files and output TypeScript files with exported string containing content of your css file.
  • CLI tool for watching and files compilation.
  • Works with node-glob pattern.

Command line

Usage

css-to-ts -h

Arguments

ArgumentTypeDefaultDescription
-h, --helpbooleanfalseShow help.
-v, --versionbooleanfalseShow current version.
--rootDirstring./Specifies the root directory of input files.
--outDirstring./Redirect output structure to the directory.
--outExtstringtsSpecifies extension of output TypeScript file.
--patternstring**/*.cssFiles glob pattern.
-w, --watchbooleanfalseWatch for changes of input files.
--prefixstringPrefix added to output file name.
--suffixstringSuffix added to output file name.
--delimiterstring-Specifies delimiter for prefix and suffix. Required if one of these are set.
--removeSourcebooleanfalseRemove all source files specified by glob pattern.
--headerstringSpecifies header comment in generated TS file.
--cwdstringprocess.cwd()Specifies current working directory.
--excludearray["**/node_modules/**"]Specifies an array of globs to exclude.
--varNamestringSpecifies name of variable to be exported in TypeScript file.
--varTypestringconstSpecifies type of variable to be exported in TypeScript file.

Example

css-to-ts --rootDir "./src" --outDir "./dist" --pattern "*.css" --header "File generated with css-to-ts"

Input file ./src/orange.css:

.orange {
    color: orange;
    border: 1px solid yellow;
}

Generated ./dist/orange.ts:

// File generated with css-to-ts
export const Orange = `.orange {
    color: orange;
    border: 1px solid yellow;
}`;

API

ConvertCssToTs(stringifiedCss: string, variableName: string, headerComment?: string, varType: VarType = VarType.Const): string

Takes stringified css and outputs TypeScript code with exported string containing content of your css file.

Usage:

import { ConvertCssToTs } from "css-to-ts";
ArgumentTypeRequiredDescription
stringifiedCssstring*Stringified css to be exported in TypeScript file.
variableNamestring*Name of variable to be exported in TypeScript file.
headerCommentstringComment placed in the top of exported TypeScript file.
varTypestringType of variable to be exported in TypeScript file.
export enum VarType {
    Var = "var",
    Let = "let",
    Const = "const"
}

new CssToTsConverter(outputDir, outputFileName, cssDir, cssFileName, varName, header, removeSource, varType)

Compiles css files to importable TypeScript files.

Usage:

import { CssToTsConverter } from "css-to-ts";

const converter = new CssToTsConverter(
    outputDir,
    outputFileName,
    cssDir,
    cssFileName,
    varName,
    header,
    removeSource
);

try {
    await converter.Convert();
} catch(error) {
    console.error(error);
}
Constructor argumentTypeRequiredDescription
outputDirstring*Directory of output file.
outputFileNamestring*Name of output file.
cssDirstring*Directory of css file.
cssFileNamestring*File name of css file.
varNamestring*Name of variable to be exported in TypeScript file.
headerstringComment placed in the top of exported TypeScript file.
removeSourcebooleanShould css file be deleted after TypeScript file emitted.
varTypeVarTypeType of variable to be exported in TypeScript file.

License

Released under the MIT license.