Debian Codegraph @

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