@unocss/vite 0.30.13 → 0.31.0

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
@@ -52,7 +52,8 @@ function getHashPlaceholder(hash) {
52
52
  const INCLUDE_COMMENT = "@unocss-include";
53
53
  const CSS_PLACEHOLDER = "@unocss-placeholder";
54
54
 
55
- function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
55
+ function createContext(configOrPath, defaults = {}, extraConfigSources = [], resolveConfigResult = () => {
56
+ }) {
56
57
  const loadConfig = config.createConfigLoader(configOrPath, extraConfigSources);
57
58
  let rawConfig = {};
58
59
  const uno = core.createGenerator(rawConfig, defaults);
@@ -63,6 +64,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
63
64
  const ready = reloadConfig();
64
65
  async function reloadConfig() {
65
66
  const result = await loadConfig();
67
+ resolveConfigResult(result);
66
68
  rawConfig = result.config;
67
69
  uno.setConfig(rawConfig);
68
70
  uno.config.envMode = "dev";
@@ -250,7 +252,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
250
252
  }
251
253
 
252
254
  const WARN_TIMEOUT = 2e4;
253
- const WS_EVENT_PREFIX = "custom:unocss:";
255
+ const WS_EVENT_PREFIX = "unocss:hmr";
254
256
  function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
255
257
  const servers = [];
256
258
  let base = "";
@@ -319,18 +321,9 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
319
321
  configResolved,
320
322
  async configureServer(_server) {
321
323
  servers.push(_server);
322
- _server.ws.on("connection", (ws) => {
323
- ws.on("open", () => {
324
- invalidate();
325
- });
326
- ws.on("message", (msg) => {
327
- const message = String(msg);
328
- if (!message.startsWith(WS_EVENT_PREFIX))
329
- return;
330
- const servedTime = +message.slice(WS_EVENT_PREFIX.length);
331
- if (servedTime < lastUpdate)
332
- invalidate(0);
333
- });
324
+ _server.ws.on(WS_EVENT_PREFIX, (servedTime) => {
325
+ if (servedTime < lastUpdate)
326
+ invalidate(0);
334
327
  });
335
328
  },
336
329
  async buildStart() {
@@ -374,18 +367,9 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
374
367
  },
375
368
  enforce: "post",
376
369
  transform(code, id) {
377
- if (id.includes("@vite/client") || id.includes("vite/dist/client/client.mjs")) {
378
- return code.replace("return hot", [
379
- "let __buffer = []",
380
- "function unoSendBuffer() { if(socket.readyState !== 1) return; __buffer.forEach(msg => socket.send(msg)); __buffer = [] }",
381
- 'socket.addEventListener("open", () => unoSendBuffer())',
382
- "hot.unoSend = (data) => {__buffer.push(data);unoSendBuffer()}",
383
- "return hot"
384
- ].join(";"));
385
- }
386
370
  if (entries.has(getPath(id)) && code.includes("import.meta.hot")) {
387
371
  const snippet = `
388
- if (import.meta.hot) { try { import.meta.hot.unoSend('${WS_EVENT_PREFIX}${lastServed}') } catch (e) { console.warn('[unocss-hmr]', e) } }`;
372
+ if (import.meta.hot) { try { import.meta.hot.send('${WS_EVENT_PREFIX}', ${lastServed}) } catch (e) { console.warn('[unocss-hmr]', e) } }`;
389
373
  return code + snippet;
390
374
  }
391
375
  }
package/dist/index.mjs CHANGED
@@ -42,7 +42,8 @@ function getHashPlaceholder(hash) {
42
42
  const INCLUDE_COMMENT = "@unocss-include";
43
43
  const CSS_PLACEHOLDER = "@unocss-placeholder";
44
44
 
45
- function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
45
+ function createContext(configOrPath, defaults = {}, extraConfigSources = [], resolveConfigResult = () => {
46
+ }) {
46
47
  const loadConfig = createConfigLoader(configOrPath, extraConfigSources);
47
48
  let rawConfig = {};
48
49
  const uno = createGenerator(rawConfig, defaults);
@@ -53,6 +54,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
53
54
  const ready = reloadConfig();
54
55
  async function reloadConfig() {
55
56
  const result = await loadConfig();
57
+ resolveConfigResult(result);
56
58
  rawConfig = result.config;
57
59
  uno.setConfig(rawConfig);
58
60
  uno.config.envMode = "dev";
@@ -240,7 +242,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
240
242
  }
241
243
 
242
244
  const WARN_TIMEOUT = 2e4;
243
- const WS_EVENT_PREFIX = "custom:unocss:";
245
+ const WS_EVENT_PREFIX = "unocss:hmr";
244
246
  function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
245
247
  const servers = [];
246
248
  let base = "";
@@ -309,18 +311,9 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
309
311
  configResolved,
310
312
  async configureServer(_server) {
311
313
  servers.push(_server);
312
- _server.ws.on("connection", (ws) => {
313
- ws.on("open", () => {
314
- invalidate();
315
- });
316
- ws.on("message", (msg) => {
317
- const message = String(msg);
318
- if (!message.startsWith(WS_EVENT_PREFIX))
319
- return;
320
- const servedTime = +message.slice(WS_EVENT_PREFIX.length);
321
- if (servedTime < lastUpdate)
322
- invalidate(0);
323
- });
314
+ _server.ws.on(WS_EVENT_PREFIX, (servedTime) => {
315
+ if (servedTime < lastUpdate)
316
+ invalidate(0);
324
317
  });
325
318
  },
326
319
  async buildStart() {
@@ -364,18 +357,9 @@ function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }) {
364
357
  },
365
358
  enforce: "post",
366
359
  transform(code, id) {
367
- if (id.includes("@vite/client") || id.includes("vite/dist/client/client.mjs")) {
368
- return code.replace("return hot", [
369
- "let __buffer = []",
370
- "function unoSendBuffer() { if(socket.readyState !== 1) return; __buffer.forEach(msg => socket.send(msg)); __buffer = [] }",
371
- 'socket.addEventListener("open", () => unoSendBuffer())',
372
- "hot.unoSend = (data) => {__buffer.push(data);unoSendBuffer()}",
373
- "return hot"
374
- ].join(";"));
375
- }
376
360
  if (entries.has(getPath(id)) && code.includes("import.meta.hot")) {
377
361
  const snippet = `
378
- if (import.meta.hot) { try { import.meta.hot.unoSend('${WS_EVENT_PREFIX}${lastServed}') } catch (e) { console.warn('[unocss-hmr]', e) } }`;
362
+ if (import.meta.hot) { try { import.meta.hot.send('${WS_EVENT_PREFIX}', ${lastServed}) } catch (e) { console.warn('[unocss-hmr]', e) } }`;
379
363
  return code + snippet;
380
364
  }
381
365
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.30.13",
3
+ "version": "0.31.0",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -38,13 +38,16 @@
38
38
  "dist"
39
39
  ],
40
40
  "sideEffects": false,
41
+ "peerDependencies": {
42
+ "vite": "^2.9.0"
43
+ },
41
44
  "dependencies": {
42
45
  "@rollup/pluginutils": "^4.2.0",
43
- "@unocss/config": "0.30.13",
44
- "@unocss/core": "0.30.13",
45
- "@unocss/inspector": "0.30.13",
46
- "@unocss/scope": "0.30.13",
47
- "@unocss/transformer-directives": "0.30.13",
46
+ "@unocss/config": "0.31.0",
47
+ "@unocss/core": "0.31.0",
48
+ "@unocss/inspector": "0.31.0",
49
+ "@unocss/scope": "0.31.0",
50
+ "@unocss/transformer-directives": "0.31.0",
48
51
  "magic-string": "^0.26.1"
49
52
  },
50
53
  "devDependencies": {