How to exclude webpack from bundling .spec.js files - javascript

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.

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

Match file within nested witin specific folder

I am trying to match viable package.json files to get all modules in a directory, including nested ones. I ran into an issue where someone has a two package.json files in their module. One is in the root and one is nested. I'd like to only match the regex with the first line and not the second. Do I have to parse the strings into arrays with path.sep or can I do this solely with regex?
/Users/thomas/Desktop/exmaple/node_modules/stream-http/package.json
/Users/thomas/Desktop/exmaple/node_modules/stream-http/test/browser/package.json
Pattern
^.+\/node_modules\/.+\/package.json$
https://regexr.com/45qvr
Using .+ greedily catches any characters, so it will always match as much as possible to find a package.json at the end. If you use [^\/]+ instead, it will only match characters that are not /, making sure that the package.json is only matched after exactly one directory under the node_modules/.
^.+\/node_modules\/[^\/]+\/package.json$

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 all files from folder for plato metric

I'm deploy my webapp at a windows maschine (non POSIX command line) and I'm generating JS metric with Plato.
My Question is how can I exclude all files from a given folder that contains subfolders via regular expressions with plato.js.
I tried this command for excluding all minified JS Libs:
$ plato -x "^js[a-zA-Z0-9-.\/]*.?js" -r -d report src/app/
All JS files of src/app/js/**/*.js are not excluded.
I test my regex with rubular: http://rubular.com/r/zbTsv1nIWY (fix underscore issue is optional)
Can somebody help me please?
Remember to escape (.) because you want to match it literally.
i dunno why have you left out the _ (underscore) but an optional solution can be something like this :
^js\/(.*)\.js$
folder name starting with js and file name ending with .js
and as you are saying another solution can be to just include the _
^js[a-zA-Z0-9-\.\/_]*.?js
demo here : http://rubular.com/r/DPqnxtDB6E

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

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?

Categories