diff --git a/src/helpers/DtsSnapshotCreator.ts b/src/helpers/DtsSnapshotCreator.ts index feeea94..74c9491 100644 --- a/src/helpers/DtsSnapshotCreator.ts +++ b/src/helpers/DtsSnapshotCreator.ts @@ -37,25 +37,51 @@ const getFilePath = (fileName: string) => export class DtsSnapshotCreator { constructor(private readonly logger: Logger) {} - getClasses(processor: postcss.Processor, css: string, fileName: string) { + getClasses( + processor: postcss.Processor, + css: string, + fileName: string, + options: Options = {}, + ) { try { const fileType = getFileType(fileName); let transformedCss = ''; + let renderOptions: Options['renderOptions'] | {} = {}; + if (options.renderOptions) { + if (fileType === FileTypes.less && options.renderOptions.less) { + renderOptions = options.renderOptions.less; + } else if (fileType === FileTypes.scss && options.renderOptions.scss) { + renderOptions = options.renderOptions.scss; + } + } + if (fileType === FileTypes.less) { + let error; less.render( css, - { syncImport: true, filename: fileName } as any, + { + syncImport: true, + filename: fileName, + ...renderOptions, + } as any, (err, output) => { - transformedCss = output.css.toString(); + error = err; + if (output) { + transformedCss = output.css.toString(); + } }, ); + if (error) { + throw error; + } } else if (fileType === FileTypes.scss) { const filePath = getFilePath(fileName); transformedCss = sass .renderSync({ data: css, includePaths: [filePath], + ...options, }) .css.toString(); } else { @@ -116,7 +142,7 @@ export default classes; return scriptSnapshot; } - const classes = this.getClasses(processor, css, fileName); + const classes = this.getClasses(processor, css, fileName, options); const dts = this.createExports(classes, options); return ts.ScriptSnapshot.fromString(dts); } diff --git a/src/options.ts b/src/options.ts index 4e143c6..77f1894 100644 --- a/src/options.ts +++ b/src/options.ts @@ -1,6 +1,12 @@ +import { Options as SassOptions } from 'sass'; + export interface Options { camelCase?: CamelCaseOptions; customMatcher?: string; + renderOptions?: { + less?: Partial; + scss?: Partial; + }; } export type CamelCaseOptions =