From 980164553060897aa8234ce7ccd2cec6ad10fd4c Mon Sep 17 00:00:00 2001 From: qyzz Date: Thu, 11 Jan 2024 19:38:09 +0800 Subject: [PATCH] feat: customRender return sourceMap support --- README.md | 5 +++++ src/helpers/getCssExports.ts | 8 +++++++- src/options.ts | 8 +++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 59d0190..02eb451 100644 --- a/README.md +++ b/README.md @@ -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); } diff --git a/src/helpers/getCssExports.ts b/src/helpers/getCssExports.ts index 6eb91ba..559f834 100644 --- a/src/helpers/getCssExports.ts +++ b/src/helpers/getCssExports.ts @@ -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: diff --git a/src/options.ts b/src/options.ts index de1f8be..6234ba2 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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[1]; @@ -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;