From 30dc2990c300cf3446ff5d795d47be146b8ee413 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Wed, 12 May 2021 16:58:03 -0400 Subject: [PATCH] Error when `@layer` used without matching `@tailwind` directive (#4335) Also refactor to only detect `@tailwind` directives once per build to improve performance. --- src/jit/index.js | 8 +- src/jit/lib/expandTailwindAtRules.js | 13 +-- src/jit/lib/removeLayerAtRules.js | 19 +++- src/jit/lib/rewriteTailwindImports.js | 8 ++ src/jit/lib/setupContext.js | 11 +-- tests/jit/layer-without-tailwind.test.css | 5 + tests/jit/layer-without-tailwind.test.html | 1 + tests/jit/layer-without-tailwind.test.js | 101 +++++++++++++++++++++ tests/jit/prefix.test.js | 2 +- 9 files changed, 145 insertions(+), 23 deletions(-) create mode 100644 tests/jit/layer-without-tailwind.test.css create mode 100644 tests/jit/layer-without-tailwind.test.html create mode 100644 tests/jit/layer-without-tailwind.test.js diff --git a/src/jit/index.js b/src/jit/index.js index 0c8f63c2f..d155f1ee4 100644 --- a/src/jit/index.js +++ b/src/jit/index.js @@ -30,9 +30,9 @@ export default function (configOrPath = {}) { }) } - rewriteTailwindImports(root) + let tailwindDirectives = rewriteTailwindImports(root) - let context = setupContext(configOrPath)(result, root) + let context = setupContext(configOrPath, tailwindDirectives)(result, root) if (!env.TAILWIND_DISABLE_TOUCH) { if (context.configPath !== null) { @@ -41,8 +41,8 @@ export default function (configOrPath = {}) { } return postcss([ - removeLayerAtRules(context), - expandTailwindAtRules(context, registerDependency), + removeLayerAtRules(context, tailwindDirectives), + expandTailwindAtRules(context, registerDependency, tailwindDirectives), expandApplyAtRules(context), evaluateTailwindFunctions(context.tailwindConfig), substituteScreenAtRules(context.tailwindConfig), diff --git a/src/jit/lib/expandTailwindAtRules.js b/src/jit/lib/expandTailwindAtRules.js index cd528f1c0..6b421d8ac 100644 --- a/src/jit/lib/expandTailwindAtRules.js +++ b/src/jit/lib/expandTailwindAtRules.js @@ -111,9 +111,12 @@ function buildStylesheet(rules, context) { return returnValue } -export default function expandTailwindAtRules(context, registerDependency) { +export default function expandTailwindAtRules(context, registerDependency, tailwindDirectives) { return (root) => { - let foundTailwind = false + if (tailwindDirectives.size === 0) { + return root + } + let layerNodes = { base: null, components: null, @@ -126,8 +129,6 @@ export default function expandTailwindAtRules(context, registerDependency) { // file as a dependency since the output of this CSS does not depend on // the source of any templates. Think Vue