Skip to content

Commit

Permalink
feat: add source map support to customRenderer (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
qyzzzz authored Feb 11, 2024
1 parent 1b0cc06 commit 86d2c78
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ module.exports = (css, { fileName, logger }) => {
try {
// ...process your css here.
return renderedCss;
// css and sourceMap
return {
css: renderedCss,
map: sourceMap,
};
} catch (error) {
logger.error(error.message);
}
Expand Down
8 changes: 7 additions & 1 deletion src/helpers/getCssExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,17 @@ export const getCssExports = ({
if (options.customRenderer) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const customRenderer = require(options.customRenderer) as CustomRenderer;
transformedCss = customRenderer(rawCss, {
const customResult = customRenderer(rawCss, {
fileName,
logger,
compilerOptions,
});
if (typeof customResult === 'string') {
transformedCss = customResult;
} else if (customResult.css) {
transformedCss = customResult.css;
sourceMap = customResult.map;
}
} else {
switch (fileType) {
case FileType.less:
Expand Down
8 changes: 7 additions & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DotenvConfigOptions } from 'dotenv';
import { CSSExports } from 'icss-utils';
import stylus from 'stylus';
import { Logger } from './helpers/logger';
import type { RawSourceMap } from 'source-map-js';

// NOTE: Stylus doesn't directly export RenderOptions.
type StylusRenderOptions = Parameters<typeof stylus>[1];
Expand Down Expand Up @@ -52,7 +53,12 @@ export interface CustomRendererOptions {
export type CustomRenderer = (
css: string,
options: CustomRendererOptions,
) => string;
) =>
| string
| {
css: string;
map?: RawSourceMap;
};

export interface CustomTemplateOptions {
classes: CSSExports;
Expand Down

0 comments on commit 86d2c78

Please sign in to comment.