esm-styles 0.2.5 → 0.2.7
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/dist/lib/build.js +45 -18
- package/doc/usage-guide.md +3 -0
- package/package.json +1 -1
package/dist/lib/build.js
CHANGED
|
@@ -22,6 +22,12 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
22
22
|
const suffix = config.sourceFilesSuffix || '.styles.mjs';
|
|
23
23
|
const floors = config.floors || [];
|
|
24
24
|
const mainCssFile = config.mainCssFile || 'styles.css';
|
|
25
|
+
// Helper function to generate CSS comment header
|
|
26
|
+
const generateCssComment = (sourceName) => {
|
|
27
|
+
const normalizedBasePath = (config.basePath || '.').replace(/^\.\//, '');
|
|
28
|
+
const sourceFilePath = path.join(normalizedBasePath, config.sourcePath || '', `${sourceName}${suffix}`);
|
|
29
|
+
return `/* This CSS was automatically generated from ${sourceFilePath}, do not edit directly */\n`;
|
|
30
|
+
};
|
|
25
31
|
// Merge media shorthands
|
|
26
32
|
const mediaShorthands = getMediaShorthands(config);
|
|
27
33
|
// Ensure output directory exists
|
|
@@ -35,7 +41,8 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
35
41
|
const varsObj = (await import(fileUrl)).default;
|
|
36
42
|
const cssVars = getCssVariables(varsObj);
|
|
37
43
|
const rootSelector = config.globalRootSelector || ':root';
|
|
38
|
-
const
|
|
44
|
+
const comment = generateCssComment(config.globalVariables);
|
|
45
|
+
const wrappedCss = `${comment}${rootSelector} {\n${cssVars}\n}`;
|
|
39
46
|
await fs.writeFile(outputFile, wrappedCss, 'utf8');
|
|
40
47
|
cssFiles.push({ type: 'global', file: 'global.css' });
|
|
41
48
|
}
|
|
@@ -73,7 +80,8 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
73
80
|
fullSelector = `${rootSelector} ${selector}`;
|
|
74
81
|
}
|
|
75
82
|
}
|
|
76
|
-
const
|
|
83
|
+
const comment = generateCssComment(setName);
|
|
84
|
+
const block = `${comment}${fullSelector} {\n${cssVars}\n}`;
|
|
77
85
|
await fs.writeFile(outputFile, block, 'utf8');
|
|
78
86
|
cssFiles.push({
|
|
79
87
|
type: 'media',
|
|
@@ -163,6 +171,8 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
163
171
|
}
|
|
164
172
|
}
|
|
165
173
|
// 4. Process each floor (replaces legacy layers)
|
|
174
|
+
const importFloors = config.importFloors || floors.map((f) => f.source);
|
|
175
|
+
// Track unique layer names in order of first appearance (for imported floors only)
|
|
166
176
|
const uniqueLayers = [];
|
|
167
177
|
// Track output files for each floor
|
|
168
178
|
const floorFiles = [];
|
|
@@ -176,38 +186,55 @@ export async function build(configPath = 'esm-styles.config.js') {
|
|
|
176
186
|
...mediaShorthands,
|
|
177
187
|
globalRootSelector: config.globalRootSelector,
|
|
178
188
|
});
|
|
179
|
-
|
|
189
|
+
const comment = generateCssComment(source);
|
|
190
|
+
let wrappedCss = `${comment}${css}`;
|
|
180
191
|
if (layer) {
|
|
181
|
-
|
|
192
|
+
// add layer to order in any case, even if the floor is not imported
|
|
182
193
|
if (!uniqueLayers.includes(layer)) {
|
|
183
194
|
uniqueLayers.push(layer);
|
|
184
195
|
}
|
|
196
|
+
wrappedCss = `${comment}@layer ${layer} {\n${css}\n}`;
|
|
185
197
|
}
|
|
186
198
|
await fs.writeFile(outputFile, wrappedCss, 'utf8');
|
|
187
|
-
floorFiles.push({ file: `${source}.css`, layer });
|
|
199
|
+
floorFiles.push({ file: `${source}.css`, layer, source });
|
|
188
200
|
cssFiles.push({ floor: source, file: `${source}.css`, layer });
|
|
189
201
|
}
|
|
190
202
|
// 5. Create main CSS file
|
|
191
203
|
const mainCssPath = path.join(outputPath, mainCssFile);
|
|
204
|
+
// Type guard for cssFiles entries with type 'global' or 'media'
|
|
205
|
+
function isGlobalOrMedia(f) {
|
|
206
|
+
return ((f.type === 'global' || f.type === 'media') && typeof f.file === 'string');
|
|
207
|
+
}
|
|
192
208
|
// Compose imports for variable sets
|
|
193
|
-
|
|
194
|
-
.filter((f) => f.type === 'global' || f.type === 'media')
|
|
195
|
-
.map((f) => {
|
|
209
|
+
function toImportStatement(f) {
|
|
196
210
|
if (f.mediaQuery) {
|
|
197
211
|
return `@import '${f.file}' ${f.mediaQuery};`;
|
|
198
212
|
}
|
|
199
213
|
return `@import '${f.file}';`;
|
|
200
|
-
}
|
|
214
|
+
}
|
|
215
|
+
const varImports = cssFiles.filter(isGlobalOrMedia)
|
|
216
|
+
.map(toImportStatement)
|
|
201
217
|
.join('\n');
|
|
202
|
-
// Compose imports for floors (in order)
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
218
|
+
// Compose imports for floors (in order, only those in importFloors)
|
|
219
|
+
function isImportedFloor(f) {
|
|
220
|
+
return importFloors.includes(f.source);
|
|
221
|
+
}
|
|
222
|
+
function importStatement(f) {
|
|
223
|
+
return `@import '${f.file}';`;
|
|
224
|
+
}
|
|
225
|
+
const importedFloorFiles = floorFiles.filter(isImportedFloor);
|
|
226
|
+
const floorImports = importedFloorFiles.map(importStatement).join('\n');
|
|
227
|
+
const normalizedBasePath = (config.basePath || '.').replace(/^\.\//, '');
|
|
228
|
+
const mainCssComment = `/* This CSS was automatically generated as the main styles file from ${normalizedBasePath}, do not edit directly */\n`;
|
|
229
|
+
const mainCss = mainCssComment +
|
|
230
|
+
[
|
|
231
|
+
uniqueLayers.length ? `@layer ${uniqueLayers.join(', ')};` : '',
|
|
232
|
+
floorImports,
|
|
233
|
+
varImports,
|
|
234
|
+
]
|
|
235
|
+
.filter(Boolean)
|
|
236
|
+
.join('\n') +
|
|
237
|
+
'\n';
|
|
211
238
|
await fs.writeFile(mainCssPath, mainCss, 'utf8');
|
|
212
239
|
}
|
|
213
240
|
// Helper for file URL import
|
package/doc/usage-guide.md
CHANGED
|
@@ -85,6 +85,9 @@ export default {
|
|
|
85
85
|
// Output
|
|
86
86
|
mainCssFile: 'styles.css',
|
|
87
87
|
|
|
88
|
+
// floors to include in the main CSS file
|
|
89
|
+
importFloors: ['defaults', 'components', 'layout'],
|
|
90
|
+
|
|
88
91
|
// CSS Variables configuration
|
|
89
92
|
globalVariables: 'global',
|
|
90
93
|
globalRootSelector: ':root',
|