@unocss/vite 0.16.1 → 0.17.2

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
@@ -43,6 +43,7 @@ function getHashPlaceholder(hash) {
43
43
  }
44
44
 
45
45
  const INCLUDE_COMMENT = "@unocss-include";
46
+ const CSS_PLACEHOLDER = "@unocss-placeholder";
46
47
 
47
48
  function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
48
49
  const loadConfig = config.createConfigLoader(configOrPath, extraConfigSources);
@@ -76,7 +77,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
76
77
  invalidate();
77
78
  }
78
79
  const filter = (code, id) => {
79
- return code.includes(INCLUDE_COMMENT) || rollupFilter(id);
80
+ return code.includes(INCLUDE_COMMENT) || code.includes(CSS_PLACEHOLDER) || rollupFilter(id);
80
81
  };
81
82
  async function getConfig() {
82
83
  await ready;
@@ -461,6 +462,97 @@ function VueScopedPlugin({ uno, ready }) {
461
462
  };
462
463
  }
463
464
 
465
+ function ShadowDomModuleModePlugin({ uno }) {
466
+ const partExtractorRegex = /^part-\[(.+)]:/;
467
+ const nameRegexp = /<([^\s^!>]+)\s*([^>]*)>/;
468
+ const checkElement = (useParts, idxResolver, element) => {
469
+ if (!element)
470
+ return null;
471
+ const applyParts = useParts.filter((p) => element[2].includes(p.rule));
472
+ if (applyParts.length === 0)
473
+ return null;
474
+ const name = element[1];
475
+ const idx = idxResolver(name);
476
+ return {
477
+ name,
478
+ entries: applyParts.map(({ rule, part }) => [
479
+ `.${rule.replace(/[:[\]]/g, "\\$&")}::part(${part})`,
480
+ `${name}:nth-of-type(${idx})::part(${part})`
481
+ ])
482
+ };
483
+ };
484
+ const idxMapFactory = () => {
485
+ const elementIdxMap = /* @__PURE__ */ new Map();
486
+ return {
487
+ idxResolver: (name) => {
488
+ let idx = elementIdxMap.get(name);
489
+ if (!idx) {
490
+ idx = 1;
491
+ elementIdxMap.set(name, idx);
492
+ }
493
+ return idx;
494
+ },
495
+ incrementIdx: (name) => {
496
+ elementIdxMap.set(name, elementIdxMap.get(name) + 1);
497
+ }
498
+ };
499
+ };
500
+ const transformWebComponent = async (code) => {
501
+ if (!code.match(CSS_PLACEHOLDER))
502
+ return code;
503
+ let { css, matched } = await uno.generate(code, { preflights: false });
504
+ if (css && matched) {
505
+ const useParts = Array.from(matched).reduce((acc, rule) => {
506
+ const matcher = rule.match(partExtractorRegex);
507
+ if (matcher)
508
+ acc.push({ part: matcher[1], rule });
509
+ return acc;
510
+ }, new Array());
511
+ if (useParts.length > 0) {
512
+ let useCode = code;
513
+ let element;
514
+ const partsToApply = /* @__PURE__ */ new Map();
515
+ const { idxResolver, incrementIdx } = idxMapFactory();
516
+ while (element = nameRegexp.exec(useCode)) {
517
+ const result = checkElement(useParts, idxResolver, element);
518
+ if (result) {
519
+ result.entries.forEach(([name, replacement]) => {
520
+ let list = partsToApply.get(name);
521
+ if (!list) {
522
+ list = [];
523
+ partsToApply.set(name, list);
524
+ }
525
+ list.push(replacement);
526
+ });
527
+ incrementIdx(result.name);
528
+ }
529
+ useCode = useCode.slice(element[0].length + 1);
530
+ }
531
+ if (partsToApply.size > 0) {
532
+ css = Array.from(partsToApply.entries()).reduce((k, [r, name]) => {
533
+ return k.replace(r, name.join(",\n"));
534
+ }, css);
535
+ }
536
+ }
537
+ }
538
+ return code.replace(CSS_PLACEHOLDER, css || "");
539
+ };
540
+ return {
541
+ name: "unocss:shadow-dom",
542
+ enforce: "pre",
543
+ async transform(code) {
544
+ return transformWebComponent(code);
545
+ },
546
+ handleHotUpdate(ctx) {
547
+ const read = ctx.read;
548
+ ctx.read = async () => {
549
+ const code = await read();
550
+ return await transformWebComponent(code);
551
+ };
552
+ }
553
+ };
554
+ }
555
+
464
556
  function ConfigHMRPlugin(ctx) {
465
557
  const { ready, uno } = ctx;
466
558
  return {
@@ -503,6 +595,8 @@ function UnocssPlugin(configOrPath, defaults = {}) {
503
595
  plugins.push(PerModuleModePlugin(ctx));
504
596
  } else if (mode === "vue-scoped") {
505
597
  plugins.push(VueScopedPlugin(ctx));
598
+ } else if (mode === "shadow-dom") {
599
+ plugins.push(ShadowDomModuleModePlugin(ctx));
506
600
  } else if (mode === "global") {
507
601
  plugins.push(...GlobalModePlugin(ctx));
508
602
  } else if (mode === "dist-chunk") {
package/dist/index.d.ts CHANGED
@@ -20,7 +20,7 @@ interface VitePluginConfig<Theme extends {} = {}> extends UserConfig<Theme> {
20
20
  *
21
21
  * @default 'global'
22
22
  */
23
- mode?: 'global' | 'per-module' | 'vue-scoped' | 'dist-chunk';
23
+ mode?: 'global' | 'per-module' | 'vue-scoped' | 'dist-chunk' | 'shadow-dom';
24
24
  }
25
25
 
26
26
  interface UnocssPluginContext<Config extends UserConfig = UserConfig> {
package/dist/index.mjs CHANGED
@@ -35,6 +35,7 @@ function getHashPlaceholder(hash) {
35
35
  }
36
36
 
37
37
  const INCLUDE_COMMENT = "@unocss-include";
38
+ const CSS_PLACEHOLDER = "@unocss-placeholder";
38
39
 
39
40
  function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
40
41
  const loadConfig = createConfigLoader(configOrPath, extraConfigSources);
@@ -68,7 +69,7 @@ function createContext(configOrPath, defaults = {}, extraConfigSources = []) {
68
69
  invalidate();
69
70
  }
70
71
  const filter = (code, id) => {
71
- return code.includes(INCLUDE_COMMENT) || rollupFilter(id);
72
+ return code.includes(INCLUDE_COMMENT) || code.includes(CSS_PLACEHOLDER) || rollupFilter(id);
72
73
  };
73
74
  async function getConfig() {
74
75
  await ready;
@@ -453,6 +454,97 @@ function VueScopedPlugin({ uno, ready }) {
453
454
  };
454
455
  }
455
456
 
457
+ function ShadowDomModuleModePlugin({ uno }) {
458
+ const partExtractorRegex = /^part-\[(.+)]:/;
459
+ const nameRegexp = /<([^\s^!>]+)\s*([^>]*)>/;
460
+ const checkElement = (useParts, idxResolver, element) => {
461
+ if (!element)
462
+ return null;
463
+ const applyParts = useParts.filter((p) => element[2].includes(p.rule));
464
+ if (applyParts.length === 0)
465
+ return null;
466
+ const name = element[1];
467
+ const idx = idxResolver(name);
468
+ return {
469
+ name,
470
+ entries: applyParts.map(({ rule, part }) => [
471
+ `.${rule.replace(/[:[\]]/g, "\\$&")}::part(${part})`,
472
+ `${name}:nth-of-type(${idx})::part(${part})`
473
+ ])
474
+ };
475
+ };
476
+ const idxMapFactory = () => {
477
+ const elementIdxMap = /* @__PURE__ */ new Map();
478
+ return {
479
+ idxResolver: (name) => {
480
+ let idx = elementIdxMap.get(name);
481
+ if (!idx) {
482
+ idx = 1;
483
+ elementIdxMap.set(name, idx);
484
+ }
485
+ return idx;
486
+ },
487
+ incrementIdx: (name) => {
488
+ elementIdxMap.set(name, elementIdxMap.get(name) + 1);
489
+ }
490
+ };
491
+ };
492
+ const transformWebComponent = async (code) => {
493
+ if (!code.match(CSS_PLACEHOLDER))
494
+ return code;
495
+ let { css, matched } = await uno.generate(code, { preflights: false });
496
+ if (css && matched) {
497
+ const useParts = Array.from(matched).reduce((acc, rule) => {
498
+ const matcher = rule.match(partExtractorRegex);
499
+ if (matcher)
500
+ acc.push({ part: matcher[1], rule });
501
+ return acc;
502
+ }, new Array());
503
+ if (useParts.length > 0) {
504
+ let useCode = code;
505
+ let element;
506
+ const partsToApply = /* @__PURE__ */ new Map();
507
+ const { idxResolver, incrementIdx } = idxMapFactory();
508
+ while (element = nameRegexp.exec(useCode)) {
509
+ const result = checkElement(useParts, idxResolver, element);
510
+ if (result) {
511
+ result.entries.forEach(([name, replacement]) => {
512
+ let list = partsToApply.get(name);
513
+ if (!list) {
514
+ list = [];
515
+ partsToApply.set(name, list);
516
+ }
517
+ list.push(replacement);
518
+ });
519
+ incrementIdx(result.name);
520
+ }
521
+ useCode = useCode.slice(element[0].length + 1);
522
+ }
523
+ if (partsToApply.size > 0) {
524
+ css = Array.from(partsToApply.entries()).reduce((k, [r, name]) => {
525
+ return k.replace(r, name.join(",\n"));
526
+ }, css);
527
+ }
528
+ }
529
+ }
530
+ return code.replace(CSS_PLACEHOLDER, css || "");
531
+ };
532
+ return {
533
+ name: "unocss:shadow-dom",
534
+ enforce: "pre",
535
+ async transform(code) {
536
+ return transformWebComponent(code);
537
+ },
538
+ handleHotUpdate(ctx) {
539
+ const read = ctx.read;
540
+ ctx.read = async () => {
541
+ const code = await read();
542
+ return await transformWebComponent(code);
543
+ };
544
+ }
545
+ };
546
+ }
547
+
456
548
  function ConfigHMRPlugin(ctx) {
457
549
  const { ready, uno } = ctx;
458
550
  return {
@@ -495,6 +587,8 @@ function UnocssPlugin(configOrPath, defaults = {}) {
495
587
  plugins.push(PerModuleModePlugin(ctx));
496
588
  } else if (mode === "vue-scoped") {
497
589
  plugins.push(VueScopedPlugin(ctx));
590
+ } else if (mode === "shadow-dom") {
591
+ plugins.push(ShadowDomModuleModePlugin(ctx));
498
592
  } else if (mode === "global") {
499
593
  plugins.push(...GlobalModePlugin(ctx));
500
594
  } else if (mode === "dist-chunk") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unocss/vite",
3
- "version": "0.16.1",
3
+ "version": "0.17.2",
4
4
  "description": "The Vite plugin for UnoCSS",
5
5
  "keywords": [
6
6
  "unocss",
@@ -34,14 +34,14 @@
34
34
  "dist"
35
35
  ],
36
36
  "dependencies": {
37
- "@rollup/pluginutils": "^4.1.1",
38
- "@unocss/config": "0.16.1",
39
- "@unocss/core": "0.16.1",
40
- "@unocss/inspector": "0.16.1",
41
- "@unocss/scope": "0.16.1"
37
+ "@rollup/pluginutils": "^4.1.2",
38
+ "@unocss/config": "0.17.2",
39
+ "@unocss/core": "0.17.2",
40
+ "@unocss/inspector": "0.17.2",
41
+ "@unocss/scope": "0.17.2"
42
42
  },
43
43
  "devDependencies": {
44
- "vite": "^2.7.1"
44
+ "vite": "^2.7.4"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "unbuild",