@unocss/transformer-directives 0.26.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 hannoeru
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @unocss/transformer-directives
2
+
3
+ UnoCSS transformer for `@apply` directive
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i -D @unocss/transformer-directives
9
+ ```
10
+
11
+ ```ts
12
+ import Unocss from 'unocss/vite'
13
+ import transformerDirective from '@unocss/transformer-directives'
14
+
15
+ Unocss({
16
+ transformers: [
17
+ transformerDirective(),
18
+ ]
19
+ })
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Currently only `@apply` is supported.
25
+
26
+ ## License
27
+
28
+ MIT License © 2022 [hannoeru](https://github.com/hannoeru)
package/dist/index.cjs ADDED
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const core = require('@unocss/core');
6
+ const cssTree = require('css-tree');
7
+
8
+ function transformerDirectives() {
9
+ return {
10
+ name: "css-directive",
11
+ enforce: "pre",
12
+ idFilter: (id) => id.endsWith(".css"),
13
+ transform: (code, id, ctx) => {
14
+ return transformDirectives(code, ctx.uno, id);
15
+ }
16
+ };
17
+ }
18
+ async function transformDirectives(css, uno, filename) {
19
+ if (!css.includes("@apply"))
20
+ return css;
21
+ const ast = cssTree.parse(css, {
22
+ parseAtrulePrelude: false,
23
+ positions: true,
24
+ filename
25
+ });
26
+ if (ast.type !== "StyleSheet")
27
+ return css;
28
+ const stack = [];
29
+ const processNode = async (node, item, list) => {
30
+ if (node.type !== "Rule")
31
+ return;
32
+ await Promise.all(node.block.children.map(async (childNode, childItem) => {
33
+ if (!(childNode.type === "Atrule" && childNode.name === "apply" && childNode.prelude))
34
+ return;
35
+ if (childNode.prelude.type !== "Raw")
36
+ return;
37
+ const classNames = core.expandVariantGroup(childNode.prelude.value).split(/\s+/g);
38
+ const utils = (await Promise.all(classNames.map((i) => uno.parseToken(i, "-")))).filter(core.notNull).flat().sort((a, b) => a[0] - b[0]).reduce((acc, item2) => {
39
+ const target = acc.find((i) => i[1] === item2[1] && i[3] === item2[3]);
40
+ if (target)
41
+ target[2] += item2[2];
42
+ else
43
+ acc.push([...item2]);
44
+ return acc;
45
+ }, []);
46
+ if (!utils.length)
47
+ return;
48
+ const parentSelector = cssTree.generate(node.prelude);
49
+ for (const i of utils) {
50
+ const [, selector, body, parent] = i;
51
+ if (parent) {
52
+ const newNodeCss = `${parent}{${parentSelector}{${body}}}`;
53
+ const insertNodeAst = cssTree.parse(newNodeCss);
54
+ list.insertList(insertNodeAst.children, item);
55
+ } else if (selector && selector !== ".\\-") {
56
+ const pseudoClassSelectors = cssTree.parse(selector, {
57
+ context: "selector"
58
+ }).children.filter((i2) => i2.type === "PseudoClassSelector");
59
+ const parentSelectorAst = cssTree.clone(node.prelude);
60
+ parentSelectorAst.children.forEach((i2) => {
61
+ if (i2.type === "Selector")
62
+ i2.children.appendList(pseudoClassSelectors.copy());
63
+ });
64
+ const newNodeCss = `${cssTree.generate(parentSelectorAst)}{${body}}`;
65
+ const insertNodeAst = cssTree.parse(newNodeCss);
66
+ list.insertList(insertNodeAst.children, item);
67
+ } else {
68
+ const rules = new cssTree.List().fromArray(body.replace(/;$/, "").split(";")).map((i2) => cssTree.parse(i2, {
69
+ context: "declaration"
70
+ }));
71
+ node.block.children.insertList(rules, childItem);
72
+ }
73
+ }
74
+ node.block.children.remove(childItem);
75
+ }).toArray());
76
+ };
77
+ cssTree.walk(ast, (...args) => stack.push(processNode(...args)));
78
+ await Promise.all(stack);
79
+ return cssTree.generate(ast);
80
+ }
81
+
82
+ exports["default"] = transformerDirectives;
83
+ exports.transformDirectives = transformDirectives;
@@ -0,0 +1,6 @@
1
+ import { SourceCodeTransformer, UnoGenerator } from '@unocss/core';
2
+
3
+ declare function transformerDirectives(): SourceCodeTransformer;
4
+ declare function transformDirectives(css: string, uno: UnoGenerator, filename?: string): Promise<string>;
5
+
6
+ export { transformerDirectives as default, transformDirectives };
package/dist/index.mjs ADDED
@@ -0,0 +1,78 @@
1
+ import { expandVariantGroup, notNull } from '@unocss/core';
2
+ import { parse, walk, generate, clone, List } from 'css-tree';
3
+
4
+ function transformerDirectives() {
5
+ return {
6
+ name: "css-directive",
7
+ enforce: "pre",
8
+ idFilter: (id) => id.endsWith(".css"),
9
+ transform: (code, id, ctx) => {
10
+ return transformDirectives(code, ctx.uno, id);
11
+ }
12
+ };
13
+ }
14
+ async function transformDirectives(css, uno, filename) {
15
+ if (!css.includes("@apply"))
16
+ return css;
17
+ const ast = parse(css, {
18
+ parseAtrulePrelude: false,
19
+ positions: true,
20
+ filename
21
+ });
22
+ if (ast.type !== "StyleSheet")
23
+ return css;
24
+ const stack = [];
25
+ const processNode = async (node, item, list) => {
26
+ if (node.type !== "Rule")
27
+ return;
28
+ await Promise.all(node.block.children.map(async (childNode, childItem) => {
29
+ if (!(childNode.type === "Atrule" && childNode.name === "apply" && childNode.prelude))
30
+ return;
31
+ if (childNode.prelude.type !== "Raw")
32
+ return;
33
+ const classNames = expandVariantGroup(childNode.prelude.value).split(/\s+/g);
34
+ const utils = (await Promise.all(classNames.map((i) => uno.parseToken(i, "-")))).filter(notNull).flat().sort((a, b) => a[0] - b[0]).reduce((acc, item2) => {
35
+ const target = acc.find((i) => i[1] === item2[1] && i[3] === item2[3]);
36
+ if (target)
37
+ target[2] += item2[2];
38
+ else
39
+ acc.push([...item2]);
40
+ return acc;
41
+ }, []);
42
+ if (!utils.length)
43
+ return;
44
+ const parentSelector = generate(node.prelude);
45
+ for (const i of utils) {
46
+ const [, selector, body, parent] = i;
47
+ if (parent) {
48
+ const newNodeCss = `${parent}{${parentSelector}{${body}}}`;
49
+ const insertNodeAst = parse(newNodeCss);
50
+ list.insertList(insertNodeAst.children, item);
51
+ } else if (selector && selector !== ".\\-") {
52
+ const pseudoClassSelectors = parse(selector, {
53
+ context: "selector"
54
+ }).children.filter((i2) => i2.type === "PseudoClassSelector");
55
+ const parentSelectorAst = clone(node.prelude);
56
+ parentSelectorAst.children.forEach((i2) => {
57
+ if (i2.type === "Selector")
58
+ i2.children.appendList(pseudoClassSelectors.copy());
59
+ });
60
+ const newNodeCss = `${generate(parentSelectorAst)}{${body}}`;
61
+ const insertNodeAst = parse(newNodeCss);
62
+ list.insertList(insertNodeAst.children, item);
63
+ } else {
64
+ const rules = new List().fromArray(body.replace(/;$/, "").split(";")).map((i2) => parse(i2, {
65
+ context: "declaration"
66
+ }));
67
+ node.block.children.insertList(rules, childItem);
68
+ }
69
+ }
70
+ node.block.children.remove(childItem);
71
+ }).toArray());
72
+ };
73
+ walk(ast, (...args) => stack.push(processNode(...args)));
74
+ await Promise.all(stack);
75
+ return generate(ast);
76
+ }
77
+
78
+ export { transformerDirectives as default, transformDirectives };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@unocss/transformer-directives",
3
+ "version": "0.26.0",
4
+ "description": "UnoCSS transformer for `@apply` directive",
5
+ "keywords": [
6
+ "unocss",
7
+ "unocss-transformer"
8
+ ],
9
+ "homepage": "https://github.com/antfu/unocss/tree/main/packages/transformer-directives#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/antfu/unocss/issues"
12
+ },
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/antfu/unocss.git",
17
+ "directory": "packages/transformer-directives"
18
+ },
19
+ "author": "hannoeru <me@hanlee.co>",
20
+ "sideEffects": false,
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "require": "./dist/index.cjs",
27
+ "import": "./dist/index.mjs"
28
+ }
29
+ },
30
+ "main": "./dist/index.cjs",
31
+ "module": "./dist/index.mjs",
32
+ "types": "./dist/index.d.ts",
33
+ "peerDependencies": {
34
+ "@unocss/core": "0.26.0"
35
+ },
36
+ "dependencies": {
37
+ "css-tree": "^2.0.4"
38
+ },
39
+ "devDependencies": {
40
+ "@unocss/core": "0.26.0"
41
+ },
42
+ "scripts": {
43
+ "build": "unbuild",
44
+ "stub": "unbuild --stub"
45
+ }
46
+ }