Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 9243dd3

Browse files
committed
Add basic @layer support
1 parent 9d80c6d commit 9243dd3

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

src/index.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,14 @@ module.exports = (pluginOptions = {}) => {
866866
return context
867867
}
868868

869+
let foundTailwind = false
870+
let layerNodes = {
871+
base: null,
872+
components: null,
873+
utilities: null,
874+
screens: null,
875+
}
876+
869877
return {
870878
postcssPlugin: 'tailwindcss-jit',
871879
plugins: [
@@ -898,14 +906,6 @@ module.exports = (pluginOptions = {}) => {
898906
// a lot of work and bail early. Also we don't have to register our touch
899907
// file as a dependency since the output of this CSS does not depend on
900908
// the source of any templates. Think Vue <style> blocks for example.
901-
let foundTailwind = false
902-
let layerNodes = {
903-
base: null,
904-
components: null,
905-
utilities: null,
906-
screens: null,
907-
}
908-
909909
root.walkAtRules('tailwind', (rule) => {
910910
foundTailwind = true
911911

@@ -1006,22 +1006,18 @@ module.exports = (pluginOptions = {}) => {
10061006

10071007
if (layerNodes.base) {
10081008
layerNodes.base.before([...context.baseRules])
1009-
layerNodes.base.remove()
10101009
}
10111010

10121011
if (layerNodes.components) {
10131012
layerNodes.components.before([...componentNodes])
1014-
layerNodes.components.remove()
10151013
}
10161014

10171015
if (layerNodes.utilities) {
10181016
layerNodes.utilities.before([...utilityNodes])
1019-
layerNodes.utilities.remove()
10201017
}
10211018

10221019
if (layerNodes.screens) {
10231020
layerNodes.screens.before([...screenNodes])
1024-
layerNodes.screens.remove()
10251021
} else {
10261022
root.append([...screenNodes])
10271023
}
@@ -1040,6 +1036,33 @@ module.exports = (pluginOptions = {}) => {
10401036
// Clear the cache for the changed files
10411037
context.changedFiles.clear()
10421038
},
1039+
function (root) {
1040+
if (!foundTailwind) {
1041+
return root
1042+
}
1043+
1044+
root.walkAtRules('layer', (rule) => {
1045+
let layerName = rule.params
1046+
layerNodes[layerName].append(rule.nodes)
1047+
rule.remove()
1048+
})
1049+
1050+
for (let layerName in layerNodes) {
1051+
let node = layerNodes[layerName]
1052+
1053+
if (node === null) {
1054+
continue
1055+
}
1056+
1057+
if (node.nodes === undefined) {
1058+
node.remove()
1059+
continue
1060+
}
1061+
1062+
node.before(node.nodes)
1063+
node.remove()
1064+
}
1065+
},
10431066
function (root) {
10441067
let applyCandidates = new Set()
10451068

src/index.test.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ h1 {
115115
h1:first-child {
116116
margin-top: 0px;
117117
}
118+
div {
119+
background: #654321;
120+
}
118121
.container {
119122
width: 100%;
120123
}
@@ -198,6 +201,9 @@ h1:first-child {
198201
.aspect-h-4 {
199202
--tw-aspect-h: 4;
200203
}
204+
.custom-component {
205+
background: #123456;
206+
}
201207
.grid-cols-\[200px\,repeat\(auto-fill\,minmax\(15\%\,100px\)\)\,300px\] {
202208
grid-template-columns: 200px repeat(auto-fill, minmax(15%, 100px)) 300px;
203209
}
@@ -257,6 +263,9 @@ h1:first-child {
257263
.filter-grayscale {
258264
filter: grayscale(100%);
259265
}
266+
.custom-util {
267+
background: #abcdef;
268+
}
260269
.first\:pt-0:first-child {
261270
padding-top: 0px;
262271
}

src/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ test('it works', () => {
4646

4747
return run(
4848
`
49+
@layer utilities {
50+
.custom-util {
51+
background: #abcdef;
52+
}
53+
}
54+
@layer components {
55+
.custom-component {
56+
background: #123456;
57+
}
58+
}
59+
@layer base {
60+
div {
61+
background: #654321;
62+
}
63+
}
4964
.theme-test {
5065
font-family: theme('fontFamily.sans');
5166
color: theme('colors.blue.500');

0 commit comments

Comments
 (0)