Skip to content

Instantly share code, notes, and snippets.

@eduardolat
Created December 15, 2023 05:41
Show Gist options
  • Save eduardolat/438a1de077ccac6b9792153e708c1824 to your computer and use it in GitHub Desktop.
Save eduardolat/438a1de077ccac6b9792153e708c1824 to your computer and use it in GitHub Desktop.
Regex for TailwindCSS + Gomponents
/** @type {import('tailwindcss').Config} */
module.exports = {
// Change this to your project source folder
content: ['./web/**/*.go'],
plugins: [],
theme: {
extend: {},
}
}
{
// Tailwind CSS + Gomponents
// It can handle single and multiple lines inside the matches
// It can handle double quotes (") and backticks (`)
"tailwindCSS.includeLanguages": {
"go": "go",
},
"tailwindCSS.experimental.classRegex": [
["Class\\(([^)]*)\\)", "[\"`]([^\"`]*)[\"`]"], // Class("...") or Class(`...`)
["Classes\\(([^)]*)\\)", "[\"`]([^\"`]*)[\"`]"], // Classes("...") or Classes(`...`)
["Class\\{([^)]*)\\}", "[\"`]([^\"`]*)[\"`]"], // Class{"..."} or Class{`...`}
["Classes\\{([^)]*)\\}", "[\"`]([^\"`]*)[\"`]"], // Classes{"..."} or Classes{`...`}
["Class:\\s*[\"`]([^\"`]*)[\"`]"], // Class: "..." or Class: `...`
["Classes:\\s*[\"`]([^\"`]*)[\"`]"], // Classes: "..." or Classes: `...`
],
}
@cameres
Copy link

cameres commented Jul 23, 2024

Nice! This also appears to work for the IntelliJ/GoLand plugin.

"experimental": {
    "configFile": null,
    "classRegex": [
      ["Class\\(([^)]*)\\)", "[\"`]([^\"`]*)[\"`]"], 
      ["Classes\\(([^)]*)\\)", "[\"`]([^\"`]*)[\"`]"], 
      ["Class\\{([^)]*)\\}", "[\"`]([^\"`]*)[\"`]"],
      ["Classes\\{([^)]*)\\}", "[\"`]([^\"`]*)[\"`]"], 
      ["Class:\\s*[\"`]([^\"`]*)[\"`]"],
      ["Classes:\\s*[\"`]([^\"`]*)[\"`]"]
    ]
  }

@markuswustenberg
Copy link

Here's my whole current config for GoLand, going to Settings -> Languages & Frameworks -> Style Sheets -> TailwindCSS:

{
  "includeLanguages": {
    "ftl": "html",
    "jinja": "html",
    "jinja2": "html",
    "smarty": "html",
    "tmpl": "gohtml",
    "cshtml": "html",
    "vbhtml": "html",
    "razor": "html",
    "go": "html"
  },
  "files": {
    "exclude": [
      "**/.git/**",
      "**/node_modules/**",
      "**/.hg/**",
      "**/.svn/**"
    ]
  },
  "emmetCompletions": false,
  "classAttributes": ["class", "className", "ngClass"],
  "colorDecorators": false,
  "showPixelEquivalents": true,
  "rootFontSize": 16,
  "hovers": true,
  "suggestions": true,
  "codeActions": true,
  "validate": true,
  "lint": {
    "invalidScreen": "error",
    "invalidVariant": "error",
    "invalidTailwindDirective": "error",
    "invalidApply": "error",
    "invalidConfigPath": "error",
    "cssConflict": "warning",
    "recommendedVariantOrder": "warning"
  },
  "experimental": {
    "configFile": null,
    "classRegex": [
      ["Class(?:es)?[({]([^)}]*)[)}]", "[\"`]([^\"`]*)[\"`]"]
    ]
  }
}

Note that I had to install TailwindCSS through npm for this to work, the Tailwind CLI doesn't seem to be enough:

npm install -D tailwindcss

@markuswustenberg
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment