@unocss/webpack 0.58.5 → 0.58.7

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
@@ -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);
package/dist/index.mjs CHANGED
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/webpack",
3
- "version": "0.58.5",
3
+ "version": "0.58.7",
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.6",
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.5",
47
- "@unocss/core": "0.58.5"
46
+ "@unocss/config": "0.58.7",
47
+ "@unocss/core": "0.58.7"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@types/webpack": "^5.28.5",
51
51
  "@types/webpack-sources": "^3.2.3",
52
- "webpack": "^5.90.1"
52
+ "webpack": "^5.91.0"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "unbuild",