Update
This commit is contained in:
46
src/index.ts
46
src/index.ts
@ -2,7 +2,7 @@ import hash from "@emotion/hash";
|
||||
import type { Config } from "tailwindcss";
|
||||
import type postcss from "postcss";
|
||||
import { memoize } from "lodash";
|
||||
import { babelTailwind } from "./babel-tailwind";
|
||||
import { type ClassNameCollector, babelTailwind } from "./babel-tailwind";
|
||||
import { esbuildPlugin } from "./esbuild-postcss";
|
||||
import { vitePlugin } from "./vite-plugin";
|
||||
import { type StyleMap, createPostCSS, type tailwindDirectives } from "./shared";
|
||||
@ -27,7 +27,7 @@ export interface TailwindPluginOptions {
|
||||
/**
|
||||
* Directives to prefix to all Tailwind stylesheets
|
||||
*/
|
||||
directives?: (typeof tailwindDirectives)[number][] | "all";
|
||||
directives?: (typeof tailwindDirectives)[number][] | "all" | undefined;
|
||||
|
||||
/**
|
||||
* Additional PostCSS plugins (optional)
|
||||
@ -53,7 +53,7 @@ export interface TailwindPluginOptions {
|
||||
* declare const tw: TaggedTailwindFunction;
|
||||
* "tw" => tw`p-2 text-center`
|
||||
*/
|
||||
taggedTemplateName?: string;
|
||||
taggedTemplateName?: string | undefined;
|
||||
|
||||
/**
|
||||
* The prefix to use for the generated class names.
|
||||
@ -65,8 +65,22 @@ export interface TailwindPluginOptions {
|
||||
* Preferred library for classnames
|
||||
*/
|
||||
clsx: "clsx" | "classnames" | "emotion";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
styleMap?: StyleMap;
|
||||
|
||||
/**
|
||||
* Custom CSS compile function
|
||||
* @example
|
||||
* async css => (await postcss.process(css, { plugins: [tailwindcss()] })).css
|
||||
*/
|
||||
compile(css: string): Promise<string>;
|
||||
}
|
||||
|
||||
export type ResolveTailwindOptions = Required<TailwindPluginOptions>;
|
||||
|
||||
/**
|
||||
* Hashes and prefixes a string of Tailwind class names.
|
||||
* @example getClassName("p-2 text-center") // "tw-1r6fxxz"
|
||||
@ -90,15 +104,31 @@ export const getClassName: GetClassName = cls => "tw-" + hash(cls);
|
||||
* });
|
||||
*/
|
||||
export function getTailwindPlugins(options: TailwindPluginOptions) {
|
||||
const styleMap: StyleMap = new Map();
|
||||
const compile = memoize(createPostCSS(options));
|
||||
const resolvedOptions: ResolveTailwindOptions = {
|
||||
directives: undefined,
|
||||
getClassName,
|
||||
jsxAttributeAction: "delete",
|
||||
jsxAttributeName: "css",
|
||||
postCSSPlugins: [],
|
||||
styleMap: new Map(),
|
||||
taggedTemplateName: undefined,
|
||||
tailwindConfig: {},
|
||||
...options,
|
||||
};
|
||||
|
||||
const getCompiler = () => createPostCSS(resolvedOptions);
|
||||
|
||||
const { styleMap } = resolvedOptions;
|
||||
const compile = options.compile ?? memoize(getCompiler());
|
||||
|
||||
return {
|
||||
compile,
|
||||
babel: babelTailwind(styleMap, options),
|
||||
esbuild: esbuildPlugin(styleMap, compile),
|
||||
vite: vitePlugin(styleMap, compile),
|
||||
babel: (onCollect?: ClassNameCollector) => babelTailwind(resolvedOptions, onCollect),
|
||||
esbuild: () => esbuildPlugin(styleMap, compile),
|
||||
vite: () => vitePlugin(styleMap, compile),
|
||||
styleMap,
|
||||
options,
|
||||
getCompiler,
|
||||
[Symbol.dispose]() {
|
||||
styleMap.clear();
|
||||
},
|
||||
|
Reference in New Issue
Block a user