2020-07-02 16:16:32 +00:00
|
|
|
// Copyright 2020 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2020-10-05 11:34:14 +00:00
|
|
|
"fmt"
|
2020-07-02 16:16:32 +00:00
|
|
|
"os"
|
2020-07-12 10:47:14 +00:00
|
|
|
"os/exec"
|
2020-07-02 16:16:32 +00:00
|
|
|
"path/filepath"
|
2020-07-12 10:47:14 +00:00
|
|
|
"runtime"
|
2020-07-02 16:16:32 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/htesting"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
|
|
|
)
|
|
|
|
|
2020-07-12 10:47:14 +00:00
|
|
|
func TestJSBuildWithNPM(t *testing.T) {
|
2020-07-02 16:16:32 +00:00
|
|
|
if !isCI() {
|
|
|
|
t.Skip("skip (relative) long running modules test when running locally")
|
|
|
|
}
|
|
|
|
|
2020-07-12 10:47:14 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("skip NPM test on Windows")
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:16:32 +00:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
defer func() {
|
|
|
|
os.Chdir(wd)
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
mainJS := `
|
|
|
|
import "./included";
|
2020-07-12 10:47:14 +00:00
|
|
|
import { toCamelCase } from "to-camel-case";
|
|
|
|
|
2020-07-02 16:16:32 +00:00
|
|
|
console.log("main");
|
2020-07-12 10:47:14 +00:00
|
|
|
console.log("To camel:", toCamelCase("space case"));
|
|
|
|
`
|
2020-07-02 16:16:32 +00:00
|
|
|
includedJS := `
|
|
|
|
console.log("included");
|
2020-07-12 10:47:14 +00:00
|
|
|
|
2020-07-02 16:16:32 +00:00
|
|
|
`
|
|
|
|
|
2020-07-12 10:47:14 +00:00
|
|
|
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": {
|
2020-10-05 11:34:14 +00:00
|
|
|
"to-camel-case": "1.0.0"
|
2020-07-12 10:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-js-npm")
|
2020-07-02 16:16:32 +00:00
|
|
|
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())
|
|
|
|
|
|
|
|
// Need to use OS fs for this.
|
|
|
|
b.Fs = hugofs.NewDefault(v)
|
|
|
|
b.WithWorkingDir(workDir)
|
|
|
|
b.WithViper(v)
|
|
|
|
b.WithContent("p1.md", "")
|
|
|
|
|
|
|
|
b.WithTemplates("index.html", `
|
2020-07-12 10:47:14 +00:00
|
|
|
{{ $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")
|
|
|
|
}
|
|
|
|
|
2020-11-05 08:56:22 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// TODO(bep) we really need to get this working on Travis.
|
|
|
|
t.Skip("skip npm test on Windows")
|
|
|
|
}
|
|
|
|
|
2020-07-12 10:47:14 +00:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
defer func() {
|
|
|
|
os.Chdir(wd)
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := qt.New(t)
|
|
|
|
|
2020-10-05 11:34:14 +00:00
|
|
|
workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-js-mod")
|
2020-09-12 04:19:36 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
defer clean()
|
|
|
|
|
2020-10-05 11:34:14 +00:00
|
|
|
config := fmt.Sprintf(`
|
|
|
|
baseURL = "https://example.org"
|
|
|
|
workingDir = %q
|
2020-09-12 04:19:36 +00:00
|
|
|
|
2020-10-05 11:34:14 +00:00
|
|
|
disableKinds = ["page", "section", "term", "taxonomy"]
|
2020-09-12 04:19:36 +00:00
|
|
|
|
|
|
|
[module]
|
|
|
|
[[module.imports]]
|
2020-10-05 11:34:14 +00:00
|
|
|
path="github.com/gohugoio/hugoTestProjectJSModImports"
|
2020-09-12 04:19:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-05 11:34:14 +00:00
|
|
|
`, workDir)
|
2020-09-12 04:19:36 +00:00
|
|
|
|
2020-10-05 11:34:14 +00:00
|
|
|
b := newTestSitesBuilder(t)
|
|
|
|
b.Fs = hugofs.NewDefault(viper.New())
|
|
|
|
b.WithWorkingDir(workDir).WithConfigFile("toml", config).WithLogger(loggers.NewInfoLogger())
|
|
|
|
b.WithSourceFile("go.mod", `module github.com/gohugoio/tests/testHugoModules
|
|
|
|
|
|
|
|
go 1.15
|
|
|
|
|
2020-11-04 15:13:37 +00:00
|
|
|
require github.com/gohugoio/hugoTestProjectJSModImports v0.5.0 // indirect
|
2020-09-12 04:19:36 +00:00
|
|
|
|
|
|
|
`)
|
|
|
|
|
2020-10-05 11:34:14 +00:00
|
|
|
b.WithContent("p1.md", "").WithNothingAdded()
|
2020-09-12 04:19:36 +00:00
|
|
|
|
2020-11-04 15:13:37 +00:00
|
|
|
b.WithSourceFile("package.json", `{
|
|
|
|
"dependencies": {
|
|
|
|
"date-fns": "^2.16.1"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
b.Assert(os.Chdir(workDir), qt.IsNil)
|
|
|
|
_, err = exec.Command("npm", "install").CombinedOutput()
|
|
|
|
b.Assert(err, qt.IsNil)
|
|
|
|
|
2020-09-12 04:19:36 +00:00
|
|
|
b.Build(BuildCfg{})
|
|
|
|
|
2020-10-05 11:34:14 +00:00
|
|
|
b.AssertFileContent("public/js/main.js", `
|
|
|
|
greeting: "greeting configured in mod2"
|
|
|
|
Hello1 from mod1: $
|
|
|
|
return "Hello2 from mod1";
|
|
|
|
var Hugo = "Rocks!";
|
2020-11-04 15:13:37 +00:00
|
|
|
Hello3 from mod2. Date from date-fns: ${today}
|
|
|
|
Hello from lib in the main project
|
|
|
|
Hello5 from mod2.
|
2020-10-05 11:34:14 +00:00
|
|
|
var myparam = "Hugo Rocks!";`)
|
|
|
|
|
2020-09-12 04:19:36 +00:00
|
|
|
}
|