Skip to content

Commit

Permalink
postCSS: Improve validation of option 'config'
Browse files Browse the repository at this point in the history
  • Loading branch information
deining committed Jan 28, 2025
1 parent e08d9af commit 41fbca2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
12 changes: 6 additions & 6 deletions hugolib/filesystems/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,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 (b *BaseFs) ResolveJSConfigFile(name string) string {
func (fs *BaseFs) ResolveJSConfigFile(name string) (string, bool) {
// First look in assets/_jsconfig
fi, err := b.Assets.Fs.Stat(filepath.Join(files.FolderJSConfig, name))
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 = b.Work.Stat(name)
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
}

// SourceFilesystems contains the different source file systems. These can be
Expand Down
2 changes: 1 addition & 1 deletion internal/js/esbuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *BuildClient) Build(opts Options) (api.BuildResult, error) {
opts.OutDir = c.rs.AbsPublishDir
opts.ResolveDir = c.rs.Cfg.BaseConfig().WorkingDir // where node_modules gets resolved
opts.AbsWorkingDir = opts.ResolveDir
opts.TsConfig = c.rs.ResolveJSConfigFile("tsconfig.json")
opts.TsConfig, _ = c.rs.ResolveJSConfigFile("tsconfig.json")
assetsResolver := newFSResolver(c.rs.Assets.Fs)

if err := opts.validate(); err != nil {
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/cssjs/postcss.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,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

0 comments on commit 41fbca2

Please sign in to comment.