esm-styles 0.4.2 → 0.4.3

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.
Files changed (2) hide show
  1. package/dist/lib/build.js +17 -12
  2. package/package.json +1 -1
package/dist/lib/build.js CHANGED
@@ -39,6 +39,17 @@ function createAliasPlugin(aliases, sourcePath) {
39
39
  function escapeRegex(str) {
40
40
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
41
41
  }
42
+ /**
43
+ * Write a file atomically: write to a temp file in the same directory, then
44
+ * rename it over the target. rename() is atomic on the same filesystem and
45
+ * swaps the inode, so file watchers (Vite, Cursor) reliably pick up a single
46
+ * clean replacement instead of observing a truncate-then-write mid-flight.
47
+ */
48
+ async function writeAtomic(filePath, data, encoding = 'utf8') {
49
+ const tmpPath = `${filePath}.tmp`;
50
+ await fs.writeFile(tmpPath, data, encoding);
51
+ await fs.rename(tmpPath, filePath);
52
+ }
42
53
  /**
43
54
  * Import a module with alias resolution using esbuild.
44
55
  * Falls back to direct import when no aliases are configured.
@@ -103,7 +114,7 @@ export async function build(configPath = 'esm-styles.config.js') {
103
114
  const rootSelector = config.globalRootSelector || ':root';
104
115
  const comment = generateCssComment(config.globalVariables);
105
116
  const wrappedCss = `${comment}${rootSelector} {\n${cssVars}\n}`;
106
- await fs.writeFile(outputFile, wrappedCss, 'utf8');
117
+ await writeAtomic(outputFile, wrappedCss);
107
118
  cssFiles.push({ type: 'global', file: 'global.css' });
108
119
  }
109
120
  // 3. Process media variable sets
@@ -141,7 +152,7 @@ export async function build(configPath = 'esm-styles.config.js') {
141
152
  }
142
153
  const comment = generateCssComment(setName);
143
154
  const block = `${comment}${fullSelector} {\n${cssVars}\n}`;
144
- await fs.writeFile(outputFile, block, 'utf8');
155
+ await writeAtomic(outputFile, block);
145
156
  cssFiles.push({
146
157
  type: 'media',
147
158
  file: fileName,
@@ -226,7 +237,7 @@ export async function build(configPath = 'esm-styles.config.js') {
226
237
  const supportingModuleObj = buildSupportingModule([], true);
227
238
  const supportingModulePath = path.join(sourcePath, `$${mediaType}.mjs`);
228
239
  const moduleContent = `export default ${JSON.stringify(supportingModuleObj, null, 2)}\n`;
229
- await fs.writeFile(supportingModulePath, moduleContent, 'utf8');
240
+ await writeAtomic(supportingModulePath, moduleContent);
230
241
  }
231
242
  }
232
243
  // 4. Process each floor (replaces legacy layers)
@@ -270,7 +281,7 @@ export async function build(configPath = 'esm-styles.config.js') {
270
281
  });
271
282
  wrappedCss = result.code;
272
283
  }
273
- await fs.writeFile(outputFile, wrappedCss, 'utf8');
284
+ await writeAtomic(outputFile, wrappedCss);
274
285
  // Calculate relative path from default output directory for imports
275
286
  const relativePath = floorOutputPath
276
287
  ? path.relative(outputPath, outputFile)
@@ -319,17 +330,11 @@ export async function build(configPath = 'esm-styles.config.js') {
319
330
  .filter(Boolean)
320
331
  .join('\n') +
321
332
  '\n';
322
- await fs.writeFile(mainCssPath, mainCss, 'utf8');
333
+ await writeAtomic(mainCssPath, mainCss);
323
334
  // 6. Create timestamp file
324
335
  const { outputPath: timestampOutputPath, extension: timestampExtension } = config.timestamp || { outputPath: '', extension: 'mjs' };
325
336
  const timestampPath = path.join(config.basePath || '.', timestampOutputPath, 'timestamp.' + timestampExtension);
326
- // Write atomically: write to a temp file in the same directory, then rename
327
- // over the target. rename() is atomic on the same filesystem and swaps the
328
- // inode, so file watchers (Vite, Cursor) reliably pick up the change instead
329
- // of observing a truncate-then-write mid-flight.
330
- const timestampTmpPath = `${timestampPath}.tmp`;
331
- await fs.writeFile(timestampTmpPath, `export default ${Date.now()}`, 'utf8');
332
- await fs.rename(timestampTmpPath, timestampPath);
337
+ await writeAtomic(timestampPath, `export default ${Date.now()}`);
333
338
  }
334
339
  // Helper for file URL import
335
340
  import { pathToFileURL as pathToFileUrl } from 'node:url';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esm-styles",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "A library for working with ESM styles",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",