mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
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:
@@ -49,6 +49,9 @@ type BaseFs struct {
|
||||
// SourceFilesystems contains the different source file systems.
|
||||
*SourceFilesystems
|
||||
|
||||
// The project source.
|
||||
SourceFs afero.Fs
|
||||
|
||||
// The filesystem used to publish the rendered site.
|
||||
// This usually maps to /my-project/public.
|
||||
PublishFs afero.Fs
|
||||
@@ -100,6 +103,23 @@ func (b *BaseFs) RelContentDir(filename string) string {
|
||||
return filename
|
||||
}
|
||||
|
||||
// ResolveJSConfigFile resolves the JS-related config file to a absolute
|
||||
// filename. One example of such would be postcss.config.js.
|
||||
func (fs *BaseFs) ResolveJSConfigFile(name string) string {
|
||||
// First look in assets/_jsconfig
|
||||
fi, err := fs.Assets.Fs.Stat(filepath.Join(files.FolderJSConfig, name))
|
||||
if err == nil {
|
||||
return fi.(hugofs.FileMetaInfo).Meta().Filename()
|
||||
}
|
||||
// Fall back to the work dir.
|
||||
fi, err = fs.Work.Stat(name)
|
||||
if err == nil {
|
||||
return fi.(hugofs.FileMetaInfo).Meta().Filename()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// SourceFilesystems contains the different source file systems. These can be
|
||||
// composite file systems (theme and project etc.), and they have all root
|
||||
// set to the source type the provides: data, i18n, static, layouts.
|
||||
@@ -346,8 +366,10 @@ func NewBase(p *paths.Paths, logger *loggers.Logger, options ...func(*BaseFs) er
|
||||
}
|
||||
|
||||
publishFs := hugofs.NewBaseFileDecorator(afero.NewBasePathFs(fs.Destination, p.AbsPublishDir))
|
||||
sourceFs := hugofs.NewBaseFileDecorator(afero.NewBasePathFs(fs.Source, p.WorkingDir))
|
||||
|
||||
b := &BaseFs{
|
||||
SourceFs: sourceFs,
|
||||
PublishFs: publishFs,
|
||||
}
|
||||
|
||||
@@ -696,11 +718,16 @@ type filesystemsCollector struct {
|
||||
|
||||
func (c *filesystemsCollector) addDirs(rfs *hugofs.RootMappingFs) {
|
||||
for _, componentFolder := range files.ComponentFolders {
|
||||
dirs, err := rfs.Dirs(componentFolder)
|
||||
c.addDir(rfs, componentFolder)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
c.overlayDirs[componentFolder] = append(c.overlayDirs[componentFolder], dirs...)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *filesystemsCollector) addDir(rfs *hugofs.RootMappingFs, componentFolder string) {
|
||||
dirs, err := rfs.Dirs(componentFolder)
|
||||
|
||||
if err == nil {
|
||||
c.overlayDirs[componentFolder] = append(c.overlayDirs[componentFolder], dirs...)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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 ...
|
||||
|
@@ -873,6 +873,10 @@ func TestResourceChainPostCSS(t *testing.T) {
|
||||
|
||||
postcssConfig := `
|
||||
console.error("Hugo Environment:", process.env.HUGO_ENVIRONMENT );
|
||||
// https://github.com/gohugoio/hugo/issues/7656
|
||||
console.error("package.json:", process.env.HUGO_FILE_PACKAGE_JSON );
|
||||
console.error("PostCSS Config File:", process.env.HUGO_FILE_POSTCSS_CONFIG_JS );
|
||||
|
||||
|
||||
module.exports = {
|
||||
plugins: [
|
||||
@@ -954,6 +958,8 @@ class-in-b {
|
||||
|
||||
// Make sure Node sees this.
|
||||
b.Assert(logBuf.String(), qt.Contains, "Hugo Environment: production")
|
||||
b.Assert(logBuf.String(), qt.Contains, fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", workDir))
|
||||
b.Assert(logBuf.String(), qt.Contains, fmt.Sprintf("package.json: %s/package.json", workDir))
|
||||
|
||||
b.AssertFileContent("public/index.html", `
|
||||
Styles RelPermalink: /css/styles.css
|
||||
|
Reference in New Issue
Block a user