Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJS - exclude by directory (or regex)

This is a portion of my build configuration for requireJS's optimizer, r.js.

exclude: [
    'widgets/cr-log-display',
    'widgets/cr-pager',
    'widgets/cr-time-input'

My question is simply 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 another configuration parameter that I'm missing?

like image 845
Adam Rackis Avatar asked Aug 22 '26 23:08

Adam Rackis


1 Answers

I'm pretty sure you cannot pass a regular expression to exclude. I'm saying this from having read the source of r.js. The processing of exclude uses an internal function named findBuildModule, which compares what is passed to exclude against module names with ===. And by the same token, there is no way to tell r.js "exclude all modules under this directory".

The one avenue I see you might be able to use is onBuildWrite, which is a global setting that takes a function. I've used it for other purposes than what you want but perhaps this would do the trick:

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

If the module name starts with widgets/ then the contents that will be written to the bundle will be the empty string, otherwise the contents will be whatever the module's contents happen to be.

Note that this will not do exactly what exclude does. The exclude setting excludes the listed modules and their dependencies. The onBuildWrite example above is an analogue to excludeShallow in that the modules that match the regular expression will be excluded but their dependencies won't be excluded. There is no way to easily write an onBuildWrite function that will extend the exclusion to dependencies of the modules that you'd like to exclude. r.js does not give an API to query dependencies of a module.

like image 69
Louis Avatar answered Aug 24 '26 12:08

Louis