brunch config: javascript files exclude *.min.js files - javascript

I understand how brunch handles javascript files, combining them into individual output files:
files:
javascripts:
joinTo:
'javascripts/app.js': /^app/
'javascripts/vendor.js': /^vendor/
'test/javascripts/test.js': /^test(\/|\\)(?!vendor)/
'test/javascripts/test-vendor.js': /^test(\/|\\)(?=vendor)/
the 2nd line, for example, takes all the javascript files in the /vendor/ folder and makes the vendor.js
The Regex (/^vendor/) specifies a folder path correct? Any way to have the regex apply to the file names as well? For example if there was a jquery.js and a jquery.min.js, I just want the jquery.js file included. Is this possible?

You can use functions to test against paths. With them stuff should be simple:
joinTo:
'javascripts/vendor.js': (path) ->
/^vendor/.test(path) and not /\.min\.js$/.test(path)

You can give the paths to be ignored. The regex should match complete path for files, filenames included.
paths:
public: '../deploy'
ignored: 'vendor/styles/bootstrap'
test: 'spec'
ignored key: string, regExp, function or array of them. Will check
against files that would be ignored by brunch compilator.
But as Amberlamps suggested you can give regex to exclude *.min.js files in the files .
PS:
Never tested it but in the docs somewhere it is mentioned /_spec\.\w+$/ matches for user_spec.js.

I have never used brunch before, but if it is using RegExp to find specific paths you can exclude *.min.js files using:
/^((?!vendor\/.*\.min\.js).)*$/
I used following answer as reference: Regular expression to match string not containing a word?

Related

How to get .eslintrc.js ignore all none js files

How to configure .eslintr.js to ignore all non-js files?
I started a .eslintignore file with the rule !**/*.js but doesn't work.
You're close. In the .eslintignore section for negating (!) (emphasis mine):
Lines preceded by ! are negated patterns that re-include a pattern that was ignored by an earlier pattern.
So, ignore everything (you can use blob, or a specific directory, etc.) and then un-ignore js files:
src/
!src/**/*.js

Will uglify include a file twice if a wildcard selector is used

Given my grunt uglify settings below; will the file manager.js be included twice in the resulting file foo.js? Or is uglify smart enough to figure out to not to include manager.js twice?
...
uglify: {
options: {
compress: true,
},
build: {
files: {
'dist/foo.js': [
'js/manager.js', // Other files depend on this - must be included first
'js/*.js', // Does this mean manager.js will be included twice?
],
}
}
},
Folder structure:
./js/manager.js
./js/bar.js
./js/baz.js
If uglify grabs the file twice, any recomendations how I can avoid this without having to manually add each javascript file?
Short Answer
Given my grunt uglify settings below; will the file manager.js be included twice in the resulting file foo.js?
No, the content of js/manager.js will be first in the resultant file, (i.e. dist/foo.js), and will NOT be included twice using your current uglify Task configuration.
Or is uglify smart enough to figure out to not to include manager.js twice?
Yes sort of, however it's actually grunt that works this out before passing an Array of unique file paths to uglify.
Long Answer
Excerpts from the grunt documentation for Globbing patterns read:
Also, in order to simplify otherwise complicated globbing patterns, Grunt allows arrays of file paths or globbing patterns to be specified. Patterns are processed in-order, with !-prefixed matches excluding matched files from the result set. The result set is uniqued.
The pertinent part here being: The result set is uniqued
The following code example is also given in the documentation:
// Here, bar.js is first, followed by the remaining files, in alpha order:
{src: ['foo/bar.js', 'foo/*.js'], dest: ...}
To prove the points mentioned in the documentation, let's assume we have the following five source .js files stored in the js/ directory. The pseudo content for each file is provided below the filename:
manager.js
console.log('manager.js')
a.js
console.log('a.js')
bar.js
console.log('bar.js')
baz.js
console.log('baz.js')
quux.js
console.log('quux.js')
If we run grunt using the files listed above and your current uglify Task configuration the resultant output is:
foo.js
console.log("manager.js"),console.log("a.js"),console.log("bar.js"),console.log("baz.js"),console.log("quux.js");
As you can see:
console.log("manager.js") appears first and only once (i.e. the result set has been uniqued).
The remaining files (i.e. those found via the glob pattern 'js/*.js') have been added in alpha order.

How to exclude webpack from bundling .spec.js files

my Package.bundle reads
var reqContext = require.context('./', true, /\.js$/);
reqContext.keys().map(reqContext);
Which basically includes all .js files.
I want the expression to exclude any ***.spec.js files . Any regexp here to exclude .spec.js files ?
Since /\.js$/ allows all .js files (as it basically matches .js at the end of the string), and you need to allow all .js files with no .spec before them, you need a regex with a negative lookahead:
/^(?!.*\.spec\.js$).*\.js$/
See this regex demo
Details:
^ - start of string
(?!.*\.spec\.js$) - the line cannot end with .spec.js, if it does, no match will occur
.* - any 0+ chars other than linebreak symbols
\.js - .js sequence
$ - end of the string.
Although the accepted answer is technically correct, you shouldn't need to add this regex to your webpack configuration.
This is because webpack only compiles the files that are referenced via require or import and so your spec.js files won't be included in the bundle as they are not imported anywhere in the actual code.

How does this pattern work: 'test/e2e/**/*.spec.js'?

I saw this pattern used in a configuration file for protractor.
specs: [
'test/e2e/**/*.spec.js'
]
To mean it means "all files inside test/e2e". What kind of pattern is this? I think it's not regex because of those unescaped slashes. Especially, why is there ** in the middle, not just test/e2e/*.spec.js?
I tried using the search engine, but did not find anything useful probably because the asterisks don't work very well in search engines.
What kind of pattern is this?
It is called "glob". The module glob is a popular implementation for Node, and appears to be the one used by Protractor.
Especially, why is there "**" in the middle, not just "test/e2e/*.spec.js"?
** means it can match sub-directories. It's like a wildcard for sub-directories.
For example, test/e2e/*.spec.js would match test/e2e/example.spec.js, but not test/e2e/subdir/example.spec.js. However test/e2e/**/*.spec.js matches both.
It is called "glob" syntax. Glob is a tool which allows files to be specified using a series of wildcards.
*.js means "everything in a folder with a js extension.
** means "descendant files/folders.
**/*.js means "descendant files with a js extension in descendant folders."
test/e2e/**/*.spec.js' means the above, starting in the test/e2e/ directory.
So, for example, given this file system:
test/e2e/foo/a.spec.js <-- matches
test/e2e/foo/butter.js <-- does not include "spec.js"
test/e2e/bar/something.spec.js <-- matches
test/other/something-different.spec.js <-- not in /test/e2e
The final pattern would match:
test/e2e/foo/a.spec.js
test/e2e/bar/something.spec.js
It's a globbing pattern. Most javascript things using globbing patterns seem to be based around the glob npm package. It's worth taking a look at the documentation as there are some handy hints in there for when you have more complex situations.
The path you are asking about will match any file ending .spec.js in any subdirectory under test/e2e

Exclude everything but Javascript files in RequireJS Optimizer

I am using RequireJS optimizer in a gulp recipe to compile and concatenate my Modules but redundant 3rd party library files like bower.json and *.nuspec files are being copied to my output directory.
I have successfully managed to exclude full directories using fileExclusionRegExp in the requirejs.optimize options object with the following expression:
/^\.|^styles$|^templates$|^tests$|^webdriver$/
However, I cannot figure out how to exclude everything but .js file extensions. I could use the following:
/^\.|.json$|.nuspec$|^styles$|^templates$|^tests$|^webdriver$/
to exclude specific extensions but if a new type were to appear later, I would have to notice and then change the regex. Also, the regex would probably become unruly and hard to maintain with time. I have tried to use the following expressions:
/^\.|!js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|!.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|^.js$|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js$]|^styles$|^templates$|^tests$|^webdriver$/
/^\.|[^.js]$|^styles$|^templates$|^tests$|^webdriver$/
The results ranged from doing nothing (the first 3, to breaking the build, last 2) any help anyone could provide would be appreciated.
Thanks
Try this regex:
^\.|\.(json|nuspec)$|^(styles|templates|tests|webdriver)$

Categories