Skip to content
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