From 10ff72e6d67a05d61ddc2e61effd0c003c7e58a2 Mon Sep 17 00:00:00 2001 From: Patrick Gingras <775.pg.12@gmail.com> Date: Sat, 19 Oct 2019 21:31:02 -0400 Subject: [PATCH 1/2] allow passing options to less and scss render functions --- src/helpers/DtsSnapshotCreator.ts | 25 ++++++++++++++++++++++--- src/options.ts | 6 ++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/helpers/DtsSnapshotCreator.ts b/src/helpers/DtsSnapshotCreator.ts index feeea94..60beb53 100644 --- a/src/helpers/DtsSnapshotCreator.ts +++ b/src/helpers/DtsSnapshotCreator.ts @@ -37,15 +37,33 @@ 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) { less.render( css, - { syncImport: true, filename: fileName } as any, + { + syncImport: true, + filename: fileName, + ...renderOptions, + } as any, (err, output) => { transformedCss = output.css.toString(); }, @@ -56,6 +74,7 @@ export class DtsSnapshotCreator { .renderSync({ data: css, includePaths: [filePath], + ...options, }) .css.toString(); } else { @@ -116,7 +135,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 = From 6965a254f054673822158c3dc59a907cea149b6b Mon Sep 17 00:00:00 2001 From: Patrick Gingras <775.pg.12@gmail.com> Date: Sat, 19 Oct 2019 22:02:18 -0400 Subject: [PATCH 2/2] better handle less.render errors --- src/helpers/DtsSnapshotCreator.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/helpers/DtsSnapshotCreator.ts b/src/helpers/DtsSnapshotCreator.ts index 60beb53..74c9491 100644 --- a/src/helpers/DtsSnapshotCreator.ts +++ b/src/helpers/DtsSnapshotCreator.ts @@ -57,6 +57,7 @@ export class DtsSnapshotCreator { } if (fileType === FileTypes.less) { + let error; less.render( css, { @@ -65,9 +66,15 @@ export class DtsSnapshotCreator { ...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