@unocss/vite 0.38.1 → 0.38.2

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
@@ -3,12 +3,12 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const UnocssInspector = require('@unocss/inspector');
6
+ const path = require('path');
6
7
  const crypto = require('crypto');
7
8
  const core = require('@unocss/core');
8
9
  const pluginutils = require('@rollup/pluginutils');
9
10
  const MagicString = require('magic-string');
10
11
  const fs = require('fs');
11
- const path = require('path');
12
12
  const url = require('url');
13
13
  const config = require('@unocss/config');
14
14
 
@@ -202,13 +202,13 @@ function ChunkModeBuildPlugin({ uno, filter }) {
202
202
  };
203
203
  }
204
204
 
205
- function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }) {
205
+ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig }) {
206
206
  const vfsLayerMap = /* @__PURE__ */ new Map();
207
207
  const layerImporterMap = /* @__PURE__ */ new Map();
208
208
  let tasks = [];
209
209
  const cssPostPlugins = /* @__PURE__ */ new Map();
210
210
  const cssPlugins = /* @__PURE__ */ new Map();
211
- async function transformCSS(css, id, dir) {
211
+ async function applyCssTransform(css, id, dir) {
212
212
  const {
213
213
  postcss = true
214
214
  } = await getConfig();
@@ -224,6 +224,16 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
224
224
  css = css.replace(/[\n\r]/g, "");
225
225
  return css;
226
226
  }
227
+ let lastTokenSize = 0;
228
+ let lastResult;
229
+ async function generateAll() {
230
+ await Promise.all(tasks);
231
+ if (lastResult && lastTokenSize === tokens.size)
232
+ return lastResult;
233
+ lastResult = await uno.generate(tokens, { minify: true });
234
+ lastTokenSize = tokens.size;
235
+ return lastResult;
236
+ }
227
237
  return [
228
238
  {
229
239
  name: "unocss:global:build:scan",
@@ -231,6 +241,8 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
231
241
  enforce: "pre",
232
242
  buildStart() {
233
243
  tasks = [];
244
+ lastTokenSize = 0;
245
+ lastResult = void 0;
234
246
  },
235
247
  transform(code, id) {
236
248
  if (filter(code, id))
@@ -267,27 +279,24 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
267
279
  }
268
280
  },
269
281
  async configResolved(config) {
270
- cssPostPlugins.set(config.build.outDir, config.plugins.find((i) => i.name === "vite:css-post"));
271
- cssPlugins.set(config.build.outDir, config.plugins.find((i) => i.name === "vite:css"));
282
+ const distDir = path.resolve(config.root, config.build.outDir);
283
+ cssPostPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css-post"));
284
+ cssPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css"));
272
285
  await ready;
273
286
  },
274
287
  async renderChunk(_, chunk, options) {
275
- if (!cssPostPlugins.get(options.dir))
288
+ if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
276
289
  return null;
277
- const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
278
- if (!chunks.length)
290
+ const cssPost = cssPostPlugins.get(options.dir);
291
+ if (!cssPost) {
292
+ this.warn("[unocss] failed to find vite:css-post plugin. It might be an internal bug of UnoCSS");
279
293
  return null;
294
+ }
295
+ let { css } = await generateAll();
280
296
  const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
281
- const tokens2 = /* @__PURE__ */ new Set();
282
- await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
283
- let { css } = await uno.generate(tokens2, { minify: true });
284
- if (!css)
285
- return null;
286
- if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
287
- return null;
288
- css = await transformCSS(css, fakeCssId, options.dir);
297
+ css = await applyCssTransform(css, fakeCssId, options.dir);
289
298
  const hash = getHash(css);
290
- await cssPostPlugins.get(options.dir).transform(getHashPlaceholder(hash), fakeCssId);
299
+ await cssPost.transform.call({}, getHashPlaceholder(hash), fakeCssId);
291
300
  chunk.modules[fakeCssId] = {
292
301
  code: null,
293
302
  originalLength: 0,
@@ -314,8 +323,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
314
323
  this.warn(msg);
315
324
  return;
316
325
  }
317
- await Promise.all(tasks);
318
- const result = await uno.generate(tokens, { minify: true });
326
+ const result = await generateAll();
319
327
  let replaced = false;
320
328
  for (const file of cssFiles) {
321
329
  const chunk = bundle[file];
@@ -323,7 +331,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
323
331
  const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
324
332
  chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
325
333
  replaced = true;
326
- return await transformCSS(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
334
+ return await applyCssTransform(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
327
335
  });
328
336
  }
329
337
  }
package/dist/index.d.ts CHANGED
@@ -41,7 +41,7 @@ declare function ChunkModeBuildPlugin({ uno, filter }: UnocssPluginContext): Plu
41
41
 
42
42
  declare function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
43
43
 
44
- declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }: UnocssPluginContext<VitePluginConfig>): Plugin[];
44
+ declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig }: UnocssPluginContext<VitePluginConfig>): Plugin[];
45
45
 
46
46
  declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
47
47
 
package/dist/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
  import UnocssInspector from '@unocss/inspector';
2
+ import { resolve, dirname } from 'path';
2
3
  import { createHash } from 'crypto';
3
4
  import { cssIdRE, createGenerator, BetterMap, toEscapedSelector } from '@unocss/core';
4
5
  import { createFilter } from '@rollup/pluginutils';
5
6
  import MagicString from 'magic-string';
6
7
  import fs from 'fs';
7
- import { resolve, dirname } from 'path';
8
8
  import { fileURLToPath } from 'url';
9
9
  import { loadConfig } from '@unocss/config';
10
10
 
@@ -192,13 +192,13 @@ function ChunkModeBuildPlugin({ uno, filter }) {
192
192
  };
193
193
  }
194
194
 
195
- function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }) {
195
+ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, getConfig }) {
196
196
  const vfsLayerMap = /* @__PURE__ */ new Map();
197
197
  const layerImporterMap = /* @__PURE__ */ new Map();
198
198
  let tasks = [];
199
199
  const cssPostPlugins = /* @__PURE__ */ new Map();
200
200
  const cssPlugins = /* @__PURE__ */ new Map();
201
- async function transformCSS(css, id, dir) {
201
+ async function applyCssTransform(css, id, dir) {
202
202
  const {
203
203
  postcss = true
204
204
  } = await getConfig();
@@ -214,6 +214,16 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
214
214
  css = css.replace(/[\n\r]/g, "");
215
215
  return css;
216
216
  }
217
+ let lastTokenSize = 0;
218
+ let lastResult;
219
+ async function generateAll() {
220
+ await Promise.all(tasks);
221
+ if (lastResult && lastTokenSize === tokens.size)
222
+ return lastResult;
223
+ lastResult = await uno.generate(tokens, { minify: true });
224
+ lastTokenSize = tokens.size;
225
+ return lastResult;
226
+ }
217
227
  return [
218
228
  {
219
229
  name: "unocss:global:build:scan",
@@ -221,6 +231,8 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
221
231
  enforce: "pre",
222
232
  buildStart() {
223
233
  tasks = [];
234
+ lastTokenSize = 0;
235
+ lastResult = void 0;
224
236
  },
225
237
  transform(code, id) {
226
238
  if (filter(code, id))
@@ -257,27 +269,24 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
257
269
  }
258
270
  },
259
271
  async configResolved(config) {
260
- cssPostPlugins.set(config.build.outDir, config.plugins.find((i) => i.name === "vite:css-post"));
261
- cssPlugins.set(config.build.outDir, config.plugins.find((i) => i.name === "vite:css"));
272
+ const distDir = resolve(config.root, config.build.outDir);
273
+ cssPostPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css-post"));
274
+ cssPlugins.set(distDir, config.plugins.find((i) => i.name === "vite:css"));
262
275
  await ready;
263
276
  },
264
277
  async renderChunk(_, chunk, options) {
265
- if (!cssPostPlugins.get(options.dir))
278
+ if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
266
279
  return null;
267
- const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
268
- if (!chunks.length)
280
+ const cssPost = cssPostPlugins.get(options.dir);
281
+ if (!cssPost) {
282
+ this.warn("[unocss] failed to find vite:css-post plugin. It might be an internal bug of UnoCSS");
269
283
  return null;
284
+ }
285
+ let { css } = await generateAll();
270
286
  const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
271
- const tokens2 = /* @__PURE__ */ new Set();
272
- await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
273
- let { css } = await uno.generate(tokens2, { minify: true });
274
- if (!css)
275
- return null;
276
- if (!Object.keys(chunk.modules).some((i) => i.match(RESOLVED_ID_RE)))
277
- return null;
278
- css = await transformCSS(css, fakeCssId, options.dir);
287
+ css = await applyCssTransform(css, fakeCssId, options.dir);
279
288
  const hash = getHash(css);
280
- await cssPostPlugins.get(options.dir).transform(getHashPlaceholder(hash), fakeCssId);
289
+ await cssPost.transform.call({}, getHashPlaceholder(hash), fakeCssId);
281
290
  chunk.modules[fakeCssId] = {
282
291
  code: null,
283
292
  originalLength: 0,
@@ -304,8 +313,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
304
313
  this.warn(msg);
305
314
  return;
306
315
  }
307
- await Promise.all(tasks);
308
- const result = await uno.generate(tokens, { minify: true });
316
+ const result = await generateAll();
309
317
  let replaced = false;
310
318
  for (const file of cssFiles) {
311
319
  const chunk = bundle[file];
@@ -313,7 +321,7 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, g
313
321
  const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
314
322
  chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
315
323
  replaced = true;
316
- return await transformCSS(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
324
+ return await applyCssTransform(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`, options.dir);
317
325
  });
318
326
  }
319
327
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.38.1",
3
+ "version": "0.38.2",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -43,15 +43,15 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@rollup/pluginutils": "^4.2.1",
46
- "@unocss/config": "0.38.1",
47
- "@unocss/core": "0.38.1",
48
- "@unocss/inspector": "0.38.1",
49
- "@unocss/scope": "0.38.1",
50
- "@unocss/transformer-directives": "0.38.1",
46
+ "@unocss/config": "0.38.2",
47
+ "@unocss/core": "0.38.2",
48
+ "@unocss/inspector": "0.38.2",
49
+ "@unocss/scope": "0.38.2",
50
+ "@unocss/transformer-directives": "0.38.2",
51
51
  "magic-string": "^0.26.2"
52
52
  },
53
53
  "devDependencies": {
54
- "@unocss/shared-integration": "0.38.1",
54
+ "@unocss/shared-integration": "0.38.2",
55
55
  "vite": "^2.9.9"
56
56
  },
57
57
  "scripts": {