Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make small change to customRenderer #257

Merged
merged 2 commits into from
Feb 11, 2024
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
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/helpers/getCssExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type CustomRenderer = (
| string
| {
css: string;
map?: RawSourceMap;
sourceMap?: RawSourceMap;
};

export interface CustomTemplateOptions {
Expand Down
Loading