Add "hugo mod npm pack"

This commit also introduces a convention where these common JS config files, including `package.hugo.json`, gets mounted into:

```
assets/_jsconfig
´``

These files mapped to their real filename will be added to the environment when running PostCSS, Babel etc., so you can do `process.env.HUGO_FILE_TAILWIND_CONFIG_JS` to resolve the real filename.

But do note that `assets` is a composite/union filesystem, so if your config file is not meant to be overridden, name them something specific.

This commit also adds adds `workDir/node_modules` to `NODE_PATH` and `HUGO_WORKDIR` to the env when running the JS tools above.

Fixes #7644
Fixes #7656
Fixes #7675
This commit is contained in:
Bjørn Erik Pedersen
2020-09-09 22:31:43 +02:00
parent 9df60b62f9
commit 85ba9bfffb
16 changed files with 721 additions and 46 deletions

View File

@@ -22,6 +22,8 @@ import (
"testing"
"time"
"github.com/gohugoio/hugo/modules/npm"
"github.com/gohugoio/hugo/common/loggers"
"github.com/spf13/afero"
@@ -38,7 +40,6 @@ import (
"github.com/spf13/viper"
)
// https://github.com/gohugoio/hugo/issues/6730
func TestHugoModulesVariants(t *testing.T) {
if !isCI() {
t.Skip("skip (relative) long running modules test when running locally")
@@ -60,8 +61,10 @@ path="github.com/gohugoio/hugoTestModule2"
newTestBuilder := func(t testing.TB, moduleOpts string) (*sitesBuilder, func()) {
b := newTestSitesBuilder(t)
workingDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-modules-variants")
tempDir, clean, err := htesting.CreateTempDir(hugofs.Os, "hugo-modules-variants")
b.Assert(err, qt.IsNil)
workingDir := filepath.Join(tempDir, "myhugosite")
b.Assert(os.MkdirAll(workingDir, 0777), qt.IsNil)
b.Fs = hugofs.NewDefault(viper.New())
b.WithWorkingDir(workingDir).WithConfigFile("toml", createConfig(workingDir, moduleOpts))
b.WithTemplates(
@@ -129,6 +132,158 @@ JS imported in module: |
`)
})
t.Run("Create package.json", func(t *testing.T) {
b, clean := newTestBuilder(t, "")
defer clean()
b.WithSourceFile("package.json", `{
"name": "mypack",
"version": "1.2.3",
"scripts": {},
"dependencies": {
"nonon": "error"
}
}`)
b.WithSourceFile("package.hugo.json", `{
"name": "mypack",
"version": "1.2.3",
"scripts": {},
"dependencies": {
"foo": "1.2.3"
},
"devDependencies": {
"postcss-cli": "7.8.0",
"tailwindcss": "1.8.0"
}
}`)
b.Build(BuildCfg{})
b.Assert(npm.Pack(b.H.BaseFs.SourceFs, b.H.BaseFs.Assets.Dirs), qt.IsNil)
b.AssertFileContentFn("package.json", func(s string) bool {
return s == `{
"comments": {
"dependencies": {
"foo": "project",
"react-dom": "github.com/gohugoio/hugoTestModule2"
},
"devDependencies": {
"@babel/cli": "github.com/gohugoio/hugoTestModule2",
"@babel/core": "github.com/gohugoio/hugoTestModule2",
"@babel/preset-env": "github.com/gohugoio/hugoTestModule2",
"postcss-cli": "project",
"tailwindcss": "project"
}
},
"dependencies": {
"foo": "1.2.3",
"react-dom": "^16.13.1"
},
"devDependencies": {
"@babel/cli": "7.8.4",
"@babel/core": "7.9.0",
"@babel/preset-env": "7.9.5",
"postcss-cli": "7.8.0",
"tailwindcss": "1.8.0"
},
"name": "mypack",
"scripts": {},
"version": "1.2.3"
}`
})
})
t.Run("Create package.json, no default", func(t *testing.T) {
b, clean := newTestBuilder(t, "")
defer clean()
b.WithSourceFile("package.json", `{
"name": "mypack",
"version": "1.2.3",
"scripts": {},
"dependencies": {
"moo": "1.2.3"
}
}`)
b.Build(BuildCfg{})
b.Assert(npm.Pack(b.H.BaseFs.SourceFs, b.H.BaseFs.Assets.Dirs), qt.IsNil)
b.AssertFileContentFn("package.json", func(s string) bool {
return s == `{
"comments": {
"dependencies": {
"moo": "project",
"react-dom": "github.com/gohugoio/hugoTestModule2"
},
"devDependencies": {
"@babel/cli": "github.com/gohugoio/hugoTestModule2",
"@babel/core": "github.com/gohugoio/hugoTestModule2",
"@babel/preset-env": "github.com/gohugoio/hugoTestModule2",
"postcss-cli": "github.com/gohugoio/hugoTestModule2",
"tailwindcss": "github.com/gohugoio/hugoTestModule2"
}
},
"dependencies": {
"moo": "1.2.3",
"react-dom": "^16.13.1"
},
"devDependencies": {
"@babel/cli": "7.8.4",
"@babel/core": "7.9.0",
"@babel/preset-env": "7.9.5",
"postcss-cli": "7.1.0",
"tailwindcss": "1.2.0"
},
"name": "mypack",
"scripts": {},
"version": "1.2.3"
}`
})
})
t.Run("Create package.json, no default, no package.json", func(t *testing.T) {
b, clean := newTestBuilder(t, "")
defer clean()
b.Build(BuildCfg{})
b.Assert(npm.Pack(b.H.BaseFs.SourceFs, b.H.BaseFs.Assets.Dirs), qt.IsNil)
b.AssertFileContentFn("package.json", func(s string) bool {
return s == `{
"comments": {
"dependencies": {
"react-dom": "github.com/gohugoio/hugoTestModule2"
},
"devDependencies": {
"@babel/cli": "github.com/gohugoio/hugoTestModule2",
"@babel/core": "github.com/gohugoio/hugoTestModule2",
"@babel/preset-env": "github.com/gohugoio/hugoTestModule2",
"postcss-cli": "github.com/gohugoio/hugoTestModule2",
"tailwindcss": "github.com/gohugoio/hugoTestModule2"
}
},
"dependencies": {
"react-dom": "^16.13.1"
},
"devDependencies": {
"@babel/cli": "7.8.4",
"@babel/core": "7.9.0",
"@babel/preset-env": "7.9.5",
"postcss-cli": "7.1.0",
"tailwindcss": "1.2.0"
},
"name": "myhugosite",
"version": "0.1.0"
}`
})
})
}
// TODO(bep) this fails when testmodBuilder is also building ...