@unocss/webpack 0.62.3 → 0.63.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.
@@ -0,0 +1,466 @@
1
+ 'use strict';
2
+
3
+ const node_path = require('node:path');
4
+ const process$1 = require('node:process');
5
+ const unplugin$1 = require('unplugin');
6
+ const WebpackSources = require('webpack-sources');
7
+ const fs = require('node:fs/promises');
8
+ const tinyglobby = require('tinyglobby');
9
+ const remapping = require('@ampproject/remapping');
10
+ const MagicString = require('magic-string');
11
+ const core = require('@unocss/core');
12
+ const pluginutils = require('@rollup/pluginutils');
13
+ const config = require('@unocss/config');
14
+ const node_crypto = require('node:crypto');
15
+
16
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
17
+
18
+ const process__default = /*#__PURE__*/_interopDefaultCompat(process$1);
19
+ const WebpackSources__default = /*#__PURE__*/_interopDefaultCompat(WebpackSources);
20
+ const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
21
+ const remapping__default = /*#__PURE__*/_interopDefaultCompat(remapping);
22
+ const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
23
+
24
+ const INCLUDE_COMMENT = "@unocss-include";
25
+ const IGNORE_COMMENT = "@unocss-ignore";
26
+ const CSS_PLACEHOLDER = "@unocss-placeholder";
27
+ const SKIP_START_COMMENT = "@unocss-skip-start";
28
+ const SKIP_END_COMMENT = "@unocss-skip-end";
29
+ const SKIP_COMMENT_RE = new RegExp(`(//\\s*?${SKIP_START_COMMENT}\\s*?|\\/\\*\\s*?${SKIP_START_COMMENT}\\s*?\\*\\/|<!--\\s*?${SKIP_START_COMMENT}\\s*?-->)[\\s\\S]*?(//\\s*?${SKIP_END_COMMENT}\\s*?|\\/\\*\\s*?${SKIP_END_COMMENT}\\s*?\\*\\/|<!--\\s*?${SKIP_END_COMMENT}\\s*?-->)`, "g");
30
+
31
+ function getPath(id) {
32
+ return id.replace(/\?.*$/, "");
33
+ }
34
+ function isCssId(id) {
35
+ return core.cssIdRE.test(id);
36
+ }
37
+ function hash(str) {
38
+ let i;
39
+ let l;
40
+ let hval = 2166136261;
41
+ for (i = 0, l = str.length; i < l; i++) {
42
+ hval ^= str.charCodeAt(i);
43
+ hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
44
+ }
45
+ return `00000${(hval >>> 0).toString(36)}`.slice(-6);
46
+ }
47
+ function transformSkipCode(code, map, SKIP_RULES_RE, keyFlag) {
48
+ for (const item of Array.from(code.matchAll(SKIP_RULES_RE))) {
49
+ if (item != null) {
50
+ const matched = item[0];
51
+ const withHashKey = `${keyFlag}${hash(matched)}`;
52
+ map.set(withHashKey, matched);
53
+ code = code.replace(matched, withHashKey);
54
+ }
55
+ }
56
+ return code;
57
+ }
58
+ function restoreSkipCode(code, map) {
59
+ for (const [withHashKey, matched] of map.entries())
60
+ code = code.replaceAll(withHashKey, matched);
61
+ return code;
62
+ }
63
+
64
+ async function applyTransformers(ctx, original, id, enforce = "default") {
65
+ if (original.includes(IGNORE_COMMENT))
66
+ return;
67
+ const transformers = (ctx.uno.config.transformers || []).filter((i) => (i.enforce || "default") === enforce);
68
+ if (!transformers.length)
69
+ return;
70
+ const skipMap = /* @__PURE__ */ new Map();
71
+ let code = original;
72
+ let s = new MagicString__default(transformSkipCode(code, skipMap, SKIP_COMMENT_RE, "@unocss-skip-placeholder-"));
73
+ const maps = [];
74
+ for (const t of transformers) {
75
+ if (t.idFilter) {
76
+ if (!t.idFilter(id))
77
+ continue;
78
+ } else if (!ctx.filter(code, id)) {
79
+ continue;
80
+ }
81
+ await t.transform(s, id, ctx);
82
+ if (s.hasChanged()) {
83
+ code = restoreSkipCode(s.toString(), skipMap);
84
+ maps.push(s.generateMap({ hires: true, source: id }));
85
+ s = new MagicString__default(code);
86
+ }
87
+ }
88
+ if (code !== original) {
89
+ return {
90
+ code,
91
+ map: remapping__default(maps, (_, ctx2) => {
92
+ ctx2.content = code;
93
+ return null;
94
+ })
95
+ };
96
+ }
97
+ }
98
+
99
+ async function setupContentExtractor(ctx, shouldWatch = false) {
100
+ const { content } = await ctx.getConfig();
101
+ const { extract, tasks, root, filter } = ctx;
102
+ if (content?.inline) {
103
+ await Promise.all(
104
+ content.inline.map(async (c, idx) => {
105
+ if (typeof c === "function")
106
+ c = await c();
107
+ if (typeof c === "string")
108
+ c = { code: c };
109
+ return extract(c.code, c.id ?? `__plain_content_${idx}__`);
110
+ })
111
+ );
112
+ }
113
+ if (content?.filesystem) {
114
+ const files = await tinyglobby.glob(content.filesystem, { cwd: root, expandDirectories: false });
115
+ async function extractFile(file) {
116
+ file = node_path.isAbsolute(file) ? file : node_path.resolve(root, file);
117
+ const code = await fs__default.readFile(file, "utf-8");
118
+ if (!filter(code, file))
119
+ return;
120
+ const preTransform = await applyTransformers(ctx, code, file, "pre");
121
+ const defaultTransform = await applyTransformers(ctx, preTransform?.code || code, file);
122
+ await applyTransformers(ctx, defaultTransform?.code || preTransform?.code || code, file, "post");
123
+ return await extract(preTransform?.code || code, file);
124
+ }
125
+ if (shouldWatch) {
126
+ const { watch } = await import('chokidar');
127
+ const ignored = ["**/{.git,node_modules}/**"];
128
+ const watcher = watch(files, {
129
+ ignorePermissionErrors: true,
130
+ ignored,
131
+ cwd: root,
132
+ ignoreInitial: true
133
+ });
134
+ watcher.on("all", (type, file) => {
135
+ if (type === "add" || type === "change") {
136
+ const absolutePath = node_path.resolve(root, file);
137
+ tasks.push(extractFile(absolutePath));
138
+ }
139
+ });
140
+ }
141
+ await Promise.all(files.map(extractFile));
142
+ }
143
+ }
144
+
145
+ const defaultPipelineExclude = [core.cssIdRE];
146
+ const defaultPipelineInclude = [/\.(vue|svelte|[jt]sx|mdx?|astro|elm|php|phtml|html)($|\?)/];
147
+
148
+ function deprecationCheck(config) {
149
+ let warned = false;
150
+ function warn(msg) {
151
+ warned = true;
152
+ console.warn(`[unocss] ${msg}`);
153
+ }
154
+ if (config.include)
155
+ warn("`include` option is deprecated, use `content.pipeline.include` instead.");
156
+ if (config.exclude)
157
+ warn("`exclude` option is deprecated, use `content.pipeline.exclude` instead.");
158
+ if (config.extraContent)
159
+ warn("`extraContent` option is deprecated, use `content` instead.");
160
+ if (config.content?.plain)
161
+ warn("`content.plain` option is renamed to `content.inline`.");
162
+ if (warned && typeof process !== "undefined" && process.env.CI)
163
+ throw new Error("deprecation warning");
164
+ }
165
+
166
+ function createContext(configOrPath, defaults = {}, extraConfigSources = [], resolveConfigResult = () => {
167
+ }) {
168
+ let root = process__default.cwd();
169
+ let rawConfig = {};
170
+ let configFileList = [];
171
+ const uno = core.createGenerator(rawConfig, defaults);
172
+ let rollupFilter = pluginutils.createFilter(
173
+ defaultPipelineInclude,
174
+ defaultPipelineExclude,
175
+ { resolve: typeof configOrPath === "string" ? configOrPath : root }
176
+ );
177
+ const invalidations = [];
178
+ const reloadListeners = [];
179
+ const modules = new core.BetterMap();
180
+ const tokens = /* @__PURE__ */ new Set();
181
+ const tasks = [];
182
+ const affectedModules = /* @__PURE__ */ new Set();
183
+ const loadConfig = config.createRecoveryConfigLoader();
184
+ let ready = reloadConfig();
185
+ async function reloadConfig() {
186
+ const result = await loadConfig(root, configOrPath, extraConfigSources, defaults);
187
+ resolveConfigResult(result);
188
+ deprecationCheck(result.config);
189
+ rawConfig = result.config;
190
+ configFileList = result.sources;
191
+ uno.setConfig(rawConfig);
192
+ uno.config.envMode = "dev";
193
+ rollupFilter = rawConfig.content?.pipeline === false ? () => false : pluginutils.createFilter(
194
+ rawConfig.content?.pipeline?.include || rawConfig.include || defaultPipelineInclude,
195
+ rawConfig.content?.pipeline?.exclude || rawConfig.exclude || defaultPipelineExclude,
196
+ { resolve: typeof configOrPath === "string" ? configOrPath : root }
197
+ );
198
+ tokens.clear();
199
+ await Promise.all(modules.map((code, id) => uno.applyExtractors(code.replace(SKIP_COMMENT_RE, ""), id, tokens)));
200
+ invalidate();
201
+ dispatchReload();
202
+ const presets = /* @__PURE__ */ new Set();
203
+ uno.config.presets.forEach((i) => {
204
+ if (!i.name)
205
+ return;
206
+ if (presets.has(i.name))
207
+ console.warn(`[unocss] duplication of preset ${i.name} found, there might be something wrong with your config.`);
208
+ else
209
+ presets.add(i.name);
210
+ });
211
+ return result;
212
+ }
213
+ async function updateRoot(newRoot) {
214
+ if (newRoot !== root) {
215
+ root = newRoot;
216
+ ready = reloadConfig();
217
+ }
218
+ return await ready;
219
+ }
220
+ function invalidate() {
221
+ invalidations.forEach((cb) => cb());
222
+ }
223
+ function dispatchReload() {
224
+ reloadListeners.forEach((cb) => cb());
225
+ }
226
+ async function extract(code, id) {
227
+ if (id)
228
+ modules.set(id, code);
229
+ const len = tokens.size;
230
+ await uno.applyExtractors(code.replace(SKIP_COMMENT_RE, ""), id, tokens);
231
+ if (tokens.size > len)
232
+ invalidate();
233
+ }
234
+ function filter(code, id) {
235
+ if (code.includes(IGNORE_COMMENT))
236
+ return false;
237
+ return code.includes(INCLUDE_COMMENT) || code.includes(CSS_PLACEHOLDER) || rollupFilter(id.replace(/\?v=\w+$/, ""));
238
+ }
239
+ async function getConfig() {
240
+ await ready;
241
+ return rawConfig;
242
+ }
243
+ async function flushTasks() {
244
+ const _tasks = [...tasks];
245
+ await Promise.all(_tasks);
246
+ if (tasks[0] === _tasks[0])
247
+ tasks.splice(0, _tasks.length);
248
+ }
249
+ return {
250
+ get ready() {
251
+ return ready;
252
+ },
253
+ tokens,
254
+ modules,
255
+ affectedModules,
256
+ tasks,
257
+ flushTasks,
258
+ invalidate,
259
+ onInvalidate(fn) {
260
+ invalidations.push(fn);
261
+ },
262
+ filter,
263
+ reloadConfig,
264
+ onReload(fn) {
265
+ reloadListeners.push(fn);
266
+ },
267
+ uno,
268
+ extract,
269
+ getConfig,
270
+ get root() {
271
+ return root;
272
+ },
273
+ updateRoot,
274
+ getConfigFileList: () => configFileList
275
+ };
276
+ }
277
+
278
+ function getHash(input, length = 8) {
279
+ return node_crypto.createHash("sha256").update(input).digest("hex").slice(0, length);
280
+ }
281
+
282
+ const VIRTUAL_ENTRY_ALIAS = [
283
+ /^(?:virtual:)?uno(?::(.+))?\.css(\?.*)?$/
284
+ ];
285
+ const LAYER_MARK_ALL = "__ALL__";
286
+ const RESOLVED_ID_WITH_QUERY_RE = /[/\\]__uno(_.*?)?\.css(\?.*)?$/;
287
+ const RESOLVED_ID_RE = /[/\\]__uno(?:_(.*?))?\.css$/;
288
+ function resolveId(id) {
289
+ if (id.match(RESOLVED_ID_WITH_QUERY_RE))
290
+ return id;
291
+ for (const alias of VIRTUAL_ENTRY_ALIAS) {
292
+ const match = id.match(alias);
293
+ if (match) {
294
+ return match[1] ? `/__uno_${match[1]}.css` : "/__uno.css";
295
+ }
296
+ }
297
+ }
298
+ function resolveLayer(id) {
299
+ const match = id.match(RESOLVED_ID_RE);
300
+ if (match)
301
+ return match[1] || LAYER_MARK_ALL;
302
+ }
303
+ const LAYER_PLACEHOLDER_RE = /#--unocss--\s*\{\s*layer\s*:\s*(.+?)\s*(?:;\s*escape-view\s*:\s*(.+?)\s*)?;?\s*\}/g;
304
+ function getLayerPlaceholder(layer) {
305
+ return `#--unocss--{layer:${layer};escape-view:\\"\\'\\\`\\\\}`;
306
+ }
307
+ function getCssEscaperForJsContent(view) {
308
+ if (!view)
309
+ return (css) => css;
310
+ const prefix = {};
311
+ const escapeViewRe = /(\\*)\\(["'`\\])/g;
312
+ view.trim().replace(escapeViewRe, (_, bs, char) => {
313
+ prefix[char] = bs.replace(/\\\\/g, "\\");
314
+ return "";
315
+ });
316
+ return (css) => css.replace(/["'`\\]/g, (v) => {
317
+ return (prefix[v] || "") + v;
318
+ });
319
+ }
320
+ const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*\{\s*content\s*:\s*\\*"([^\\"]+)\\*";?\s*\}/g;
321
+ function getHashPlaceholder(hash) {
322
+ return `#--unocss-hash--{content:"${hash}"}`;
323
+ }
324
+
325
+ const PLUGIN_NAME = "unocss:webpack";
326
+ const UPDATE_DEBOUNCE = 10;
327
+ function unplugin(configOrPath, defaults) {
328
+ return unplugin$1.createUnplugin(() => {
329
+ const ctx = createContext(configOrPath, {
330
+ envMode: process__default.env.NODE_ENV === "development" ? "dev" : "build",
331
+ ...defaults
332
+ });
333
+ const { uno, tokens, filter, extract, onInvalidate, tasks, flushTasks } = ctx;
334
+ let timer;
335
+ onInvalidate(() => {
336
+ clearTimeout(timer);
337
+ timer = setTimeout(updateModules, UPDATE_DEBOUNCE);
338
+ });
339
+ const nonPreTransformers = ctx.uno.config.transformers?.filter((i) => i.enforce !== "pre");
340
+ if (nonPreTransformers?.length) {
341
+ console.warn(
342
+ // eslint-disable-next-line prefer-template
343
+ '[unocss] webpack integration only supports "pre" enforce transformers currently.the following transformers will be ignored\n' + nonPreTransformers.map((i) => ` - ${i.name}`).join("\n")
344
+ );
345
+ }
346
+ tasks.push(setupContentExtractor(ctx, typeof configOrPath === "object" && configOrPath?.watch));
347
+ const entries = /* @__PURE__ */ new Set();
348
+ const hashes = /* @__PURE__ */ new Map();
349
+ const plugin = {
350
+ name: "unocss:webpack",
351
+ enforce: "pre",
352
+ transformInclude(id) {
353
+ return filter("", id) && !id.endsWith(".html") && !RESOLVED_ID_RE.test(id);
354
+ },
355
+ async transform(code, id) {
356
+ const result = await applyTransformers(ctx, code, id, "pre");
357
+ if (isCssId(id))
358
+ return result;
359
+ if (result == null)
360
+ tasks.push(extract(code, id));
361
+ else
362
+ tasks.push(extract(result.code, id));
363
+ return result;
364
+ },
365
+ resolveId(id) {
366
+ const entry = resolveId(id);
367
+ if (entry === id)
368
+ return;
369
+ if (entry) {
370
+ let query = "";
371
+ const queryIndex = id.indexOf("?");
372
+ if (queryIndex >= 0)
373
+ query = id.slice(queryIndex);
374
+ entries.add(entry);
375
+ return entry + query;
376
+ }
377
+ },
378
+ loadInclude(id) {
379
+ const layer = getLayer(id);
380
+ return !!layer;
381
+ },
382
+ // serve the placeholders in virtual module
383
+ load(id) {
384
+ const layer = getLayer(id);
385
+ const hash = hashes.get(id);
386
+ if (layer)
387
+ return (hash ? getHashPlaceholder(hash) : "") + getLayerPlaceholder(layer);
388
+ },
389
+ webpack(compiler) {
390
+ compiler.hooks.beforeCompile.tapPromise(PLUGIN_NAME, async () => {
391
+ await ctx.ready;
392
+ });
393
+ compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
394
+ const optimizeAssetsHook = (
395
+ /* webpack 5 & 6 */
396
+ compilation.hooks.processAssets || /* webpack 4 */
397
+ compilation.hooks.optimizeAssets
398
+ );
399
+ optimizeAssetsHook.tapPromise(PLUGIN_NAME, async () => {
400
+ const files = Object.keys(compilation.assets);
401
+ await flushTasks();
402
+ const result = await uno.generate(tokens, { minify: true });
403
+ for (const file of files) {
404
+ if (file === "*")
405
+ return;
406
+ let code = compilation.assets[file].source().toString();
407
+ let escapeCss;
408
+ let replaced = false;
409
+ code = code.replace(HASH_PLACEHOLDER_RE, "");
410
+ code = code.replace(LAYER_PLACEHOLDER_RE, (_, layer, escapeView) => {
411
+ replaced = true;
412
+ const css = layer.trim() === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
413
+ escapeCss = escapeCss ?? getCssEscaperForJsContent(escapeView.trim());
414
+ return escapeCss(css);
415
+ });
416
+ if (replaced)
417
+ compilation.assets[file] = new WebpackSources__default.SourceMapSource(code, file, compilation.assets[file].map());
418
+ }
419
+ });
420
+ });
421
+ },
422
+ get rspack() {
423
+ return this.webpack;
424
+ }
425
+ };
426
+ let lastTokenSize = tokens.size;
427
+ async function updateModules() {
428
+ if (!plugin.__vfsModules)
429
+ return;
430
+ await flushTasks();
431
+ const result = await uno.generate(tokens);
432
+ if (lastTokenSize === tokens.size)
433
+ return;
434
+ lastTokenSize = tokens.size;
435
+ Array.from(plugin.__vfsModules).forEach((id) => {
436
+ let path = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
437
+ path = normalizeAbsolutePath(path);
438
+ const layer = resolveLayer(path);
439
+ if (!layer)
440
+ return;
441
+ const code = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
442
+ const hash = getHash(code);
443
+ hashes.set(path, hash);
444
+ plugin.__vfs.writeModule(id, code);
445
+ });
446
+ }
447
+ return plugin;
448
+ });
449
+ }
450
+ function getLayer(id) {
451
+ let layer = resolveLayer(getPath(id));
452
+ if (!layer) {
453
+ const entry = resolveId(id);
454
+ if (entry)
455
+ layer = resolveLayer(entry);
456
+ }
457
+ return layer;
458
+ }
459
+ function normalizeAbsolutePath(path) {
460
+ if (node_path.isAbsolute(path))
461
+ return node_path.normalize(path);
462
+ else
463
+ return path;
464
+ }
465
+
466
+ exports.unplugin = unplugin;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/webpack",
3
3
  "type": "module",
4
- "version": "0.62.3",
4
+ "version": "0.63.0",
5
5
  "description": "The Webpack plugin for UnoCSS",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -30,6 +30,16 @@
30
30
  "types": "./dist/index.d.cts",
31
31
  "default": "./dist/index.cjs"
32
32
  }
33
+ },
34
+ "./rspack": {
35
+ "import": {
36
+ "types": "./dist/rspack.d.mts",
37
+ "default": "./dist/rspack.mjs"
38
+ },
39
+ "require": {
40
+ "types": "./dist/rspack.d.cts",
41
+ "default": "./dist/rspack.cjs"
42
+ }
33
43
  }
34
44
  },
35
45
  "main": "./dist/index.cjs",
@@ -43,19 +53,19 @@
43
53
  },
44
54
  "dependencies": {
45
55
  "@ampproject/remapping": "^2.3.0",
46
- "@rollup/pluginutils": "^5.1.0",
56
+ "@rollup/pluginutils": "^5.1.2",
47
57
  "chokidar": "^3.6.0",
48
58
  "magic-string": "^0.30.11",
49
- "tinyglobby": "^0.2.5",
50
- "unplugin": "^1.12.2",
59
+ "tinyglobby": "^0.2.6",
60
+ "unplugin": "^1.14.1",
51
61
  "webpack-sources": "^3.2.3",
52
- "@unocss/core": "0.62.3",
53
- "@unocss/config": "0.62.3"
62
+ "@unocss/config": "0.63.0",
63
+ "@unocss/core": "0.63.0"
54
64
  },
55
65
  "devDependencies": {
56
66
  "@types/webpack": "^5.28.5",
57
67
  "@types/webpack-sources": "^3.2.3",
58
- "webpack": "^5.94.0"
68
+ "webpack": "^5.95.0"
59
69
  },
60
70
  "scripts": {
61
71
  "build": "unbuild",