@unocss/webpack 0.58.4 → 0.58.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.cjs CHANGED
@@ -3,13 +3,13 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const process$1 = require('node:process');
6
+ const node_path = require('node:path');
6
7
  const unplugin = require('unplugin');
7
8
  const WebpackSources = require('webpack-sources');
8
9
  const pluginutils = require('@rollup/pluginutils');
9
10
  const config = require('@unocss/config');
10
11
  const core = require('@unocss/core');
11
12
  const fs = require('node:fs/promises');
12
- const node_path = require('node:path');
13
13
  const fg = require('fast-glob');
14
14
  const MagicString = require('magic-string');
15
15
  const remapping = require('@ampproject/remapping');
@@ -276,7 +276,7 @@ const VIRTUAL_ENTRY_ALIAS = [
276
276
  ];
277
277
  const LAYER_MARK_ALL = "__ALL__";
278
278
  const RESOLVED_ID_WITH_QUERY_RE = /[\/\\]__uno(?:(_.*?))?\.css(\?.*)?$/;
279
- const RESOLVED_ID_RE = /[\/\\]__uno(?:(_.*?))?\.css$/;
279
+ const RESOLVED_ID_RE = /[\/\\]__uno(?:_(.*?))?\.css$/;
280
280
  function resolveId(id) {
281
281
  if (id.match(RESOLVED_ID_WITH_QUERY_RE))
282
282
  return id;
@@ -292,9 +292,22 @@ function resolveLayer(id) {
292
292
  if (match)
293
293
  return match[1] || LAYER_MARK_ALL;
294
294
  }
295
- const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
295
+ const LAYER_PLACEHOLDER_RE = /#--unocss--\s*{\s*layer\s*:\s*(.+?)\s*(?:;\s*escape-view\s*:\s*(.+?)\s*)?;?\s*}/g;
296
296
  function getLayerPlaceholder(layer) {
297
- return `#--unocss--{layer:${layer}}`;
297
+ return `#--unocss--{layer:${layer};escape-view:\\"\\'\\\`\\\\}`;
298
+ }
299
+ function getCssEscaperForJsContent(view) {
300
+ if (!view)
301
+ return (css) => css;
302
+ const prefix = {};
303
+ const escapeViewRe = /(\\*)\\(["'`\\])/g;
304
+ view.trim().replace(escapeViewRe, (_, bs, char) => {
305
+ prefix[char] = bs;
306
+ return "";
307
+ });
308
+ return (css) => css.replace(/["'`\\]/g, (v) => {
309
+ return (prefix[v] || "") + v;
310
+ });
298
311
  }
299
312
  const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*\\*"(.+?)\\*";?\s*}/g;
300
313
  function getHashPlaceholder(hash) {
@@ -377,7 +390,12 @@ function WebpackPlugin(configOrPath, defaults) {
377
390
  },
378
391
  webpack(compiler) {
379
392
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
380
- compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
393
+ const optimizeAssetsHook = (
394
+ /* webpack 5 & 6 */
395
+ compilation.hooks.processAssets || /* webpack 4 */
396
+ compilation.hooks.optimizeAssets
397
+ );
398
+ optimizeAssetsHook.tapPromise(PLUGIN_NAME, async () => {
381
399
  const files = Object.keys(compilation.assets);
382
400
  await flushTasks();
383
401
  const result = await uno.generate(tokens, { minify: true });
@@ -387,15 +405,11 @@ function WebpackPlugin(configOrPath, defaults) {
387
405
  let code = compilation.assets[file].source().toString();
388
406
  let replaced = false;
389
407
  code = code.replace(HASH_PLACEHOLDER_RE, "");
390
- code = code.replace(LAYER_PLACEHOLDER_RE, (_, quote, layer) => {
408
+ code = code.replace(LAYER_PLACEHOLDER_RE, (_, layer, escapeView) => {
391
409
  replaced = true;
392
410
  const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
393
- if (!quote)
394
- return css;
395
- let escaped = JSON.stringify(css).slice(1, -1);
396
- if (quote === '\\"')
397
- escaped = JSON.stringify(escaped).slice(1, -1);
398
- return quote + escaped;
411
+ const escapeCss = getCssEscaperForJsContent(escapeView);
412
+ return escapeCss(css);
399
413
  });
400
414
  if (replaced)
401
415
  compilation.assets[file] = new WebpackSources__default.RawSource(code);
@@ -414,7 +428,8 @@ function WebpackPlugin(configOrPath, defaults) {
414
428
  return;
415
429
  lastTokenSize = tokens.size;
416
430
  Array.from(plugin.__vfsModules).forEach((id) => {
417
- const path = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
431
+ let path = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
432
+ path = normalizeAbsolutePath(path);
418
433
  const layer = resolveLayer(path);
419
434
  if (!layer)
420
435
  return;
@@ -436,6 +451,12 @@ function getLayer(id) {
436
451
  }
437
452
  return layer;
438
453
  }
454
+ function normalizeAbsolutePath(path) {
455
+ if (node_path.isAbsolute(path))
456
+ return node_path.normalize(path);
457
+ else
458
+ return path;
459
+ }
439
460
 
440
461
  exports.default = WebpackPlugin;
441
462
  exports.defineConfig = defineConfig;
package/dist/index.mjs CHANGED
@@ -1,11 +1,11 @@
1
1
  import process$1 from 'node:process';
2
+ import { resolve, isAbsolute, normalize } from 'node:path';
2
3
  import { createUnplugin } from 'unplugin';
3
4
  import WebpackSources from 'webpack-sources';
4
5
  import { createFilter } from '@rollup/pluginutils';
5
6
  import { loadConfig } from '@unocss/config';
6
7
  import { cssIdRE, createGenerator, BetterMap } from '@unocss/core';
7
8
  import fs from 'node:fs/promises';
8
- import { resolve, isAbsolute } from 'node:path';
9
9
  import fg from 'fast-glob';
10
10
  import MagicString from 'magic-string';
11
11
  import remapping from '@ampproject/remapping';
@@ -263,7 +263,7 @@ const VIRTUAL_ENTRY_ALIAS = [
263
263
  ];
264
264
  const LAYER_MARK_ALL = "__ALL__";
265
265
  const RESOLVED_ID_WITH_QUERY_RE = /[\/\\]__uno(?:(_.*?))?\.css(\?.*)?$/;
266
- const RESOLVED_ID_RE = /[\/\\]__uno(?:(_.*?))?\.css$/;
266
+ const RESOLVED_ID_RE = /[\/\\]__uno(?:_(.*?))?\.css$/;
267
267
  function resolveId(id) {
268
268
  if (id.match(RESOLVED_ID_WITH_QUERY_RE))
269
269
  return id;
@@ -279,9 +279,22 @@ function resolveLayer(id) {
279
279
  if (match)
280
280
  return match[1] || LAYER_MARK_ALL;
281
281
  }
282
- const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g;
282
+ const LAYER_PLACEHOLDER_RE = /#--unocss--\s*{\s*layer\s*:\s*(.+?)\s*(?:;\s*escape-view\s*:\s*(.+?)\s*)?;?\s*}/g;
283
283
  function getLayerPlaceholder(layer) {
284
- return `#--unocss--{layer:${layer}}`;
284
+ return `#--unocss--{layer:${layer};escape-view:\\"\\'\\\`\\\\}`;
285
+ }
286
+ function getCssEscaperForJsContent(view) {
287
+ if (!view)
288
+ return (css) => css;
289
+ const prefix = {};
290
+ const escapeViewRe = /(\\*)\\(["'`\\])/g;
291
+ view.trim().replace(escapeViewRe, (_, bs, char) => {
292
+ prefix[char] = bs;
293
+ return "";
294
+ });
295
+ return (css) => css.replace(/["'`\\]/g, (v) => {
296
+ return (prefix[v] || "") + v;
297
+ });
285
298
  }
286
299
  const HASH_PLACEHOLDER_RE = /#--unocss-hash--\s*{\s*content\s*:\s*\\*"(.+?)\\*";?\s*}/g;
287
300
  function getHashPlaceholder(hash) {
@@ -364,7 +377,12 @@ function WebpackPlugin(configOrPath, defaults) {
364
377
  },
365
378
  webpack(compiler) {
366
379
  compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
367
- compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, async () => {
380
+ const optimizeAssetsHook = (
381
+ /* webpack 5 & 6 */
382
+ compilation.hooks.processAssets || /* webpack 4 */
383
+ compilation.hooks.optimizeAssets
384
+ );
385
+ optimizeAssetsHook.tapPromise(PLUGIN_NAME, async () => {
368
386
  const files = Object.keys(compilation.assets);
369
387
  await flushTasks();
370
388
  const result = await uno.generate(tokens, { minify: true });
@@ -374,15 +392,11 @@ function WebpackPlugin(configOrPath, defaults) {
374
392
  let code = compilation.assets[file].source().toString();
375
393
  let replaced = false;
376
394
  code = code.replace(HASH_PLACEHOLDER_RE, "");
377
- code = code.replace(LAYER_PLACEHOLDER_RE, (_, quote, layer) => {
395
+ code = code.replace(LAYER_PLACEHOLDER_RE, (_, layer, escapeView) => {
378
396
  replaced = true;
379
397
  const css = layer === LAYER_MARK_ALL ? result.getLayers(void 0, Array.from(entries).map((i) => resolveLayer(i)).filter((i) => !!i)) : result.getLayer(layer) || "";
380
- if (!quote)
381
- return css;
382
- let escaped = JSON.stringify(css).slice(1, -1);
383
- if (quote === '\\"')
384
- escaped = JSON.stringify(escaped).slice(1, -1);
385
- return quote + escaped;
398
+ const escapeCss = getCssEscaperForJsContent(escapeView);
399
+ return escapeCss(css);
386
400
  });
387
401
  if (replaced)
388
402
  compilation.assets[file] = new WebpackSources.RawSource(code);
@@ -401,7 +415,8 @@ function WebpackPlugin(configOrPath, defaults) {
401
415
  return;
402
416
  lastTokenSize = tokens.size;
403
417
  Array.from(plugin.__vfsModules).forEach((id) => {
404
- const path = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
418
+ let path = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
419
+ path = normalizeAbsolutePath(path);
405
420
  const layer = resolveLayer(path);
406
421
  if (!layer)
407
422
  return;
@@ -423,5 +438,11 @@ function getLayer(id) {
423
438
  }
424
439
  return layer;
425
440
  }
441
+ function normalizeAbsolutePath(path) {
442
+ if (isAbsolute(path))
443
+ return normalize(path);
444
+ else
445
+ return path;
446
+ }
426
447
 
427
448
  export { WebpackPlugin as default, defineConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/webpack",
3
- "version": "0.58.4",
3
+ "version": "0.58.6",
4
4
  "description": "The Webpack plugin for UnoCSS",
5
5
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
@@ -36,20 +36,20 @@
36
36
  "webpack": "^4 || ^5"
37
37
  },
38
38
  "dependencies": {
39
- "@ampproject/remapping": "^2.2.1",
39
+ "@ampproject/remapping": "^2.3.0",
40
40
  "@rollup/pluginutils": "^5.1.0",
41
- "chokidar": "^3.5.3",
41
+ "chokidar": "^3.6.0",
42
42
  "fast-glob": "^3.3.2",
43
- "magic-string": "^0.30.5",
44
- "unplugin": "^1.6.0",
43
+ "magic-string": "^0.30.8",
44
+ "unplugin": "^1.10.0",
45
45
  "webpack-sources": "^3.2.3",
46
- "@unocss/config": "0.58.4",
47
- "@unocss/core": "0.58.4"
46
+ "@unocss/config": "0.58.6",
47
+ "@unocss/core": "0.58.6"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@types/webpack": "^5.28.5",
51
51
  "@types/webpack-sources": "^3.2.3",
52
- "webpack": "^5.90.0"
52
+ "webpack": "^5.90.3"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "unbuild",