Skip to content

Commit a03a245

Browse files
committed
Fix it so css.TailwindCSS inlineImports options isn't always enabled
To avoid breaking existing setup and to make a better default option, the option is now `disableInlineImports` (default false). Fixes #13719
1 parent 5a81a3a commit a03a245

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

resources/resource_transformers/cssjs/postcss.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func (c *PostCSSClient) Process(res resources.ResourceTransformer, options map[s
6969
}
7070

7171
type InlineImports struct {
72-
// Service `mapstructure:",squash"`
7372
// Enable inlining of @import statements.
7473
// Does so recursively, but currently once only per file;
7574
// that is, it's not possible to import the same file in
@@ -78,6 +77,11 @@ type InlineImports struct {
7877
// so you can have @import anywhere in the file.
7978
InlineImports bool
8079

80+
// See issue https://github.com/gohugoio/hugo/issues/13719
81+
// Disable inlining of @import statements
82+
// This is currenty only used for css.TailwindCSS.
83+
DisableInlineImports bool
84+
8185
// When InlineImports is enabled, we fail the build if an import cannot be resolved.
8286
// You can enable this to allow the build to continue and leave the import statement in place.
8387
// Note that the inline importer does not process url location or imports with media queries,

resources/resource_transformers/cssjs/tailwindcss.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ func (t *tailwindcssTransformation) Transform(ctx *resources.ResourceTransformat
129129
t.rs.Assets.Fs, t.rs.Logger, ctx.DependencyManager,
130130
)
131131

132-
src, err = imp.resolve()
133-
if err != nil {
134-
return err
132+
if !options.InlineImports.DisableInlineImports {
133+
src, err = imp.resolve()
134+
if err != nil {
135+
return err
136+
}
135137
}
136138

137139
go func() {
@@ -146,7 +148,11 @@ func (t *tailwindcssTransformation) Transform(ctx *resources.ResourceTransformat
146148
Cause: err,
147149
}
148150
}
149-
return imp.toFileError(errBuf.String())
151+
s := errBuf.String()
152+
if options.InlineImports.DisableInlineImports && strings.Contains(s, "Can't resolve") {
153+
s += "You may want to set the 'disableInlineImports' option to false to inline imports, see https://gohugo.io/functions/css/tailwindcss/#disableinlineimports"
154+
}
155+
return imp.toFileError(s)
150156
}
151157

152158
return nil

resources/resource_transformers/cssjs/tailwindcss_integration_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"testing"
1818

1919
"github.com/bep/logg"
20+
qt "github.com/frankban/quicktest"
2021
"github.com/gohugoio/hugo/htesting"
2122
"github.com/gohugoio/hugo/hugolib"
2223
)
@@ -70,3 +71,66 @@ CSS: {{ $css.Content | safeCSS }}|
7071

7172
b.AssertFileContent("public/index.html", "/*! tailwindcss v4.")
7273
}
74+
75+
func TestTailwindCSSNoInlineImportsIssue13719(t *testing.T) {
76+
t.Parallel()
77+
78+
files := `
79+
-- hugo.toml --
80+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
81+
theme = 'my-theme'
82+
83+
[[module.mounts]]
84+
source = 'assets'
85+
target = 'assets'
86+
87+
[[module.mounts]]
88+
source = 'other'
89+
target = 'assets/css'
90+
-- assets/css/main.css --
91+
@import "tailwindcss";
92+
93+
@import "colors/red.css";
94+
@import "colors/blue.css";
95+
@import "colors/purple.css";
96+
-- assets/css/colors/red.css --
97+
@import "green.css";
98+
99+
.red {color: red;}
100+
-- assets/css/colors/green.css --
101+
.green {color: green;}
102+
-- themes/my-theme/assets/css/colors/blue.css --
103+
.blue {color: blue;}
104+
-- other/colors/purple.css --
105+
.purple {color: purple;}
106+
-- layouts/home.html --
107+
{{ with (templates.Defer (dict "key" "global")) }}
108+
{{ with resources.Get "css/main.css" }}
109+
{{ $opts := dict "disableInlineImports" true }}
110+
{{ with . | css.TailwindCSS $opts }}
111+
<link rel="stylesheet" href="{{ .RelPermalink }}">
112+
{{ end }}
113+
{{ end }}
114+
{{ end }}
115+
-- package.json --
116+
{
117+
"devDependencies": {
118+
"@tailwindcss/cli": "^4.1.7",
119+
"tailwindcss": "^4.1.7"
120+
}
121+
}
122+
`
123+
124+
b, err := hugolib.NewIntegrationTestBuilder(
125+
hugolib.IntegrationTestConfig{
126+
T: t,
127+
TxtarString: files,
128+
NeedsOsFS: true,
129+
NeedsNpmInstall: true,
130+
LogLevel: logg.LevelInfo,
131+
}).BuildE()
132+
133+
b.Assert(err, qt.IsNotNil)
134+
b.Assert(err.Error(), qt.Contains, "Can't resolve 'colors/red.css'")
135+
b.Assert(err.Error(), qt.Contains, "You may want to set the 'disableInlineImports' option to false")
136+
}

0 commit comments

Comments
 (0)