| 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 |
"os" |
| 18 |
"os/exec" |
| 19 |
"runtime" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
"github.com/gohugoio/hugo/htesting" |
| 25 |
"github.com/gohugoio/hugo/hugolib" |
| 26 |
) |
| 27 |
|
| 28 |
func TestTailwindV4Basic(t *testing.T) { |
| •29 |
if os.Getenv("AUTOPKGTEST_TMP") == "" { |
| 30 |
t.Skip("Skip test as network is inaccessible during Debian package build") |
| 31 |
} |
| 32 |
if !(runtime.GOARCH == "amd64" || runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") { |
| 33 |
t.Skipf("Skip test: prebuild of @parcel/watcher is available only for x64 (amd64), arm and arm64; see https://www.npmjs.com/search?q=%%40parcel%%2Fwatcher-linux") |
| 34 |
} |
| 35 |
if runtime.GOARCH == "arm" { |
| 36 |
cmd := exec.Command("/usr/bin/go", "env", "GOARM") |
| 37 |
out, err := cmd.Output() |
| 38 |
if err != nil { |
| 39 |
t.Skipf("Skip test: failed to check GOARM: %v: %v", cmd, err) |
| 40 |
} |
| 41 |
goarm := strings.TrimSpace(string(out)) |
| 42 |
if goarm == "5,softfloat" { |
| 43 |
t.Skipf("Skip test: prebuild of @parcel/watcher is available for armhf but not for armel (GOARM=%v); see https://www.npmjs.com/search?q=%%40parcel%%2Fwatcher-linux", goarm) |
| 44 |
} |
| 45 |
} |
| 46 |
if !htesting.IsCI() { |
| 47 |
t.Skip("Skip long running test when running locally") |
| 48 |
} |
| 49 |
|
| 50 |
files := ` |
| 51 |
-- hugo.toml -- |
| 52 |
security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$'] |
| 53 |
-- package.json -- |
| 54 |
{ |
| 55 |
"license": "MIT", |
| 56 |
"repository": { |
| 57 |
"type": "git", |
| 58 |
"url": "https://github.com/bep/hugo-starter-tailwind-basic.git" |
| 59 |
}, |
| 60 |
"devDependencies": { |
| 61 |
"@tailwindcss/cli": "^4.0.1", |
| 62 |
"tailwindcss": "^4.0.1" |
| 63 |
}, |
| 64 |
"name": "hugo-starter-tailwind-basic", |
| 65 |
"version": "0.1.0" |
| 66 |
} |
| 67 |
-- assets/css/styles.css -- |
| 68 |
@import "tailwindcss"; |
| 69 |
|
| 70 |
@theme { |
| 71 |
--font-family-display: "Satoshi", "sans-serif"; |
| 72 |
|
| 73 |
--breakpoint-3xl: 1920px; |
| 74 |
|
| 75 |
--color-neon-pink: oklch(71.7% 0.25 360); |
| 76 |
--color-neon-lime: oklch(91.5% 0.258 129); |
| 77 |
--color-neon-cyan: oklch(91.3% 0.139 195.8); |
| 78 |
} |
| 79 |
-- layouts/home.html -- |
| 80 |
{{ $css := resources.Get "css/styles.css" | css.TailwindCSS }} |
| 81 |
CSS: {{ $css.Content | safeCSS }}| |
| 82 |
` |
| 83 |
|
| 84 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo()) |
| 85 |
|
| 86 |
b.AssertFileContent("public/index.html", "/*! tailwindcss v4.") |
| 87 |
} |
| 88 |
|
| 89 |
// See issue 15103. |
| 90 |
func TestTailwindCSSImportContext(t *testing.T) { |
| •91 |
t.Skip("This test needs npm with internet access") |
| 92 |
|
| 93 |
t.Parallel() |
| 94 |
htesting.SkipSlowTestUnlessCI(t) |
| 95 |
|
| 96 |
files := ` |
| 97 |
-- hugo.toml -- |
| 98 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 99 |
security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$'] |
| 100 |
-- assets/css/main.css -- |
| 101 |
@import "tailwindcss"; |
| 102 |
|
| 103 |
@import "foo.css"; |
| 104 |
@import "bar.css"; |
| 105 |
-- assets/css/foo.css -- |
| 106 |
.foo {color: orange;} |
| 107 |
-- layouts/home.html -- |
| 108 |
{{ $foo := resources.FromString "foo.css" ".foo {color: blue;}" }} |
| 109 |
{{ $bar := resources.FromString "bar.css" ".bar {color: green;}" }} |
| 110 |
{{ $opts := dict "importContext" (slice $foo $bar) }} |
| 111 |
{{ $css := resources.Get "css/main.css" | css.TailwindCSS $opts }} |
| 112 |
CSS: {{ $css.Content | safeCSS }}| |
| 113 |
-- package.json -- |
| 114 |
{ |
| 115 |
"devDependencies": { |
| 116 |
"@tailwindcss/cli": "^4.1.7", |
| 117 |
"tailwindcss": "^4.1.7" |
| 118 |
} |
| 119 |
} |
| 120 |
` |
| 121 |
|
| 122 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo()) |
| 123 |
|
| 124 |
// foo.css resolves in the import context before the assets filesystem. |
| 125 |
b.AssertFileContent("public/index.html", |
| 126 |
".foo {\n color: blue;\n }", |
| 127 |
".bar {\n color: green;\n }", |
| 128 |
) |
| 129 |
} |
| 130 |
|
| 131 |
func TestTailwindCSSNoInlineImportsIssue13719(t *testing.T) { |
| •132 |
t.Skip("This tests needs internet access") |
| 133 |
t.Parallel() |
| 134 |
htesting.SkipSlowTestUnlessCI(t) |
| 135 |
|
| 136 |
files := ` |
| 137 |
-- hugo.toml -- |
| 138 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 139 |
theme = 'my-theme' |
| 140 |
|
| 141 |
security.exec.allow = ['^go$', '^git$', '^node$', '^tailwindcss$'] |
| 142 |
|
| 143 |
[[module.mounts]] |
| 144 |
source = 'assets' |
| 145 |
target = 'assets' |
| 146 |
|
| 147 |
[[module.mounts]] |
| 148 |
source = 'other' |
| 149 |
target = 'assets/css' |
| 150 |
-- assets/css/main.css -- |
| 151 |
@import "tailwindcss"; |
| 152 |
|
| 153 |
@import "colors/red.css"; |
| 154 |
@import "colors/blue.css"; |
| 155 |
@import "colors/purple.css"; |
| 156 |
-- assets/css/colors/red.css -- |
| 157 |
@import "green.css"; |
| 158 |
|
| 159 |
.red {color: red;} |
| 160 |
-- assets/css/colors/green.css -- |
| 161 |
.green {color: green;} |
| 162 |
-- themes/my-theme/assets/css/colors/blue.css -- |
| 163 |
.blue {color: blue;} |
| 164 |
-- other/colors/purple.css -- |
| 165 |
.purple {color: purple;} |
| 166 |
-- layouts/home.html -- |
| 167 |
{{ with (templates.Defer (dict "key" "global")) }} |
| 168 |
{{ with resources.Get "css/main.css" }} |
| 169 |
{{ $opts := dict "disableInlineImports" true }} |
| 170 |
{{ with . | css.TailwindCSS $opts }} |
| 171 |
<link rel="stylesheet" href="{{ .RelPermalink }}"> |
| 172 |
{{ end }} |
| 173 |
{{ end }} |
| 174 |
{{ end }} |
| 175 |
-- package.json -- |
| 176 |
{ |
| 177 |
"devDependencies": { |
| 178 |
"@tailwindcss/cli": "^4.1.7", |
| 179 |
"tailwindcss": "^4.1.7" |
| 180 |
} |
| 181 |
} |
| 182 |
` |
| 183 |
|
| 184 |
b, err := hugolib.TestE(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo()) |
| 185 |
|
| 186 |
b.Assert(err, qt.IsNotNil) |
| 187 |
b.Assert(err.Error(), qt.Contains, "Can't resolve 'colors/red.css'") |
| 188 |
b.Assert(err.Error(), qt.Contains, "You may want to set the 'disableInlineImports' option to false") |
| 189 |
} |