Debian Codegraph @

resources/resource_transformers/cssjs/tailwindcss_integration_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package cssjs_test
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 "github.com/gohugoio/hugo/htesting"
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 func TestTailwindV4Basic(t *testing.T) {
25 if !htesting.IsCI() {
26 t.Skip("Skip long running test when running locally")
27 }
28
29 files := `
30 -- hugo.toml --
31 security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$']
32 -- package.json --
33 {
34 "license": "MIT",
35 "repository": {
36 "type": "git",
37 "url": "https://github.com/bep/hugo-starter-tailwind-basic.git"
38 },
39 "devDependencies": {
40 "@tailwindcss/cli": "^4.0.1",
41 "tailwindcss": "^4.0.1"
42 },
43 "name": "hugo-starter-tailwind-basic",
44 "version": "0.1.0"
45 }
46 -- assets/css/styles.css --
47 @import "tailwindcss";
48
49 @theme {
50 --font-family-display: "Satoshi", "sans-serif";
51
52 --breakpoint-3xl: 1920px;
53
54 --color-neon-pink: oklch(71.7% 0.25 360);
55 --color-neon-lime: oklch(91.5% 0.258 129);
56 --color-neon-cyan: oklch(91.3% 0.139 195.8);
57 }
58 -- layouts/home.html --
59 {{ $css := resources.Get "css/styles.css" | css.TailwindCSS }}
60 CSS: {{ $css.Content | safeCSS }}|
61 `
62
63 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
64
65 b.AssertFileContent("public/index.html", "/*! tailwindcss v4.")
66 }
67
68 // See issue 15103.
69 func TestTailwindCSSImportContext(t *testing.T) {
70 t.Parallel()
71 htesting.SkipSlowTestUnlessCI(t)
72
73 files := `
74 -- hugo.toml --
75 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
76 security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$']
77 -- assets/css/main.css --
78 @import "tailwindcss";
79
80 @import "foo.css";
81 @import "bar.css";
82 -- assets/css/foo.css --
83 .foo {color: orange;}
84 -- layouts/home.html --
85 {{ $foo := resources.FromString "foo.css" ".foo {color: blue;}" }}
86 {{ $bar := resources.FromString "bar.css" ".bar {color: green;}" }}
87 {{ $opts := dict "importContext" (slice $foo $bar) }}
88 {{ $css := resources.Get "css/main.css" | css.TailwindCSS $opts }}
89 CSS: {{ $css.Content | safeCSS }}|
90 -- package.json --
91 {
92 "devDependencies": {
93 "@tailwindcss/cli": "^4.1.7",
94 "tailwindcss": "^4.1.7"
95 }
96 }
97 `
98
99 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
100
101 // foo.css resolves in the import context before the assets filesystem.
102 b.AssertFileContent(
103 "public/index.html",
104 ".foo {\n color: blue;\n }",
105 ".bar {\n color: green;\n }",
106 )
107 }
108
109 func TestTailwindCSSNoInlineImportsIssue13719(t *testing.T) {
110 t.Parallel()
111 htesting.SkipSlowTestUnlessCI(t)
112
113 files := `
114 -- hugo.toml --
115 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
116 theme = 'my-theme'
117
118 security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$']
119
120 [[module.mounts]]
121 source = 'assets'
122 target = 'assets'
123
124 [[module.mounts]]
125 source = 'other'
126 target = 'assets/css'
127 -- assets/css/main.css --
128 @import "tailwindcss";
129
130 @import "colors/red.css";
131 @import "colors/blue.css";
132 @import "colors/purple.css";
133 -- assets/css/colors/red.css --
134 @import "green.css";
135
136 .red {color: red;}
137 -- assets/css/colors/green.css --
138 .green {color: green;}
139 -- themes/my-theme/assets/css/colors/blue.css --
140 .blue {color: blue;}
141 -- other/colors/purple.css --
142 .purple {color: purple;}
143 -- layouts/home.html --
144 {{ with (templates.Defer (dict "key" "global")) }}
145 {{ with resources.Get "css/main.css" }}
146 {{ $opts := dict "disableInlineImports" true }}
147 {{ with . | css.TailwindCSS $opts }}
148 <link rel="stylesheet" href="{{ .RelPermalink }}">
149 {{ end }}
150 {{ end }}
151 {{ end }}
152 -- package.json --
153 {
154 "devDependencies": {
155 "@tailwindcss/cli": "^4.1.7",
156 "tailwindcss": "^4.1.7"
157 }
158 }
159 `
160
161 b, err := hugolib.TestE(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
162
163 b.Assert(err, qt.IsNotNil)
164 b.Assert(err.Error(), qt.Contains, "Can't resolve 'colors/red.css'")
165 b.Assert(err.Error(), qt.Contains, "You may want to set the 'disableInlineImports' option to false")
166 }
167
168 // This tests an internal feature used in the Hugo themes sites building to allow us to build with
169 // default security policy and still get a partial build even for themes using TailwindCSS.
170 func TestTailwindCSSIgnoreSecError(t *testing.T) {
171 if !htesting.IsCI() {
172 t.Skip("Skip long running test when running locally")
173 }
174
175 files := `
176 -- hugo.toml --
177 [internalExternal]
178 ignoreTailwindCSSSecurityError = true
179 -- package.json --
180 {
181 "license": "MIT",
182 "repository": {
183 "type": "git",
184 "url": "https://github.com/bep/hugo-starter-tailwind-basic.git"
185 },
186 "devDependencies": {
187 "@tailwindcss/cli": "^4.0.1",
188 "tailwindcss": "^4.0.1"
189 },
190 "name": "hugo-starter-tailwind-basic",
191 "version": "0.1.0"
192 }
193 -- assets/css/styles.css --
194 @import "tailwindcss";
195
196 @theme {
197 --font-family-display: "Satoshi", "sans-serif";
198
199 --breakpoint-3xl: 1920px;
200
201 --color-neon-pink: oklch(71.7% 0.25 360);
202 --color-neon-lime: oklch(91.5% 0.258 129);
203 --color-neon-cyan: oklch(91.3% 0.139 195.8);
204 }
205 -- layouts/home.html --
206 {{ $css := resources.Get "css/styles.css" | css.TailwindCSS }}
207 CSS: {{ $css.Content | safeCSS }}|
208 `
209
210 b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo())
211
212 b.AssertFileContent("public/index.html", ".tailwindcss-security-error-ignored")
213 }