github.com/gohugoio/hugo@v0.88.1/hugolib/resource_chain_babel_test.go (about) 1 // Copyright 2020 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 hugolib 15 16 import ( 17 "bytes" 18 "os" 19 "path/filepath" 20 "testing" 21 22 "github.com/gohugoio/hugo/config" 23 24 "github.com/gohugoio/hugo/common/hexec" 25 26 jww "github.com/spf13/jwalterweatherman" 27 28 "github.com/gohugoio/hugo/htesting" 29 30 qt "github.com/frankban/quicktest" 31 32 "github.com/gohugoio/hugo/hugofs" 33 34 "github.com/gohugoio/hugo/common/loggers" 35 ) 36 37 func TestResourceChainBabel(t *testing.T) { 38 if !htesting.IsCI() { 39 t.Skip("skip (relative) long running modules test when running locally") 40 } 41 42 wd, _ := os.Getwd() 43 defer func() { 44 os.Chdir(wd) 45 }() 46 47 c := qt.New(t) 48 49 packageJSON := `{ 50 "scripts": {}, 51 52 "devDependencies": { 53 "@babel/cli": "7.8.4", 54 "@babel/core": "7.9.0", 55 "@babel/preset-env": "7.9.5" 56 } 57 } 58 ` 59 60 babelConfig := ` 61 console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT ); 62 63 module.exports = { 64 presets: ["@babel/preset-env"], 65 }; 66 67 ` 68 69 js := ` 70 /* A Car */ 71 class Car { 72 constructor(brand) { 73 this.carname = brand; 74 } 75 } 76 ` 77 78 js2 := ` 79 /* A Car2 */ 80 class Car2 { 81 constructor(brand) { 82 this.carname = brand; 83 } 84 } 85 ` 86 87 workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-babel") 88 c.Assert(err, qt.IsNil) 89 defer clean() 90 91 var logBuf bytes.Buffer 92 logger := loggers.NewBasicLoggerForWriter(jww.LevelInfo, &logBuf) 93 94 v := config.New() 95 v.Set("workingDir", workDir) 96 v.Set("disableKinds", []string{"taxonomy", "term", "page"}) 97 b := newTestSitesBuilder(t).WithLogger(logger) 98 99 // Need to use OS fs for this. 100 b.Fs = hugofs.NewDefault(v) 101 b.WithWorkingDir(workDir) 102 b.WithViper(v) 103 b.WithContent("p1.md", "") 104 105 b.WithTemplates("index.html", ` 106 {{ $options := dict "noComments" true }} 107 {{ $transpiled := resources.Get "js/main.js" | babel -}} 108 Transpiled: {{ $transpiled.Content | safeJS }} 109 110 {{ $transpiled := resources.Get "js/main2.js" | babel (dict "sourceMap" "inline") -}} 111 Transpiled2: {{ $transpiled.Content | safeJS }} 112 113 {{ $transpiled := resources.Get "js/main2.js" | babel (dict "sourceMap" "external") -}} 114 Transpiled3: {{ $transpiled.Permalink }} 115 116 `) 117 118 jsDir := filepath.Join(workDir, "assets", "js") 119 b.Assert(os.MkdirAll(jsDir, 0777), qt.IsNil) 120 b.WithSourceFile("assets/js/main.js", js) 121 b.WithSourceFile("assets/js/main2.js", js2) 122 b.WithSourceFile("package.json", packageJSON) 123 b.WithSourceFile("babel.config.js", babelConfig) 124 125 b.Assert(os.Chdir(workDir), qt.IsNil) 126 cmd, _ := hexec.SafeCommand("npm", "install") 127 _, err = cmd.CombinedOutput() 128 b.Assert(err, qt.IsNil) 129 130 b.Build(BuildCfg{}) 131 132 // Make sure Node sees this. 133 b.Assert(logBuf.String(), qt.Contains, "babel: Hugo Environment: production") 134 b.Assert(err, qt.IsNil) 135 136 b.AssertFileContent("public/index.html", ` 137 var Car = function Car(brand) { 138 _classCallCheck(this, Car); 139 140 this.carname = brand; 141 }; 142 `) 143 b.AssertFileContent("public/index.html", ` 144 var Car2 = function Car2(brand) { 145 _classCallCheck(this, Car2); 146 147 this.carname = brand; 148 }; 149 `) 150 b.AssertFileContent("public/js/main2.js", ` 151 var Car2 = function Car2(brand) { 152 _classCallCheck(this, Car2); 153 154 this.carname = brand; 155 }; 156 `) 157 b.AssertFileContent("public/js/main2.js.map", `{"version":3,`) 158 b.AssertFileContent("public/index.html", ` 159 //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozL`) 160 }