Skip to content

Commit 5178720

Browse files
authored
feat: add additonalData feature (#203)
1 parent c179960 commit 5178720

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ Please note that no options are required. However, depending on your configurati
9999

100100
| Option | Default value | Description |
101101
| -------------------------- | ---------------------------------- | ------------------------------------------------------------------------------ |
102+
| `additonalData` | `undefined` | An optional string to append to the top of source files. |
102103
| `allowUnknownClassnames` | `false` | Disables TypeScript warnings on unknown classnames (for default imports only). |
103-
| `classnameTransform` | `asIs` | See [`classnameTransform`](#classnameTransform) below. |
104+
| `classnameTransform` | `"asIs"` | See [`classnameTransform`](#classnameTransform) below. |
104105
| `customMatcher` | `"\\.module\\.(c\|le\|sa\|sc)ss$"` | Changes the file extensions that this plugin processes. |
105106
| `customRenderer` | `false` | See [`customRenderer`](#customRenderer) below. |
106107
| `customTemplate` | `false` | See [`customTemplate`](#customTemplate) below. |

src/helpers/__tests__/getDtsSnapshot.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,32 @@ describe('helpers / cssSnapshots', () => {
330330
expect(dts).toMatchSnapshot();
331331
});
332332
});
333+
334+
describe('with additonalData enabled', () => {
335+
const fileName = join(__dirname, 'fixtures', 'test.module.scss');
336+
const css = readFileSync(fileName, 'utf8');
337+
const options: Options = {
338+
additonalData: '.my-data {\n color: red;\n}\n\n',
339+
};
340+
341+
const cssExports = getCssExports({
342+
css,
343+
fileName,
344+
logger,
345+
options,
346+
processor,
347+
compilerOptions,
348+
directory: __dirname,
349+
});
350+
351+
it('should return a dts file that contains additional data', () => {
352+
const dts = createDtsExports({
353+
cssExports,
354+
fileName,
355+
logger,
356+
options,
357+
});
358+
expect(dts).toContain('my-data');
359+
});
360+
});
333361
});

src/helpers/getCssExports.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ export const getCssExports = ({
5252
compilerOptions: tsModule.CompilerOptions;
5353
directory: string;
5454
}): CSSExportsWithSourceMap => {
55-
try {
56-
const fileType = getFileType(fileName);
57-
const rendererOptions = options.rendererOptions ?? {};
55+
const rawCss = options.additonalData ? options.additonalData + css : css;
56+
57+
const fileType = getFileType(fileName);
58+
const rendererOptions = options.rendererOptions ?? {};
5859

59-
let transformedCss = '';
60-
let sourceMap: RawSourceMap | undefined;
60+
let transformedCss = '';
61+
let sourceMap: RawSourceMap | undefined;
6162

63+
try {
6264
if (options.customRenderer) {
6365
// eslint-disable-next-line @typescript-eslint/no-var-requires
6466
const customRenderer = require(options.customRenderer) as CustomRenderer;
65-
transformedCss = customRenderer(css, {
67+
transformedCss = customRenderer(rawCss, {
6668
fileName,
6769
logger,
6870
compilerOptions,
@@ -71,7 +73,7 @@ export const getCssExports = ({
7173
switch (fileType) {
7274
case FileType.less:
7375
less.render(
74-
css,
76+
rawCss,
7577
{
7678
syncImport: true,
7779
filename: fileName,
@@ -122,10 +124,12 @@ export const getCssExports = ({
122124

123125
const importers = [aliasImporter, sassTildeImporter];
124126

125-
const result = sass.compile(fileName, {
127+
const result = sass.compileString(rawCss, {
126128
importers,
127129
loadPaths: [filePath, 'node_modules', ...(loadPaths ?? [])],
128130
sourceMap: true,
131+
syntax: fileType === FileType.sass ? 'indented' : 'scss',
132+
url: new URL(`file://${fileName}`),
129133
...sassOptions,
130134
});
131135

@@ -135,14 +139,14 @@ export const getCssExports = ({
135139
}
136140

137141
case FileType.styl:
138-
transformedCss = stylus(css, {
142+
transformedCss = stylus(rawCss, {
139143
...(rendererOptions.stylus ?? {}),
140144
filename: fileName,
141145
}).render();
142146
break;
143147

144148
default:
145-
transformedCss = css;
149+
transformedCss = rawCss;
146150
break;
147151
}
148152
}

src/options.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface RendererOptions {
2020
}
2121

2222
export interface Options {
23+
additonalData?: string;
2324
allowUnknownClassnames?: boolean;
2425
classnameTransform?: ClassnameTransformOptions;
2526
customMatcher?: string;

0 commit comments

Comments
 (0)