Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
oofdere committed Aug 3, 2024
1 parent 8f83d28 commit 72557f2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 59 deletions.
48 changes: 4 additions & 44 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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<CustomAtRules> = {
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> = {
customAtRules: {
screen: {
prelude: '<custom-ident>',
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 }
export { composePlugins, type Plugin, screens, spacing }
4 changes: 2 additions & 2 deletions playground.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
33 changes: 33 additions & 0 deletions src/screens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { CustomAtRules } from "lightningcss";
import type { Plugin } from "./plugin";

export const screens: Plugin<CustomAtRules> = {
customAtRules: {
screen: {
prelude: '<custom-ident>',
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;
13 changes: 13 additions & 0 deletions src/spacing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { CustomAtRules, Visitor } from "lightningcss";

export const spacing: Visitor<CustomAtRules> = {
Token: {
dimension(token) {
if (token.unit === 'tw') {
return {
raw: `${token.value * 0.25}rem`
}
}
}
}
} as const;
15 changes: 2 additions & 13 deletions tests/test.test.ts
Original file line number Diff line number Diff line change
@@ -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<C extends CustomAtRules>(visitors: (Plugin<C> | Visitor<C>)[], 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}'))
Expand Down
13 changes: 13 additions & 0 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { transform, type CustomAtRules, type Visitor } from "lightningcss";
import { composePlugins, type Plugin } from "../src/plugin";

export function testHelper<C extends CustomAtRules>(visitors: (Plugin<C> | Visitor<C>)[], input: string) {
let { code } = transform({
filename: 'test.css',
minify: true,
code: new TextEncoder().encode(input),
...composePlugins(visitors)
});

return code.toString()
}

0 comments on commit 72557f2

Please sign in to comment.