Add vite plugin

This commit is contained in:
Alex
2024-03-30 21:26:11 -04:00
parent 8f6e77f899
commit dbfdf67983
3 changed files with 36 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@aet/babel-tailwind", "name": "@aet/babel-tailwind",
"version": "0.0.1-beta.0", "version": "0.0.1-beta.2",
"main": "dist/index.js", "main": "dist/index.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
@ -21,6 +21,7 @@
"tailwindcss": "^3.4.3", "tailwindcss": "^3.4.3",
"tsup": "^8.0.2", "tsup": "^8.0.2",
"typescript": "^5.4.3", "typescript": "^5.4.3",
"vite": "^5.2.7",
"vitest": "^1.4.0" "vitest": "^1.4.0"
}, },
"peerDependencies": { "peerDependencies": {

3
pnpm-lock.yaml generated
View File

@ -49,6 +49,9 @@ devDependencies:
typescript: typescript:
specifier: ^5.4.3 specifier: ^5.4.3
version: 5.4.3 version: 5.4.3
vite:
specifier: ^5.2.7
version: 5.2.7(@types/node@20.12.2)
vitest: vitest:
specifier: ^1.4.0 specifier: ^1.4.0
version: 1.4.0(@types/node@20.12.2) version: 1.4.0(@types/node@20.12.2)

View File

@ -3,13 +3,15 @@ import { basename, dirname, extname, join } from "node:path";
import { once } from "lodash"; import { once } from "lodash";
import hash from "@emotion/hash"; import hash from "@emotion/hash";
import type babel from "@babel/core"; import type babel from "@babel/core";
import { type NodePath, type types as t, transformSync } from "@babel/core"; import type * as vite from "vite";
import type * as esbuild from "esbuild"; import type * as esbuild from "esbuild";
import { type NodePath, type types as t, transformSync } from "@babel/core";
import tailwind, { type Config } from "tailwindcss"; import tailwind, { type Config } from "tailwindcss";
import postcss from "postcss"; import postcss from "postcss";
const PLUGIN_NAME = "tailwind"; const PLUGIN_NAME = "tailwind";
const ESBUILD_NAMESPACE = "babel-tailwind"; const ESBUILD_NAMESPACE = "babel-tailwind";
const ROLLUP_PREFIX = "\0tailwind:";
const definePlugin = const definePlugin =
<T>(fn: (runtime: typeof babel) => babel.Visitor<babel.PluginPass & T>) => <T>(fn: (runtime: typeof babel) => babel.Visitor<babel.PluginPass & T>) =>
@ -132,6 +134,7 @@ const babelTailwind = (
const result = Array.from(tailwindMap) const result = Array.from(tailwindMap)
.map(([className, value]) => `.${className} {\n @apply ${value}\n}`) .map(([className, value]) => `.${className} {\n @apply ${value}\n}`)
.join("\n"); .join("\n");
styleMap.set(join(dirname(filename), cssName), result); styleMap.set(join(dirname(filename), cssName), result);
}, },
}, },
@ -301,9 +304,33 @@ const esbuildPlugin = (
}, },
}); });
const vitePlugin = (styleMap: Map<string, string>, compile: Compile): vite.Plugin => ({
name: "tailwind",
resolveId(id, importer) {
if (id.startsWith("tailwind:")) {
const resolved = join(dirname(importer!), id.slice("tailwind:".length));
if (styleMap.has(resolved)) {
return {
id: ROLLUP_PREFIX + resolved,
moduleSideEffects: true,
};
}
}
},
async load(id: string) {
if (id.startsWith(ROLLUP_PREFIX)) {
const resolved = id.slice(ROLLUP_PREFIX.length);
if (styleMap.has(resolved)) {
const result = await compile(styleMap.get(resolved)!);
return result.css;
}
}
},
});
/** /**
* Main entry. Returns the esbuild, babel plugins and utilities for processing * Main entry. Returns the plugins and utilities for processing Tailwind
* Tailwind classNames in JS. * classNames in JS.
* *
* @example * @example
* import { build } from "esbuild"; * import { build } from "esbuild";
@ -325,6 +352,7 @@ export function getTailwindPlugins(options: TailwindPluginOptions) {
compile, compile,
babel: babelTailwind(styleMap, options), babel: babelTailwind(styleMap, options),
esbuild: esbuildPlugin(styleMap, compile), esbuild: esbuildPlugin(styleMap, compile),
vite: vitePlugin(styleMap, compile),
styleMap, styleMap,
[Symbol.dispose]() { [Symbol.dispose]() {
styleMap.clear(); styleMap.clear();