Skip to content

Commit

Permalink
plugin system
Browse files Browse the repository at this point in the history
should prob open a discussion on the lightningcss side to figure out a better way of doing this, but this works for now
  • Loading branch information
oofdere committed Aug 3, 2024
1 parent 012088c commit 8f83d28
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
Binary file modified bun.lockb
Binary file not shown.
41 changes: 33 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { composeVisitors, transform, type CustomAtRules, type Visitor } from 'lightningcss';
import { transform, type CustomAtRules, type Visitor } from 'lightningcss';
import { composePlugins, type Plugin } from './src/plugin';

export const spacing: Visitor<CustomAtRules> = {
Token: {
Expand All @@ -12,11 +13,35 @@ export const spacing: Visitor<CustomAtRules> = {
}
} as const;

let { code, map } = transform({
filename: 'test.css',
minify: true,
code: new TextEncoder().encode('.foo {size: 0.25tw}'),
visitor: composeVisitors([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;

console.log(code.toString())
export { composePlugins, type Plugin }
14 changes: 14 additions & 0 deletions playground.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { transform } from "lightningcss";
import { composePlugins, spacing, screen } 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]),
}

let { code, map } = transform(a);
console.log(a)

console.log(code.toString())
24 changes: 24 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { composeVisitors, type CustomAtRules, type Visitor } from 'lightningcss';

export type Plugin<C extends CustomAtRules> = {
visitor: Visitor<C>,
customAtRules?: C
}

export function composePlugins<C extends CustomAtRules>(plugins: (Plugin<C> | Visitor<C>)[]) {
let customAtRules: CustomAtRules = {};
let visitors: Visitor<CustomAtRules>[] = [];

plugins.forEach(p => {
if ('visitor' in p) {
visitors.push(p.visitor)
if (p.customAtRules) { Object.assign(customAtRules, p.customAtRules) }
} else {
visitors.push(p)
}
});

return {
customAtRules, visitor: composeVisitors(visitors)
}
}
7 changes: 3 additions & 4 deletions tests/test.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { composeVisitors, transform, type CustomAtRules, type Visitor } from "lightningcss";
import { spacing } from "../index";
import { composePlugins, spacing, type Plugin } from "../index";

import { expect, test } from "bun:test";

function testHelper(visitors: Visitor<CustomAtRules>[], input: string) {
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),
visitor: composeVisitors(visitors),

...composePlugins(visitors)
});

return code.toString()
Expand Down

0 comments on commit 8f83d28

Please sign in to comment.