-
Notifications
You must be signed in to change notification settings - Fork 6
Add Tailwind to CSS-build task #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The Tailwind plugin's internals assumes that the file specified in the `from` option in the PostCSS config exists and tries to `stat` it. This failed on first build because we were passing the output CSS path for this option. We don't have an input CSS file, because the CSS is generated from SASS inputs and only exists in memory, but we do have an input .scss file path, so use that instead.
robertknight
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed a fix for the build error if the output does not already exist. I also pushed an example of how we could make Tailwind an optional dependency for projects that don't need it. We should chat about whether this is something we want to do or not.
| * @param {TailwindConfig} [options.tailwindConfig] | ||
| * Optional tailwind config object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to remove this comment line, as it essentially restates the type info.
|
|
||
| const postcssResult = await cssProcessor.process(sassResult.css, { | ||
| from: output, | ||
| from: input, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to fix an error if the output file does not already exist. See notes in commit.
| "rollup": "^2.58.0", | ||
| "sass": "^1.43.2" | ||
| "sass": "^1.43.2", | ||
| "tailwindcss": "^2.2.19" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make sense to make this an optional peer dependency for downstream projects that don't use Tailwind. This is done in the peerDependenciesMeta section - https://classic.yarnpkg.com/en/docs/package-json#toc-peerdependenciesmeta. Additionally you'll need to put any optional functionality under a separate entry point or use dynamic import to load it.
Example of making Tailwind optional:
diff --git a/lib/sass.js b/lib/sass.js
index e07139e..723f098 100644
--- a/lib/sass.js
+++ b/lib/sass.js
@@ -4,7 +4,6 @@ import { basename, dirname, extname } from 'path';
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
import sass from 'sass';
-import tailwindcss from 'tailwindcss';
/**
* @typedef {import('tailwindcss/tailwind-config').TailwindConfig} TailwindConfig
@@ -34,6 +33,14 @@ export async function buildCSS(
const minify = process.env.NODE_ENV === 'production';
await mkdir(outDir, { recursive: true });
+ /** @type {typeof import('tailwindcss')} */
+ let tailwindcss;
+ try {
+ tailwindcss = (await import('tailwindcss')).default;
+ } catch {
+ // Ignored
+ }
+
await Promise.all(
inputs.map(async input => {
const output = `${outDir}/${basename(input, extname(input))}.css`;
@@ -46,10 +53,12 @@ export async function buildCSS(
sourceMap: sourcemapPath,
});
- const cssProcessor = postcss([
- tailwindcss(tailwindConfig),
- autoprefixer(),
- ]);
+ const optionalPlugins = [];
+ if (tailwindcss) {
+ optionalPlugins.push(tailwindcss(tailwindConfig));
+ }
+
+ const cssProcessor = postcss([...optionalPlugins, autoprefixer()]);
const postcssResult = await cssProcessor.process(sassResult.css, {
from: input,
diff --git a/package.json b/package.json
index ec7ef8a..c82f751 100644
--- a/package.json
+++ b/package.json
@@ -42,6 +42,11 @@
"sass": "^1.43.2",
"tailwindcss": "^2.2.19"
},
+ "peerDependenciesMeta": {
+ "tailwindcss": {
+ "optional": true
+ }
+ },
"prettier": {
"arrowParens": "avoid",
"singleQuote": true|
Thanks, @robertknight ! I've taken your suggestion of making Tailwind an optional dependency and then tested this against master of the It may end up being the case that Tailwind will be a hard dependency for applications using We good to land and release here? |
This PR adds Tailwind support to the CSS build task in
sass. It keeps to the convention of addingtailwindcssas apeerDependency, which means that downstream apps that use this package's CSS-building task will be required to installtailwindcssor will crash on build. We can certainly discuss handling this another way.Running source (S)CSS through
tailwindcssis a no-op if the downstream application doesn't provide configuration to the CSS-build task (and also make use of Tailwind directives in its source SCSS).