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

feat(transformer): support custom aliases #169

Merged
merged 1 commit into from
Mar 12, 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
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,33 @@ you need to add the Tailwind Variants `wrapper` to your TailwindCSS config file
// tailwind.config.js

const { withTV } = require('tailwind-variants/transformer')


/** @type {import('tailwindcss').Config} */
module.exports = withTV({
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
})
```

If you're using a custom path to import Tailwind variants, such as creating a custom tv instance with `createTV`, it's recommended to include this path in the transformer configuration:

```js
// tailwind.config.js

const { withTV } = require('tailwind-variants/transformer')

/** @type {import('tailwindcss').Config} */
module.exports = withTV({
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
}, {
aliases: ["@/lib/tv"]
})
```

Expand Down
38 changes: 38 additions & 0 deletions src/__tests__/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,44 @@ describe("Responsive Variants", () => {
expect(result).toBe(expectedContent(sourceCode, transformedContent));
});

test("should return a transformed content (with custom aliases)", () => {
const sourceCode = `
import {tv} from "@/lib/tv";

const button = tv(
{
variants: {
color: {
primary: "text-blue-50 bg-blue-600 rounded"
}
}
},
{
responsiveVariants: true
}
);
`;

const result = tvTransformer(sourceCode, defaultScreens, {aliases: ["@/lib/tv"]});

const transformedContent = [
{
color: {
primary: {
original: "text-blue-50 bg-blue-600 rounded",
sm: "sm:text-blue-50 sm:bg-blue-600 sm:rounded",
md: "md:text-blue-50 md:bg-blue-600 md:rounded",
lg: "lg:text-blue-50 lg:bg-blue-600 lg:rounded",
xl: "xl:text-blue-50 xl:bg-blue-600 xl:rounded",
"2xl": "2xl:text-blue-50 2xl:bg-blue-600 2xl:rounded",
},
},
},
];

expect(result).toBe(expectedContent(sourceCode, transformedContent));
});

test("should return tailwind config with built-in transformer (withTV content array)", () => {
const expectedResult = {
content: {
Expand Down
1 change: 0 additions & 1 deletion src/__tests__/tv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ describe("Tailwind Variants (TV) - Default", () => {
expect(h1({bool: undefined})).toHaveClass(["text-3xl", "truncate"]);
});


test("should support false only variant -- default variant", () => {
const h1 = tv({
base: "text-3xl",
Expand Down
12 changes: 11 additions & 1 deletion src/transformer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ export type WithTV = {

export declare const withTV: WithTV;

export type TVTransformerConfig = {
/**
* Optional array of custom aliases where Tailwind Variants might be resolved.
* This can be useful if you're using a custom path to import Tailwind Variants.
*
* @example ["@/lib/tv"]
*/
aliases?: string[];
};

export type TVTransformer = {
(content: string, screens?: string[] | DefaultScreens[]): string;
(content: string, screens?: string[] | DefaultScreens[], config?: TVTransformerConfig): string;
};

export declare const tvTransformer: TVTransformer;
16 changes: 11 additions & 5 deletions src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,16 @@ const transformContent = ({options, config}, screens) => {
}
};

export const tvTransformer = (content, screens) => {
export const tvTransformer = (content, screens, config) => {
try {
// TODO: support package alias
if (!content.includes("tailwind-variants")) return content;
const defaultImportPaths = ["tailwind-variants"];

const importPaths = isArray(config?.aliases)
? [...config.aliases, ...defaultImportPaths]
: defaultImportPaths;
const containsImportPath = importPaths.some((path) => content.includes(path));

if (!containsImportPath) return content;

const tvs = getTVObjects(content);

Expand Down Expand Up @@ -250,7 +256,7 @@ const getExtensions = (files) => {
return Array.from(new Set(extensions)).filter((ext) => ext !== "html");
};

export const withTV = (tailwindConfig) => {
export const withTV = (tailwindConfig, transformerConfig) => {
let config = resolveConfig(tailwindConfig);

// generate types
Expand All @@ -261,7 +267,7 @@ export const withTV = (tailwindConfig) => {

// with tailwind configured screens
const transformer = (content) => {
return tvTransformer(content, Object.keys(config.theme?.screens ?? {}));
return tvTransformer(content, Object.keys(config.theme?.screens ?? {}), transformerConfig);
};

// custom transform
Expand Down
Loading