r.js - excluding specific list of files

2019-07-20 06:28发布

问题:

Hello evryone,

I'm trying to find a way to make the r.js not to copy all the files form my /lib/ directory except for (for example) jquery.js and require.js.

I'm using fileExclusionRegExp option to exclude all *.js files except for the above mentioned.

 fileExclusionRegExp: '\/lib\/(?!jquery|require).*\.js'

But after the optimization, I can still see that other files have been copied over too.

Is there anything I'm doing wrong ? Or is the regex incorrect?

Thanks in advance

回答1:

The problem you've run into is that you are trying to make fileExclusionRegExp match the entire path of a file, but r.js uses it only to test against the base name of files. You can infer this from the description of the option and its default value. The description says:

//When the optimizer copies files from the source location to the
//destination directory, it will skip directories and files that start
//with a ".".

The default value is:

/^\./

If this were to be tested against a full path, it would not be able to exclude files that start with a period if they are in a subdirectory. We also find confirmation of this behavior in this issue report.

If you have only one file named require.js and one file named jquery.js, then you can get by with this regexp:

fileExclusionRegExp: /^(?!jquery|require).*\.js$/

Otherwise, fileExclusionRegExp is not going to do it, and you should use a build step to clean your directory, as suggested by the author of RequireJS in the issue report I mention above.