@salty-css/vite 0.0.1-alpha.2 → 0.0.1-alpha.200
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +164 -43
- package/index.cjs +44 -0
- package/index.d.ts +3 -0
- package/index.js +595 -0
- package/package.json +12 -3
package/README.md
CHANGED
@@ -1,15 +1,132 @@
|
|
1
|
-
|
1
|
+

|
2
2
|
|
3
|
-
|
3
|
+
# Salty CSS - CSS-in-JS library that is kinda sweet
|
4
4
|
|
5
|
-
|
5
|
+
Is there anything saltier than CSS in frontend web development? Salty CSS is built to provide better developer experience for developers looking for performant and feature rich CSS-in-JS solutions.
|
6
6
|
|
7
|
-
|
8
|
-
2. Create `salty-config.ts` to the root of your project
|
9
|
-
3. Import global styles to any regular .css file from `saltygen/index.css` (does not exist during first run, cli command coming later)
|
10
|
-
4. Create salty components with styled only inside files that end with `.css.ts`, `.salty.ts` `.styled.ts` or `.styles.ts`
|
7
|
+
## Features
|
11
8
|
|
12
|
-
|
9
|
+
- Build time compilation to achieve awesome runtime performance and minimal size
|
10
|
+
- Next.js, React Server Components, Vite and Webpack support
|
11
|
+
- Type safety with out of the box TypeScript and ESLint plugin
|
12
|
+
- Advanced CSS variables configuration to allow smooth token usage
|
13
|
+
- Style templates to create reusable styles easily
|
14
|
+
|
15
|
+
## Get started
|
16
|
+
|
17
|
+
Fastest way to get started with any framework is `npx salty-css init [directory]` command
|
18
|
+
|
19
|
+
- Next.js → [Next.js guide](#nextjs) + [Next.js example app](https://github.com/margarita-form/salty-css-website)
|
20
|
+
- React + Vite → [React + Vite guide](#react--vite) + [React example code](#code-examples)
|
21
|
+
- React + Webpack → Guide coming soon
|
22
|
+
|
23
|
+
## Useful commands
|
24
|
+
|
25
|
+
- Create component: `npx salty-css generate [filePath]`
|
26
|
+
- Build: `npx salty-css build [directory]`
|
27
|
+
- Update Salty CSS packages: `npx salty-css up`
|
28
|
+
|
29
|
+
## Salty CSS styled function
|
30
|
+
|
31
|
+
```ts
|
32
|
+
// components/wrapper.css.ts
|
33
|
+
import { styled } from '@salty-css/react/styled';
|
34
|
+
|
35
|
+
// Define a component with styled function. First argument is the component name or existing component to extend and second argument is the object containing the styles and other options
|
36
|
+
export const Component = styled('div', {
|
37
|
+
className: 'wrapper', // Define custom class name that will be included for this component
|
38
|
+
element: 'section', // Define the html element that will be rendered for this component, overrides the first 'div' argument
|
39
|
+
base: {
|
40
|
+
// 👉 Add your CSS-in-JS base styles here! 👈
|
41
|
+
},
|
42
|
+
variants: {
|
43
|
+
// Define conditional styles that will be applied to the component based on the variant prop values
|
44
|
+
},
|
45
|
+
compoundVariants: [
|
46
|
+
// Define conditional styles that will be applied to the component based on the combination of variant prop values
|
47
|
+
],
|
48
|
+
defaultVariants: {
|
49
|
+
// Set default variant prop values
|
50
|
+
},
|
51
|
+
defaultProps: {
|
52
|
+
// Add additional default props for the component (eg, id and other html element attributes)
|
53
|
+
},
|
54
|
+
passProps: true, // Pass variant props to the rendered element / parent component (default: false)
|
55
|
+
});
|
56
|
+
```
|
57
|
+
|
58
|
+
## Salty CSS CLI
|
59
|
+
|
60
|
+
In your existing repository you can use `npx salty-css [command]` to initialize a project, generate components, update related packages and build required files.
|
61
|
+
|
62
|
+
- Initialize project → `npx salty-css init [directory]` - Installs required packages, detects framework in use and creates project files to the provided directory. Directory can be left blank if you want files to be created to the current directory.
|
63
|
+
- Generate component → `npx salty-css update [version]` - Update @salty-css packages in your repository. Default version is "latest". Additional options like `--dir`, `--tag`, `--name` and `--className` are also supported.
|
64
|
+
- Build files → `npx salty-css build [directory]` - Compile Salty CSS related files in your project. This should not be needed if you are using tools like Next.js or Vite
|
65
|
+
|
66
|
+
## Usage
|
67
|
+
|
68
|
+
### Next.js
|
69
|
+
|
70
|
+

|
71
|
+
|
72
|
+
Salty CSS provides Next.js App & Pages router support with full React Server Components support.
|
73
|
+
|
74
|
+
### Add Salty CSS to Next.js
|
75
|
+
|
76
|
+
1. In your existing Next.js repository you can run `npx salty-css init` to automatically configure Salty CSS.
|
77
|
+
2. Create your first Salty CSS component with `npx salty-css generate [filePath]` (e.g. src/custom-wrapper)
|
78
|
+
3. Import your component for example to `page.tsx` and see it working!
|
79
|
+
|
80
|
+
And note: steps 2 & 3 are just to show how get new components up and running, step 1 does all of the important stuff 🤯
|
81
|
+
|
82
|
+
#### Manual configuration
|
83
|
+
|
84
|
+
1. For Next.js support install `npm i @salty-css/next @salty-css/core @salty-css/react`
|
85
|
+
2. Create `salty.config.ts` to your app directory
|
86
|
+
3. Add Salty CSS plugin to next.js config
|
87
|
+
|
88
|
+
- **Next.js 15:** In `next.config.ts` add import for salty plugin `import { withSaltyCss } from '@salty-css/next';` and then add `withSaltyCss` to wrap your nextConfig export like so `export default withSaltyCss(nextConfig);`
|
89
|
+
- **Next.js 14 and older:** In `next.config.js` add import for salty plugin `const { withSaltyCss } = require('@salty-css/next');` and then add `withSaltyCss` to wrap your nextConfig export like so `module.exports = withSaltyCss(nextConfig);`
|
90
|
+
|
91
|
+
4. Make sure that `salty.config.ts` and `next.config.ts` are in the same folder!
|
92
|
+
5. Build `saltygen` directory by running your app once or with cli `npx salty-css build [directory]`
|
93
|
+
6. Import global styles from `saltygen/index.css` to some global css file with `@import 'insert_path_to_index_css';`.
|
94
|
+
|
95
|
+
[Check out Next.js demo project](https://github.com/margarita-form/salty-css-website) or [react example code](#code-examples)
|
96
|
+
|
97
|
+
---
|
98
|
+
|
99
|
+
### React + Vite
|
100
|
+
|
101
|
+

|
102
|
+
|
103
|
+
### Add Salty CSS to your React + Vite app
|
104
|
+
|
105
|
+
1. In your existing Vite repository you can run `npx salty-css init` to automatically configure Salty CSS.
|
106
|
+
2. Create your first Salty CSS component with `npx salty-css generate [filePath]` (e.g. src/custom-wrapper)
|
107
|
+
3. Import your component for example to `main.tsx` and see it working!
|
108
|
+
|
109
|
+
And note: steps 2 & 3 are just to show how get new components up and running, step 1 does all of the important stuff 🤯
|
110
|
+
|
111
|
+
#### Manual configuration
|
112
|
+
|
113
|
+
1. For Vite support install `npm i @salty-css/vite @salty-css/core`
|
114
|
+
2. In `vite.config` add import for salty plugin `import { saltyPlugin } from '@salty-css/vite';` and then add `saltyPlugin(__dirname)` to your vite configuration plugins
|
115
|
+
3. Make sure that `salty.config.ts` and `vite.config.ts` are in the same folder!
|
116
|
+
4. Build `saltygen` directory by running your app once or with cli `npx salty-css build [directory]`
|
117
|
+
5. Import global styles from `saltygen/index.css` to some global css file with `@import 'insert_path_to_index_css';`.
|
118
|
+
|
119
|
+
[Check out react example code](#code-examples)
|
120
|
+
|
121
|
+
---
|
122
|
+
|
123
|
+
### Create components
|
124
|
+
|
125
|
+
1. Create salty components with styled only inside files that end with `.css.ts`, `.salty.ts` `.styled.ts` or `.styles.ts`
|
126
|
+
|
127
|
+
## Code examples
|
128
|
+
|
129
|
+
### Basic usage example with Button
|
13
130
|
|
14
131
|
**Salty config**
|
15
132
|
|
@@ -31,31 +148,16 @@ export const config = defineConfig({
|
|
31
148
|
});
|
32
149
|
```
|
33
150
|
|
34
|
-
**Your React component file**
|
35
|
-
|
36
|
-
```tsx
|
37
|
-
import { Wrapper } from '../components/wrapper/wrapper.css';
|
38
|
-
import { Button } from '../components/button/button.css';
|
39
|
-
|
40
|
-
export const IndexPage = () => {
|
41
|
-
return (
|
42
|
-
<Wrapper>
|
43
|
-
<Button variant="solid" onClick={() => alert('It is a button.')}>
|
44
|
-
Outlined
|
45
|
-
</Button>
|
46
|
-
</Wrapper>
|
47
|
-
);
|
48
|
-
};
|
49
|
-
```
|
50
|
-
|
51
151
|
**Wrapper** (`components/wrapper/wrapper.css.ts`)
|
52
152
|
|
53
153
|
```tsx
|
54
154
|
import { styled } from '@salty-css/react/styled';
|
55
155
|
|
56
156
|
export const Wrapper = styled('div', {
|
57
|
-
|
58
|
-
|
157
|
+
base: {
|
158
|
+
display: 'block',
|
159
|
+
padding: '2vw',
|
160
|
+
},
|
59
161
|
});
|
60
162
|
```
|
61
163
|
|
@@ -65,22 +167,24 @@ export const Wrapper = styled('div', {
|
|
65
167
|
import { styled } from '@salty-css/react/styled';
|
66
168
|
|
67
169
|
export const Button = styled('button', {
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
170
|
+
base: {
|
171
|
+
display: 'block',
|
172
|
+
padding: `0.6em 1.2em`,
|
173
|
+
border: '1px solid currentColor',
|
174
|
+
background: 'transparent',
|
175
|
+
color: 'currentColor',
|
176
|
+
cursor: 'pointer',
|
177
|
+
transition: '200ms',
|
178
|
+
textDecoration: 'none',
|
179
|
+
'&:hover': {
|
180
|
+
background: 'black',
|
181
|
+
borderColor: 'black',
|
182
|
+
color: 'white',
|
183
|
+
},
|
184
|
+
'&:disabled': {
|
185
|
+
opacity: 0.25,
|
186
|
+
pointerEvents: 'none',
|
187
|
+
},
|
84
188
|
},
|
85
189
|
variants: {
|
86
190
|
variant: {
|
@@ -104,4 +208,21 @@ export const Button = styled('button', {
|
|
104
208
|
});
|
105
209
|
```
|
106
210
|
|
211
|
+
**Your React component file**
|
212
|
+
|
213
|
+
```tsx
|
214
|
+
import { Wrapper } from '../components/wrapper/wrapper.css';
|
215
|
+
import { Button } from '../components/button/button.css';
|
216
|
+
|
217
|
+
export const IndexPage = () => {
|
218
|
+
return (
|
219
|
+
<Wrapper>
|
220
|
+
<Button variant="solid" onClick={() => alert('It is a button.')}>
|
221
|
+
Outlined
|
222
|
+
</Button>
|
223
|
+
</Wrapper>
|
224
|
+
);
|
225
|
+
};
|
226
|
+
```
|
227
|
+
|
107
228
|
More examples coming soon
|
package/index.cjs
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const Nt=require("esbuild"),Pt=require("child_process"),l=require("path"),u=require("fs"),X=require("fs/promises"),G=require("winston");var nt=typeof document<"u"?document.currentScript:null;function kt(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const s in t)if(s!=="default"){const n=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,n.get?n:{enumerable:!0,get:()=>t[s]})}}return e.default=t,Object.freeze(e)}const pt=kt(Nt),ft=t=>String.fromCharCode(t+(t>25?39:97)),_t=(t,e)=>{let s="",n;for(n=Math.abs(t);n>52;n=n/52|0)s=ft(n%52)+s;return s=ft(n%52)+s,s.length<e?s=s.padStart(e,"a"):s.length>e&&(s=s.slice(-e)),s},Dt=(t,e)=>{let s=e.length;for(;s;)t=t*33^e.charCodeAt(--s);return t},I=(t,e=5)=>{const s=Dt(5381,JSON.stringify(t))>>>0;return _t(s,e)};function E(t){return t?typeof t!="string"?E(String(t)):t.replace(/[\s.]/g,"-").replace(/[A-Z](?:(?=[^A-Z])|[A-Z]*(?=[A-Z][^A-Z]|$))/g,(e,s)=>(s>0?"-":"")+e.toLowerCase()):""}const Ot=(t,e)=>{if(typeof t!="string")return{result:t};if(!e)return{result:t};const s=[];return Object.values(e).forEach(n=>{const{pattern:o,transform:a}=n;t=t.replace(o,i=>{const{value:p,css:r}=a(i);return r&&s.push(r),p})}),{result:t,additionalCss:s}},yt=t=>typeof t!="string"?{result:t}:/\{[^{}]+\}/g.test(t)?{result:t.replace(/\{([^{}]+)\}/g,(...n)=>`var(--${E(n[1].replaceAll(".","-"))})`)}:{result:t},Tt=["top","right","bottom","left","min-width",/.*width.*/,/^[^line]*height.*/,/padding.*/,/margin.*/,/border.*/,/inset.*/,/.*radius.*/,/.*spacing.*/,/.*gap.*/,/.*indent.*/,/.*offset.*/,/.*size.*/,/.*thickness.*/,/.*font-size.*/],Et=(t,e,s)=>Tt.some(o=>typeof o=="string"?o===t:o.test(t))?`${e}px`:`${e}`,Mt=["Webkit","Moz","ms","O"],Rt=t=>t.startsWith("-")?t:Mt.some(e=>t.startsWith(e))?`-${E(t)}`:E(t),z=(t,e,s)=>{if(!t)return"";const n=[],o=Object.entries(t).reduce((i,[p,r])=>{const c=p.trim(),g=Rt(c),h=(j,_=";")=>i=`${i}${j}${_}`,y=j=>h(`${g}:${j}`);if(typeof r=="function"&&(r=r()),typeof r=="object"){if(!r)return i;if(r.isColor)return y(r.toString()),i;if(c==="variants")return Object.entries(r).forEach(([D,x])=>{x&&Object.entries(x).forEach(([T,P])=>{if(!P)return;const M=`${e}.${D}-${T}`,f=z(P,M);n.push(f)})}),i;if(c==="defaultVariants")return i;if(c==="compoundVariants")return r.forEach(D=>{const{css:x,...T}=D,P=Object.entries(T).reduce((f,[m,b])=>`${f}.${m}-${b}`,e),M=z(x,P);n.push(M)}),i;if(c.startsWith("@")){const D=c,x=z(r,e),T=`${D} {
|
2
|
+
${x.replace(`
|
3
|
+
`,`
|
4
|
+
`)}
|
5
|
+
}`;return n.push(T),i}const j=p.includes("&")?c.replace("&",e):c.startsWith(":")?`${e}${c}`:`${e} ${c}`,_=z(r,j);return n.push(_),i}if(typeof r=="number"){const j=Et(g,r);return y(j)}if(typeof r!="string")if("toString"in r)r=r.toString();else return i;const{modifiers:$}={},O=function*(){yield yt(r),yield Ot(r,$)}();for(const{result:j,additionalCss:_=[]}of O)r=j,_.forEach(D=>{const x=z(D,"");h(x,"")});return y(r)},"");return o?e?[`${e} { ${o} }`,...n].join(`
|
6
|
+
`):o:n.join(`
|
7
|
+
`)},gt=(t,e=[])=>{if(!t)return"";const s=[],n={};if(Object.entries(t).forEach(([o,a])=>{if(typeof a=="object"){if(!a)return;const i=o.trim(),p=gt(a,[...e,i]);s.push(p)}else n[o]=a}),Object.keys(n).length){const o=e.map(E).join("-"),a=z(n,`.${o}`);s.push(a)}return s.join(`
|
8
|
+
`)},Vt=t=>t?Object.entries(t).reduce((e,[s,n])=>(typeof n=="object"&&(e[s]=ht(n).map(o=>`"${o}"`).join(" | ")),e),{}):{},ht=(t,e="",s=new Set)=>t?(Object.entries(t).forEach(([n,o])=>{const a=e?`${e}.${n}`:n;return typeof o=="object"?ht(o,a,s):s.add(e)}),[...s]):[],mt=t=>{if(!t||t==="/")throw new Error("Could not find package.json file");const e=l.join(t,"package.json");return u.existsSync(e)?e:mt(l.join(t,".."))},zt=async t=>{const e=mt(t);return await X.readFile(e,"utf-8").then(JSON.parse).catch(()=>{})},Jt=async t=>{const e=await zt(t);if(e)return e.type};let A;const St=async t=>{if(A)return A;const e=await Jt(t);return e==="module"?A="esm":(e==="commonjs"||(typeof document>"u"?require("url").pathToFileURL(__filename).href:nt&&nt.tagName.toUpperCase()==="SCRIPT"&&nt.src||new URL("index.cjs",document.baseURI).href).endsWith(".cjs"))&&(A="cjs"),A||"esm"},ot=G.createLogger({level:"debug",format:G.format.combine(G.format.colorize(),G.format.cli()),transports:[new G.transports.Console({})]});function bt(t){return t?typeof t!="string"?bt(String(t)):t.replace(/[\s-]/g,".").replace(/[A-Z](?:(?=[^A-Z])|[A-Z]*(?=[A-Z][^A-Z]|$))/g,(e,s)=>(s>0?".":"")+e.toLowerCase()):""}const Wt={"*, *::before, *::after":{boxSizing:"border-box"},"*":{margin:0},html:{lineHeight:1.15,textSizeAdjust:"100%",WebkitFontSmoothing:"antialiased"},"img, picture, video, canvas, svg":{display:"block",maxWidth:"100%"},"p, h1, h2, h3, h4, h5, h6":{overflowWrap:"break-word"},p:{textWrap:"pretty"},"h1, h2, h3, h4, h5, h6":{textWrap:"balance"},a:{color:"currentColor"},button:{lineHeight:"1em",color:"currentColor"},"input, optgroup, select, textarea":{fontFamily:"inherit",fontSize:"100%",lineHeight:"1.15em"}},Z=(...t)=>t.flat().reduce((e,s)=>s!=null&&s._current?{...e,...s._current}:{...e,...s},{}),V={externalModules:[],rcFile:void 0,destDir:void 0},$t=t=>{if(V.externalModules.length>0)return V.externalModules;const s=u.readFileSync(t,"utf8").match(/externalModules:\s?\[(.*)\]/);if(!s)return[];const n=s[1].split(",").map(o=>o.replace(/['"`]/g,"").trim());return V.externalModules=n,n},J=async t=>{if(V.destDir)return V.destDir;const e=await rt(t),s=l.join(t,(e==null?void 0:e.saltygenDir)||"saltygen");return V.destDir=s,s},jt=["salty","css","styles","styled"],At=(t=[])=>new RegExp(`\\.(${[...jt,...t].join("|")})\\.`),q=(t,e=[])=>At(e).test(t),wt=async t=>{if(V.rcFile)return V.rcFile;if(t==="/")throw new Error("Could not find .saltyrc.json file");const e=l.join(t,".saltyrc.json"),s=await X.readFile(e,"utf-8").then(JSON.parse).catch(()=>{});return s?(V.rcFile=s,s):wt(l.join(t,".."))},rt=async t=>{var n,o;const e=await wt(t),s=(n=e.projects)==null?void 0:n.find(a=>t.endsWith(a.dir||""));return s||((o=e.projects)==null?void 0:o.find(a=>a.dir===e.defaultProject))},Zt=async t=>{const e=await rt(t),s=await J(t),n=l.join(t,(e==null?void 0:e.configDir)||"","salty.config.ts"),o=l.join(s,"salty.config.js"),a=await St(t),i=$t(n);await pt.build({entryPoints:[n],minify:!0,treeShaking:!0,bundle:!0,outfile:o,format:a,external:i});const p=Date.now(),{config:r}=await import(`${o}?t=${p}`);return r},It=async(t,e)=>{var at,lt;const s=await J(t),n={mediaQueries:[],globalStyles:[],variables:[],templates:[]};await Promise.all([...e].map(async w=>{const F=await Y(t,w,s);Object.entries(F).forEach(([R,C])=>{C.isMedia?n.mediaQueries.push([R,C]):C.isGlobalDefine?n.globalStyles.push(C):C.isDefineVariables?n.variables.push(C):C.isDefineTemplates&&n.templates.push(C)})}));const o=await Zt(t),a={...o},i=new Set,p=(w,F=[])=>w?Object.entries(w).flatMap(([R,C])=>{if(!C)return;if(typeof C=="object")return p(C,[...F,R]);const v=bt(R),tt=E(R),et=[...F,v].join(".");i.add(`"${et}"`);const st=[...F.map(E),tt].join("-"),{result:xt}=yt(C);return`--${st}: ${xt};`}):[],r=w=>w?Object.entries(w).flatMap(([F,R])=>{const C=p(R);return F==="base"?C.join(""):`${F} { ${C.join("")} }`}):[],c=w=>w?Object.entries(w).flatMap(([F,R])=>Object.entries(R).flatMap(([C,v])=>{const tt=p(v,[F]),et=`.${F}-${C}, [data-${F}="${C}"]`,st=tt.join("");return`${et} { ${st} }`})):[],g=w=>({...w,responsive:void 0,conditional:void 0}),h=w=>n.variables.map(F=>w==="static"?g(F._current):F._current[w]),y=Z(g(o.variables),h("static")),$=p(y),S=Z((at=o.variables)==null?void 0:at.responsive,h("responsive")),O=r(S),j=Z((lt=o.variables)==null?void 0:lt.conditional,h("conditional")),_=c(j),D=l.join(s,"css/_variables.css"),x=`:root { ${$.join("")} ${O.join("")} } ${_.join("")}`;u.writeFileSync(D,x),a.staticVariables=y;const T=l.join(s,"css/_global.css"),P=Z(o.global,n.globalStyles),M=z(P,"");u.writeFileSync(T,`@layer global { ${M} }`);const f=l.join(s,"css/_reset.css"),b=o.reset==="none"?{}:typeof o.reset=="object"?o.reset:Wt,d=z(b,"");u.writeFileSync(f,`@layer reset { ${d} }`);const N=l.join(s,"css/_templates.css"),k=Z(o.templates,n.templates),H=gt(k),L=Vt(k);u.writeFileSync(N,H),a.templates=k;const{mediaQueries:W}=n;a.mediaQueries=Object.fromEntries(W.map(([w,F])=>[`@${w}`,F]));const K=W.map(([w])=>`'@${w}'`).join(" | "),U=l.join(s,"types/css-tokens.d.ts"),B=`
|
9
|
+
// Variable types
|
10
|
+
type VariableTokens = ${[...i].join("|")};
|
11
|
+
type PropertyValueToken = \`{\${VariableTokens}}\`;
|
12
|
+
|
13
|
+
// Template types
|
14
|
+
type TemplateTokens = {
|
15
|
+
${Object.entries(L).map(([w,F])=>`${w}?: ${F}`).join(`
|
16
|
+
`)}
|
17
|
+
}
|
18
|
+
|
19
|
+
// Media query types
|
20
|
+
type MediaQueryKeys = ${K};
|
21
|
+
`;u.writeFileSync(U,B);const Q=l.join(s,"cache/config-cache.json");u.writeFileSync(Q,JSON.stringify(a,null,2))},ut=t=>t.replace(/styled\(([^"'`{,]+),/g,(e,s)=>{if(/^['"`]/.test(s))return e;const o=new RegExp(`import[^;]*${s}[,\\s{][^;]*from\\s?([^{};]+);`);if(!o.test(t))return e;const i=o.exec(t);if(i){const p=i.at(1);if(jt.some(c=>p==null?void 0:p.includes(c)))return e}return"styled('div',"}),qt=(t,e)=>{try{const s=u.readFileSync(l.join(e,"saltygen/cache/config-cache.json"),"utf8");return s?`globalThis.saltyConfig = ${s};
|
22
|
+
|
23
|
+
${t}`:`globalThis.saltyConfig = {};
|
24
|
+
|
25
|
+
${t}`}catch{return t}},Y=async(t,e,s)=>{const n=I(e),o=l.join(s,"./temp");u.existsSync(o)||u.mkdirSync(o);const a=l.parse(e);let i=u.readFileSync(e,"utf8");i=ut(i),i=qt(i,t);const p=l.join(s,"js",n+".js"),r=await rt(t),c=l.join(t,(r==null?void 0:r.configDir)||"","salty.config.ts"),g=$t(c),h=await St(t);await pt.build({stdin:{contents:i,sourcefile:a.base,resolveDir:a.dir,loader:"tsx"},minify:!1,treeShaking:!0,bundle:!0,outfile:p,format:h,target:["node20"],keepNames:!0,external:g,packages:"external",plugins:[{name:"test",setup:S=>{S.onLoad({filter:/.*\.css|salty|styles|styled\.ts/},O=>{const j=u.readFileSync(O.path,"utf8");return{contents:ut(j),loader:"ts"}})}}]});const y=Date.now();return await import(`${p}?t=${y}`)},Ht=async t=>{const e=await J(t),s=l.join(e,"cache/config-cache.json"),n=u.readFileSync(s,"utf8");if(!n)throw new Error("Could not find config cache file");return JSON.parse(n)},it=async t=>{const e=await Ht(t),s=await J(t),n=l.join(s,"salty.config.js"),o=Date.now(),{config:a}=await import(`${n}?t=${o}`);return Z(a,e)},Ft=()=>{try{return process.env.NODE_ENV==="production"}catch{return!1}},Lt=async(t,e=Ft(),s=!0)=>{try{const n=Date.now();e?ot.info("Generating CSS in production mode! 🔥"):ot.info("Generating CSS in development mode! 🚀");const o=[],a=[],i=await J(t),p=l.join(i,"index.css");s&&(()=>{u.existsSync(i)&&Pt.execSync("rm -rf "+i),u.mkdirSync(i,{recursive:!0}),u.mkdirSync(l.join(i,"css")),u.mkdirSync(l.join(i,"types")),u.mkdirSync(l.join(i,"js")),u.mkdirSync(l.join(i,"cache"))})();const c=new Set,g=new Set;async function h(f){const m=["node_modules","saltygen"],b=u.statSync(f);if(b.isDirectory()){const d=u.readdirSync(f);if(m.some(k=>f.includes(k)))return;await Promise.all(d.map(k=>h(l.join(f,k))))}else if(b.isFile()&&q(f)){c.add(f);const N=u.readFileSync(f,"utf8");/define[\w\d]+\(/.test(N)&&g.add(f)}}await h(t),await It(t,g);const y={keyframes:[],components:[],classNames:[]};await Promise.all([...c].map(async f=>{const m=await Y(t,f,i);Object.entries(m).forEach(([b,d])=>{d.isKeyframes?y.keyframes.push({value:d,src:f,name:b}):d.isClassName?y.classNames.push({...d,src:f,name:b}):d.generator&&y.components.push({...d,src:f,name:b})})}));const $=await it(t);for(const f of y.keyframes){const{value:m}=f,b=`a_${m.animationName}.css`,d=`css/${b}`,N=l.join(i,d);o.push(b),u.writeFileSync(N,m.css)}const S={};for(const f of y.components){const{src:m,name:b}=f,d=f.generator._withBuildContext({callerName:b,isProduction:e,config:$});a[d.priority]||(a[d.priority]=[]),a[d.priority].push(d.cssFileName);const N=`css/${d.cssFileName}`,k=l.join(i,N);u.writeFileSync(k,d.css),$.importStrategy==="component"&&(S[m]?S[m].push(d.cssFileName):S[m]=[d.cssFileName])}for(const f of y.classNames){const{src:m,name:b}=f,d=f.generator._withBuildContext({callerName:b,isProduction:e,config:$});a[0].push(d.cssFileName);const N=`css/${d.cssFileName}`,k=l.join(i,N);u.writeFileSync(k,d.css),$.importStrategy==="component"&&(S[m]?S[m].push(d.cssFileName):S[m]=[d.cssFileName])}$.importStrategy==="component"&&Object.entries(S).forEach(([f,m])=>{const b=m.map(L=>`@import url('./${L}');`).join(`
|
26
|
+
`),d=I(f,6),N=l.parse(f),k=E(N.name),H=l.join(i,`css/f_${k}-${d}.css`);u.writeFileSync(H,b)});const O=o.map(f=>`@import url('./css/${f}');`).join(`
|
27
|
+
`);let x=`@layer reset, global, l0, l1, l2, l3, l4, l5, l6, l7, l8;
|
28
|
+
|
29
|
+
${["_variables.css","_reset.css","_global.css","_templates.css"].filter(f=>{try{return u.readFileSync(l.join(i,"css",f),"utf8").length>0}catch{return!1}}).map(f=>`@import url('./css/${f}');`).join(`
|
30
|
+
`)}
|
31
|
+
${O}`;if($.importStrategy!=="component"){const f=a.reduce((m,b,d)=>{const N=b.reduce((W,K)=>{var Q;const U=l.join(i,"css",K),ct=u.readFileSync(U,"utf8"),B=((Q=/.*-([^-]+)-\d+.css/.exec(K))==null?void 0:Q.at(1))||I(U,6);return W.includes(B)?W:`${W}
|
32
|
+
/*start:${B}*/
|
33
|
+
${ct}
|
34
|
+
/*end:${B}*/
|
35
|
+
`},""),k=`l_${d}.css`,H=l.join(i,"css",k),L=`@layer l${d} { ${N}
|
36
|
+
}`;return u.writeFileSync(H,L),`${m}
|
37
|
+
@import url('./css/${k}');`},"");x+=f}u.writeFileSync(p,x);const P=Date.now()-n,M=P<200?"🔥":P<500?"🚀":P<1e3?"🎉":P<2e3?"🚗":P<5e3?"🤔":"🥴";ot.info(`Generated CSS in ${P}ms! ${M}`)}catch(n){console.error(n)}},Bt=async(t,e)=>{try{const s=await J(t);if(q(e)){const o=[],a=await it(t),i=await Y(t,e,s);if(Object.entries(i).forEach(([p,r])=>{if(r.isKeyframes&&r.css){const $=`css/${`a_${r.animationName}.css`}`,S=l.join(s,$);u.writeFileSync(S,r.css);return}if(r.isClassName){const y=r.factory._withBuildContext({name:p});o[0].push(y.cssFileName);const $=`css/${y.cssFileName}`,S=l.join(s,$);u.writeFileSync(S,y.css)}if(!r.generator)return;const c=r.generator._withBuildContext({name:p,config:a}),g=`css/${c.cssFileName}`,h=l.join(s,g);u.writeFileSync(h,c.css),o[c.priority]||(o[c.priority]=[]),o[c.priority].push(c.cssFileName)}),a.importStrategy!=="component")o.forEach((p,r)=>{const c=`l_${r}.css`,g=l.join(s,"css",c);let h=u.readFileSync(g,"utf8");p.forEach(y=>{var j;const $=l.join(s,"css",y),S=((j=/.*-([^-]+)-\d+.css/.exec(y))==null?void 0:j.at(1))||I($,6);if(!h.includes(S)){const _=u.readFileSync($,"utf8"),D=`/*start:${S}*/
|
38
|
+
${_}
|
39
|
+
/*end:${S}*/
|
40
|
+
`;h=`${h.replace(/\}$/,"")}
|
41
|
+
${D}
|
42
|
+
}`}}),u.writeFileSync(g,h)});else{const p=o.flat().map(y=>`@import url('./${y}');`).join(`
|
43
|
+
`),r=I(e,6),c=l.parse(e),g=E(c.name),h=l.join(s,`css/f_${g}-${r}.css`);u.writeFileSync(h,p)}}}catch(s){console.error(s)}},Gt=async(t,e,s=Ft())=>{try{const n=await J(t);if(q(e)){const a=u.readFileSync(e,"utf8");a.replace(/^(?!export\s)const\s.*/gm,g=>`export ${g}`)!==a&&await X.writeFile(e,a);const p=await it(t),r=await Y(t,e,n);let c=a;if(Object.entries(r).forEach(([g,h])=>{var d;if(h.isKeyframes||!h.generator)return;const y=h.generator._withBuildContext({name:g,config:p,prod:s}),$=new RegExp(`\\s${g}[=\\s]+[^()]+styled\\(([^,]+),`,"g").exec(a);if(!$)return console.error("Could not find the original declaration");const S=(d=$.at(1))==null?void 0:d.trim(),O=new RegExp(`\\s${g}[=\\s]+styled\\(`,"g").exec(c);if(!O)return console.error("Could not find the original declaration");const{index:j}=O;let _=!1;const D=setTimeout(()=>_=!0,5e3);let x=0,T=!1,P=0;for(;!T&&!_;){const N=c[j+x];N==="("&&P++,N===")"&&P--,P===0&&N===")"&&(T=!0),x>c.length&&(_=!0),x++}if(!_)clearTimeout(D);else throw new Error("Failed to find the end of the styled call and timed out");const M=j+x,f=c.slice(j,M),m=c,b=` ${g} = styled(${S}, "${y.classNames}", ${JSON.stringify(y.clientProps)});`;c=c.replace(f,b),m===c&&console.error("Minimize file failed to change content",{name:g,tagName:S})}),p.importStrategy==="component"){const g=I(e,6),h=l.parse(e);c=`import '../../saltygen/css/${`f_${E(h.name)}-${g}.css`}';
|
44
|
+
${c}`}return c=c.replace("{ styled }","{ styledClient as styled }"),c=c.replace("@salty-css/react/styled","@salty-css/react/styled-client"),c}}catch(n){console.error("Error in minimizeFile:",n)}},dt=async t=>{if(!t||t.includes("node_modules")||t.includes("saltygen"))return!1;if(t.includes("salty.config"))return!0;if(!q(t))return!1;const n=await X.readFile(t,"utf-8");return!!/.+define[A-Z]\w+/.test(n)},Ct=t=>({name:"stylegen",buildStart:()=>Lt(t),load:async e=>{if(q(e))return await Gt(t,e)},handleHotUpdate:async({file:e,server:s})=>{await dt(e)&&await s.restart()},watchChange:{handler:async e=>{q(e)&&(await dt(e)||await Bt(t,e))}}});exports.default=Ct;exports.saltyPlugin=Ct;
|
package/index.d.ts
ADDED
package/index.js
ADDED
@@ -0,0 +1,595 @@
|
|
1
|
+
import * as mt from "esbuild";
|
2
|
+
import { execSync as kt } from "child_process";
|
3
|
+
import { join as f, parse as Y } from "path";
|
4
|
+
import { existsSync as it, mkdirSync as H, statSync as Dt, readdirSync as _t, readFileSync as O, writeFileSync as k } from "fs";
|
5
|
+
import { readFile as ct, writeFile as Et } from "fs/promises";
|
6
|
+
import { createLogger as Tt, format as ot, transports as Ot } from "winston";
|
7
|
+
const pt = (t) => String.fromCharCode(t + (t > 25 ? 39 : 97)), Vt = (t, e) => {
|
8
|
+
let s = "", n;
|
9
|
+
for (n = Math.abs(t); n > 52; n = n / 52 | 0) s = pt(n % 52) + s;
|
10
|
+
return s = pt(n % 52) + s, s.length < e ? s = s.padStart(e, "a") : s.length > e && (s = s.slice(-e)), s;
|
11
|
+
}, Mt = (t, e) => {
|
12
|
+
let s = e.length;
|
13
|
+
for (; s; ) t = t * 33 ^ e.charCodeAt(--s);
|
14
|
+
return t;
|
15
|
+
}, B = (t, e = 5) => {
|
16
|
+
const s = Mt(5381, JSON.stringify(t)) >>> 0;
|
17
|
+
return Vt(s, e);
|
18
|
+
};
|
19
|
+
function V(t) {
|
20
|
+
return t ? typeof t != "string" ? V(String(t)) : t.replace(/[\s.]/g, "-").replace(/[A-Z](?:(?=[^A-Z])|[A-Z]*(?=[A-Z][^A-Z]|$))/g, (e, s) => (s > 0 ? "-" : "") + e.toLowerCase()) : "";
|
21
|
+
}
|
22
|
+
const Rt = (t, e) => {
|
23
|
+
if (typeof t != "string") return { result: t };
|
24
|
+
if (!e) return { result: t };
|
25
|
+
const s = [];
|
26
|
+
return Object.values(e).forEach((n) => {
|
27
|
+
const { pattern: o, transform: a } = n;
|
28
|
+
t = t.replace(o, (i) => {
|
29
|
+
const { value: d, css: r } = a(i);
|
30
|
+
return r && s.push(r), d;
|
31
|
+
});
|
32
|
+
}), { result: t, additionalCss: s };
|
33
|
+
}, yt = (t) => typeof t != "string" ? { result: t } : /\{[^{}]+\}/g.test(t) ? { result: t.replace(/\{([^{}]+)\}/g, (...n) => `var(--${V(n[1].replaceAll(".", "-"))})`) } : { result: t }, Jt = [
|
34
|
+
"top",
|
35
|
+
"right",
|
36
|
+
"bottom",
|
37
|
+
"left",
|
38
|
+
"min-width",
|
39
|
+
/.*width.*/,
|
40
|
+
/^[^line]*height.*/,
|
41
|
+
// Exclude line-height
|
42
|
+
/padding.*/,
|
43
|
+
/margin.*/,
|
44
|
+
/border.*/,
|
45
|
+
/inset.*/,
|
46
|
+
/.*radius.*/,
|
47
|
+
/.*spacing.*/,
|
48
|
+
/.*gap.*/,
|
49
|
+
/.*indent.*/,
|
50
|
+
/.*offset.*/,
|
51
|
+
/.*size.*/,
|
52
|
+
/.*thickness.*/,
|
53
|
+
/.*font-size.*/
|
54
|
+
], Wt = (t, e, s) => Jt.some((o) => typeof o == "string" ? o === t : o.test(t)) ? `${e}px` : `${e}`, zt = ["Webkit", "Moz", "ms", "O"], At = (t) => t.startsWith("-") ? t : zt.some((e) => t.startsWith(e)) ? `-${V(t)}` : V(t), W = (t, e, s) => {
|
55
|
+
if (!t) return "";
|
56
|
+
const n = [], o = Object.entries(t).reduce((i, [d, r]) => {
|
57
|
+
const c = d.trim(), g = At(c), h = (w, D = ";") => i = `${i}${w}${D}`, p = (w) => h(`${g}:${w}`);
|
58
|
+
if (typeof r == "function" && (r = r()), typeof r == "object") {
|
59
|
+
if (!r) return i;
|
60
|
+
if (r.isColor)
|
61
|
+
return p(r.toString()), i;
|
62
|
+
if (c === "variants")
|
63
|
+
return Object.entries(r).forEach(([_, F]) => {
|
64
|
+
F && Object.entries(F).forEach(([T, N]) => {
|
65
|
+
if (!N) return;
|
66
|
+
const M = `${e}.${_}-${T}`, l = W(N, M);
|
67
|
+
n.push(l);
|
68
|
+
});
|
69
|
+
}), i;
|
70
|
+
if (c === "defaultVariants")
|
71
|
+
return i;
|
72
|
+
if (c === "compoundVariants")
|
73
|
+
return r.forEach((_) => {
|
74
|
+
const { css: F, ...T } = _, N = Object.entries(T).reduce((l, [m, $]) => `${l}.${m}-${$}`, e), M = W(F, N);
|
75
|
+
n.push(M);
|
76
|
+
}), i;
|
77
|
+
if (c.startsWith("@")) {
|
78
|
+
const _ = c, F = W(r, e), T = `${_} {
|
79
|
+
${F.replace(`
|
80
|
+
`, `
|
81
|
+
`)}
|
82
|
+
}`;
|
83
|
+
return n.push(T), i;
|
84
|
+
}
|
85
|
+
const w = d.includes("&") ? c.replace("&", e) : c.startsWith(":") ? `${e}${c}` : `${e} ${c}`, D = W(r, w);
|
86
|
+
return n.push(D), i;
|
87
|
+
}
|
88
|
+
if (typeof r == "number") {
|
89
|
+
const w = Wt(g, r);
|
90
|
+
return p(w);
|
91
|
+
}
|
92
|
+
if (typeof r != "string")
|
93
|
+
if ("toString" in r) r = r.toString();
|
94
|
+
else return i;
|
95
|
+
const { modifiers: b } = {}, E = function* () {
|
96
|
+
yield yt(r), yield Rt(r, b);
|
97
|
+
}();
|
98
|
+
for (const { result: w, additionalCss: D = [] } of E)
|
99
|
+
r = w, D.forEach((_) => {
|
100
|
+
const F = W(_, "");
|
101
|
+
h(F, "");
|
102
|
+
});
|
103
|
+
return p(r);
|
104
|
+
}, "");
|
105
|
+
return o ? e ? [`${e} { ${o} }`, ...n].join(`
|
106
|
+
`) : o : n.join(`
|
107
|
+
`);
|
108
|
+
}, $t = (t, e = []) => {
|
109
|
+
if (!t) return "";
|
110
|
+
const s = [], n = {};
|
111
|
+
if (Object.entries(t).forEach(([o, a]) => {
|
112
|
+
if (typeof a == "object") {
|
113
|
+
if (!a) return;
|
114
|
+
const i = o.trim(), d = $t(a, [...e, i]);
|
115
|
+
s.push(d);
|
116
|
+
} else
|
117
|
+
n[o] = a;
|
118
|
+
}), Object.keys(n).length) {
|
119
|
+
const o = e.map(V).join("-"), a = W(n, `.${o}`);
|
120
|
+
s.push(a);
|
121
|
+
}
|
122
|
+
return s.join(`
|
123
|
+
`);
|
124
|
+
}, Zt = (t) => t ? Object.entries(t).reduce((e, [s, n]) => (typeof n == "object" && (e[s] = bt(n).map((o) => `"${o}"`).join(" | ")), e), {}) : {}, bt = (t, e = "", s = /* @__PURE__ */ new Set()) => t ? (Object.entries(t).forEach(([n, o]) => {
|
125
|
+
const a = e ? `${e}.${n}` : n;
|
126
|
+
return typeof o == "object" ? bt(o, a, s) : s.add(e);
|
127
|
+
}), [...s]) : [], wt = (t) => {
|
128
|
+
if (!t || t === "/") throw new Error("Could not find package.json file");
|
129
|
+
const e = f(t, "package.json");
|
130
|
+
return it(e) ? e : wt(f(t, ".."));
|
131
|
+
}, Ht = async (t) => {
|
132
|
+
const e = wt(t);
|
133
|
+
return await ct(e, "utf-8").then(JSON.parse).catch(() => {
|
134
|
+
});
|
135
|
+
}, It = async (t) => {
|
136
|
+
const e = await Ht(t);
|
137
|
+
if (e)
|
138
|
+
return e.type;
|
139
|
+
};
|
140
|
+
let Z;
|
141
|
+
const St = async (t) => {
|
142
|
+
if (Z) return Z;
|
143
|
+
const e = await It(t);
|
144
|
+
return e === "module" ? Z = "esm" : (e === "commonjs" || import.meta.url.endsWith(".cjs")) && (Z = "cjs"), Z || "esm";
|
145
|
+
}, rt = Tt({
|
146
|
+
level: "debug",
|
147
|
+
format: ot.combine(ot.colorize(), ot.cli()),
|
148
|
+
transports: [new Ot.Console({})]
|
149
|
+
});
|
150
|
+
function Ct(t) {
|
151
|
+
return t ? typeof t != "string" ? Ct(String(t)) : t.replace(/[\s-]/g, ".").replace(/[A-Z](?:(?=[^A-Z])|[A-Z]*(?=[A-Z][^A-Z]|$))/g, (e, s) => (s > 0 ? "." : "") + e.toLowerCase()) : "";
|
152
|
+
}
|
153
|
+
const Bt = {
|
154
|
+
/** Set box model to border-box */
|
155
|
+
"*, *::before, *::after": {
|
156
|
+
boxSizing: "border-box"
|
157
|
+
},
|
158
|
+
/** Remove default margin and padding */
|
159
|
+
"*": {
|
160
|
+
margin: 0
|
161
|
+
},
|
162
|
+
/** Remove adjust font properties */
|
163
|
+
html: {
|
164
|
+
lineHeight: 1.15,
|
165
|
+
textSizeAdjust: "100%",
|
166
|
+
WebkitFontSmoothing: "antialiased"
|
167
|
+
},
|
168
|
+
/** Make media elements responsive */
|
169
|
+
"img, picture, video, canvas, svg": {
|
170
|
+
display: "block",
|
171
|
+
maxWidth: "100%"
|
172
|
+
},
|
173
|
+
/** Avoid overflow of text */
|
174
|
+
"p, h1, h2, h3, h4, h5, h6": {
|
175
|
+
overflowWrap: "break-word"
|
176
|
+
},
|
177
|
+
/** Improve text wrapping */
|
178
|
+
p: {
|
179
|
+
textWrap: "pretty"
|
180
|
+
},
|
181
|
+
"h1, h2, h3, h4, h5, h6": {
|
182
|
+
textWrap: "balance"
|
183
|
+
},
|
184
|
+
/** Improve link color */
|
185
|
+
a: {
|
186
|
+
color: "currentColor"
|
187
|
+
},
|
188
|
+
/** Improve button line height */
|
189
|
+
button: {
|
190
|
+
lineHeight: "1em",
|
191
|
+
color: "currentColor"
|
192
|
+
},
|
193
|
+
/** Improve form elements */
|
194
|
+
"input, optgroup, select, textarea": {
|
195
|
+
fontFamily: "inherit",
|
196
|
+
fontSize: "100%",
|
197
|
+
lineHeight: "1.15em"
|
198
|
+
}
|
199
|
+
}, I = (...t) => t.flat().reduce((e, s) => s != null && s._current ? { ...e, ...s._current } : { ...e, ...s }, {}), J = {
|
200
|
+
externalModules: [],
|
201
|
+
rcFile: void 0,
|
202
|
+
destDir: void 0
|
203
|
+
}, jt = (t) => {
|
204
|
+
if (J.externalModules.length > 0) return J.externalModules;
|
205
|
+
const s = O(t, "utf8").match(/externalModules:\s?\[(.*)\]/);
|
206
|
+
if (!s) return [];
|
207
|
+
const n = s[1].split(",").map((o) => o.replace(/['"`]/g, "").trim());
|
208
|
+
return J.externalModules = n, n;
|
209
|
+
}, z = async (t) => {
|
210
|
+
if (J.destDir) return J.destDir;
|
211
|
+
const e = await at(t), s = f(t, (e == null ? void 0 : e.saltygenDir) || "saltygen");
|
212
|
+
return J.destDir = s, s;
|
213
|
+
}, Ft = ["salty", "css", "styles", "styled"], Gt = (t = []) => new RegExp(`\\.(${[...Ft, ...t].join("|")})\\.`), G = (t, e = []) => Gt(e).test(t), xt = async (t) => {
|
214
|
+
if (J.rcFile) return J.rcFile;
|
215
|
+
if (t === "/") throw new Error("Could not find .saltyrc.json file");
|
216
|
+
const e = f(t, ".saltyrc.json"), s = await ct(e, "utf-8").then(JSON.parse).catch(() => {
|
217
|
+
});
|
218
|
+
return s ? (J.rcFile = s, s) : xt(f(t, ".."));
|
219
|
+
}, at = async (t) => {
|
220
|
+
var n, o;
|
221
|
+
const e = await xt(t), s = (n = e.projects) == null ? void 0 : n.find((a) => t.endsWith(a.dir || ""));
|
222
|
+
return s || ((o = e.projects) == null ? void 0 : o.find((a) => a.dir === e.defaultProject));
|
223
|
+
}, Kt = async (t) => {
|
224
|
+
const e = await at(t), s = await z(t), n = f(t, (e == null ? void 0 : e.configDir) || "", "salty.config.ts"), o = f(s, "salty.config.js"), a = await St(t), i = jt(n);
|
225
|
+
await mt.build({
|
226
|
+
entryPoints: [n],
|
227
|
+
minify: !0,
|
228
|
+
treeShaking: !0,
|
229
|
+
bundle: !0,
|
230
|
+
outfile: o,
|
231
|
+
format: a,
|
232
|
+
external: i
|
233
|
+
});
|
234
|
+
const d = Date.now(), { config: r } = await import(`${o}?t=${d}`);
|
235
|
+
return r;
|
236
|
+
}, Lt = async (t, e) => {
|
237
|
+
var ut, dt;
|
238
|
+
const s = await z(t), n = {
|
239
|
+
mediaQueries: [],
|
240
|
+
globalStyles: [],
|
241
|
+
variables: [],
|
242
|
+
templates: []
|
243
|
+
};
|
244
|
+
await Promise.all(
|
245
|
+
[...e].map(async (S) => {
|
246
|
+
const C = await v(t, S, s);
|
247
|
+
Object.entries(C).forEach(([R, j]) => {
|
248
|
+
j.isMedia ? n.mediaQueries.push([R, j]) : j.isGlobalDefine ? n.globalStyles.push(j) : j.isDefineVariables ? n.variables.push(j) : j.isDefineTemplates && n.templates.push(j);
|
249
|
+
});
|
250
|
+
})
|
251
|
+
);
|
252
|
+
const o = await Kt(t), a = { ...o }, i = /* @__PURE__ */ new Set(), d = (S, C = []) => S ? Object.entries(S).flatMap(([R, j]) => {
|
253
|
+
if (!j) return;
|
254
|
+
if (typeof j == "object") return d(j, [...C, R]);
|
255
|
+
const tt = Ct(R), et = V(R), st = [...C, tt].join(".");
|
256
|
+
i.add(`"${st}"`);
|
257
|
+
const nt = [...C.map(V), et].join("-"), { result: Pt } = yt(j);
|
258
|
+
return `--${nt}: ${Pt};`;
|
259
|
+
}) : [], r = (S) => S ? Object.entries(S).flatMap(([C, R]) => {
|
260
|
+
const j = d(R);
|
261
|
+
return C === "base" ? j.join("") : `${C} { ${j.join("")} }`;
|
262
|
+
}) : [], c = (S) => S ? Object.entries(S).flatMap(([C, R]) => Object.entries(R).flatMap(([j, tt]) => {
|
263
|
+
const et = d(tt, [C]), st = `.${C}-${j}, [data-${C}="${j}"]`, nt = et.join("");
|
264
|
+
return `${st} { ${nt} }`;
|
265
|
+
})) : [], g = (S) => ({ ...S, responsive: void 0, conditional: void 0 }), h = (S) => n.variables.map((C) => S === "static" ? g(C._current) : C._current[S]), p = I(g(o.variables), h("static")), b = d(p), y = I((ut = o.variables) == null ? void 0 : ut.responsive, h("responsive")), E = r(y), w = I((dt = o.variables) == null ? void 0 : dt.conditional, h("conditional")), D = c(w), _ = f(s, "css/_variables.css"), F = `:root { ${b.join("")} ${E.join("")} } ${D.join("")}`;
|
266
|
+
k(_, F), a.staticVariables = p;
|
267
|
+
const T = f(s, "css/_global.css"), N = I(o.global, n.globalStyles), M = W(N, "");
|
268
|
+
k(T, `@layer global { ${M} }`);
|
269
|
+
const l = f(s, "css/_reset.css"), $ = o.reset === "none" ? {} : typeof o.reset == "object" ? o.reset : Bt, u = W($, "");
|
270
|
+
k(l, `@layer reset { ${u} }`);
|
271
|
+
const x = f(s, "css/_templates.css"), P = I(o.templates, n.templates), K = $t(P), L = Zt(P);
|
272
|
+
k(x, K), a.templates = P;
|
273
|
+
const { mediaQueries: A } = n;
|
274
|
+
a.mediaQueries = Object.fromEntries(A.map(([S, C]) => [`@${S}`, C]));
|
275
|
+
const U = A.map(([S]) => `'@${S}'`).join(" | "), q = f(s, "types/css-tokens.d.ts"), Q = `
|
276
|
+
// Variable types
|
277
|
+
type VariableTokens = ${[...i].join("|")};
|
278
|
+
type PropertyValueToken = \`{\${VariableTokens}}\`;
|
279
|
+
|
280
|
+
// Template types
|
281
|
+
type TemplateTokens = {
|
282
|
+
${Object.entries(L).map(([S, C]) => `${S}?: ${C}`).join(`
|
283
|
+
`)}
|
284
|
+
}
|
285
|
+
|
286
|
+
// Media query types
|
287
|
+
type MediaQueryKeys = ${U};
|
288
|
+
`;
|
289
|
+
k(q, Q);
|
290
|
+
const X = f(s, "cache/config-cache.json");
|
291
|
+
k(X, JSON.stringify(a, null, 2));
|
292
|
+
}, gt = (t) => t.replace(/styled\(([^"'`{,]+),/g, (e, s) => {
|
293
|
+
if (/^['"`]/.test(s)) return e;
|
294
|
+
const o = new RegExp(`import[^;]*${s}[,\\s{][^;]*from\\s?([^{};]+);`);
|
295
|
+
if (!o.test(t)) return e;
|
296
|
+
const i = o.exec(t);
|
297
|
+
if (i) {
|
298
|
+
const d = i.at(1);
|
299
|
+
if (Ft.some((c) => d == null ? void 0 : d.includes(c))) return e;
|
300
|
+
}
|
301
|
+
return "styled('div',";
|
302
|
+
}), Qt = (t, e) => {
|
303
|
+
try {
|
304
|
+
const s = O(f(e, "saltygen/cache/config-cache.json"), "utf8");
|
305
|
+
return s ? `globalThis.saltyConfig = ${s};
|
306
|
+
|
307
|
+
${t}` : `globalThis.saltyConfig = {};
|
308
|
+
|
309
|
+
${t}`;
|
310
|
+
} catch {
|
311
|
+
return t;
|
312
|
+
}
|
313
|
+
}, v = async (t, e, s) => {
|
314
|
+
const n = B(e), o = f(s, "./temp");
|
315
|
+
it(o) || H(o);
|
316
|
+
const a = Y(e);
|
317
|
+
let i = O(e, "utf8");
|
318
|
+
i = gt(i), i = Qt(i, t);
|
319
|
+
const d = f(s, "js", n + ".js"), r = await at(t), c = f(t, (r == null ? void 0 : r.configDir) || "", "salty.config.ts"), g = jt(c), h = await St(t);
|
320
|
+
await mt.build({
|
321
|
+
stdin: {
|
322
|
+
contents: i,
|
323
|
+
sourcefile: a.base,
|
324
|
+
resolveDir: a.dir,
|
325
|
+
loader: "tsx"
|
326
|
+
},
|
327
|
+
minify: !1,
|
328
|
+
treeShaking: !0,
|
329
|
+
bundle: !0,
|
330
|
+
outfile: d,
|
331
|
+
format: h,
|
332
|
+
target: ["node20"],
|
333
|
+
keepNames: !0,
|
334
|
+
external: g,
|
335
|
+
packages: "external",
|
336
|
+
plugins: [
|
337
|
+
{
|
338
|
+
name: "test",
|
339
|
+
setup: (y) => {
|
340
|
+
y.onLoad({ filter: /.*\.css|salty|styles|styled\.ts/ }, (E) => {
|
341
|
+
const w = O(E.path, "utf8");
|
342
|
+
return { contents: gt(w), loader: "ts" };
|
343
|
+
});
|
344
|
+
}
|
345
|
+
}
|
346
|
+
]
|
347
|
+
});
|
348
|
+
const p = Date.now();
|
349
|
+
return await import(`${d}?t=${p}`);
|
350
|
+
}, Ut = async (t) => {
|
351
|
+
const e = await z(t), s = f(e, "cache/config-cache.json"), n = O(s, "utf8");
|
352
|
+
if (!n) throw new Error("Could not find config cache file");
|
353
|
+
return JSON.parse(n);
|
354
|
+
}, lt = async (t) => {
|
355
|
+
const e = await Ut(t), s = await z(t), n = f(s, "salty.config.js"), o = Date.now(), { config: a } = await import(`${n}?t=${o}`);
|
356
|
+
return I(a, e);
|
357
|
+
}, Nt = () => {
|
358
|
+
try {
|
359
|
+
return process.env.NODE_ENV === "production";
|
360
|
+
} catch {
|
361
|
+
return !1;
|
362
|
+
}
|
363
|
+
}, qt = async (t, e = Nt(), s = !0) => {
|
364
|
+
try {
|
365
|
+
const n = Date.now();
|
366
|
+
e ? rt.info("Generating CSS in production mode! 🔥") : rt.info("Generating CSS in development mode! 🚀");
|
367
|
+
const o = [], a = [], i = await z(t), d = f(i, "index.css");
|
368
|
+
s && (() => {
|
369
|
+
it(i) && kt("rm -rf " + i), H(i, { recursive: !0 }), H(f(i, "css")), H(f(i, "types")), H(f(i, "js")), H(f(i, "cache"));
|
370
|
+
})();
|
371
|
+
const c = /* @__PURE__ */ new Set(), g = /* @__PURE__ */ new Set();
|
372
|
+
async function h(l) {
|
373
|
+
const m = ["node_modules", "saltygen"], $ = Dt(l);
|
374
|
+
if ($.isDirectory()) {
|
375
|
+
const u = _t(l);
|
376
|
+
if (m.some((P) => l.includes(P))) return;
|
377
|
+
await Promise.all(u.map((P) => h(f(l, P))));
|
378
|
+
} else if ($.isFile() && G(l)) {
|
379
|
+
c.add(l);
|
380
|
+
const x = O(l, "utf8");
|
381
|
+
/define[\w\d]+\(/.test(x) && g.add(l);
|
382
|
+
}
|
383
|
+
}
|
384
|
+
await h(t), await Lt(t, g);
|
385
|
+
const p = {
|
386
|
+
keyframes: [],
|
387
|
+
components: [],
|
388
|
+
classNames: []
|
389
|
+
};
|
390
|
+
await Promise.all(
|
391
|
+
[...c].map(async (l) => {
|
392
|
+
const m = await v(t, l, i);
|
393
|
+
Object.entries(m).forEach(([$, u]) => {
|
394
|
+
u.isKeyframes ? p.keyframes.push({
|
395
|
+
value: u,
|
396
|
+
src: l,
|
397
|
+
name: $
|
398
|
+
}) : u.isClassName ? p.classNames.push({
|
399
|
+
...u,
|
400
|
+
src: l,
|
401
|
+
name: $
|
402
|
+
}) : u.generator && p.components.push({
|
403
|
+
...u,
|
404
|
+
src: l,
|
405
|
+
name: $
|
406
|
+
});
|
407
|
+
});
|
408
|
+
})
|
409
|
+
);
|
410
|
+
const b = await lt(t);
|
411
|
+
for (const l of p.keyframes) {
|
412
|
+
const { value: m } = l, $ = `a_${m.animationName}.css`, u = `css/${$}`, x = f(i, u);
|
413
|
+
o.push($), k(x, m.css);
|
414
|
+
}
|
415
|
+
const y = {};
|
416
|
+
for (const l of p.components) {
|
417
|
+
const { src: m, name: $ } = l, u = l.generator._withBuildContext({
|
418
|
+
callerName: $,
|
419
|
+
isProduction: e,
|
420
|
+
config: b
|
421
|
+
});
|
422
|
+
a[u.priority] || (a[u.priority] = []), a[u.priority].push(u.cssFileName);
|
423
|
+
const x = `css/${u.cssFileName}`, P = f(i, x);
|
424
|
+
k(P, u.css), b.importStrategy === "component" && (y[m] ? y[m].push(u.cssFileName) : y[m] = [u.cssFileName]);
|
425
|
+
}
|
426
|
+
for (const l of p.classNames) {
|
427
|
+
const { src: m, name: $ } = l, u = l.generator._withBuildContext({
|
428
|
+
callerName: $,
|
429
|
+
isProduction: e,
|
430
|
+
config: b
|
431
|
+
});
|
432
|
+
a[0].push(u.cssFileName);
|
433
|
+
const x = `css/${u.cssFileName}`, P = f(i, x);
|
434
|
+
k(P, u.css), b.importStrategy === "component" && (y[m] ? y[m].push(u.cssFileName) : y[m] = [u.cssFileName]);
|
435
|
+
}
|
436
|
+
b.importStrategy === "component" && Object.entries(y).forEach(([l, m]) => {
|
437
|
+
const $ = m.map((L) => `@import url('./${L}');`).join(`
|
438
|
+
`), u = B(l, 6), x = Y(l), P = V(x.name), K = f(i, `css/f_${P}-${u}.css`);
|
439
|
+
k(K, $);
|
440
|
+
});
|
441
|
+
const E = o.map((l) => `@import url('./css/${l}');`).join(`
|
442
|
+
`);
|
443
|
+
let F = `@layer reset, global, l0, l1, l2, l3, l4, l5, l6, l7, l8;
|
444
|
+
|
445
|
+
${["_variables.css", "_reset.css", "_global.css", "_templates.css"].filter((l) => {
|
446
|
+
try {
|
447
|
+
return O(f(i, "css", l), "utf8").length > 0;
|
448
|
+
} catch {
|
449
|
+
return !1;
|
450
|
+
}
|
451
|
+
}).map((l) => `@import url('./css/${l}');`).join(`
|
452
|
+
`)}
|
453
|
+
${E}`;
|
454
|
+
if (b.importStrategy !== "component") {
|
455
|
+
const l = a.reduce((m, $, u) => {
|
456
|
+
const x = $.reduce((A, U) => {
|
457
|
+
var X;
|
458
|
+
const q = f(i, "css", U), ft = O(q, "utf8"), Q = ((X = /.*-([^-]+)-\d+.css/.exec(U)) == null ? void 0 : X.at(1)) || B(q, 6);
|
459
|
+
return A.includes(Q) ? A : `${A}
|
460
|
+
/*start:${Q}*/
|
461
|
+
${ft}
|
462
|
+
/*end:${Q}*/
|
463
|
+
`;
|
464
|
+
}, ""), P = `l_${u}.css`, K = f(i, "css", P), L = `@layer l${u} { ${x}
|
465
|
+
}`;
|
466
|
+
return k(K, L), `${m}
|
467
|
+
@import url('./css/${P}');`;
|
468
|
+
}, "");
|
469
|
+
F += l;
|
470
|
+
}
|
471
|
+
k(d, F);
|
472
|
+
const N = Date.now() - n, M = N < 200 ? "🔥" : N < 500 ? "🚀" : N < 1e3 ? "🎉" : N < 2e3 ? "🚗" : N < 5e3 ? "🤔" : "🥴";
|
473
|
+
rt.info(`Generated CSS in ${N}ms! ${M}`);
|
474
|
+
} catch (n) {
|
475
|
+
console.error(n);
|
476
|
+
}
|
477
|
+
}, Xt = async (t, e) => {
|
478
|
+
try {
|
479
|
+
const s = await z(t);
|
480
|
+
if (G(e)) {
|
481
|
+
const o = [], a = await lt(t), i = await v(t, e, s);
|
482
|
+
if (Object.entries(i).forEach(([d, r]) => {
|
483
|
+
if (r.isKeyframes && r.css) {
|
484
|
+
const b = `css/${`a_${r.animationName}.css`}`, y = f(s, b);
|
485
|
+
k(y, r.css);
|
486
|
+
return;
|
487
|
+
}
|
488
|
+
if (r.isClassName) {
|
489
|
+
const p = r.factory._withBuildContext({
|
490
|
+
name: d
|
491
|
+
});
|
492
|
+
o[0].push(p.cssFileName);
|
493
|
+
const b = `css/${p.cssFileName}`, y = f(s, b);
|
494
|
+
k(y, p.css);
|
495
|
+
}
|
496
|
+
if (!r.generator) return;
|
497
|
+
const c = r.generator._withBuildContext({
|
498
|
+
name: d,
|
499
|
+
config: a
|
500
|
+
}), g = `css/${c.cssFileName}`, h = f(s, g);
|
501
|
+
k(h, c.css), o[c.priority] || (o[c.priority] = []), o[c.priority].push(c.cssFileName);
|
502
|
+
}), a.importStrategy !== "component")
|
503
|
+
o.forEach((d, r) => {
|
504
|
+
const c = `l_${r}.css`, g = f(s, "css", c);
|
505
|
+
let h = O(g, "utf8");
|
506
|
+
d.forEach((p) => {
|
507
|
+
var w;
|
508
|
+
const b = f(s, "css", p), y = ((w = /.*-([^-]+)-\d+.css/.exec(p)) == null ? void 0 : w.at(1)) || B(b, 6);
|
509
|
+
if (!h.includes(y)) {
|
510
|
+
const D = O(b, "utf8"), _ = `/*start:${y}*/
|
511
|
+
${D}
|
512
|
+
/*end:${y}*/
|
513
|
+
`;
|
514
|
+
h = `${h.replace(/\}$/, "")}
|
515
|
+
${_}
|
516
|
+
}`;
|
517
|
+
}
|
518
|
+
}), k(g, h);
|
519
|
+
});
|
520
|
+
else {
|
521
|
+
const d = o.flat().map((p) => `@import url('./${p}');`).join(`
|
522
|
+
`), r = B(e, 6), c = Y(e), g = V(c.name), h = f(s, `css/f_${g}-${r}.css`);
|
523
|
+
k(h, d);
|
524
|
+
}
|
525
|
+
}
|
526
|
+
} catch (s) {
|
527
|
+
console.error(s);
|
528
|
+
}
|
529
|
+
}, Yt = async (t, e, s = Nt()) => {
|
530
|
+
try {
|
531
|
+
const n = await z(t);
|
532
|
+
if (G(e)) {
|
533
|
+
const a = O(e, "utf8");
|
534
|
+
a.replace(/^(?!export\s)const\s.*/gm, (g) => `export ${g}`) !== a && await Et(e, a);
|
535
|
+
const d = await lt(t), r = await v(t, e, n);
|
536
|
+
let c = a;
|
537
|
+
if (Object.entries(r).forEach(([g, h]) => {
|
538
|
+
var u;
|
539
|
+
if (h.isKeyframes || !h.generator) return;
|
540
|
+
const p = h.generator._withBuildContext({
|
541
|
+
name: g,
|
542
|
+
config: d,
|
543
|
+
prod: s
|
544
|
+
}), b = new RegExp(`\\s${g}[=\\s]+[^()]+styled\\(([^,]+),`, "g").exec(a);
|
545
|
+
if (!b) return console.error("Could not find the original declaration");
|
546
|
+
const y = (u = b.at(1)) == null ? void 0 : u.trim(), E = new RegExp(`\\s${g}[=\\s]+styled\\(`, "g").exec(c);
|
547
|
+
if (!E) return console.error("Could not find the original declaration");
|
548
|
+
const { index: w } = E;
|
549
|
+
let D = !1;
|
550
|
+
const _ = setTimeout(() => D = !0, 5e3);
|
551
|
+
let F = 0, T = !1, N = 0;
|
552
|
+
for (; !T && !D; ) {
|
553
|
+
const x = c[w + F];
|
554
|
+
x === "(" && N++, x === ")" && N--, N === 0 && x === ")" && (T = !0), F > c.length && (D = !0), F++;
|
555
|
+
}
|
556
|
+
if (!D) clearTimeout(_);
|
557
|
+
else throw new Error("Failed to find the end of the styled call and timed out");
|
558
|
+
const M = w + F, l = c.slice(w, M), m = c, $ = ` ${g} = styled(${y}, "${p.classNames}", ${JSON.stringify(p.clientProps)});`;
|
559
|
+
c = c.replace(l, $), m === c && console.error("Minimize file failed to change content", { name: g, tagName: y });
|
560
|
+
}), d.importStrategy === "component") {
|
561
|
+
const g = B(e, 6), h = Y(e);
|
562
|
+
c = `import '../../saltygen/css/${`f_${V(h.name)}-${g}.css`}';
|
563
|
+
${c}`;
|
564
|
+
}
|
565
|
+
return c = c.replace("{ styled }", "{ styledClient as styled }"), c = c.replace("@salty-css/react/styled", "@salty-css/react/styled-client"), c;
|
566
|
+
}
|
567
|
+
} catch (n) {
|
568
|
+
console.error("Error in minimizeFile:", n);
|
569
|
+
}
|
570
|
+
}, ht = async (t) => {
|
571
|
+
if (!t || t.includes("node_modules") || t.includes("saltygen")) return !1;
|
572
|
+
if (t.includes("salty.config")) return !0;
|
573
|
+
if (!G(t)) return !1;
|
574
|
+
const n = await ct(t, "utf-8");
|
575
|
+
return !!/.+define[A-Z]\w+/.test(n);
|
576
|
+
}, oe = (t) => ({
|
577
|
+
name: "stylegen",
|
578
|
+
buildStart: () => qt(t),
|
579
|
+
load: async (e) => {
|
580
|
+
if (G(e))
|
581
|
+
return await Yt(t, e);
|
582
|
+
},
|
583
|
+
handleHotUpdate: async ({ file: e, server: s }) => {
|
584
|
+
await ht(e) && await s.restart();
|
585
|
+
},
|
586
|
+
watchChange: {
|
587
|
+
handler: async (e) => {
|
588
|
+
G(e) && (await ht(e) || await Xt(t, e));
|
589
|
+
}
|
590
|
+
}
|
591
|
+
});
|
592
|
+
export {
|
593
|
+
oe as default,
|
594
|
+
oe as saltyPlugin
|
595
|
+
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@salty-css/vite",
|
3
|
-
"version": "0.0.1-alpha.
|
3
|
+
"version": "0.0.1-alpha.200",
|
4
4
|
"main": "./dist/index.js",
|
5
5
|
"module": "./dist/index.mjs",
|
6
6
|
"typings": "./dist/index.d.ts",
|
@@ -10,15 +10,21 @@
|
|
10
10
|
"publishConfig": {
|
11
11
|
"access": "public"
|
12
12
|
},
|
13
|
-
"
|
13
|
+
"description": "Vite plugin for Salty CSS",
|
14
|
+
"homepage": "https://salty-css.dev/",
|
15
|
+
"repository": {
|
16
|
+
"type": "git",
|
17
|
+
"url": "git+https://github.com/margarita-form/salty-css.git"
|
18
|
+
},
|
14
19
|
"bugs": {
|
15
20
|
"url": "https://github.com/margarita-form/salty-css/issues"
|
16
21
|
},
|
17
22
|
"files": [
|
18
|
-
"
|
23
|
+
"**/*",
|
19
24
|
"!**/*.tsbuildinfo"
|
20
25
|
],
|
21
26
|
"nx": {
|
27
|
+
"sourceRoot": "libs/vite/src",
|
22
28
|
"name": "vite"
|
23
29
|
},
|
24
30
|
"exports": {
|
@@ -26,5 +32,8 @@
|
|
26
32
|
"import": "./index.js",
|
27
33
|
"require": "./index.cjs"
|
28
34
|
}
|
35
|
+
},
|
36
|
+
"dependencies": {
|
37
|
+
"@salty-css/core": "^0.0.1-alpha.200"
|
29
38
|
}
|
30
39
|
}
|