Skip to content

Allow passing options to SCSS/Less render functions #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/helpers/DtsSnapshotCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Options as SassOptions } from 'sass';

export interface Options {
camelCase?: CamelCaseOptions;
customMatcher?: string;
renderOptions?: {
less?: Partial<Less.Options>;
scss?: Partial<SassOptions>;
};
}

export type CamelCaseOptions =
Expand Down