From ee14d81e8ee79e80e63604b1ca0f4619309d04e5 Mon Sep 17 00:00:00 2001 From: Alex <8125011+alex-kinokon@users.noreply.github.com> Date: Sat, 6 Apr 2024 21:04:33 -0400 Subject: [PATCH] Add extra processing options --- package.json | 6 ++-- src/esbuild-babel.ts | 51 +++++++++++++++++++++++++++++ src/index.test.ts | 78 ++++++++++++++++++++++++++++++++++---------- src/index.ts | 77 +++++++++++++++---------------------------- 4 files changed, 140 insertions(+), 72 deletions(-) create mode 100644 src/esbuild-babel.ts diff --git a/package.json b/package.json index 82efab3..2d7f5c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aet/babel-tailwind", - "version": "0.0.1-beta.4", + "version": "0.0.1-beta.5", "main": "dist/index.js", "license": "MIT", "private": true, @@ -12,7 +12,7 @@ "dist" ], "devDependencies": { - "@aet/eslint-rules": "^0.0.19", + "@aet/eslint-rules": "^0.0.21", "@types/babel__core": "^7.20.5", "@types/bun": "^1.0.12", "@types/lodash": "^4.17.0", @@ -43,4 +43,4 @@ "singleQuote": false, "trailingComma": "es5" } -} +} \ No newline at end of file diff --git a/src/esbuild-babel.ts b/src/esbuild-babel.ts new file mode 100644 index 0000000..82bffda --- /dev/null +++ b/src/esbuild-babel.ts @@ -0,0 +1,51 @@ +import { readFileSync } from "node:fs"; +import { extname } from "node:path"; +import { once } from "lodash"; +import type babel from "@babel/core"; +import type * as esbuild from "esbuild"; +import { transformSync } from "@babel/core"; + +/** + * An esbuild plugin that processes files with Babel if `getPlugins` returns any plugins. + */ +export const babelPlugin = ({ + filter = /\.[jt]sx?$/, + plugins: getPlugins, +}: { + filter?: RegExp; + plugins: + | babel.PluginItem[] + | ((file: { path: string; contents: string }) => babel.PluginItem[]); +}): esbuild.Plugin => ({ + name: "babel-plugin", + setup(build) { + build.onLoad({ filter }, ({ path }) => { + const load = once(() => readFileSync(path, "utf-8")); + const plugins = Array.isArray(getPlugins) + ? getPlugins + : getPlugins({ + path, + get contents() { + return load(); + }, + }); + + if (!plugins.length) { + return; + } + + const { code } = transformSync(load(), { + parserOpts: { + plugins: ["jsx", "decorators", "typescript", "importAttributes"], + }, + filename: path, + plugins, + })!; + + return { + contents: code!, + loader: extname(path).slice(1) as "js" | "ts", + }; + }); + }, +}); diff --git a/src/index.test.ts b/src/index.test.ts index 125e63c..9143ebe 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,14 +1,14 @@ import { promises as fs } from "node:fs"; import { resolve } from "node:path"; -import { build, transformSync } from "esbuild"; +import { type OutputFile, build, transformSync } from "esbuild"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { name } from "../package.json" with { type: "json" }; import { type TailwindPluginOptions, babelPlugin, + createPostCSS, getClassName, getTailwindPlugins, - createPostCSS, } from "./index"; const folder = resolve(import.meta.dirname, "temp"); @@ -28,8 +28,17 @@ describe("babel-tailwind", () => { return resolved; } + const minCSS = (text: string) => + transformSync(text, { minify: true, loader: "css" }).code; + + const findByExt = (outputFiles: OutputFile[], ext: string) => + outputFiles.find(file => file.path.endsWith(ext))!; + async function compileESBuild(options: TailwindPluginOptions, javascript: string) { - const tailwind = getTailwindPlugins(options); + const tailwind = getTailwindPlugins({ + tailwindConfig: {}, + ...options, + }); const result = await build({ bundle: true, write: false, @@ -37,7 +46,7 @@ describe("babel-tailwind", () => { outdir: "dist", format: "esm", entryPoints: [await write("index.tsx", javascript)], - plugins: [babelPlugin({ getPlugins: () => [tailwind.babel] }), tailwind.esbuild], + plugins: [babelPlugin({ plugins: [tailwind.babel] }), tailwind.esbuild], }); const { errors, warnings, outputFiles } = result; @@ -49,10 +58,7 @@ describe("babel-tailwind", () => { it("supports ESBuild", async () => { const outputFiles = await compileESBuild( - { - tailwindConfig: {}, - clsx: "emotion", - }, + { clsx: "emotion" }, /* tsx */ ` export function Hello() { return ( @@ -65,33 +71,69 @@ describe("babel-tailwind", () => { ); expect(outputFiles).toHaveLength(2); - const js = outputFiles.find(file => file.path.endsWith(".js"))!; - const css = outputFiles.find(file => file.path.endsWith(".css"))!; + const js = findByExt(outputFiles, ".js"); + const css = findByExt(outputFiles, ".css"); const clsName = getClassName("text-center"); expect(js.text).toContain(`className: "${clsName}"`); expect(css.text).toMatch(`.${clsName} {\n text-align: center;\n}`); }); - const minCSS = (text: string) => - transformSync(text, { minify: true, loader: "css" })!.code; + it("does not remove the attribute if `preserveAttribute` is true", async () => { + const outputFiles = await compileESBuild( + { clsx: "emotion", jsxAttributeAction: "preserve" }, + /* tsx */ ` + export function Hello() { + return ( +