2020-07-02 12:16:32 -04: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 07:34:14 -04:00
|
|
|
"fmt"
|
2020-07-02 12:16:32 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-07-12 06:47:14 -04:00
|
|
|
"runtime"
|
2020-07-02 12:16:32 -04:00
|
|
|
"testing"
|
|
|
|
|
2020-12-18 12:20:12 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
2021-06-09 04:58:18 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2020-12-18 12:20:12 -05:00
|
|
|
|
2020-07-02 12:16:32 -04:00
|
|
|
"github.com/gohugoio/hugo/htesting"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
|
|
|
)
|
|
|
|
|
2020-07-12 06:47:14 -04:00
|
|
|
func TestJSBuildWithNPM(t *testing.T) {
|
2020-12-23 13:47:20 -05:00
|
|
|
if !htesting.IsCI() {
|
2020-07-02 12:16:32 -04:00
|
|
|
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";
|
2020-07-12 06:47:14 -04:00
|
|
|
import { toCamelCase } from "to-camel-case";
|
|
|
|
|
2020-07-02 12:16:32 -04:00
|
|
|
console.log("main");
|
2020-07-12 06:47:14 -04:00
|
|
|
console.log("To camel:", toCamelCase("space case"));
|
|
|
|
`
|
2020-07-02 12:16:32 -04:00
|
|
|
includedJS := `
|
|
|
|
console.log("included");
|
2020-07-12 06:47:14 -04:00
|
|
|
|
2020-07-02 12:16:32 -04:00
|
|
|
`
|
|
|
|
|
2020-07-12 06:47:14 -04: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 07:34:14 -04:00
|
|
|
"to-camel-case": "1.0.0"
|
2020-07-12 06:47:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-js-npm")
|
2020-07-02 12:16:32 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
defer clean()
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
v := config.New()
|
2020-07-02 12:16:32 -04:00
|
|
|
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 06:47:14 -04: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 }}
|
2021-01-18 04:38:09 -05:00
|
|
|
{{ $ts := resources.Get "js/myts.ts" | js.Build (dict "sourcemap" "inline")}}
|
2020-07-12 06:47:14 -04:00
|
|
|
TS: {{ template "print" $ts }}
|
2021-01-18 04:38:09 -05:00
|
|
|
{{ $ts2 := resources.Get "js/myts.ts" | js.Build (dict "sourcemap" "external" "TargetPath" "js/myts2.js")}}
|
|
|
|
TS2: {{ template "print" $ts2 }}
|
2020-07-12 06:47:14 -04:00
|
|
|
{{ define "print" }}RelPermalink: {{.RelPermalink}}|MIME: {{ .MediaType }}|Content: {{ .Content | safeJS }}{{ end }}
|
|
|
|
|
|
|
|
`)
|
|
|
|
|
|
|
|
jsDir := filepath.Join(workDir, "assets", "js")
|
2021-01-18 04:38:09 -05:00
|
|
|
fmt.Println(workDir)
|
2020-07-12 06:47:14 -04:00
|
|
|
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)
|
|
|
|
|
2020-12-18 12:20:12 -05:00
|
|
|
cmd, err := hexec.SafeCommand("npm", "install")
|
|
|
|
b.Assert(err, qt.IsNil)
|
|
|
|
out, err := cmd.CombinedOutput()
|
2020-07-12 06:47:14 -04:00
|
|
|
b.Assert(err, qt.IsNil, qt.Commentf(string(out)))
|
|
|
|
|
|
|
|
b.Build(BuildCfg{})
|
|
|
|
|
2021-01-18 04:38:09 -05:00
|
|
|
b.AssertFileContent("public/js/myts.js", `//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJz`)
|
|
|
|
b.AssertFileContent("public/js/myts2.js.map", `"version": 3,`)
|
2020-07-12 06:47:14 -04:00
|
|
|
b.AssertFileContent("public/index.html", `
|
|
|
|
console.log("included");
|
|
|
|
if (hasSpace.test(string))
|
2020-11-25 11:39:51 -05:00
|
|
|
var React = __toModule(require("react"));
|
2020-07-12 06:47:14 -04:00
|
|
|
function greeter(person) {
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSBuild(t *testing.T) {
|
2020-12-23 13:47:20 -05:00
|
|
|
if !htesting.IsCI() {
|
2020-07-12 06:47:14 -04:00
|
|
|
t.Skip("skip (relative) long running modules test when running locally")
|
|
|
|
}
|
|
|
|
|
2020-11-05 03:56:22 -05: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 06:47:14 -04:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
defer func() {
|
|
|
|
os.Chdir(wd)
|
|
|
|
}()
|
|
|
|
|
|
|
|
c := qt.New(t)
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
workDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-test-js-mod")
|
2020-09-12 00:19:36 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
defer clean()
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
tomlConfig := fmt.Sprintf(`
|
2020-10-05 07:34:14 -04:00
|
|
|
baseURL = "https://example.org"
|
|
|
|
workingDir = %q
|
2020-09-12 00:19:36 -04:00
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
disableKinds = ["page", "section", "term", "taxonomy"]
|
2020-09-12 00:19:36 -04:00
|
|
|
|
|
|
|
[module]
|
|
|
|
[[module.imports]]
|
2020-10-05 07:34:14 -04:00
|
|
|
path="github.com/gohugoio/hugoTestProjectJSModImports"
|
2020-09-12 00:19:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
`, workDir)
|
2020-09-12 00:19:36 -04:00
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
b := newTestSitesBuilder(t)
|
2021-06-09 04:58:18 -04:00
|
|
|
b.Fs = hugofs.NewDefault(config.New())
|
|
|
|
b.WithWorkingDir(workDir).WithConfigFile("toml", tomlConfig).WithLogger(loggers.NewInfoLogger())
|
2020-10-05 07:34:14 -04:00
|
|
|
b.WithSourceFile("go.mod", `module github.com/gohugoio/tests/testHugoModules
|
|
|
|
|
|
|
|
go 1.15
|
|
|
|
|
2021-01-22 11:07:23 -05:00
|
|
|
require github.com/gohugoio/hugoTestProjectJSModImports v0.9.0 // indirect
|
2020-09-12 00:19:36 -04:00
|
|
|
|
|
|
|
`)
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
b.WithContent("p1.md", "").WithNothingAdded()
|
2020-09-12 00:19:36 -04:00
|
|
|
|
2020-11-04 10:13:37 -05:00
|
|
|
b.WithSourceFile("package.json", `{
|
|
|
|
"dependencies": {
|
|
|
|
"date-fns": "^2.16.1"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
b.Assert(os.Chdir(workDir), qt.IsNil)
|
2020-12-18 12:20:12 -05:00
|
|
|
cmd, _ := hexec.SafeCommand("npm", "install")
|
|
|
|
_, err = cmd.CombinedOutput()
|
2020-11-04 10:13:37 -05:00
|
|
|
b.Assert(err, qt.IsNil)
|
|
|
|
|
2020-09-12 00:19:36 -04:00
|
|
|
b.Build(BuildCfg{})
|
|
|
|
|
2020-10-05 07:34:14 -04: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 10:13:37 -05:00
|
|
|
Hello3 from mod2. Date from date-fns: ${today}
|
|
|
|
Hello from lib in the main project
|
|
|
|
Hello5 from mod2.
|
2021-01-22 11:07:23 -05:00
|
|
|
var myparam = "Hugo Rocks!";
|
|
|
|
shim cwd
|
|
|
|
`)
|
2021-01-21 10:52:32 -05:00
|
|
|
|
|
|
|
// React JSX, verify the shimming.
|
2021-01-22 11:07:23 -05:00
|
|
|
b.AssertFileContent("public/js/like.js", `@v0.9.0/assets/js/shims/react.js
|
2021-01-21 10:52:32 -05:00
|
|
|
module.exports = window.ReactDOM;
|
|
|
|
`)
|
2020-09-12 00:19:36 -04:00
|
|
|
}
|