| 1 |
// Copyright 2021 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 babel_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
"github.com/gohugoio/hugo/htesting" |
| 24 |
"github.com/gohugoio/hugo/hugofs" |
| 25 |
"github.com/gohugoio/hugo/hugolib" |
| 26 |
) |
| 27 |
|
| 28 |
func TestTransformBabel(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 !htesting.IsCI() { |
| 33 |
t.Skip("Skip long running test when running locally") |
| 34 |
} |
| 35 |
|
| 36 |
files := ` |
| 37 |
-- assets/js/main.js -- |
| 38 |
/* A Car */ |
| 39 |
class Car { |
| 40 |
constructor(brand) { |
| 41 |
this.carname = brand; |
| 42 |
} |
| 43 |
} |
| 44 |
-- assets/js/main2.js -- |
| 45 |
/* A Car2 */ |
| 46 |
class Car2 { |
| 47 |
constructor(brand) { |
| 48 |
this.carname = brand; |
| 49 |
} |
| 50 |
} |
| 51 |
-- babel.config.js -- |
| 52 |
console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT ); |
| 53 |
|
| 54 |
module.exports = { |
| 55 |
presets: ["@babel/preset-env"], |
| 56 |
}; |
| 57 |
-- hugo.toml -- |
| 58 |
disablekinds = ['taxonomy', 'term', 'page'] |
| 59 |
[security] |
| 60 |
[security.exec] |
| 61 |
allow = ['^node$', '^babel$'] |
| 62 |
-- layouts/home.html -- |
| 63 |
{{ $options := dict "noComments" true }} |
| 64 |
{{ $transpiled := resources.Get "js/main.js" | babel -}} |
| 65 |
Transpiled: {{ $transpiled.Content | safeJS }} |
| 66 |
|
| 67 |
{{ $transpiled := resources.Get "js/main2.js" | babel (dict "sourceMap" "inline") -}} |
| 68 |
Transpiled2: {{ $transpiled.Content | safeJS }} |
| 69 |
|
| 70 |
{{ $transpiled := resources.Get "js/main2.js" | babel (dict "sourceMap" "external") -}} |
| 71 |
Transpiled3: {{ $transpiled.Permalink }} |
| 72 |
-- package.json -- |
| 73 |
{ |
| 74 |
"scripts": {}, |
| 75 |
|
| 76 |
"devDependencies": { |
| 77 |
"@babel/cli": "7.28.6", |
| 78 |
"@babel/core": "7.28.6", |
| 79 |
"@babel/preset-env": "7.28.6" |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
` |
| 84 |
|
| 85 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptInfo()) |
| 86 |
|
| 87 |
b.AssertLogContains("babel: Hugo Environment: production") |
| 88 |
b.AssertFileContent("public/index.html", `var Car2 =`) |
| 89 |
b.AssertFileContent("public/js/main2.js", `var Car2 =`) |
| 90 |
b.AssertFileContent("public/js/main2.js.map", `{"version":3,`) |
| 91 |
b.AssertFileContent("public/index.html", ` |
| 92 |
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozL`) |
| 93 |
} |
| 94 |
|
| 95 |
// See Issue 15043. |
| 96 |
// See Issue 15040. |
| 97 |
func TestTransformBabelConfigResolution(t *testing.T) { |
| •98 |
t.Skip("This tests needs internet access") |
| 99 |
|
| 100 |
if !htesting.IsCI() { |
| 101 |
t.Skip("Skip long running test when running locally") |
| 102 |
} |
| 103 |
|
| 104 |
files := ` |
| 105 |
-- hugo.toml -- |
| 106 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 107 |
[[module.imports]] |
| 108 |
path = "github.com/bep/hugo-mod-nop" |
| 109 |
[security] |
| 110 |
[security.exec] |
| 111 |
allow = ['^go$', '^node$', '^babel$'] |
| 112 |
-- assets/js/main.js -- |
| 113 |
/* A Car */ |
| 114 |
class Car { |
| 115 |
constructor(brand) { |
| 116 |
this.carname = brand; |
| 117 |
} |
| 118 |
} |
| 119 |
-- layouts/home.html -- |
| 120 |
{{ $js := resources.Get "js/main.js" | babel }} |
| 121 |
RelPermalink: {{ $js.RelPermalink }}|HasBody: {{ gt (len $js.Content) 0 }}| |
| 122 |
-- package.json -- |
| 123 |
{ |
| 124 |
"devDependencies": { |
| 125 |
"@babel/cli": "7.28.6", |
| 126 |
"@babel/core": "7.28.6" |
| 127 |
} |
| 128 |
} |
| 129 |
-- go.mod -- |
| 130 |
module github.com/example/project |
| 131 |
|
| 132 |
go 1.26 |
| 133 |
|
| 134 |
replace github.com/bep/hugo-mod-nop => ../external-module |
| 135 |
-- ../external-module/go.mod -- |
| 136 |
module github.com/bep/hugo-mod-nop |
| 137 |
|
| 138 |
go 1.26 |
| 139 |
-- ../external-module/CONFIG_FILE_NAME -- |
| 140 |
CONFIG_FILE_CONTENT |
| 141 |
` |
| 142 |
|
| 143 |
tests := []struct { |
| 144 |
name string |
| 145 |
configFileName string |
| 146 |
configFileContent string |
| 147 |
}{ |
| 148 |
{ |
| 149 |
name: "mjs in module", |
| 150 |
configFileName: "babel.config.mjs", |
| 151 |
configFileContent: "export default {};\n", |
| 152 |
}, |
| 153 |
{ |
| 154 |
name: "cjs in module", |
| 155 |
configFileName: "babel.config.cjs", |
| 156 |
configFileContent: "module.exports = {};\n", |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "js in module", |
| 160 |
configFileName: "babel.config.js", |
| 161 |
configFileContent: "module.exports = {};\n", |
| 162 |
}, |
| 163 |
} |
| 164 |
|
| 165 |
for _, tt := range tests { |
| 166 |
t.Run(tt.name, func(t *testing.T) { |
| 167 |
c := qt.New(t) |
| 168 |
rootDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-integration-test") |
| 169 |
c.Assert(err, qt.IsNil) |
| 170 |
c.Cleanup(clean) |
| 171 |
|
| 172 |
projectDir := filepath.Join(rootDir, "project") |
| 173 |
moduleDir := filepath.Join(rootDir, "external-module") |
| 174 |
c.Assert(os.MkdirAll(projectDir, 0o755), qt.IsNil) |
| 175 |
c.Assert(os.MkdirAll(moduleDir, 0o755), qt.IsNil) |
| 176 |
|
| 177 |
f := strings.ReplaceAll(files, "CONFIG_FILE_NAME", tt.configFileName) |
| 178 |
f = strings.ReplaceAll(f, "CONFIG_FILE_CONTENT", tt.configFileContent) |
| 179 |
|
| 180 |
b := hugolib.Test(c, f, |
| 181 |
hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) { |
| 182 |
cfg.WorkingDir = projectDir |
| 183 |
cfg.NeedsOsFS = true |
| 184 |
cfg.NeedsNpmInstall = true |
| 185 |
}), |
| 186 |
hugolib.TestOptInfo(), |
| 187 |
) |
| 188 |
|
| 189 |
b.AssertFileContent("public/index.html", |
| 190 |
"RelPermalink: /js/main.js|HasBody: true|", |
| 191 |
) |
| 192 |
b.AssertLogContains(tt.configFileName) |
| 193 |
}) |
| 194 |
} |
| 195 |
} |