From e68b922edbd3cb10b6707a98afed9fefe250b5a6 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 7 Jun 2022 18:29:00 +0200 Subject: [PATCH] ensure we can use `@import 'tailwindcss/...'` without node_modules This is useful if you are using `npx tailwindcs ...` and to prevent that postcss-import crashes on the tailwind specific imports which we will replace anyway. --- src/cli.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/cli.js b/src/cli.js index ad6cdf1c710e..712274ae186f 100644 --- a/src/cli.js +++ b/src/cli.js @@ -568,10 +568,20 @@ async function build() { tailwindPlugin.postcss = true + let IMPORT_COMMENT = '__TAILWIND_RESTORE_IMPORT__: ' + let [beforePlugins, afterPlugins, postcssOptions] = includePostCss ? await loadPostCssPlugins() : [ [ + (root) => { + root.walkAtRules('import', (rule) => { + if (rule.params.slice(1).startsWith('tailwindcss/')) { + rule.after(postcss.comment({ text: IMPORT_COMMENT + rule.params })) + rule.remove() + } + }) + }, (() => { try { return require('postcss-import') @@ -579,6 +589,19 @@ async function build() { return lazyPostcssImport() })(), + (root) => { + root.walkComments((rule) => { + if (!rule.text.startsWith(IMPORT_COMMENT)) { + rule.after( + postcss.atRule({ + name: 'import', + params: rule.text.replace(IMPORT_COMMENT, ''), + }) + ) + rule.remove() + } + }) + }, ], [], {},