How to ignore directories when rendering (not only files)

Hi all,

I frankly confess that im not very familiar with regex …

I have a directory node_modules which has to be inside static but it doesn’t need to be rendered to public on deployment. How can I make Hugo ignore this directory (I know the ignoreFiles feature but can’t get it working with whole directories)?

Many thanks!

I build source on localhost. I push to testing branch on Github. I pull on testing server and run my build script where Hugo builds the final source into the virtual hosts’ document root. This way, I can control the building, etc.

I would just script something in bash to remove the node_modules directory after the site has been built so that it remains in your souce but when built, does not show up in static (as you’ll be deleting it)

I’m not entirely sure I understand the issue, perhaps there could be a more logical or different/better approach.

You could try something like this at https://regex101.com

You can learn regex here and also how to match directory and file patterns.

	ignoreFiles := cast.ToStringSlice(s.Cfg.Get("ignoreFiles"))
	if len(ignoreFiles) > 0 {
		for _, ignorePattern := range ignoreFiles {
			match, err := regexp.MatchString(ignorePattern, filePath)
			if err != nil {
				helpers.DistinctErrorLog.Printf("Invalid regexp '%s' in ignoreFiles: %s", ignorePattern, err)
				return false
			} else if match {
				return true
			}
		}
	}

The above is the hugo code. It’s basically processing whate regex list you give it, and you can probably give it a regex which matches a directory path instead of files.

In the documentation examples, they are ignoring files ending with .foo and .bar, using regular expression.

^ == start of line
. == match anything
$ end of line

I won’t do into the details of regex here, but you can even specify multiple directories:

So possibly try something like

ignoreFiles = [ "(.*/home/user/dev/node_modules/|/usr/share/node_modules)"]

or

ignoreFiles = [ "^(.*/home/user/dev/node_modules/)$", "^(.*/directory_2/)$"] etc.

I’m unsure which, I’ve never used this directive, and I haven’t the means to test it out locally right now.

Thank you for your comprehensive reply. Very much appreciated.

The issue is simple: How can I avoid the directory /static/node_modules/ from being copied to /public/ when running hugo?

you can probably give it a regex which matches a directory path instead of files.

I have tried all kinds of regexes (full path, directory name without path, backslash escaped/not escaped and so on …) and tested successfully against regex101.com – but no luck in hugo. I’m either not firm enough with regex or ignoreFiles function doesn’t accept directories but only files.

The static folder has only one purpose - to copy files to the public folder. You need to place your folder in any other place. It looks strange to place your folder in the static folder, If you do not want to copy it into public.

1 Like

If it’s not needed in the final output, it’s not needed to be put into the static directory.

Hi;
after reading this threat, because I have the same issue I permit myself to post my question
because the directory I want to ignore is in static dir from the theme I use.

I means the directory I want to ignore is font-awesome-v4.7.0 which come with the theme Agency
so I tried :

ignoreFiles = [ “themes/agency/static/font-awesome-v4.7.0” ]

ignoreFiles = [ “themes/agency/static/font-awesome-v4.7.0/*” ]

ignoreFiles = [ “static/font-awesome-v4.7.0” ]

ignoreFiles = [ “font-awesome-v4.7.0/*” ]

ignoreFiles = [ “font-awesome-v4.7.0” ]

ignoreFiles = [ “font-awesome-v4.7.0/*” ]

whithout any success
when I run hugo font-awesome-v4.7.0 always render as ** ./font-awesome-v4.7.0