github.com/neohugo/neohugo@v0.123.8/resources/resource_transformers/babel/babel_integration_test.go (about)

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