]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix it so css.TailwindCSS inlineImports options isn't always enabled
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 19 May 2025 10:08:40 +0000 (12:08 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 19 May 2025 17:36:48 +0000 (19:36 +0200)
To avoid breaking existing setup and to make a better default option, the option is now `disableInlineImports` (default false).
Fixes #13719

resources/resource_transformers/cssjs/postcss.go
resources/resource_transformers/cssjs/tailwindcss.go
resources/resource_transformers/cssjs/tailwindcss_integration_test.go

index 1a9e01142e1942a43d11e767027560fe6e7565ec..98bdc9249583e988b2ae0a0e84ae7dd465c973a2 100644 (file)
@@ -69,7 +69,6 @@ func (c *PostCSSClient) Process(res resources.ResourceTransformer, options map[s
 }
 
 type InlineImports struct {
-       // Service `mapstructure:",squash"`
        // Enable inlining of @import statements.
        // Does so recursively, but currently once only per file;
        // that is, it's not possible to import the same file in
@@ -78,6 +77,11 @@ type InlineImports struct {
        // so you can have @import anywhere in the file.
        InlineImports bool
 
+       // See issue https://github.com/gohugoio/hugo/issues/13719
+       // Disable inlining of @import statements
+       // This is currenty only used for css.TailwindCSS.
+       DisableInlineImports bool
+
        // When InlineImports is enabled, we fail the build if an import cannot be resolved.
        // You can enable this to allow the build to continue and leave the import statement in place.
        // Note that the inline importer does not process url location or imports with media queries,
index beda7a646fe56a2499a35fa5bdb7fc351cc7457d..a60a16222ac7918860c0010b22116bcb00e23edd 100644 (file)
@@ -129,9 +129,11 @@ func (t *tailwindcssTransformation) Transform(ctx *resources.ResourceTransformat
                t.rs.Assets.Fs, t.rs.Logger, ctx.DependencyManager,
        )
 
-       src, err = imp.resolve()
-       if err != nil {
-               return err
+       if !options.InlineImports.DisableInlineImports {
+               src, err = imp.resolve()
+               if err != nil {
+                       return err
+               }
        }
 
        go func() {
@@ -146,7 +148,11 @@ func (t *tailwindcssTransformation) Transform(ctx *resources.ResourceTransformat
                                Cause: err,
                        }
                }
-               return imp.toFileError(errBuf.String())
+               s := errBuf.String()
+               if options.InlineImports.DisableInlineImports && strings.Contains(s, "Can't resolve") {
+                       s += "You may want to set the 'disableInlineImports' option to false to inline imports, see https://gohugo.io/functions/css/tailwindcss/#disableinlineimports"
+               }
+               return imp.toFileError(s)
        }
 
        return nil
index ae70042a08c7831db29a08937a12b053be17612e..734ffe759d713d8fb105c56f67841aced3d37ca1 100644 (file)
@@ -17,6 +17,7 @@ import (
        "testing"
 
        "github.com/bep/logg"
+       qt "github.com/frankban/quicktest"
        "github.com/gohugoio/hugo/htesting"
        "github.com/gohugoio/hugo/hugolib"
 )
@@ -70,3 +71,66 @@ CSS: {{ $css.Content | safeCSS }}|
 
        b.AssertFileContent("public/index.html", "/*! tailwindcss v4.")
 }
+
+func TestTailwindCSSNoInlineImportsIssue13719(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+disableKinds = ['page','rss','section','sitemap','taxonomy','term']
+theme = 'my-theme'
+
+[[module.mounts]]
+source = 'assets'
+target = 'assets'
+
+[[module.mounts]]
+source = 'other'
+target = 'assets/css'
+-- assets/css/main.css --
+@import "tailwindcss";
+
+@import "colors/red.css";
+@import "colors/blue.css";
+@import "colors/purple.css";
+-- assets/css/colors/red.css --
+@import "green.css";
+
+.red {color: red;}
+-- assets/css/colors/green.css --
+.green {color: green;}
+-- themes/my-theme/assets/css/colors/blue.css --
+.blue {color: blue;}
+-- other/colors/purple.css --
+.purple {color: purple;}
+-- layouts/home.html --
+{{ with (templates.Defer (dict "key" "global")) }}
+  {{ with resources.Get "css/main.css" }}
+    {{ $opts := dict "disableInlineImports" true }}
+    {{ with . | css.TailwindCSS $opts }}
+      <link rel="stylesheet" href="{{ .RelPermalink }}">
+    {{ end }}
+  {{ end }}
+{{ end }}
+-- package.json --
+{
+  "devDependencies": {
+    "@tailwindcss/cli": "^4.1.7",
+    "tailwindcss": "^4.1.7"
+  }
+}
+`
+
+       b, err := hugolib.NewIntegrationTestBuilder(
+               hugolib.IntegrationTestConfig{
+                       T:               t,
+                       TxtarString:     files,
+                       NeedsOsFS:       true,
+                       NeedsNpmInstall: true,
+                       LogLevel:        logg.LevelInfo,
+               }).BuildE()
+
+       b.Assert(err, qt.IsNotNil)
+       b.Assert(err.Error(), qt.Contains, "Can't resolve 'colors/red.css'")
+       b.Assert(err.Error(), qt.Contains, "You may want to set the 'disableInlineImports' option to false")
+}