plugin.js
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "nesting", {
enumerable: true,
get: ()=>nesting
});
const _postcss = /*#__PURE__*/ _interopRequireDefault(require("postcss"));
const _postcssNested = /*#__PURE__*/ _interopRequireDefault(require("postcss-nested"));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function nesting(opts = _postcssNested.default) {
return (root, result)=>{
root.walkAtRules("screen", (rule)=>{
rule.name = "media";
rule.params = `screen(${rule.params})`;
});
root.walkAtRules("apply", (rule)=>{
rule.before(_postcss.default.decl({
prop: "__apply",
value: rule.params,
source: rule.source
}));
rule.remove();
});
let plugin = (()=>{
var ref;
if (typeof opts === "function" || typeof opts === "object" && (opts === null || opts === void 0 ? void 0 : (ref = opts.hasOwnProperty) === null || ref === void 0 ? void 0 : ref.call(opts, "postcssPlugin"))) {
return opts;
}
if (typeof opts === "string") {
return require(opts);
}
if (Object.keys(opts).length <= 0) {
return _postcssNested.default;
}
throw new Error("tailwindcss/nesting should be loaded with a nesting plugin.");
})();
(0, _postcss.default)([
plugin
]).process(root, result.opts).sync();
root.walkDecls("__apply", (decl)=>{
decl.before(_postcss.default.atRule({
name: "apply",
params: decl.value,
source: decl.source
}));
decl.remove();
});
/**
* Use a private PostCSS API to remove the "clean" flag from the entire AST.
* This is done because running process() on the AST will set the "clean"
* flag on all nodes, which we don't want.
*
* This causes downstream plugins using the visitor API to be skipped.
*
* This is guarded because the PostCSS API is not public
* and may change in future versions of PostCSS.
*
* See https://github.com/postcss/postcss/issues/1712 for more details
*
* @param {import('postcss').Node} node
*/ function markDirty(node) {
if (!("markDirty" in node)) {
return;
}
// Traverse the tree down to the leaf nodes
if (node.nodes) {
node.nodes.forEach((n)=>markDirty(n));
}
// If it's a leaf node mark it as dirty
// We do this here because marking a node as dirty
// will walk up the tree and mark all parents as dirty
// resulting in a lot of unnecessary work if we did this
// for every single node
if (!node.nodes) {
node.markDirty();
}
}
markDirty(root);
return root;
};
}