From 5d1b5180b602f8c28c6b83e31df2bb1cd4915f3d Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Wed, 20 Jun 2018 09:00:37 +0200 Subject: [PATCH 1/2] Add type definition for csso --- types/csso/csso-tests.ts | 40 ++++++++++++++++ types/csso/index.d.ts | 100 +++++++++++++++++++++++++++++++++++++++ types/csso/tsconfig.json | 23 +++++++++ types/csso/tslint.json | 1 + 4 files changed, 164 insertions(+) create mode 100644 types/csso/csso-tests.ts create mode 100644 types/csso/index.d.ts create mode 100644 types/csso/tsconfig.json create mode 100644 types/csso/tslint.json diff --git a/types/csso/csso-tests.ts b/types/csso/csso-tests.ts new file mode 100644 index 0000000000..74e0b51dd9 --- /dev/null +++ b/types/csso/csso-tests.ts @@ -0,0 +1,40 @@ +import csso = require('csso'); + +csso.minify('.test { color: #ff0000; }').css; +csso.minify('.test { color: #ff0000; }').map; +csso.minify('.test { color: #ff0000; }', { + sourceMap: true, + filename: '', + debug: true, + beforeCompress: () => {}, + afterCompress: () => {}, + restructure: false, + forceMediaMerge: true, + clone: false, + comments: '', + logger: () => {} +}); + +csso.minifyBlock('color: rgba(255, 0, 0, 1); color: #ff0000').css; +csso.minifyBlock('color: rgba(255, 0, 0, 1); color: #ff0000').map; +csso.minifyBlock('color: rgba(255, 0, 0, 1); color: #ff0000', { + sourceMap: true, + filename: '', + debug: true, + beforeCompress: () => {}, + afterCompress: () => {}, + restructure: false, + forceMediaMerge: true, + clone: false, + comments: '', + logger: () => {} +}); + +csso.compress({}).ast; +csso.compress({}, { + restructure: false, + forceMediaMerge: true, + clone: false, + comments: '', + logger: () => {} +}).ast; diff --git a/types/csso/index.d.ts b/types/csso/index.d.ts new file mode 100644 index 0000000000..5aa19ac63d --- /dev/null +++ b/types/csso/index.d.ts @@ -0,0 +1,100 @@ +// Type definitions for csso 3.5 +// Project: https://github.com/css/csso +// Definitions by: Christian Rackerseder +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.6 + +export interface Csso { + /** + * Resulting CSS. + */ + css: string; + /** + * Instance of SourceMapGenerator or null. + */ + map: object | null; +} + +export type BeforeCompressFn = (ast: object, options: CompressOptions) => void; +export type AfterCompressFn = (compressResult: string, options: CompressOptions) => void; + +export interface MinifyOptions { + /** + * Generate a source map when true. + * @default false + */ + sourceMap?: boolean; + /** + * Filename of input CSS, uses for source map generation. + * @default '' + */ + filename?: string; + /** + * Output debug information to stderr. + * @default false + */ + debug?: boolean; + /** + * Called right after parse is run. + */ + beforeCompress?: BeforeCompressFn | BeforeCompressFn[]; + /** + * Called right after compress() is run. + */ + afterCompress?: AfterCompressFn | AfterCompressFn[]; + restructure?: boolean; +} + +/** + * Minify source CSS passed as String + * @param source + * @param options + */ +export function minify(source: string, options?: MinifyOptions & CompressOptions): Csso; + +/** + * The same as minify() but for list of declarations. Usually it's a style attribute value. + * @param source + * @param options + */ +export function minifyBlock(source: string, options?: MinifyOptions & CompressOptions): Csso; + +export interface CompressOptions { + /** + * Disable or enable a structure optimisations. + * @default true + */ + restructure?: boolean; + /** + * Enables merging of @media rules with the same media query by splitted by other rules. + * The optimisation is unsafe in general, but should work fine in most cases. Use it on your own risk. + * @default false + */ + forceMediaMerge?: boolean; + /** + * Transform a copy of input AST if true. Useful in case of AST reuse. + * @default false + */ + clone?: boolean; + /** + * Specify what comments to leave: + * - 'exclamation' or true – leave all exclamation comments + * - 'first-exclamation' – remove every comment except first one + * - false – remove all comments + * @default true + */ + comments?: string | boolean; + /** + * Usage data for advanced optimisations. + */ + usage?: object; + /** + * Function to track every step of transformation. + */ + logger?: () => void; +} + +/** + * Does the main task – compress an AST. + */ +export function compress(ast: object, options?: CompressOptions): { ast: object }; diff --git a/types/csso/tsconfig.json b/types/csso/tsconfig.json new file mode 100644 index 0000000000..9ed6695bc9 --- /dev/null +++ b/types/csso/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "csso-tests.ts" + ] +} diff --git a/types/csso/tslint.json b/types/csso/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/csso/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From d1dbad93d8b4941ba9702bff980bc8c0c577c5e3 Mon Sep 17 00:00:00 2001 From: Christian Rackerseder Date: Wed, 20 Jun 2018 21:02:26 +0200 Subject: [PATCH 2/2] Fix definition to match CommonJS-style --- types/csso/index.d.ts | 177 ++++++++++++++++++++++-------------------- 1 file changed, 92 insertions(+), 85 deletions(-) diff --git a/types/csso/index.d.ts b/types/csso/index.d.ts index 5aa19ac63d..f89f395129 100644 --- a/types/csso/index.d.ts +++ b/types/csso/index.d.ts @@ -4,97 +4,104 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 -export interface Csso { - /** - * Resulting CSS. - */ - css: string; - /** - * Instance of SourceMapGenerator or null. - */ - map: object | null; +declare namespace csso { + interface Result { + /** + * Resulting CSS. + */ + css: string; + /** + * Instance of SourceMapGenerator or null. + */ + map: object | null; + } + + interface CompressOptions { + /** + * Disable or enable a structure optimisations. + * @default true + */ + restructure?: boolean; + /** + * Enables merging of @media rules with the same media query by splitted by other rules. + * The optimisation is unsafe in general, but should work fine in most cases. Use it on your own risk. + * @default false + */ + forceMediaMerge?: boolean; + /** + * Transform a copy of input AST if true. Useful in case of AST reuse. + * @default false + */ + clone?: boolean; + /** + * Specify what comments to leave: + * - 'exclamation' or true – leave all exclamation comments + * - 'first-exclamation' – remove every comment except first one + * - false – remove all comments + * @default true + */ + comments?: string | boolean; + /** + * Usage data for advanced optimisations. + */ + usage?: object; + /** + * Function to track every step of transformation. + */ + logger?: () => void; + } + + interface MinifyOptions { + /** + * Generate a source map when true. + * @default false + */ + sourceMap?: boolean; + /** + * Filename of input CSS, uses for source map generation. + * @default '' + */ + filename?: string; + /** + * Output debug information to stderr. + * @default false + */ + debug?: boolean; + /** + * Called right after parse is run. + */ + beforeCompress?: BeforeCompressFn | BeforeCompressFn[]; + /** + * Called right after compress() is run. + */ + afterCompress?: AfterCompressFn | AfterCompressFn[]; + restructure?: boolean; + } + + type BeforeCompressFn = (ast: object, options: CompressOptions) => void; + type AfterCompressFn = (compressResult: string, options: CompressOptions) => void; } -export type BeforeCompressFn = (ast: object, options: CompressOptions) => void; -export type AfterCompressFn = (compressResult: string, options: CompressOptions) => void; +interface Csso { + /** + * Minify source CSS passed as String + * @param source + * @param options + */ + minify(source: string, options?: csso.MinifyOptions & csso.CompressOptions): csso.Result; -export interface MinifyOptions { /** - * Generate a source map when true. - * @default false + * The same as minify() but for list of declarations. Usually it's a style attribute value. + * @param source + * @param options */ - sourceMap?: boolean; + minifyBlock(source: string, options?: csso.MinifyOptions & csso.CompressOptions): csso.Result; + /** - * Filename of input CSS, uses for source map generation. - * @default '' + * Does the main task – compress an AST. */ - filename?: string; - /** - * Output debug information to stderr. - * @default false - */ - debug?: boolean; - /** - * Called right after parse is run. - */ - beforeCompress?: BeforeCompressFn | BeforeCompressFn[]; - /** - * Called right after compress() is run. - */ - afterCompress?: AfterCompressFn | AfterCompressFn[]; - restructure?: boolean; + compress(ast: object, options?: csso.CompressOptions): { ast: object }; } -/** - * Minify source CSS passed as String - * @param source - * @param options - */ -export function minify(source: string, options?: MinifyOptions & CompressOptions): Csso; - -/** - * The same as minify() but for list of declarations. Usually it's a style attribute value. - * @param source - * @param options - */ -export function minifyBlock(source: string, options?: MinifyOptions & CompressOptions): Csso; - -export interface CompressOptions { - /** - * Disable or enable a structure optimisations. - * @default true - */ - restructure?: boolean; - /** - * Enables merging of @media rules with the same media query by splitted by other rules. - * The optimisation is unsafe in general, but should work fine in most cases. Use it on your own risk. - * @default false - */ - forceMediaMerge?: boolean; - /** - * Transform a copy of input AST if true. Useful in case of AST reuse. - * @default false - */ - clone?: boolean; - /** - * Specify what comments to leave: - * - 'exclamation' or true – leave all exclamation comments - * - 'first-exclamation' – remove every comment except first one - * - false – remove all comments - * @default true - */ - comments?: string | boolean; - /** - * Usage data for advanced optimisations. - */ - usage?: object; - /** - * Function to track every step of transformation. - */ - logger?: () => void; -} - -/** - * Does the main task – compress an AST. - */ -export function compress(ast: object, options?: CompressOptions): { ast: object }; +declare const csso: Csso; +export = csso;