esm-styles 0.2.4 → 0.2.5
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 +47 -13
- package/dist/lib/build.js +26 -16
- package/doc/usage-guide.md +8 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,8 +8,7 @@ A CSS-in-JS solution for JavaScript/TypeScript projects.
|
|
|
8
8
|
- Build CSS from organized source files with a simple CLI
|
|
9
9
|
- CSS layering support for proper style encapsulation
|
|
10
10
|
- Media query and device/theme selectors with shorthands
|
|
11
|
-
- CSS
|
|
12
|
-
- Supporting modules for easy CSS variable usage
|
|
11
|
+
- CSS variable sets for different themes and devices
|
|
13
12
|
|
|
14
13
|
## Installation
|
|
15
14
|
|
|
@@ -21,28 +20,63 @@ npm install esm-styles
|
|
|
21
20
|
|
|
22
21
|
### Basic Concept
|
|
23
22
|
|
|
24
|
-
ESM Styles lets you write
|
|
23
|
+
ESM Styles lets you write and store styles in JavaScript this way:
|
|
25
24
|
|
|
26
25
|
```js
|
|
27
|
-
//
|
|
26
|
+
// article.styles.mjs
|
|
27
|
+
import $device from './$device.mjs'
|
|
28
|
+
import $theme from './$theme.mjs'
|
|
29
|
+
import { card } from './card.styles.mjs'
|
|
30
|
+
|
|
28
31
|
export default {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
borderRadius: '4px',
|
|
32
|
+
article: {
|
|
33
|
+
display: 'flex',
|
|
34
|
+
|
|
35
|
+
card,
|
|
34
36
|
|
|
35
|
-
'
|
|
36
|
-
|
|
37
|
+
'@max-tablet': {
|
|
38
|
+
flexDirection: 'column',
|
|
37
39
|
},
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
button: {
|
|
42
|
+
borderRadius: $device.radii.md,
|
|
43
|
+
backgroundColor: $theme.paper.tinted,
|
|
44
|
+
|
|
45
|
+
'@dark': {
|
|
46
|
+
fontWeight: 300,
|
|
47
|
+
},
|
|
41
48
|
},
|
|
42
49
|
},
|
|
43
50
|
}
|
|
44
51
|
```
|
|
45
52
|
|
|
53
|
+
### Sample Directory Structure
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
monorepo/
|
|
57
|
+
├── package.json
|
|
58
|
+
└── packages/
|
|
59
|
+
├── app/
|
|
60
|
+
│ ├── package.json
|
|
61
|
+
│ └── src/
|
|
62
|
+
│ ├── css/
|
|
63
|
+
│ ├── styles.css
|
|
64
|
+
│ └── components/
|
|
65
|
+
│ ├── article.tsx
|
|
66
|
+
│ ├── button.tsx
|
|
67
|
+
│ └── card.tsx
|
|
68
|
+
└── styles/
|
|
69
|
+
├── $device.mjs
|
|
70
|
+
├── $theme.mjs
|
|
71
|
+
├── components/
|
|
72
|
+
│ ├── article.styles.mjs
|
|
73
|
+
│ ├── button.styles.mjs
|
|
74
|
+
│ └── card.styles.mjs
|
|
75
|
+
├── components.styles.mjs
|
|
76
|
+
├── esm-styles.config.js
|
|
77
|
+
└── package.json
|
|
78
|
+
```
|
|
79
|
+
|
|
46
80
|
### CLI Usage
|
|
47
81
|
|
|
48
82
|
Build your styles by creating a configuration file and running the CLI:
|
package/dist/lib/build.js
CHANGED
|
@@ -4,6 +4,7 @@ import { getMediaShorthands } from './utils/media-shorthand.js';
|
|
|
4
4
|
import { isEndValue } from './utils/index.js';
|
|
5
5
|
import path from 'node:path';
|
|
6
6
|
import fs from 'node:fs/promises';
|
|
7
|
+
// import { inspect } from 'node:util'
|
|
7
8
|
import _ from 'lodash';
|
|
8
9
|
export async function build(configPath = 'esm-styles.config.js') {
|
|
9
10
|
// --- Supporting module generation ---
|
|
@@ -19,7 +20,7 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
19
20
|
const sourcePath = path.join(basePath, config.sourcePath || '');
|
|
20
21
|
const outputPath = path.join(basePath, config.outputPath || '');
|
|
21
22
|
const suffix = config.sourceFilesSuffix || '.styles.mjs';
|
|
22
|
-
const
|
|
23
|
+
const floors = config.floors || [];
|
|
23
24
|
const mainCssFile = config.mainCssFile || 'styles.css';
|
|
24
25
|
// Merge media shorthands
|
|
25
26
|
const mediaShorthands = getMediaShorthands(config);
|
|
@@ -161,20 +162,30 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
161
162
|
await fs.writeFile(supportingModulePath, moduleContent, 'utf8');
|
|
162
163
|
}
|
|
163
164
|
}
|
|
164
|
-
// 4. Process each
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
165
|
+
// 4. Process each floor (replaces legacy layers)
|
|
166
|
+
const uniqueLayers = [];
|
|
167
|
+
// Track output files for each floor
|
|
168
|
+
const floorFiles = [];
|
|
169
|
+
for (const floor of floors) {
|
|
170
|
+
const { source, layer } = floor;
|
|
171
|
+
const inputFile = path.join(sourcePath, `${source}${suffix}`);
|
|
172
|
+
const outputFile = path.join(outputPath, `${source}.css`);
|
|
168
173
|
const fileUrl = pathToFileUrl(inputFile).href + `?update=${Date.now()}`;
|
|
169
174
|
const stylesObj = (await import(fileUrl)).default;
|
|
170
|
-
// Always call getCss for the layer object, so media queries are rendered
|
|
171
175
|
const css = getCss(stylesObj, {
|
|
172
176
|
...mediaShorthands,
|
|
173
177
|
globalRootSelector: config.globalRootSelector,
|
|
174
178
|
});
|
|
175
|
-
|
|
179
|
+
let wrappedCss = css;
|
|
180
|
+
if (layer) {
|
|
181
|
+
wrappedCss = `@layer ${layer} {\n${css}\n}`;
|
|
182
|
+
if (!uniqueLayers.includes(layer)) {
|
|
183
|
+
uniqueLayers.push(layer);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
176
186
|
await fs.writeFile(outputFile, wrappedCss, 'utf8');
|
|
177
|
-
|
|
187
|
+
floorFiles.push({ file: `${source}.css`, layer });
|
|
188
|
+
cssFiles.push({ floor: source, file: `${source}.css`, layer });
|
|
178
189
|
}
|
|
179
190
|
// 5. Create main CSS file
|
|
180
191
|
const mainCssPath = path.join(outputPath, mainCssFile);
|
|
@@ -188,14 +199,13 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
188
199
|
return `@import '${f.file}';`;
|
|
189
200
|
})
|
|
190
201
|
.join('\n');
|
|
191
|
-
// Compose imports for
|
|
192
|
-
const
|
|
193
|
-
const
|
|
194
|
-
.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const mainCss = [layerNames ? `@layer ${layerNames};` : '', layerImports, varImports]
|
|
202
|
+
// Compose imports for floors (in order)
|
|
203
|
+
const floorImports = floorFiles.map((f) => `@import '${f.file}';`).join('\n');
|
|
204
|
+
const mainCss = [
|
|
205
|
+
uniqueLayers.length ? `@layer ${uniqueLayers.join(', ')};` : '',
|
|
206
|
+
floorImports,
|
|
207
|
+
varImports,
|
|
208
|
+
]
|
|
199
209
|
.filter(Boolean)
|
|
200
210
|
.join('\n') + '\n';
|
|
201
211
|
await fs.writeFile(mainCssPath, mainCss, 'utf8');
|
package/doc/usage-guide.md
CHANGED
|
@@ -73,8 +73,14 @@ export default {
|
|
|
73
73
|
outputPath: 'css',
|
|
74
74
|
sourceFilesSuffix: '.styles.mjs',
|
|
75
75
|
|
|
76
|
-
// Input layers
|
|
77
|
-
|
|
76
|
+
// Input files and their layers (order of layers matters)
|
|
77
|
+
floors: [
|
|
78
|
+
{ source: 'defaults', layer: 'defaults' }, // wrap in layer 'defaults'
|
|
79
|
+
{ source: 'components', layer: 'components' }, // wrap in layer 'components'
|
|
80
|
+
{ source: 'layout', layer: 'layout' }, // wrap in layer 'layout'
|
|
81
|
+
{ source: 'basic' }, // stay out of any layer
|
|
82
|
+
{ source: 'more-layout', layer: 'layout' }, // wrap in layer 'layout'
|
|
83
|
+
],
|
|
78
84
|
|
|
79
85
|
// Output
|
|
80
86
|
mainCssFile: 'styles.css',
|