@unocss/vite 0.31.16 → 0.32.1

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
@@ -125,6 +125,26 @@ function getHash(input, length = 8) {
125
125
  function getPath(id) {
126
126
  return id.replace(/\?.*$/, "");
127
127
  }
128
+ function replaceAsync(string, searchValue, replacer) {
129
+ try {
130
+ if (typeof replacer === "function") {
131
+ const values = [];
132
+ String.prototype.replace.call(string, searchValue, (...args) => {
133
+ values.push(replacer(...args));
134
+ return "";
135
+ });
136
+ return Promise.all(values).then((resolvedValues) => {
137
+ return String.prototype.replace.call(string, searchValue, () => {
138
+ return resolvedValues.shift() || "";
139
+ });
140
+ });
141
+ } else {
142
+ return Promise.resolve(String.prototype.replace.call(string, searchValue, replacer));
143
+ }
144
+ } catch (error) {
145
+ return Promise.reject(error);
146
+ }
147
+ }
128
148
 
129
149
  function ChunkModeBuildPlugin({ uno, filter }) {
130
150
  let cssPlugin;
@@ -168,10 +188,27 @@ function ChunkModeBuildPlugin({ uno, filter }) {
168
188
  };
169
189
  }
170
190
 
171
- function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter }) {
191
+ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }) {
172
192
  const vfsLayerMap = /* @__PURE__ */ new Map();
173
193
  let tasks = [];
194
+ let cssPostPlugin;
174
195
  let cssPlugin;
196
+ async function transformCSS(css, id) {
197
+ const {
198
+ postcss = true
199
+ } = await getConfig();
200
+ if (!cssPlugin || !postcss)
201
+ return css;
202
+ const result = await cssPlugin.transform(css, id);
203
+ if (!result)
204
+ return css;
205
+ if (typeof result === "string")
206
+ css = result;
207
+ else if (result.code)
208
+ css = result.code;
209
+ css = css.replace(/[\n\r]/g, "");
210
+ return css;
211
+ }
175
212
  return [
176
213
  {
177
214
  name: "unocss:global:build:scan",
@@ -205,23 +242,25 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
205
242
  return getLayerPlaceholder(layer);
206
243
  },
207
244
  async configResolved(config) {
208
- cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
245
+ cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
246
+ cssPlugin = config.plugins.find((i) => i.name === "vite:css");
209
247
  await ready;
210
248
  },
211
249
  async renderChunk(_, chunk) {
212
- if (!cssPlugin)
250
+ if (!cssPostPlugin)
213
251
  return null;
214
252
  const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
215
253
  if (!chunks.length)
216
254
  return null;
217
255
  const tokens2 = /* @__PURE__ */ new Set();
218
256
  await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
219
- const { css } = await uno.generate(tokens2, { minify: true });
257
+ let { css } = await uno.generate(tokens2, { minify: true });
220
258
  if (!css)
221
259
  return null;
222
- const hash = getHash(css);
223
260
  const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
224
- await cssPlugin.transform(getHashPlaceholder(hash), fakeCssId);
261
+ css = await transformCSS(css, fakeCssId);
262
+ const hash = getHash(css);
263
+ await cssPostPlugin.transform(getHashPlaceholder(hash), fakeCssId);
225
264
  chunk.modules[fakeCssId] = {
226
265
  code: null,
227
266
  originalLength: 0,
@@ -249,9 +288,10 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
249
288
  for (const file of cssFiles) {
250
289
  const chunk = bundle[file];
251
290
  if (chunk.type === "asset" && typeof chunk.source === "string") {
252
- chunk.source = chunk.source.replace(HASH_PLACEHOLDER_RE, "").replace(LAYER_PLACEHOLDER_RE, (_2, __, layer) => {
291
+ const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
292
+ chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_2, __, layer) => {
253
293
  replaced = true;
254
- return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "";
294
+ return await transformCSS(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`);
255
295
  });
256
296
  }
257
297
  }
package/dist/index.d.ts CHANGED
@@ -29,13 +29,19 @@ interface VitePluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
29
29
  * @default false
30
30
  */
31
31
  transformCSS?: boolean | 'pre' | 'post';
32
+ /**
33
+ *
34
+ * make the generate css processed by postcss(https://vitejs.dev/guide/features.html#postcss)
35
+ * @default true
36
+ */
37
+ postcss?: boolean;
32
38
  }
33
39
 
34
40
  declare function ChunkModeBuildPlugin({ uno, filter }: UnocssPluginContext): Plugin;
35
41
 
36
42
  declare function GlobalModeDevPlugin({ uno, tokens, onInvalidate, extract, filter }: UnocssPluginContext): Plugin[];
37
43
 
38
- declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter }: UnocssPluginContext): Plugin[];
44
+ declare function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }: UnocssPluginContext<VitePluginConfig>): Plugin[];
39
45
 
40
46
  declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
41
47
 
package/dist/index.mjs CHANGED
@@ -115,6 +115,26 @@ function getHash(input, length = 8) {
115
115
  function getPath(id) {
116
116
  return id.replace(/\?.*$/, "");
117
117
  }
118
+ function replaceAsync(string, searchValue, replacer) {
119
+ try {
120
+ if (typeof replacer === "function") {
121
+ const values = [];
122
+ String.prototype.replace.call(string, searchValue, (...args) => {
123
+ values.push(replacer(...args));
124
+ return "";
125
+ });
126
+ return Promise.all(values).then((resolvedValues) => {
127
+ return String.prototype.replace.call(string, searchValue, () => {
128
+ return resolvedValues.shift() || "";
129
+ });
130
+ });
131
+ } else {
132
+ return Promise.resolve(String.prototype.replace.call(string, searchValue, replacer));
133
+ }
134
+ } catch (error) {
135
+ return Promise.reject(error);
136
+ }
137
+ }
118
138
 
119
139
  function ChunkModeBuildPlugin({ uno, filter }) {
120
140
  let cssPlugin;
@@ -158,10 +178,27 @@ function ChunkModeBuildPlugin({ uno, filter }) {
158
178
  };
159
179
  }
160
180
 
161
- function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter }) {
181
+ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter, getConfig }) {
162
182
  const vfsLayerMap = /* @__PURE__ */ new Map();
163
183
  let tasks = [];
184
+ let cssPostPlugin;
164
185
  let cssPlugin;
186
+ async function transformCSS(css, id) {
187
+ const {
188
+ postcss = true
189
+ } = await getConfig();
190
+ if (!cssPlugin || !postcss)
191
+ return css;
192
+ const result = await cssPlugin.transform(css, id);
193
+ if (!result)
194
+ return css;
195
+ if (typeof result === "string")
196
+ css = result;
197
+ else if (result.code)
198
+ css = result.code;
199
+ css = css.replace(/[\n\r]/g, "");
200
+ return css;
201
+ }
165
202
  return [
166
203
  {
167
204
  name: "unocss:global:build:scan",
@@ -195,23 +232,25 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
195
232
  return getLayerPlaceholder(layer);
196
233
  },
197
234
  async configResolved(config) {
198
- cssPlugin = config.plugins.find((i) => i.name === "vite:css-post");
235
+ cssPostPlugin = config.plugins.find((i) => i.name === "vite:css-post");
236
+ cssPlugin = config.plugins.find((i) => i.name === "vite:css");
199
237
  await ready;
200
238
  },
201
239
  async renderChunk(_, chunk) {
202
- if (!cssPlugin)
240
+ if (!cssPostPlugin)
203
241
  return null;
204
242
  const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
205
243
  if (!chunks.length)
206
244
  return null;
207
245
  const tokens2 = /* @__PURE__ */ new Set();
208
246
  await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
209
- const { css } = await uno.generate(tokens2, { minify: true });
247
+ let { css } = await uno.generate(tokens2, { minify: true });
210
248
  if (!css)
211
249
  return null;
212
- const hash = getHash(css);
213
250
  const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
214
- await cssPlugin.transform(getHashPlaceholder(hash), fakeCssId);
251
+ css = await transformCSS(css, fakeCssId);
252
+ const hash = getHash(css);
253
+ await cssPostPlugin.transform(getHashPlaceholder(hash), fakeCssId);
215
254
  chunk.modules[fakeCssId] = {
216
255
  code: null,
217
256
  originalLength: 0,
@@ -239,9 +278,10 @@ function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, filter })
239
278
  for (const file of cssFiles) {
240
279
  const chunk = bundle[file];
241
280
  if (chunk.type === "asset" && typeof chunk.source === "string") {
242
- chunk.source = chunk.source.replace(HASH_PLACEHOLDER_RE, "").replace(LAYER_PLACEHOLDER_RE, (_2, __, layer) => {
281
+ const css = chunk.source.replace(HASH_PLACEHOLDER_RE, "");
282
+ chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_2, __, layer) => {
243
283
  replaced = true;
244
- return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "";
284
+ return await transformCSS(layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "", `${chunk.fileName}.css`);
245
285
  });
246
286
  }
247
287
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.31.16",
3
+ "version": "0.32.1",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -43,11 +43,11 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "@rollup/pluginutils": "^4.2.1",
46
- "@unocss/config": "0.31.16",
47
- "@unocss/core": "0.31.16",
48
- "@unocss/inspector": "0.31.16",
49
- "@unocss/scope": "0.31.16",
50
- "@unocss/transformer-directives": "0.31.16",
46
+ "@unocss/config": "0.32.1",
47
+ "@unocss/core": "0.32.1",
48
+ "@unocss/inspector": "0.32.1",
49
+ "@unocss/scope": "0.32.1",
50
+ "@unocss/transformer-directives": "0.32.1",
51
51
  "magic-string": "^0.26.1"
52
52
  },
53
53
  "devDependencies": {