Skip to content

Commit 41fbca2

Browse files
committed
postCSS: Improve validation of option 'config'
1 parent e08d9af commit 41fbca2

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

Diff for: hugolib/filesystems/basefs.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -209,19 +209,19 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string, error) {
209209

210210
// ResolveJSConfigFile resolves the JS-related config file to a absolute
211211
// filename. One example of such would be postcss.config.js.
212-
func (b *BaseFs) ResolveJSConfigFile(name string) string {
212+
func (fs *BaseFs) ResolveJSConfigFile(name string) (string, bool) {
213213
// First look in assets/_jsconfig
214-
fi, err := b.Assets.Fs.Stat(filepath.Join(files.FolderJSConfig, name))
214+
fi, err := fs.Assets.Fs.Stat(filepath.Join(files.FolderJSConfig, name))
215215
if err == nil {
216-
return fi.(hugofs.FileMetaInfo).Meta().Filename
216+
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
217217
}
218218
// Fall back to the work dir.
219-
fi, err = b.Work.Stat(name)
219+
fi, err = fs.Work.Stat(name)
220220
if err == nil {
221-
return fi.(hugofs.FileMetaInfo).Meta().Filename
221+
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
222222
}
223223

224-
return ""
224+
return "", false
225225
}
226226

227227
// SourceFilesystems contains the different source file systems. These can be

Diff for: internal/js/esbuild/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *BuildClient) Build(opts Options) (api.BuildResult, error) {
5555
opts.OutDir = c.rs.AbsPublishDir
5656
opts.ResolveDir = c.rs.Cfg.BaseConfig().WorkingDir // where node_modules gets resolved
5757
opts.AbsWorkingDir = opts.ResolveDir
58-
opts.TsConfig = c.rs.ResolveJSConfigFile("tsconfig.json")
58+
opts.TsConfig, _ = c.rs.ResolveJSConfigFile("tsconfig.json")
5959
assetsResolver := newFSResolver(c.rs.Assets.Fs)
6060

6161
if err := opts.validate(); err != nil {

Diff for: resources/resource_transformers/babel/babel.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,19 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
134134
}
135135

136136
configFile = filepath.Clean(configFile)
137+
isConfigFileDir := false
137138

138139
// We need an absolute filename to the config file.
139140
if !filepath.IsAbs(configFile) {
140-
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
141-
if configFile == "" && t.options.Config != "" {
142-
// Only fail if the user specified config file is not found.
143-
return fmt.Errorf("babel config %q not found", configFile)
141+
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
142+
if t.options.Config != "" {
143+
if configFile == "" {
144+
// Only fail if the user specified config file is not found.
145+
return fmt.Errorf("babel config file %q not found", configFile)
146+
}
147+
if isConfigFileDir {
148+
loggers.Log().Warnf("babel config %q must be a file, not a directory", configFile)
149+
}
144150
}
145151
}
146152

Diff for: resources/resource_transformers/cssjs/postcss.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,19 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
157157
}
158158

159159
configFile = filepath.Clean(configFile)
160+
isConfigFileDir := false
160161

161162
// We need an absolute filename to the config file.
162163
if !filepath.IsAbs(configFile) {
163-
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
164-
if configFile == "" && options.Config != "" {
165-
// Only fail if the user specified config file is not found.
166-
return fmt.Errorf("postcss config %q not found", options.Config)
164+
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
165+
if options.Config != "" {
166+
if configFile == "" {
167+
// Only fail if the user specified config file is not found.
168+
return fmt.Errorf("postcss config directory %q not found", options.Config)
169+
}
170+
if !isConfigFileDir {
171+
loggers.Log().Warnf("postcss config %q must be a directory", options.Config)
172+
}
167173
}
168174
}
169175

0 commit comments

Comments
 (0)