Skip to content

Commit 14e7063

Browse files
p7gmrmckeb
authored andcommitted
Allow passing options to Sass and Less renderers (#54)
1 parent c9f17cf commit 14e7063

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/helpers/DtsSnapshotCreator.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,51 @@ const getFilePath = (fileName: string) =>
3737
export class DtsSnapshotCreator {
3838
constructor(private readonly logger: Logger) {}
3939

40-
getClasses(processor: postcss.Processor, css: string, fileName: string) {
40+
getClasses(
41+
processor: postcss.Processor,
42+
css: string,
43+
fileName: string,
44+
options: Options = {},
45+
) {
4146
try {
4247
const fileType = getFileType(fileName);
4348
let transformedCss = '';
4449

50+
let renderOptions: Options['renderOptions'] | {} = {};
51+
if (options.renderOptions) {
52+
if (fileType === FileTypes.less && options.renderOptions.less) {
53+
renderOptions = options.renderOptions.less;
54+
} else if (fileType === FileTypes.scss && options.renderOptions.scss) {
55+
renderOptions = options.renderOptions.scss;
56+
}
57+
}
58+
4559
if (fileType === FileTypes.less) {
60+
let error;
4661
less.render(
4762
css,
48-
{ syncImport: true, filename: fileName } as any,
63+
{
64+
syncImport: true,
65+
filename: fileName,
66+
...renderOptions,
67+
} as any,
4968
(err, output) => {
50-
transformedCss = output.css.toString();
69+
error = err;
70+
if (output) {
71+
transformedCss = output.css.toString();
72+
}
5173
},
5274
);
75+
if (error) {
76+
throw error;
77+
}
5378
} else if (fileType === FileTypes.scss) {
5479
const filePath = getFilePath(fileName);
5580
transformedCss = sass
5681
.renderSync({
5782
data: css,
5883
includePaths: [filePath],
84+
...options,
5985
})
6086
.css.toString();
6187
} else {
@@ -116,7 +142,7 @@ export default classes;
116142
return scriptSnapshot;
117143
}
118144

119-
const classes = this.getClasses(processor, css, fileName);
145+
const classes = this.getClasses(processor, css, fileName, options);
120146
const dts = this.createExports(classes, options);
121147
return ts.ScriptSnapshot.fromString(dts);
122148
}

src/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import { Options as SassOptions } from 'sass';
2+
13
export interface Options {
24
camelCase?: CamelCaseOptions;
35
customMatcher?: string;
6+
renderOptions?: {
7+
less?: Partial<Less.Options>;
8+
scss?: Partial<SassOptions>;
9+
};
410
}
511

612
export type CamelCaseOptions =

0 commit comments

Comments
 (0)