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

Commit aa912ad

Browse files
authored
Direct support for @import 'tailwindcss/{layer}' syntax (#145)
* Direct support for @import 'tailwindcss/{layer}' syntax * Ensure imports are rewritten before setting up context * Avoid executing unnecessary conditionals
1 parent 3c8284d commit aa912ad

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const postcss = require('postcss')
33
const evaluateTailwindFunctions = require('tailwindcss/lib/lib/evaluateTailwindFunctions').default
44
const substituteScreenAtRules = require('tailwindcss/lib/lib/substituteScreenAtRules').default
55

6+
const rewriteTailwindImports = require('./lib/rewriteTailwindImports')
67
const setupContext = require('./lib/setupContext')
78
const removeLayerAtRules = require('./lib/removeLayerAtRules')
89
const expandTailwindAtRules = require('./lib/expandTailwindAtRules')
@@ -31,6 +32,8 @@ module.exports = (configOrPath = {}) => {
3132
})
3233
}
3334

35+
rewriteTailwindImports(root)
36+
3437
let context = setupContext(configOrPath)(result, root)
3538

3639
if (context.configPath !== null) {

src/lib/rewriteTailwindImports.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function rewriteTailwindImports(root) {
2+
root.walkAtRules('import', (atRule) => {
3+
if (atRule.params === '"tailwindcss/base"' || atRule.params === "'tailwindcss/base'") {
4+
atRule.name = 'tailwind'
5+
atRule.params = 'base'
6+
} else if (
7+
atRule.params === '"tailwindcss/components"' ||
8+
atRule.params === "'tailwindcss/components'"
9+
) {
10+
atRule.name = 'tailwind'
11+
atRule.params = 'components'
12+
} else if (
13+
atRule.params === '"tailwindcss/utilities"' ||
14+
atRule.params === "'tailwindcss/utilities'"
15+
) {
16+
atRule.name = 'tailwind'
17+
atRule.params = 'utilities'
18+
} else if (
19+
atRule.params === '"tailwindcss/screens"' ||
20+
atRule.params === "'tailwindcss/screens'"
21+
) {
22+
atRule.name = 'tailwind'
23+
atRule.params = 'screens'
24+
}
25+
})
26+
}
27+
28+
module.exports = rewriteTailwindImports

tests/12-import-syntax.test.css

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
* {
2+
--tw-shadow: 0 0 #0000;
3+
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
4+
--tw-ring-offset-width: 0px;
5+
--tw-ring-offset-color: #fff;
6+
--tw-ring-color: rgba(59, 130, 246, 0.5);
7+
--tw-ring-offset-shadow: 0 0 #0000;
8+
--tw-ring-shadow: 0 0 #0000;
9+
}
10+
h1 {
11+
font-size: 32px;
12+
}
13+
.container {
14+
width: 100%;
15+
}
16+
@media (min-width: 640px) {
17+
.container {
18+
max-width: 640px;
19+
}
20+
}
21+
@media (min-width: 768px) {
22+
.container {
23+
max-width: 768px;
24+
}
25+
}
26+
@media (min-width: 1024px) {
27+
.container {
28+
max-width: 1024px;
29+
}
30+
}
31+
@media (min-width: 1280px) {
32+
.container {
33+
max-width: 1280px;
34+
}
35+
}
36+
@media (min-width: 1536px) {
37+
.container {
38+
max-width: 1536px;
39+
}
40+
}
41+
.mt-6 {
42+
margin-top: 1.5rem;
43+
}
44+
.bg-black {
45+
--tw-bg-opacity: 1;
46+
background-color: rgba(0, 0, 0, var(--tw-bg-opacity));
47+
}
48+
@media (min-width: 768px) {
49+
.md\:hover\:text-center:hover {
50+
text-align: center;
51+
}
52+
}

tests/12-import-syntax.test.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>Hello world!</h1>
2+
<div class="container"></div>
3+
<div class="mt-6"></div>
4+
<div class="bg-black"></div>
5+
<div class="md:hover:text-center"></div>

tests/12-import-syntax.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const postcss = require('postcss')
2+
const tailwind = require('../src/index.js')
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
function run(input, config = {}) {
7+
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
8+
}
9+
10+
test('using @import instead of @tailwind', () => {
11+
let config = {
12+
purge: [path.resolve(__dirname, './12-import-syntax.test.html')],
13+
corePlugins: { preflight: false },
14+
theme: {},
15+
plugins: [
16+
function ({ addBase }) {
17+
addBase({
18+
h1: {
19+
fontSize: '32px',
20+
},
21+
})
22+
},
23+
],
24+
}
25+
26+
let css = `
27+
@import "tailwindcss/base";
28+
@import "tailwindcss/components";
29+
@import "tailwindcss/utilities";
30+
`
31+
32+
return run(css, config).then((result) => {
33+
let expectedPath = path.resolve(__dirname, './12-import-syntax.test.css')
34+
let expected = fs.readFileSync(expectedPath, 'utf8')
35+
36+
expect(result.css).toMatchCss(expected)
37+
})
38+
})

0 commit comments

Comments
 (0)