@unocss/vite 0.45.4 → 0.45.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/index.cjs CHANGED
@@ -222,6 +222,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
222
222
  const vfsLayers = /* @__PURE__ */ new Set();
223
223
  const layerImporterMap = /* @__PURE__ */ new Map();
224
224
  let tasks = [];
225
+ let viteConfig;
225
226
  const cssPostPlugins = /* @__PURE__ */ new Map();
226
227
  const cssPlugins = /* @__PURE__ */ new Map();
227
228
  async function applyCssTransform(css, id, dir) {
@@ -331,12 +332,14 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
331
332
  apply(options, { command }) {
332
333
  return command === "build" && !options.build?.ssr;
333
334
  },
335
+ configResolved(config) {
336
+ viteConfig = config;
337
+ },
334
338
  enforce: "post",
335
339
  async generateBundle(options, bundle) {
336
- const files = Object.entries(bundle);
337
- const cssFiles = files.filter((i) => i[0].endsWith(".css"));
338
- const jsFiles = files.filter((i) => i[0].endsWith(".js"));
339
- if (!cssFiles.length && !jsFiles.length)
340
+ const checkJs = ["umd", "amd", "iife"].includes(options.format);
341
+ const files = Object.keys(bundle).filter((i) => i.endsWith(".css") || checkJs && i.endsWith(".js"));
342
+ if (!files.length)
340
343
  return;
341
344
  if (!vfsLayers.size) {
342
345
  const msg = "[unocss] entry module not found, have you add `import 'uno.css'` in your main entry?";
@@ -345,18 +348,15 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
345
348
  }
346
349
  const result = await generateAll();
347
350
  let replaced = false;
348
- for (const [, chunk] of cssFiles) {
351
+ for (const file of files) {
352
+ const chunk = bundle[file];
349
353
  if (chunk.type === "asset" && typeof chunk.source === "string") {
350
354
  const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
351
355
  chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
352
356
  replaced = true;
353
- const css2 = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayers)) : result.getLayer(layer) || "";
354
- return await applyCssTransform(css2, `${chunk.fileName}.css`, options.dir);
357
+ return await applyCssTransform(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayers)) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
355
358
  });
356
- }
357
- }
358
- for (const [, chunk] of jsFiles) {
359
- if (chunk.type === "chunk" && typeof chunk.code === "string") {
359
+ } else if (chunk.type === "chunk" && typeof chunk.code === "string") {
360
360
  const js = chunk.code.replace(HASH_PLACEHOLDER_RE, "");
361
361
  chunk.code = await replaceAsync(js, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
362
362
  replaced = true;
@@ -365,8 +365,14 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
365
365
  });
366
366
  }
367
367
  }
368
- if (!replaced)
369
- this.error(new Error("[unocss] does not found CSS placeholder in the generated chunks,\nthis is likely an internal bug of unocss vite plugin"));
368
+ if (!replaced) {
369
+ let msg = "[unocss] does not found CSS placeholder in the generated chunks";
370
+ if (viteConfig.build.lib && checkJs)
371
+ msg += "\nIt seems you are building in library mode, it's recommanded to set `build.cssCodeSplit` to true.\nSee https://github.com/vitejs/vite/issues/1579";
372
+ else
373
+ msg += "\nThis is likely an internal bug of unocss vite plugin";
374
+ this.error(new Error(msg));
375
+ }
370
376
  }
371
377
  }
372
378
  ];
@@ -374,14 +380,15 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
374
380
 
375
381
  const WARN_TIMEOUT = 2e4;
376
382
  const WS_EVENT_PREFIX = "unocss:hmr";
383
+ const HASH_LENGTH = 6;
377
384
  function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extract, filter }) {
378
385
  const servers = [];
379
386
  let base = "";
380
387
  const tasks = [];
381
388
  const entries = /* @__PURE__ */ new Set();
382
389
  let invalidateTimer;
383
- let lastUpdate = Date.now();
384
- let lastServed = 0;
390
+ const lastServedHash = /* @__PURE__ */ new Map();
391
+ let lastServedTime = Date.now();
385
392
  let resolved = false;
386
393
  let resolvedWarnTimer;
387
394
  function configResolved(config) {
@@ -401,10 +408,12 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
401
408
  }
402
409
  }
403
410
  clearTimeout(invalidateTimer);
404
- invalidateTimer = setTimeout(() => sendUpdate(ids), timer);
411
+ invalidateTimer = setTimeout(() => {
412
+ lastServedHash.clear();
413
+ sendUpdate(ids);
414
+ }, timer);
405
415
  }
406
416
  function sendUpdate(ids) {
407
- lastUpdate = Date.now();
408
417
  for (const server of servers) {
409
418
  server.ws.send({
410
419
  type: "update",
@@ -415,7 +424,7 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
415
424
  return {
416
425
  acceptedPath: mod.url,
417
426
  path: mod.url,
418
- timestamp: lastUpdate,
427
+ timestamp: lastServedTime,
419
428
  type: "js-update"
420
429
  };
421
430
  }).filter(core.notNull)
@@ -439,7 +448,7 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
439
448
  }
440
449
  }
441
450
  onInvalidate(() => {
442
- invalidate(0, /* @__PURE__ */ new Set([...entries, ...affectedModules]));
451
+ invalidate(10, /* @__PURE__ */ new Set([...entries, ...affectedModules]));
443
452
  });
444
453
  return [
445
454
  {
@@ -449,9 +458,9 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
449
458
  configResolved,
450
459
  async configureServer(_server) {
451
460
  servers.push(_server);
452
- _server.ws.on(WS_EVENT_PREFIX, (servedTime) => {
453
- if (servedTime < lastUpdate)
454
- invalidate(0);
461
+ _server.ws.on(WS_EVENT_PREFIX, ([layer, hash]) => {
462
+ if (lastServedHash.get(layer) !== hash)
463
+ sendUpdate(entries);
455
464
  });
456
465
  },
457
466
  buildStart() {
@@ -483,8 +492,11 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
483
492
  return null;
484
493
  await Promise.all(tasks);
485
494
  const result = await uno.generate(tokens);
486
- lastServed = Date.now();
487
- return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer);
495
+ const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer);
496
+ const hash = getHash(css || "", HASH_LENGTH);
497
+ lastServedHash.set(layer, hash);
498
+ lastServedTime = Date.now();
499
+ return `/*${hash}*/${css}`;
488
500
  }
489
501
  },
490
502
  {
@@ -495,10 +507,11 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
495
507
  },
496
508
  enforce: "post",
497
509
  transform(code, id) {
498
- if (entries.has(getPath(id)) && code.includes("import.meta.hot")) {
510
+ const layer = resolveLayer(getPath(id));
511
+ if (layer && code.includes("import.meta.hot")) {
499
512
  return `${code}
500
513
  if (import.meta.hot) {
501
- try { await import.meta.hot.send('${WS_EVENT_PREFIX}', ${lastServed}); }
514
+ try { await import.meta.hot.send('${WS_EVENT_PREFIX}', ['${layer}', __vite__css.slice(2,${2 + HASH_LENGTH})]); }
502
515
  catch (e) { console.warn('[unocss-hmr]', e) }
503
516
  if (!import.meta.url.includes('?'))
504
517
  await new Promise(resolve => setTimeout(resolve, 100))
package/dist/index.mjs CHANGED
@@ -211,6 +211,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
211
211
  const vfsLayers = /* @__PURE__ */ new Set();
212
212
  const layerImporterMap = /* @__PURE__ */ new Map();
213
213
  let tasks = [];
214
+ let viteConfig;
214
215
  const cssPostPlugins = /* @__PURE__ */ new Map();
215
216
  const cssPlugins = /* @__PURE__ */ new Map();
216
217
  async function applyCssTransform(css, id, dir) {
@@ -320,12 +321,14 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
320
321
  apply(options, { command }) {
321
322
  return command === "build" && !options.build?.ssr;
322
323
  },
324
+ configResolved(config) {
325
+ viteConfig = config;
326
+ },
323
327
  enforce: "post",
324
328
  async generateBundle(options, bundle) {
325
- const files = Object.entries(bundle);
326
- const cssFiles = files.filter((i) => i[0].endsWith(".css"));
327
- const jsFiles = files.filter((i) => i[0].endsWith(".js"));
328
- if (!cssFiles.length && !jsFiles.length)
329
+ const checkJs = ["umd", "amd", "iife"].includes(options.format);
330
+ const files = Object.keys(bundle).filter((i) => i.endsWith(".css") || checkJs && i.endsWith(".js"));
331
+ if (!files.length)
329
332
  return;
330
333
  if (!vfsLayers.size) {
331
334
  const msg = "[unocss] entry module not found, have you add `import 'uno.css'` in your main entry?";
@@ -334,18 +337,15 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
334
337
  }
335
338
  const result = await generateAll();
336
339
  let replaced = false;
337
- for (const [, chunk] of cssFiles) {
340
+ for (const file of files) {
341
+ const chunk = bundle[file];
338
342
  if (chunk.type === "asset" && typeof chunk.source === "string") {
339
343
  const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
340
344
  chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
341
345
  replaced = true;
342
- const css2 = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayers)) : result.getLayer(layer) || "";
343
- return await applyCssTransform(css2, `${chunk.fileName}.css`, options.dir);
346
+ return await applyCssTransform(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayers)) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
344
347
  });
345
- }
346
- }
347
- for (const [, chunk] of jsFiles) {
348
- if (chunk.type === "chunk" && typeof chunk.code === "string") {
348
+ } else if (chunk.type === "chunk" && typeof chunk.code === "string") {
349
349
  const js = chunk.code.replace(HASH_PLACEHOLDER_RE, "");
350
350
  chunk.code = await replaceAsync(js, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
351
351
  replaced = true;
@@ -354,8 +354,14 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
354
354
  });
355
355
  }
356
356
  }
357
- if (!replaced)
358
- this.error(new Error("[unocss] does not found CSS placeholder in the generated chunks,\nthis is likely an internal bug of unocss vite plugin"));
357
+ if (!replaced) {
358
+ let msg = "[unocss] does not found CSS placeholder in the generated chunks";
359
+ if (viteConfig.build.lib && checkJs)
360
+ msg += "\nIt seems you are building in library mode, it's recommanded to set `build.cssCodeSplit` to true.\nSee https://github.com/vitejs/vite/issues/1579";
361
+ else
362
+ msg += "\nThis is likely an internal bug of unocss vite plugin";
363
+ this.error(new Error(msg));
364
+ }
359
365
  }
360
366
  }
361
367
  ];
@@ -363,14 +369,15 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig
363
369
 
364
370
  const WARN_TIMEOUT = 2e4;
365
371
  const WS_EVENT_PREFIX = "unocss:hmr";
372
+ const HASH_LENGTH = 6;
366
373
  function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extract, filter }) {
367
374
  const servers = [];
368
375
  let base = "";
369
376
  const tasks = [];
370
377
  const entries = /* @__PURE__ */ new Set();
371
378
  let invalidateTimer;
372
- let lastUpdate = Date.now();
373
- let lastServed = 0;
379
+ const lastServedHash = /* @__PURE__ */ new Map();
380
+ let lastServedTime = Date.now();
374
381
  let resolved = false;
375
382
  let resolvedWarnTimer;
376
383
  function configResolved(config) {
@@ -390,10 +397,12 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
390
397
  }
391
398
  }
392
399
  clearTimeout(invalidateTimer);
393
- invalidateTimer = setTimeout(() => sendUpdate(ids), timer);
400
+ invalidateTimer = setTimeout(() => {
401
+ lastServedHash.clear();
402
+ sendUpdate(ids);
403
+ }, timer);
394
404
  }
395
405
  function sendUpdate(ids) {
396
- lastUpdate = Date.now();
397
406
  for (const server of servers) {
398
407
  server.ws.send({
399
408
  type: "update",
@@ -404,7 +413,7 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
404
413
  return {
405
414
  acceptedPath: mod.url,
406
415
  path: mod.url,
407
- timestamp: lastUpdate,
416
+ timestamp: lastServedTime,
408
417
  type: "js-update"
409
418
  };
410
419
  }).filter(notNull)
@@ -428,7 +437,7 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
428
437
  }
429
438
  }
430
439
  onInvalidate(() => {
431
- invalidate(0, /* @__PURE__ */ new Set([...entries, ...affectedModules]));
440
+ invalidate(10, /* @__PURE__ */ new Set([...entries, ...affectedModules]));
432
441
  });
433
442
  return [
434
443
  {
@@ -438,9 +447,9 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
438
447
  configResolved,
439
448
  async configureServer(_server) {
440
449
  servers.push(_server);
441
- _server.ws.on(WS_EVENT_PREFIX, (servedTime) => {
442
- if (servedTime < lastUpdate)
443
- invalidate(0);
450
+ _server.ws.on(WS_EVENT_PREFIX, ([layer, hash]) => {
451
+ if (lastServedHash.get(layer) !== hash)
452
+ sendUpdate(entries);
444
453
  });
445
454
  },
446
455
  buildStart() {
@@ -472,8 +481,11 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
472
481
  return null;
473
482
  await Promise.all(tasks);
474
483
  const result = await uno.generate(tokens);
475
- lastServed = Date.now();
476
- return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer);
484
+ const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer);
485
+ const hash = getHash(css || "", HASH_LENGTH);
486
+ lastServedHash.set(layer, hash);
487
+ lastServedTime = Date.now();
488
+ return `/*${hash}*/${css}`;
477
489
  }
478
490
  },
479
491
  {
@@ -484,10 +496,11 @@ function GlobalModeDevPlugin({ uno, tokens, affectedModules, onInvalidate, extra
484
496
  },
485
497
  enforce: "post",
486
498
  transform(code, id) {
487
- if (entries.has(getPath(id)) && code.includes("import.meta.hot")) {
499
+ const layer = resolveLayer(getPath(id));
500
+ if (layer && code.includes("import.meta.hot")) {
488
501
  return `${code}
489
502
  if (import.meta.hot) {
490
- try { await import.meta.hot.send('${WS_EVENT_PREFIX}', ${lastServed}); }
503
+ try { await import.meta.hot.send('${WS_EVENT_PREFIX}', ['${layer}', __vite__css.slice(2,${2 + HASH_LENGTH})]); }
491
504
  catch (e) { console.warn('[unocss-hmr]', e) }
492
505
  if (!import.meta.url.includes('?'))
493
506
  await new Promise(resolve => setTimeout(resolve, 100))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.45.4",
3
+ "version": "0.45.7",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -44,15 +44,15 @@
44
44
  "dependencies": {
45
45
  "@ampproject/remapping": "^2.2.0",
46
46
  "@rollup/pluginutils": "^4.2.1",
47
- "@unocss/config": "0.45.4",
48
- "@unocss/core": "0.45.4",
49
- "@unocss/inspector": "0.45.4",
50
- "@unocss/scope": "0.45.4",
51
- "@unocss/transformer-directives": "0.45.4",
47
+ "@unocss/config": "0.45.7",
48
+ "@unocss/core": "0.45.7",
49
+ "@unocss/inspector": "0.45.7",
50
+ "@unocss/scope": "0.45.7",
51
+ "@unocss/transformer-directives": "0.45.7",
52
52
  "magic-string": "^0.26.2"
53
53
  },
54
54
  "devDependencies": {
55
- "@unocss/shared-integration": "0.45.4",
55
+ "@unocss/shared-integration": "0.45.7",
56
56
  "vite": "^3.0.4"
57
57
  },
58
58
  "scripts": {