RequireJS - exclude by directory (or regex)

This is part of my build config for the requireJS optimizer, r.js.

exclude: [


My question is this: is it possible to exclude ALL dependencies starting with widgets/


The docs don't seem to indicate that a regex or anything similar can be passed here. Is there any other config option I'm missing?


source to share

1 answer

I'm pretty sure you can't pass a regex to exclude

. I talk about this after reading the source r.js

. The processing exclude

uses an internal named function findBuildModule

that compares what is passed exclude

to the module names with ===

. And yet there is no way to say r.js

"exclude all modules in this directory".

The only way I can see you can use is onBuildWrite

which is a global parameter that the function takes. I used it for other purposes than what you want, but maybe it would do the trick:

onBuildWrite: function (moduleName, path, contents) {
    return /^widgets\//.test(moduleName) ? "" : contents;


If the module name begins with widgets/

, then the content that will be written in the bundle will be an empty string, otherwise the content will be the same as the content of the module.

Note that this will not do what it does exclude

. The parameter exclude

excludes the listed modules and their dependencies. The above example onBuildWrite

is analogous excludeShallow

, since modules that match the regex will be excluded, but their dependencies will not be excluded. It is not easy to write a function onBuildWrite

that will extend the exception for module dependencies that you would like to exclude. r.js

does not provide an API for requesting module dependencies.



All Articles