From 079fa27c2d5cacd9d7f3bfbb0b8c82300a3e0db5 Mon Sep 17 00:00:00 2001 From: Brody McKee Date: Sun, 11 Feb 2024 19:47:42 +1100 Subject: [PATCH] chore: make post-merge change to `customRenderer` (#257) --- README.md | 20 ++++++++++++++++++-- src/helpers/getCssExports.ts | 12 ++++++------ src/options.ts | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8129efd..1d18a7a 100644 --- a/README.md +++ b/README.md @@ -152,11 +152,27 @@ The custom renderer itself should be a JavaScript file. The function will be cal module.exports = (css, { fileName, logger }) => { try { // ...process your css here. + + // `string` return renderedCss; - // css and sourceMap + } catch (error) { + logger.error(error.message); + } +}; +``` + +If you want to return a a source map, you can return an object from your exported function. + +```js +module.exports = (css, { fileName, logger }) => { + try { + // ...process your css here. + return { + // `string` css: renderedCss, - map: sourceMap, + // `RawSourceMap` + sourceMap: sourceMap, }; } catch (error) { logger.error(error.message); diff --git a/src/helpers/getCssExports.ts b/src/helpers/getCssExports.ts index aee6bd5..0637cb9 100644 --- a/src/helpers/getCssExports.ts +++ b/src/helpers/getCssExports.ts @@ -64,16 +64,16 @@ export const getCssExports = ({ if (options.customRenderer) { // eslint-disable-next-line @typescript-eslint/no-var-requires const customRenderer = require(options.customRenderer) as CustomRenderer; - const customResult = customRenderer(rawCss, { + const result = customRenderer(rawCss, { fileName, logger, compilerOptions, }); - if (typeof customResult === 'string') { - transformedCss = customResult; - } else if (customResult.css) { - transformedCss = customResult.css; - sourceMap = customResult.map; + if (typeof result === 'string') { + transformedCss = result; + } else if (result.css) { + transformedCss = result.css; + sourceMap = result.sourceMap; } } else { switch (fileType) { diff --git a/src/options.ts b/src/options.ts index 03069cc..3417f4e 100644 --- a/src/options.ts +++ b/src/options.ts @@ -57,7 +57,7 @@ export type CustomRenderer = ( | string | { css: string; - map?: RawSourceMap; + sourceMap?: RawSourceMap; }; export interface CustomTemplateOptions {