Skip to content

Commit 62c2ec9

Browse files
authored
Merge pull request #2 from Jackardios/dev
v1.0.0
2 parents 2993468 + 591dbc5 commit 62c2ec9

File tree

8 files changed

+1885
-1346
lines changed

8 files changed

+1885
-1346
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
## Features:
1818

19-
- supports all (or almost all) the features currently available in TailwindCSS
19+
- supports almost all the features (except custom plugins) currently available in TailwindCSS
2020
- the ability to set your own TailwindCSS configuration
2121
- colors are matched regardless of the format used
2222
- rem is converted to px (it is possible to configure the rem size)
@@ -211,11 +211,12 @@ Console output
211211

212212
### TailwindConverter(options?)
213213

214-
| Option | Type | Default | Description |
215-
| -------------- | ------------------ | ------- | ----------------------------------------------------------------------- |
216-
| remInPx | `number` \| `null` | `null` | `rem` in `px` unit. Set null if you don't want to convert rem to pixels |
217-
| tailwindConfig | `Config` | {} | Set your tailwind config here |
218-
| postCSSPlugins | `AcceptedPlugin[]` | [] | Array of acceptable postcss plugins |
214+
| Option | Type | Default | Description |
215+
| ---------------------------- | ------------------ | ------- | ---------------------------------------------------------------------------------------- |
216+
| remInPx | `number` \| `null` | `null` | `rem` in `px` unit. Set null if you don't want to convert rem to pixels |
217+
| arbitraryPropertiesIsEnabled | `boolean` | `false` | defines whether non-convertible properties should be converted as "arbitrary properties" |
218+
| tailwindConfig | `Config` | {} | Set your tailwind config here |
219+
| postCSSPlugins | `AcceptedPlugin[]` | [] | Array of acceptable postcss plugins |
219220

220221
[build-img]: https://github.com/jackardios/css-to-tailwindcss/actions/workflows/release.yml/badge.svg
221222
[build-url]: https://github.com/jackardios/css-to-tailwindcss/actions/workflows/release.yml

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-to-tailwindcss",
3-
"version": "0.1.2",
3+
"version": "1.0.0",
44
"description": "CSS to TailwindCSS converter",
55
"main": "./lib/index.js",
66
"files": [

src/TailwindConverter.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import postcss, {
1212
Document,
1313
} from 'postcss';
1414
import postcssSafeParser from 'postcss-safe-parser';
15-
import resolveConfig from 'tailwindcss/resolveConfig';
1615
import { parse, stringify, isTraversal } from 'css-what';
1716

1817
import { TailwindNode, TailwindNodesManager } from './TailwindNodesManager';
@@ -23,6 +22,7 @@ import {
2322
} from './utils/converterMappingByTailwindTheme';
2423
import {
2524
convertDeclarationValue,
25+
prepareArbitraryValue,
2626
TAILWIND_DECLARATION_CONVERTERS,
2727
} from './constants/converters';
2828
import { isChildNode } from './utils/isChildNode';
@@ -31,15 +31,18 @@ import { removeUnnecessarySpaces } from './utils/removeUnnecessarySpaces';
3131
import { reduceTailwindClasses } from './utils/reduceTailwindClasses';
3232
import { PSEUDOS_MAPPING } from './constants/pseudos-mapping';
3333
import { detectIndent } from './utils/detectIndent';
34+
import { resolveConfig, ResolvedTailwindConfig } from './utils/resolveConfig';
3435

3536
export interface TailwindConverterConfig {
3637
remInPx?: number | null;
37-
tailwindConfig: Config;
38+
tailwindConfig?: Config;
3839
postCSSPlugins: AcceptedPlugin[];
40+
arbitraryPropertiesIsEnabled: boolean;
3941
}
4042

4143
export interface ResolvedTailwindConverterConfig
4244
extends TailwindConverterConfig {
45+
tailwindConfig: ResolvedTailwindConfig;
4346
mapping: ConverterMapping;
4447
}
4548

@@ -48,6 +51,7 @@ export const DEFAULT_CONVERTER_CONFIG: Omit<
4851
'tailwindConfig'
4952
> = {
5053
postCSSPlugins: [],
54+
arbitraryPropertiesIsEnabled: false,
5155
};
5256

5357
export class TailwindConverter {
@@ -58,7 +62,7 @@ export class TailwindConverter {
5862
...converterConfig
5963
}: Partial<TailwindConverterConfig> = {}) {
6064
const resolvedTailwindConfig = resolveConfig(
61-
tailwindConfig || ({} as Config)
65+
tailwindConfig || ({ content: [] } as Config)
6266
);
6367

6468
this.config = {
@@ -146,12 +150,23 @@ export class TailwindConverter {
146150
}
147151

148152
protected convertDeclarationToClasses(declaration: Declaration) {
149-
return (
153+
let classes =
150154
TAILWIND_DECLARATION_CONVERTERS[declaration.prop]?.(
151155
declaration,
152156
this.config
153-
) || []
154-
);
157+
) || [];
158+
159+
if (classes.length === 0 && this.config.arbitraryPropertiesIsEnabled) {
160+
classes = [
161+
`[${declaration.prop}:${prepareArbitraryValue(declaration.value)}]`,
162+
];
163+
}
164+
165+
return this.config.tailwindConfig.prefix
166+
? classes.map(
167+
className => `${this.config.tailwindConfig.prefix}${className}`
168+
)
169+
: classes;
155170
}
156171

157172
protected makeTailwindNode(
@@ -222,7 +237,7 @@ export class TailwindConverter {
222237
if (selector.type === 'pseudo' || selector.type === 'pseudo-element') {
223238
const mapped = (PSEUDOS_MAPPING as any)[selector.name];
224239

225-
return mapped ? `${mapped}:` : null;
240+
return mapped ? `${mapped}${this.config.tailwindConfig.separator}` : null;
226241
}
227242

228243
if (selector.type === 'attribute') {
@@ -234,7 +249,7 @@ export class TailwindConverter {
234249
return null;
235250
}
236251

237-
return `${mapped}:`;
252+
return `${mapped}${this.config.tailwindConfig.separator}`;
238253
}
239254

240255
if (selector.name.startsWith('data-')) {
@@ -245,7 +260,7 @@ export class TailwindConverter {
245260
return null;
246261
}
247262

248-
return `${mapped}:`;
263+
return `${mapped}${this.config.tailwindConfig.separator}`;
249264
}
250265
}
251266

@@ -345,9 +360,11 @@ export class TailwindConverter {
345360
modifiers.push(mappedScreen);
346361
}
347362

348-
const classPrefix = modifiers.join(':');
363+
const classPrefix = modifiers.join(this.config.tailwindConfig.separator);
349364

350-
return classPrefix ? classPrefix + ':' : '';
365+
return classPrefix
366+
? classPrefix + this.config.tailwindConfig.separator
367+
: '';
351368
}
352369

353370
protected convertSupportsParamsToClassPrefix(supportParams: string[]) {
@@ -360,6 +377,8 @@ export class TailwindConverter {
360377
'supports'
361378
);
362379

363-
return classPrefix ? classPrefix + ':' : '';
380+
return classPrefix
381+
? classPrefix + this.config.tailwindConfig.separator
382+
: '';
364383
}
365384
}

0 commit comments

Comments
 (0)