From 72557f2a3dec0d4a34bc190be4b37b4bc17c3df1 Mon Sep 17 00:00:00 2001 From: Tibet Tornaci Date: Sat, 3 Aug 2024 15:30:10 -0700 Subject: [PATCH] refactor --- index.ts | 48 ++++------------------------------------------ playground.ts | 4 ++-- src/screens.ts | 33 +++++++++++++++++++++++++++++++ src/spacing.ts | 13 +++++++++++++ tests/test.test.ts | 15 ++------------- tests/test.ts | 13 +++++++++++++ 6 files changed, 67 insertions(+), 59 deletions(-) create mode 100644 src/screens.ts create mode 100644 src/spacing.ts create mode 100644 tests/test.ts diff --git a/index.ts b/index.ts index 690f198..805ad04 100644 --- a/index.ts +++ b/index.ts @@ -1,47 +1,7 @@ -import { transform, type CustomAtRules, type Visitor } from 'lightningcss'; import { composePlugins, type Plugin } from './src/plugin'; +import { screens } from './src/screens'; +import { spacing } from './src/spacing'; -export const spacing: Visitor = { - Token: { - dimension(token) { - if (token.unit === 'tw') { - return { - raw: `${token.value * 0.25}rem` - } - } - } - } -} as const; +export const crosswind = composePlugins([screens, spacing]) -export const screen: Plugin = { - customAtRules: { - screen: { - prelude: '', - body: 'style-block' - } - }, - visitor: { - Rule: { - custom: { - screen(rule) { - console.log(rule.body.value) - - return { - type: 'media', - value: { - rules: rule.body.value, - loc: rule.loc, - query: { - mediaQueries: [ - { raw: '(min-width: 500px)' } - ] - } - } - } - } - } - } - } -} as const; - -export { composePlugins, type Plugin } \ No newline at end of file +export { composePlugins, type Plugin, screens, spacing } \ No newline at end of file diff --git a/playground.ts b/playground.ts index 46f5096..d08df88 100644 --- a/playground.ts +++ b/playground.ts @@ -1,11 +1,11 @@ import { transform } from "lightningcss"; -import { composePlugins, spacing, screen } from "."; +import { composePlugins, spacing, screens } from "."; const a = { filename: 'test.css', minify: true, code: new TextEncoder().encode('.foo {size: 0.25tw; @screen lg {size: 1tw; color: red;}}'), - ...composePlugins([spacing, screen]), + ...composePlugins([spacing, screens]), } let { code, map } = transform(a); diff --git a/src/screens.ts b/src/screens.ts new file mode 100644 index 0000000..89082b5 --- /dev/null +++ b/src/screens.ts @@ -0,0 +1,33 @@ +import type { CustomAtRules } from "lightningcss"; +import type { Plugin } from "./plugin"; + +export const screens: Plugin = { + customAtRules: { + screen: { + prelude: '', + body: 'style-block' + } + }, + visitor: { + Rule: { + custom: { + screen(rule) { + console.log(rule.body.value) + + return { + type: 'media', + value: { + rules: rule.body.value, + loc: rule.loc, + query: { + mediaQueries: [ + { raw: '(min-width: 500px)' } + ] + } + } + } + } + } + } + } +} as const; \ No newline at end of file diff --git a/src/spacing.ts b/src/spacing.ts new file mode 100644 index 0000000..ebff933 --- /dev/null +++ b/src/spacing.ts @@ -0,0 +1,13 @@ +import type { CustomAtRules, Visitor } from "lightningcss"; + +export const spacing: Visitor = { + Token: { + dimension(token) { + if (token.unit === 'tw') { + return { + raw: `${token.value * 0.25}rem` + } + } + } + } +} as const; \ No newline at end of file diff --git a/tests/test.test.ts b/tests/test.test.ts index 04ed246..15c9c21 100644 --- a/tests/test.test.ts +++ b/tests/test.test.ts @@ -1,18 +1,7 @@ -import { composeVisitors, transform, type CustomAtRules, type Visitor } from "lightningcss"; -import { composePlugins, spacing, type Plugin } from "../index"; - import { expect, test } from "bun:test"; +import { testHelper } from "./test"; +import { spacing } from "../index"; -export function testHelper(visitors: (Plugin | Visitor)[], input: string) { - let { code } = transform({ - filename: 'test.css', - minify: true, - code: new TextEncoder().encode(input), - ...composePlugins(visitors) - }); - - return code.toString() -} test("tw units", () => { expect(testHelper([spacing], '.foo {size: 1tw}')) diff --git a/tests/test.ts b/tests/test.ts new file mode 100644 index 0000000..9b9fc5d --- /dev/null +++ b/tests/test.ts @@ -0,0 +1,13 @@ +import { transform, type CustomAtRules, type Visitor } from "lightningcss"; +import { composePlugins, type Plugin } from "../src/plugin"; + +export function testHelper(visitors: (Plugin | Visitor)[], input: string) { + let { code } = transform({ + filename: 'test.css', + minify: true, + code: new TextEncoder().encode(input), + ...composePlugins(visitors) + }); + + return code.toString() +}