I am developing a small static site generator on top of Webpack and React. I am currently making it more dynamic. One of them makes it more customizable.
Given a site structure such as
. βββ _book βββ assets βββ build βββ drafts βββ manuscript βββ node_modules βββ pages βββ project_source βββ styles
I would like to require files only from a specific directory or directories. In this case, it is enough to require Markdown files from manuscript . Naively, var req = require.context('manuscript', true, /^\.\/.*\.md$/) will work.
The problem is that this should become dynamic as I pass the directory through the node generator configuration. Since require.context relies on fixed values, it seems to me that I need to change the context to the root of the site using something like var req = require.context('.', true, /^.*\.md$/) , and then check on req.keys() so that it matches my configuration.
In practice, it is very slow, as it will pass through the whole tree! Especially node_modules can contain many files, and this should be avoided at all costs.
Is there a neat way to exclude node_modules from require.context ? I believe some form of Regex might work, although I am open to other ideas.
source share