Skip to content

Commit e7e5b3d

Browse files
authored
Merge pull request #8 from hypothesis/tailwind-support
Add Tailwind to CSS-build task
2 parents 9696355 + e7dc78e commit e7e5b3d

File tree

3 files changed

+633
-18
lines changed

3 files changed

+633
-18
lines changed

lib/sass.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,42 @@ import autoprefixer from 'autoprefixer';
55
import postcss from 'postcss';
66
import sass from 'sass';
77

8+
/**
9+
* @typedef {import('tailwindcss/tailwind-config').TailwindConfig} TailwindConfig
10+
*/
11+
const defaultTailwindConfig = /** @type {TailwindConfig} */ ({
12+
darkMode: false,
13+
theme: {},
14+
});
15+
816
/**
917
* Build CSS bundles from SASS or CSS inputs.
1018
*
1119
* @param {string[]} inputs - An array of CSS or SCSS file paths specifying the
1220
* entry points of style bundles. The output files will be written to
1321
* `build/styles/[name].css` where `[name]` is the basename of the input file
1422
* minus the file extension.
23+
* @param {object} [options]
24+
* @param {TailwindConfig} [options.tailwindConfig]
25+
* Optional tailwind config object
1526
* @return {Promise<void>} Promise for completion of the build.
1627
*/
17-
export async function buildCSS(inputs) {
28+
export async function buildCSS(
29+
inputs,
30+
{ tailwindConfig = defaultTailwindConfig } = {}
31+
) {
1832
const outDir = 'build/styles';
1933
const minify = process.env.NODE_ENV === 'production';
2034
await mkdir(outDir, { recursive: true });
2135

36+
/** @type {typeof import('tailwindcss')} */
37+
let tailwindcss;
38+
try {
39+
tailwindcss = (await import('tailwindcss')).default;
40+
} catch {
41+
// Ignored
42+
}
43+
2244
await Promise.all(
2345
inputs.map(async input => {
2446
const output = `${outDir}/${basename(input, extname(input))}.css`;
@@ -31,9 +53,15 @@ export async function buildCSS(inputs) {
3153
sourceMap: sourcemapPath,
3254
});
3355

34-
const cssProcessor = postcss([autoprefixer()]);
56+
const optionalPlugins = [];
57+
if (tailwindcss) {
58+
optionalPlugins.push(tailwindcss(tailwindConfig));
59+
}
60+
61+
const cssProcessor = postcss([...optionalPlugins, autoprefixer()]);
62+
3563
const postcssResult = await cssProcessor.process(sassResult.css, {
36-
from: output,
64+
from: input,
3765
to: output,
3866
map: {
3967
inline: false,

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
"@types/karma": "^6.3.1",
1919
"@types/node": "^16.11.1",
2020
"@types/sass": "^1.16.1",
21+
"@types/tailwindcss": "^2.2.4",
2122
"autoprefixer": "^10.3.7",
2223
"eslint": "^8.0.1",
2324
"karma": "^6.3.4",
2425
"postcss": "^8.3.9",
2526
"prettier": "^2.4.1",
2627
"rollup": "^2.58.0",
2728
"sass": "^1.43.2",
29+
"tailwindcss": "^2.2.19",
2830
"typescript": "^4.4.4"
2931
},
3032
"dependencies": {
@@ -37,7 +39,13 @@
3739
"karma": "^6.3.4",
3840
"postcss": "^8.3.9",
3941
"rollup": "^2.58.0",
40-
"sass": "^1.43.2"
42+
"sass": "^1.43.2",
43+
"tailwindcss": "^2.2.19"
44+
},
45+
"peerDependenciesMeta": {
46+
"tailwindcss": {
47+
"optional": true
48+
}
4149
},
4250
"prettier": {
4351
"arrowParens": "avoid",

0 commit comments

Comments
 (0)