integration_test.go (2545B)
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 "testing"
18
19 jww "github.com/spf13/jwalterweatherman"
20
21 "github.com/gohugoio/hugo/htesting"
22 "github.com/gohugoio/hugo/hugolib"
23 )
24
25 func TestTransformBabel(t *testing.T) {
26 if !htesting.IsCI() {
27 t.Skip("Skip long running test when running locally")
28 }
29
30 files := `
31 -- assets/js/main.js --
32 /* A Car */
33 class Car {
34 constructor(brand) {
35 this.carname = brand;
36 }
37 }
38 -- assets/js/main2.js --
39 /* A Car2 */
40 class Car2 {
41 constructor(brand) {
42 this.carname = brand;
43 }
44 }
45 -- babel.config.js --
46 console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT );
47
48 module.exports = {
49 presets: ["@babel/preset-env"],
50 };
51 -- config.toml --
52 disablekinds = ['taxonomy', 'term', 'page']
53 [security]
54 [security.exec]
55 allow = ['^npx$', '^babel$']
56 -- layouts/index.html --
57 {{ $options := dict "noComments" true }}
58 {{ $transpiled := resources.Get "js/main.js" | babel -}}
59 Transpiled: {{ $transpiled.Content | safeJS }}
60
61 {{ $transpiled := resources.Get "js/main2.js" | babel (dict "sourceMap" "inline") -}}
62 Transpiled2: {{ $transpiled.Content | safeJS }}
63
64 {{ $transpiled := resources.Get "js/main2.js" | babel (dict "sourceMap" "external") -}}
65 Transpiled3: {{ $transpiled.Permalink }}
66 -- package.json --
67 {
68 "scripts": {},
69
70 "devDependencies": {
71 "@babel/cli": "7.8.4",
72 "@babel/core": "7.9.0",
73 "@babel/preset-env": "7.9.5"
74 }
75 }
76
77 `
78
79 b := hugolib.NewIntegrationTestBuilder(
80 hugolib.IntegrationTestConfig{
81 T: t,
82 TxtarString: files,
83 NeedsOsFS: true,
84 NeedsNpmInstall: true,
85 LogLevel: jww.LevelInfo,
86 }).Build()
87
88 b.AssertLogContains("babel: Hugo Environment: production")
89 b.AssertFileContent("public/index.html", `var Car2 =`)
90 b.AssertFileContent("public/js/main2.js", `var Car2 =`)
91 b.AssertFileContent("public/js/main2.js.map", `{"version":3,`)
92 b.AssertFileContent("public/index.html", `
93 //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozL`)
94 }