Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postCSS: Improve validation of option 'config' #11186

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions hugolib/filesystems/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,19 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string, error) {

// 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 {
func (fs *BaseFs) ResolveJSConfigFile(name string) (string, bool) {
// 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
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
}
// Fall back to the work dir.
fi, err = fs.Work.Stat(name)
if err == nil {
return fi.(hugofs.FileMetaInfo).Meta().Filename
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
}

return ""
return "", false
}

// MakePathRelative creates a relative path from the given filename.
Expand Down
14 changes: 10 additions & 4 deletions resources/resource_transformers/babel/babel.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,19 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
}

configFile = filepath.Clean(configFile)
isConfigFileDir := false

// We need an absolute filename to the config file.
if !filepath.IsAbs(configFile) {
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
if configFile == "" && t.options.Config != "" {
// Only fail if the user specified config file is not found.
return fmt.Errorf("babel config %q not found:", configFile)
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
if t.options.Config != "" {
if configFile == "" {
// Only fail if the user specified config file is not found.
return fmt.Errorf("babel config file %q not found", configFile)
}
if isConfigFileDir {
loggers.Log().Warnf("babel config %q must be a file, not a directory", configFile)
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions resources/resource_transformers/postcss/postcss.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,19 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
}

configFile = filepath.Clean(configFile)
isConfigFileDir := false

// We need an absolute filename to the config file.
if !filepath.IsAbs(configFile) {
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
if configFile == "" && options.Config != "" {
// Only fail if the user specified config file is not found.
return fmt.Errorf("postcss config %q not found:", options.Config)
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
if options.Config != "" {
if configFile == "" {
// Only fail if the user specified config file is not found.
return fmt.Errorf("postcss config directory %q not found", options.Config)
}
if !isConfigFileDir {
loggers.Log().Warnf("postcss config %q must be a directory", options.Config)
}
}
}

Expand Down