diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-07-12 12:47:14 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-07-13 10:56:23 +0200 |
commit | 9df98ec49ca9fa326125ccfee626b6e46c6ab14b (patch) | |
tree | cdfcfcc9522366d69b7b543638b689852a5e1252 /hugolib/js_test.go | |
parent | 2fc33807077cd25bf91f2298bf1a8ace126881a7 (diff) |
Add proper Media Type handling in js.Build
See #732
Diffstat (limited to 'hugolib/js_test.go')
-rw-r--r-- | hugolib/js_test.go | 139 |
1 files changed, 126 insertions, 13 deletions
diff --git a/hugolib/js_test.go b/hugolib/js_test.go index 1e59927c7..a421ed338 100644 --- a/hugolib/js_test.go +++ b/hugolib/js_test.go @@ -15,7 +15,9 @@ package hugolib import ( "os" + "os/exec" "path/filepath" + "runtime" "testing" "github.com/gohugoio/hugo/htesting" @@ -29,11 +31,15 @@ import ( "github.com/gohugoio/hugo/common/loggers" ) -func TestJS_Build(t *testing.T) { +func TestJSBuildWithNPM(t *testing.T) { if !isCI() { t.Skip("skip (relative) long running modules test when running locally") } + if runtime.GOOS == "windows" { + t.Skip("skip NPM test on Windows") + } + wd, _ := os.Getwd() defer func() { os.Chdir(wd) @@ -43,13 +49,44 @@ func TestJS_Build(t *testing.T) { mainJS := ` import "./included"; + import { toCamelCase } from "to-camel-case"; + console.log("main"); - ` + console.log("To camel:", toCamelCase("space case")); +` includedJS := ` console.log("included"); + ` - workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-babel") + jsxContent := ` +import * as React from 'react' +import * as ReactDOM from 'react-dom' + + ReactDOM.render( + <h1>Hello, world!</h1>, + document.getElementById('root') + ); +` + + tsContent := `function greeter(person: string) { + return "Hello, " + person; +} + +let user = [0, 1, 2]; + +document.body.textContent = greeter(user);` + + packageJSON := `{ + "scripts": {}, + + "dependencies": { + "to-camel-case": "1.0.0" + } +} +` + + workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-js-npm") c.Assert(err, qt.IsNil) defer clean() @@ -65,23 +102,99 @@ func TestJS_Build(t *testing.T) { b.WithContent("p1.md", "") b.WithTemplates("index.html", ` - {{ $options := dict "minify" true }} - {{ $transpiled := resources.Get "js/main.js" | js.Build $options }} - Built: {{ $transpiled.Content | safeJS }} - `) +{{ $options := dict "minify" false "externals" (slice "react" "react-dom") }} +{{ $js := resources.Get "js/main.js" | js.Build $options }} +JS: {{ template "print" $js }} +{{ $jsx := resources.Get "js/myjsx.jsx" | js.Build $options }} +JSX: {{ template "print" $jsx }} +{{ $ts := resources.Get "js/myts.ts" | js.Build }} +TS: {{ template "print" $ts }} + +{{ define "print" }}RelPermalink: {{.RelPermalink}}|MIME: {{ .MediaType }}|Content: {{ .Content | safeJS }}{{ end }} + +`) + + jsDir := filepath.Join(workDir, "assets", "js") + b.Assert(os.MkdirAll(jsDir, 0777), qt.IsNil) + b.Assert(os.Chdir(workDir), qt.IsNil) + b.WithSourceFile("package.json", packageJSON) + b.WithSourceFile("assets/js/main.js", mainJS) + b.WithSourceFile("assets/js/myjsx.jsx", jsxContent) + b.WithSourceFile("assets/js/myts.ts", tsContent) + + b.WithSourceFile("assets/js/included.js", includedJS) + + out, err := exec.Command("npm", "install").CombinedOutput() + b.Assert(err, qt.IsNil, qt.Commentf(string(out))) + + b.Build(BuildCfg{}) + + b.AssertFileContent("public/index.html", ` +console.log("included"); +if (hasSpace.test(string)) +const React = __toModule(require("react")); +function greeter(person) { +`) + +} + +func TestJSBuild(t *testing.T) { + if !isCI() { + t.Skip("skip (relative) long running modules test when running locally") + } + + wd, _ := os.Getwd() + defer func() { + os.Chdir(wd) + }() + + c := qt.New(t) + + mainJS := ` + import "./included"; + + console.log("main"); + +` + includedJS := ` + console.log("included"); + + ` + + workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-js") + c.Assert(err, qt.IsNil) + defer clean() + + v := viper.New() + v.Set("workingDir", workDir) + v.Set("disableKinds", []string{"taxonomy", "term", "page"}) + b := newTestSitesBuilder(t).WithLogger(loggers.NewWarningLogger()) + + b.Fs = hugofs.NewDefault(v) + b.WithWorkingDir(workDir) + b.WithViper(v) + b.WithContent("p1.md", "") + + b.WithTemplates("index.html", ` +{{ $js := resources.Get "js/main.js" | js.Build }} +JS: {{ template "print" $js }} + + +{{ define "print" }}RelPermalink: {{.RelPermalink}}|MIME: {{ .MediaType }}|Content: {{ .Content | safeJS }}{{ end }} + +`) jsDir := filepath.Join(workDir, "assets", "js") b.Assert(os.MkdirAll(jsDir, 0777), qt.IsNil) + b.Assert(os.Chdir(workDir), qt.IsNil) b.WithSourceFile("assets/js/main.js", mainJS) b.WithSourceFile("assets/js/included.js", includedJS) - _, err = captureStdout(func() error { - return b.BuildE(BuildCfg{}) - }) - b.Assert(err, qt.IsNil) + b.Build(BuildCfg{}) b.AssertFileContent("public/index.html", ` - Built: (()=>{console.log("included");console.log("main");})(); - `) +console.log("included"); + +`) } |