/
tailwindcss.js
147 lines (123 loc) · 4.61 KB
/
tailwindcss.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const path = require('path')
const fs = require('fs-extra')
const postcss = require('postcss')
const tailwindcss = require('tailwindcss')
const postcssImport = require('postcss-import')
const postcssNested = require('tailwindcss/nesting')
const {requireUncached} = require('../utils/helpers')
const mergeLonghand = require('postcss-merge-longhand')
const {get, isObject, isEmpty, merge} = require('lodash')
const addImportantPlugin = () => {
return {
postcssPlugin: 'add-important',
Rule(rule) {
const shouldAddImportant = get(rule, 'raws.tailwind.layer') === 'variants'
|| get(rule, 'parent.type') === 'atrule'
if (shouldAddImportant) {
rule.walkDecls(decl => {
decl.important = true
})
}
}
}
}
module.exports = {
compile: async ({css = '', html = '', config = {}}) => {
// Compute the Tailwind config to use
const userConfig = config => {
const tailwindUserConfig = get(config, 'build.tailwind.config', 'tailwind.config.js')
// If a custom config object was passed, use that
if (isObject(tailwindUserConfig) && !isEmpty(tailwindUserConfig)) {
return tailwindUserConfig
}
/**
* Try loading a fresh tailwind.config.js, with fallback to an empty object.
* This will use the default Tailwind config (with rem units etc)
*/
try {
return requireUncached(path.resolve(process.cwd(), tailwindUserConfig))
} catch {
return {}
}
}
// Merge user's Tailwind config on top of a 'base' config
const layoutsRoot = get(config, 'build.layouts.root')
const componentsRoot = get(config, 'build.components.root')
const layoutsPath = typeof layoutsRoot === 'string' && layoutsRoot ?
`${layoutsRoot}/**/*.html`.replace(/\/\//g, '/') :
'./src/layouts/**/*.html'
const componentsPath = typeof componentsRoot === 'string' && componentsRoot ?
`${componentsRoot}/**/*.html`.replace(/\/\//g, '/') :
'./src/components/**/*.html'
const tailwindConfig = merge({
content: {
files: [
layoutsPath,
componentsPath,
{raw: html, extension: 'html'}
]
}
}, userConfig(config))
// If `content` is an array, add it to `content.files`
if (Array.isArray(tailwindConfig.content)) {
tailwindConfig.content = {
files: [
layoutsPath,
componentsPath,
...tailwindConfig.content,
{raw: html, extension: 'html'}
]
}
}
// Include all `build.templates.source` paths when scanning for selectors to preserve
const buildTemplates = get(config, 'build.templates')
if (buildTemplates) {
const templateObjects = Array.isArray(buildTemplates) ? buildTemplates : [buildTemplates]
const templateSources = templateObjects.map(template => {
const source = get(template, 'source')
if (typeof source === 'function') {
const sources = source(config)
if (Array.isArray(sources)) {
sources.map(s => tailwindConfig.content.files.push(s))
} else if (typeof sources === 'string') {
tailwindConfig.content.files.push(sources)
}
// Must return a valid `content` entry
return {raw: '', extension: 'html'}
}
// Support single-file sources i.e. src/templates/index.html
if (typeof source === 'string' && Boolean(path.extname(source))) {
tailwindConfig.content.files.push(source)
return {raw: '', extension: 'html'}
}
return `${source}/**/*.*`
})
tailwindConfig.content.files.push(...templateSources)
}
const userFilePath = get(config, 'build.tailwind.css', path.join(process.cwd(), 'src/css/tailwind.css'))
const userFileExists = await fs.pathExists(userFilePath)
const toProcess = [
postcssNested(),
tailwindcss(tailwindConfig),
get(tailwindConfig, 'important') === false ? () => {} : addImportantPlugin(),
get(config, 'shorthandCSS', get(config, 'shorthandInlineCSS')) === true ?
mergeLonghand() :
() => {},
...get(config, 'build.postcss.plugins', [])
]
if (userFileExists) {
css = await fs.readFile(path.resolve(userFilePath), 'utf8') + css
toProcess.unshift(
postcssImport({path: path.dirname(userFilePath)})
)
} else {
css = `@tailwind components; @tailwind utilities; ${css}`
}
return postcss([...toProcess])
.process(css, {from: userFileExists ? userFilePath : undefined})
.then(result => result.css)
.catch(error => {
throw new SyntaxError(error)
})
}
}