@unocss/vite 0.11.2 → 0.11.6

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.d.ts CHANGED
@@ -50,7 +50,7 @@ declare function ChunkModeBuildPlugin({ uno, config }: UnocssPluginContext): Plu
50
50
 
51
51
  declare function GlobalModeDevPlugin({ config, uno, tokens, onInvalidate, scan }: UnocssPluginContext): Plugin[];
52
52
 
53
- declare function GlobalModeBuildPlugin({ uno, config, scan, tokens }: UnocssPluginContext): Plugin[];
53
+ declare function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }: UnocssPluginContext): Plugin[];
54
54
 
55
55
  declare function GlobalModePlugin(ctx: UnocssPluginContext): vite.Plugin[];
56
56
 
package/dist/index.js CHANGED
@@ -120,7 +120,7 @@ function ChunkModeBuildPlugin({ uno, config }) {
120
120
  // src/modes/global/build.ts
121
121
  var import_pluginutils2 = __toModule(require("@rollup/pluginutils"));
122
122
 
123
- // src/utils.ts
123
+ // ../plugins-common/utils.ts
124
124
  var import_crypto = __toModule(require("crypto"));
125
125
  function getHash(input, length = 8) {
126
126
  return (0, import_crypto.createHash)("sha256").update(input).digest("hex").substr(0, length);
@@ -133,7 +133,6 @@ function getPath(id) {
133
133
  var VIRTUAL_ENTRY_ALIAS = [
134
134
  /^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
135
135
  ];
136
- var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
137
136
  var LAYER_MARK_ALL = "__ALL__";
138
137
  function resolveId(id) {
139
138
  for (const alias of VIRTUAL_ENTRY_ALIAS) {
@@ -149,20 +148,30 @@ function resolveId(id) {
149
148
  }
150
149
  }
151
150
  }
151
+ var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
152
152
  function getLayerPlaceholder(layer) {
153
153
  return `#--unocss--{layer:${layer}}`;
154
154
  }
155
+ var HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*"(.+?)";?\s*}/g;
156
+ function getHashPlaceholder(hash) {
157
+ return `#--unocss-hash--{content:"${hash}"}`;
158
+ }
155
159
 
156
160
  // src/modes/global/build.ts
157
- function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
161
+ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
158
162
  const filter = (0, import_pluginutils2.createFilter)(config.include || defaultInclude, config.exclude || defaultExclude);
159
- const tasks = [];
160
- const entries = new Map();
163
+ const vfsLayerMap = new Map();
164
+ let tasks = [];
165
+ let cssPlugin;
161
166
  return [
162
167
  {
163
168
  name: "unocss:global:build:scan",
164
169
  apply: "build",
165
170
  enforce: "pre",
171
+ buildStart() {
172
+ tasks = [];
173
+ vfsLayerMap.clear();
174
+ },
166
175
  transform(code, id) {
167
176
  if (filter(id))
168
177
  tasks.push(scan(code, id));
@@ -177,14 +186,35 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
177
186
  resolveId(id) {
178
187
  const entry = resolveId(id);
179
188
  if (entry) {
180
- entries.set(entry.id, entry.layer);
189
+ vfsLayerMap.set(entry.id, entry.layer);
181
190
  return entry.id;
182
191
  }
183
192
  },
184
193
  load(id) {
185
- const layer = entries.get(getPath(id));
194
+ const layer = vfsLayerMap.get(getPath(id));
186
195
  if (layer)
187
196
  return getLayerPlaceholder(layer);
197
+ },
198
+ async renderChunk(_, chunk) {
199
+ const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
200
+ if (!chunks.length)
201
+ return null;
202
+ const tokens2 = new Set();
203
+ await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
204
+ const { css } = await uno.generate(tokens2, { minify: true });
205
+ if (!css)
206
+ return null;
207
+ const hash = getHash(css);
208
+ const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
209
+ await cssPlugin.transform(getHashPlaceholder(hash), fakeCssId);
210
+ chunk.modules[fakeCssId] = {
211
+ code: null,
212
+ originalLength: 0,
213
+ removedExports: [],
214
+ renderedExports: [],
215
+ renderedLength: 0
216
+ };
217
+ return null;
188
218
  }
189
219
  },
190
220
  {
@@ -193,8 +223,11 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
193
223
  var _a;
194
224
  return command === "build" && !((_a = options.build) == null ? void 0 : _a.ssr);
195
225
  },
226
+ configResolved(config2) {
227
+ cssPlugin = config2.plugins.find((i) => i.name === "vite:css-post");
228
+ },
196
229
  enforce: "post",
197
- async generateBundle(options, bundle) {
230
+ async generateBundle(_, bundle) {
198
231
  const files = Object.keys(bundle);
199
232
  const cssFiles = files.filter((i) => i.endsWith(".css"));
200
233
  if (!cssFiles.length)
@@ -202,38 +235,17 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
202
235
  await Promise.all(tasks);
203
236
  const result = await uno.generate(tokens, { minify: true });
204
237
  let replaced = false;
205
- const cssReplacedMap = {};
206
238
  for (const file of cssFiles) {
207
239
  const chunk = bundle[file];
208
240
  if (chunk.type === "asset" && typeof chunk.source === "string") {
209
- let currentReplaced = false;
210
- chunk.source = chunk.source.replace(LAYER_PLACEHOLDER_RE, (_, __, layer) => {
211
- currentReplaced = true;
241
+ chunk.source = chunk.source.replace(HASH_PLACEHOLDER_RE, "").replace(LAYER_PLACEHOLDER_RE, (_2, __, layer) => {
212
242
  replaced = true;
213
- return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.values())) : result.getLayer(layer) || "";
243
+ return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "";
214
244
  });
215
- if (currentReplaced) {
216
- const newName = chunk.fileName.replace(/\.(\w+)\.css$/, `.${getHash(chunk.source)}.css`);
217
- cssReplacedMap[chunk.fileName] = newName;
218
- chunk.fileName = newName;
219
- }
220
245
  }
221
246
  }
222
247
  if (!replaced)
223
248
  this.error(new Error("[unocss] does not found CSS placeholder in the generated chunks,\nthis is likely an internal bug of unocss vite plugin"));
224
- const entires = Object.entries(cssReplacedMap);
225
- if (!entires.length)
226
- return;
227
- for (const file of files) {
228
- const chunk = bundle[file];
229
- if (chunk.type === "chunk" && typeof chunk.code === "string") {
230
- for (const [k, v] of entires)
231
- chunk.code = chunk.code.replace(k, v);
232
- } else if (chunk.type === "asset" && typeof chunk.source === "string") {
233
- for (const [k, v] of entires)
234
- chunk.source = chunk.source.replace(k, v);
235
- }
236
- }
237
249
  }
238
250
  }
239
251
  ];
package/dist/index.mjs CHANGED
@@ -86,7 +86,7 @@ function ChunkModeBuildPlugin({ uno, config }) {
86
86
  // src/modes/global/build.ts
87
87
  import { createFilter as createFilter2 } from "@rollup/pluginutils";
88
88
 
89
- // src/utils.ts
89
+ // ../plugins-common/utils.ts
90
90
  import { createHash } from "crypto";
91
91
  function getHash(input, length = 8) {
92
92
  return createHash("sha256").update(input).digest("hex").substr(0, length);
@@ -99,7 +99,6 @@ function getPath(id) {
99
99
  var VIRTUAL_ENTRY_ALIAS = [
100
100
  /^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
101
101
  ];
102
- var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
103
102
  var LAYER_MARK_ALL = "__ALL__";
104
103
  function resolveId(id) {
105
104
  for (const alias of VIRTUAL_ENTRY_ALIAS) {
@@ -115,20 +114,30 @@ function resolveId(id) {
115
114
  }
116
115
  }
117
116
  }
117
+ var LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
118
118
  function getLayerPlaceholder(layer) {
119
119
  return `#--unocss--{layer:${layer}}`;
120
120
  }
121
+ var HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*"(.+?)";?\s*}/g;
122
+ function getHashPlaceholder(hash) {
123
+ return `#--unocss-hash--{content:"${hash}"}`;
124
+ }
121
125
 
122
126
  // src/modes/global/build.ts
123
- function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
127
+ function GlobalModeBuildPlugin({ uno, config, scan, tokens, modules }) {
124
128
  const filter = createFilter2(config.include || defaultInclude, config.exclude || defaultExclude);
125
- const tasks = [];
126
- const entries = new Map();
129
+ const vfsLayerMap = new Map();
130
+ let tasks = [];
131
+ let cssPlugin;
127
132
  return [
128
133
  {
129
134
  name: "unocss:global:build:scan",
130
135
  apply: "build",
131
136
  enforce: "pre",
137
+ buildStart() {
138
+ tasks = [];
139
+ vfsLayerMap.clear();
140
+ },
132
141
  transform(code, id) {
133
142
  if (filter(id))
134
143
  tasks.push(scan(code, id));
@@ -143,14 +152,35 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
143
152
  resolveId(id) {
144
153
  const entry = resolveId(id);
145
154
  if (entry) {
146
- entries.set(entry.id, entry.layer);
155
+ vfsLayerMap.set(entry.id, entry.layer);
147
156
  return entry.id;
148
157
  }
149
158
  },
150
159
  load(id) {
151
- const layer = entries.get(getPath(id));
160
+ const layer = vfsLayerMap.get(getPath(id));
152
161
  if (layer)
153
162
  return getLayerPlaceholder(layer);
163
+ },
164
+ async renderChunk(_, chunk) {
165
+ const chunks = Object.keys(chunk.modules).filter((i) => modules.has(i));
166
+ if (!chunks.length)
167
+ return null;
168
+ const tokens2 = new Set();
169
+ await Promise.all(chunks.map((c) => uno.applyExtractors(modules.get(c) || "", c, tokens2)));
170
+ const { css } = await uno.generate(tokens2, { minify: true });
171
+ if (!css)
172
+ return null;
173
+ const hash = getHash(css);
174
+ const fakeCssId = `${chunk.fileName}-unocss-hash.css`;
175
+ await cssPlugin.transform(getHashPlaceholder(hash), fakeCssId);
176
+ chunk.modules[fakeCssId] = {
177
+ code: null,
178
+ originalLength: 0,
179
+ removedExports: [],
180
+ renderedExports: [],
181
+ renderedLength: 0
182
+ };
183
+ return null;
154
184
  }
155
185
  },
156
186
  {
@@ -159,8 +189,11 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
159
189
  var _a;
160
190
  return command === "build" && !((_a = options.build) == null ? void 0 : _a.ssr);
161
191
  },
192
+ configResolved(config2) {
193
+ cssPlugin = config2.plugins.find((i) => i.name === "vite:css-post");
194
+ },
162
195
  enforce: "post",
163
- async generateBundle(options, bundle) {
196
+ async generateBundle(_, bundle) {
164
197
  const files = Object.keys(bundle);
165
198
  const cssFiles = files.filter((i) => i.endsWith(".css"));
166
199
  if (!cssFiles.length)
@@ -168,38 +201,17 @@ function GlobalModeBuildPlugin({ uno, config, scan, tokens }) {
168
201
  await Promise.all(tasks);
169
202
  const result = await uno.generate(tokens, { minify: true });
170
203
  let replaced = false;
171
- const cssReplacedMap = {};
172
204
  for (const file of cssFiles) {
173
205
  const chunk = bundle[file];
174
206
  if (chunk.type === "asset" && typeof chunk.source === "string") {
175
- let currentReplaced = false;
176
- chunk.source = chunk.source.replace(LAYER_PLACEHOLDER_RE, (_, __, layer) => {
177
- currentReplaced = true;
207
+ chunk.source = chunk.source.replace(HASH_PLACEHOLDER_RE, "").replace(LAYER_PLACEHOLDER_RE, (_2, __, layer) => {
178
208
  replaced = true;
179
- return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries.values())) : result.getLayer(layer) || "";
209
+ return layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(vfsLayerMap.values())) : result.getLayer(layer) || "";
180
210
  });
181
- if (currentReplaced) {
182
- const newName = chunk.fileName.replace(/\.(\w+)\.css$/, `.${getHash(chunk.source)}.css`);
183
- cssReplacedMap[chunk.fileName] = newName;
184
- chunk.fileName = newName;
185
- }
186
211
  }
187
212
  }
188
213
  if (!replaced)
189
214
  this.error(new Error("[unocss] does not found CSS placeholder in the generated chunks,\nthis is likely an internal bug of unocss vite plugin"));
190
- const entires = Object.entries(cssReplacedMap);
191
- if (!entires.length)
192
- return;
193
- for (const file of files) {
194
- const chunk = bundle[file];
195
- if (chunk.type === "chunk" && typeof chunk.code === "string") {
196
- for (const [k, v] of entires)
197
- chunk.code = chunk.code.replace(k, v);
198
- } else if (chunk.type === "asset" && typeof chunk.source === "string") {
199
- for (const [k, v] of entires)
200
- chunk.source = chunk.source.replace(k, v);
201
- }
202
- }
203
215
  }
204
216
  }
205
217
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.11.2",
3
+ "version": "0.11.6",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -34,10 +34,10 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "@rollup/pluginutils": "^4.1.1",
37
- "@unocss/config": "0.11.2",
38
- "@unocss/core": "0.11.2",
39
- "@unocss/scope": "0.11.2",
40
- "@unocss/inspector": "0.11.2"
37
+ "@unocss/config": "0.11.6",
38
+ "@unocss/core": "0.11.6",
39
+ "@unocss/scope": "0.11.6",
40
+ "@unocss/inspector": "0.11.6"
41
41
  },
42
42
  "devDependencies": {
43
43
  "vite": "^2.6.13"