Skip to content

Commit 86d2c78

Browse files
authored
feat: add source map support to customRenderer (#249)
1 parent 1b0cc06 commit 86d2c78

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ module.exports = (css, { fileName, logger }) => {
153153
try {
154154
// ...process your css here.
155155
return renderedCss;
156+
// css and sourceMap
157+
return {
158+
css: renderedCss,
159+
map: sourceMap,
160+
};
156161
} catch (error) {
157162
logger.error(error.message);
158163
}

src/helpers/getCssExports.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ export const getCssExports = ({
6464
if (options.customRenderer) {
6565
// eslint-disable-next-line @typescript-eslint/no-var-requires
6666
const customRenderer = require(options.customRenderer) as CustomRenderer;
67-
transformedCss = customRenderer(rawCss, {
67+
const customResult = customRenderer(rawCss, {
6868
fileName,
6969
logger,
7070
compilerOptions,
7171
});
72+
if (typeof customResult === 'string') {
73+
transformedCss = customResult;
74+
} else if (customResult.css) {
75+
transformedCss = customResult.css;
76+
sourceMap = customResult.map;
77+
}
7278
} else {
7379
switch (fileType) {
7480
case FileType.less:

src/options.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DotenvConfigOptions } from 'dotenv';
44
import { CSSExports } from 'icss-utils';
55
import stylus from 'stylus';
66
import { Logger } from './helpers/logger';
7+
import type { RawSourceMap } from 'source-map-js';
78

89
// NOTE: Stylus doesn't directly export RenderOptions.
910
type StylusRenderOptions = Parameters<typeof stylus>[1];
@@ -52,7 +53,12 @@ export interface CustomRendererOptions {
5253
export type CustomRenderer = (
5354
css: string,
5455
options: CustomRendererOptions,
55-
) => string;
56+
) =>
57+
| string
58+
| {
59+
css: string;
60+
map?: RawSourceMap;
61+
};
5662

5763
export interface CustomTemplateOptions {
5864
classes: CSSExports;

0 commit comments

Comments
 (0)