tailwindlabs / tailwindcss-typography

Beautiful typographic defaults for HTML you don't control.
https://tailwindcss-typography.vercel.app/
MIT License
4.68k stars 269 forks source link

Conflict with ElderJS and PostCSS #234

Closed TotalLag closed 2 years ago

TotalLag commented 2 years ago

What version of @tailwindcss/typography are you using?

0.5.0

What version of Node.js are you using?

v16.13.1

What browser are you using?

MSEdge

What operating system are you using?

Windows 10

Reproduction repository

https://github.com/TotalLag/tailwind-postcss-elderjs-bug

Describe your issue

Not sure if this is a ElderJS bug or Tailwind, but I am initiating Tailwind inside a style tag like so: <style global lang="postcss">. I do it in this way so that Tailwind goes through the build process every time. Issues arise after upgrading to Tailwind 3.0 AND typography 5.0 with no changes from Elder.

When trying to compile with prose in use:

> Elder.js error in transform. CompileError [ValidationError]: :global(...) must contain a single selector
    at error (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:17745:20)
    at Component.error (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30831:14)
    at Selector$1.validate_global_with_multiple_selectors (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:29664:36)
    at Selector$1.validate (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:29653:15)
    at /mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30159:23
    at Array.forEach (<anonymous>)
    at Rule$1.validate (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30158:25)
    at /mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30440:20
    at Array.forEach (<anonymous>)
    at Stylesheet.validate (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30439:24) {
  code: 'css-invalid-global-selector',
  start: { line: 767, column: 16, character: 20201 },
  end: { line: 767, column: 167, character: 20352 },
  pos: 20201,
  filename: 'src/routes/home/Home.svelte',
  frame: '765:   margin-bottom: 1.25em;\n' +
    '766: }\n' +
    '767: :global(.prose) :global(:where(ul) :global(ul), :global(ul) :global(ol), :global(ol) :global(ul), :global(ol) :global(ol):not(:where([class~="not-prose"]) :global(*))) {\n' +
    '                     ^\n' +
    '768:   margin-top: 0.75em;\n' +
    '769:   margin-bottom: 0.75em;'
}
 > node_modules/svelte/compiler.js:17745:19: error: [plugin: esbuild-plugin-elderjs] :global(...) must contain a single selector
    17745 │       const error = new CompileError(message);
          ╵                     ^
    at error (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:17745:20)
    at Component.error (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30831:14)
    at Selector$1.validate_global_with_multiple_selectors (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:29664:36)
    at Selector$1.validate (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:29653:15)
    at /mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30159:23
    at Array.forEach (<anonymous>)
    at Rule$1.validate (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30158:25)
    at /mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30440:20
    at Array.forEach (<anonymous>)
    at Stylesheet.validate (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/svelte/compiler.js:30439:24)

   node_modules/@elderjs/elderjs/build/esbuild/esbuildPluginSvelte.js:108:22: note: This error came from the "onLoad" callback registered here
      108 │                 build.onLoad({ filter: /\.svelte$/ }, async ({ path }) => {
          ╵                       ~~~~~~
    at setup (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/@elderjs/elderjs/build/esbuild/esbuildPluginSvelte.js:108:23)
    at handlePlugins (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/esbuild/lib/main.js:738:23)
    at Object.buildOrServe (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/esbuild/lib/main.js:1026:7)
    at /mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/esbuild/lib/main.js:1801:17
    at new Promise (<anonymous>)
    at Object.build (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/esbuild/lib/main.js:1800:14)
    at Object.build (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/esbuild/lib/main.js:1655:51)
    at svelteHandler (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/@elderjs/elderjs/build/esbuild/esbuildBundler.js:93:40)
    at esbuildBundler (/mnt/c/Users/root/Documents/Web/tailwind-bug/node_modules/@elderjs/elderjs/build/esbuild/esbuildBundler.js:191:38)

Oddly, when using Tailwind 2.0 and Typography 3, ElderJS compiles just fine and prose works.

My workaround so far is to take Tailwind out of the <style global lang="postcss"> tag into its own css file and run tailwind in as an npm script prior to build. While this works, it's extra setup steps inside packages.json to get pieces tied.

From the error, it looks like an Elder problem, but I'm unsure what changed in Tailwind to not be able to use the global tag inside style.

Filing for posterity.

adamwathan commented 2 years ago

Hey! This sounds like some incompatibility with whatever CSS tooling is adding the :global stuff and the fairly recent :where pseudo-selector in CSS maybe.

You can likely work around this by setting target: 'legacy' when configuring the typography plugin, but that will disable a bunch of features like the not-prose class and the ability to override styles on individual typography elements with utilities, since all of that stuff depends on :where to work.

// tailwind.config.js
module.exports = {
  plugins: [
    require('@tailwindcss/typography')({
      target: 'legacy'
    })
  ]
}

This feature is sort of deliberately undocumented currently just to make sure we have a sense of how many people actually run into the need for it, by opening issues like this one 👍🏻

I'm going to close as not something we can actually do anything to "fix" as far as I can tell — the selectors we generate are totally valid CSS and you actually get the same error even if you aren't using Tailwind at all and wrote the same type of selector by hand, for example:

<script>
  export let data, helpers, settings;
</script>

<style global lang="postcss">
.foo > :where(ul > li p, ol > li p) {
  background: red;
}
</style>

<svelte:head>
  <title>Elder.js Template: Home</title>
  <meta name="description" content="Elder.js Starter Template: It's A Tutorial Too!" />
  <link href="{settings.origin}/" rel="canonical" />
</svelte:head>

<span class="text-3xl">HELLO WORLD</span>

The .foo > :where(ul > li p, ol > li p) selector causes the same error, and that's just hand-written CSS, not Tailwind-specific.

I would recommend opening an issue on the ElderJS project and including a totally Tailwind-free reproduction using some CSS like I've shared above. With any luck someone on that project will be open to working on a fix in whatever is actually struggling to handle the :where pseudo class — might be Svelte itself ultimately.

Sorry I couldn't do more on this one, hope that at least helps.

TotalLag commented 2 years ago

awesome. thanks for the response and taking a look at it! i'll work with the maintainers on that side :)