In my package.json, I have:
"scripts": {
"test": "mocha --require blanket -R html-cov > test/coverage.html --compilers coffee:'./node_modules/coffee-script/lib/coffee-script/register'"
},
So if I run npm test, I get:
npm test
> my-site#1.0.0 test /Users/me/Sites/my-site
> mocha --require blanket -R html-cov > test/coverage.html --compilers coffee:'./node_modules/coffee-script/lib/coffee-script/register'
If I take out the blanket stuff ("test": "mocha --compilers coffee:'./node_modules/coffee-script/lib/coffee-script/register'"), then my tests run correctly.
I am using CoffeeScript, if that matters. What am I doing incorrectly?
According to the blanket documentation:
If your test runner tests source files written in coffee script,
blanket still has you covered. Using a custom loader, coffeescript
files are compiled, instrumented, and tested.
The coffee script loader is located in node-loaders/coffee-script.js.
You will need to add the following to your package.json:
"config": {
"blanket:" {
"pattern" : "src", // use the correct pattern for your project
"loader": "./node-loaders/coffee-script"
}
}
For example see the blanket project package.json file.
In addition, the command line you are using for the tests is incorrect. It is missing the path to your test files and the output redirection should be at the end. You should use something like:
mocha --require blanket -R html-cov --recursive --compilers coffee:./node_modules/coffee-script/lib/coffee-script/register src > test/coverage.html
Related
I'm a bit new to babel, but what I'm trying to do is set it to run only when running npm run test and in no other contexts.
I thought that if I configure .babelrc like below, it will only take effect when I use "scripts"."test": "NODE_ENV=test ./node_modules/.bin/jest --no-cache" in package.json, but babel somehow applies (and messes up things) even with npm run watch, which uses NODE_ENV=development.
{
"env": {
"test": {
"presets":[
["#babel/preset-env"],"#babel/react"
],
"plugins": [
"transform-es2015-modules-commonjs",
"dynamic-import-node"
]
}
}
}
I get a TypeError: this.setDynamic is not a function. I have to remove entire babel dependencies and configurations for project to work again, but then I can't run the jest tests.
Starting with an empty directory, is it possible to do that? Should I use stage-0 like it is on the Babel REPL?
I hope to transpile it just like how ReactJS does it. For some reason, I always got an error for just a file containing:
let obj = { a: 1 };
let newObj = {
...obj,
ha: 3
};
Other times, I can transpile a file, but if I transpile a folder, it'd say:
foo.js: Cannot read property 'contexts' of null
The commands I tried included:
npx babel src --out-dir compiled --presets=es2015,react,minify --watch
but the errors I mentioned above appeared. Also, when I do
npm install babel-minify
it reported
found 2489 vulnerabilities (849 low, 306 moderate, 1329 high, 5 critical)
There is also a notice
As of v7.0.0-beta.55, we've removed Babel's Stage presets.
Please consider reading our blog post on this decision at
https://babeljs.io/blog/2018/07/27/removing-babels-stage-presets
for more details. TL;DR is that it's more beneficial in the
long run to explicitly add which proposals to use.
and I wonder what should be done.
Is it possible to
just continuously minify a folder
transpile some ES6 or ES7, 8 syntax that are not yet commonly supported
transpile JSX as well
?
I have found some reliable ways to make it work, although I am not sure when I should use babel.config.json and when to use .babelrc.json or .babelrc. It seems I have to run babel as ./node_modules/.bin/babel and is it true if I don't npm install babel using the -g option.
Here is what works:
create a folder, such as TryBabel
cd TryBabel
Go to https://babeljs.io/setup.html and click "CLI"
You need a package.json, so use npm init and just press Enter a few times
It should lead you to install
a. npm install --save-dev #babel/core #babel/cli
b. now look at your package.json. Remove the script about test but use this: "build": "babel src -d lib"
Now npm run build or ./node_modules/.bin/babel src -d lib should work, but make sure you have some .js files in the src folder. The transpiled result will be in the lib folder.
Now to transpile things into "pre ES6", just follow the #babel/preset-env instructions:
a. npm install #babel/preset-env --save-dev
b. make your babel.config.json to contain { "presets": ["#babel/preset-env"] }
Now you can use npm run build to transpile once, or use ./node_modules/.bin/babel src -d lib --watch to keep on running it and "watch" the src folder and transpile files in it when the files change.
To do minification or make it work with JSX/React, see
https://babeljs.io/docs/en/babel-preset-minify
and
https://babeljs.io/docs/en/babel-preset-react
and make sure your babel.config.json file looks like:
{
"presets": [
[
"#babel/preset-env",
{
"useBuiltIns": "entry"
}
],
["#babel/preset-react"],
["minify"]
]
}
and remove minify if you don't want the code to be minified.
In Visual Studio Code 1.23 you can now run npm scripts from the Explorer window with the setting "npm.enableScriptExplorer": true. I know you can exclude whole package.json files with the "npm.exclude" setting, but is it possible to exlude just specific scripts from a package.json file? Or at least have them not show up in the Explorer window?
Current:
>NPM SCRIPTS
>package.json
🔧stuff
🔧start
🔧build
🔧stuff2
Desired:
(exclude scripts 'stuff' and 'stuff2' from 'package.json')
>NPM SCRIPTS
>package.json
🔧start
🔧build
Based on this :
// Enable an explorer view for npm scripts.
"npm.enableScriptExplorer": false,
// Configure glob patterns for folders that should be excluded from automatic script detection.
"npm.exclude": "",
You can not exclude part of a script in a single file package.json
Should be in vscode v1.63: See Add setting to exclude scripts from NPM scripts view.
config.npm.scriptExplorerExclude
"An array of regular expressions that indicate which scripts should be
excluded from the NPM Scripts view."
In my testing the filter exclusion works on the key/name of the npm script, not on the actual script itself. So if you had these scripts:
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "node ./test/runTest.js"
},
the filter works on lint, pretest and test NOT on the corresponding scripts themselves (i.e., what you see in the NPM Scripts explorer view). NOT for example on the word node (since it isn't part of any script name).
So to filter out stuff and stuff2 in the original question you would have to filter based on something in their script names.
I'm running a babel cli command
babel src --out-dir lib
To copy the es6 scripts from src into lib. However, it wont copy css/scss files I have in the src/ folder. Is there a way to make it copy them as well?
Babel has the copy files option for this:
babel src --out-dir lib --copy-files
Note: It is true that babels primary purpose is to process javascript files, but babel's big suite of tools these day often makes it unnecessary to go for more complex build script setups as gulp and alike.
A gulp-less setup could be adding this to packages.json:
{
...
"devDependencies": {
"babel": "*",
"babel-cli": "^6.4.0",
"babel-preset-es2015": "^6.3.13"
},
"scripts": {
"watch": "babel --watch src --out-dir lib --source-maps inline --copy-files",
"build": "babel src --out-dir lib --source-maps inline --copy-files"
},
"babel": {
"presets": [
"es2015"
]
}
}
I found a way to do this by using the ncp module
npm install ncp
This module is basically like a cp except it works on
This isn't a global module, so to run this we use
node -e \"require('ncp').ncp('./src', './lib')\" && babel src --out-dir lib
I run a mocha command to run my tests
$ ./node_modules/.bin/mocha --compilers coffee:coffee-script -R spec
I wish to pass additional options to the coffee-script compiler (--bare to avoid the outer closure that is introduced when compiling .coffee to .js). Is there a way to do this? I tried
$ ./node_modules/.bin/mocha --compilers coffee:coffee-script --bare -R spec
but that doesn't look right. It also failed saying that --bare is not a valid option for mocha.
error: unknown option `--bare'
The --compiler option doesn't support this, but you can write a script which activates the compiler with your options, then use mocha's --require option to activate your registration script. For example, create a file at the root of the project called babelhook.js:
// This file is required in mocha.opts
// The only purpose of this file is to ensure
// the babel transpiler is activated prior to any
// test code, and using the same babel options
require("babel-register")({
experimental: true
});
Then add this to mocha.opts:
--require babelhook
And that's it. Mocha will require babelhook.js before any tests.
Simply add a .babelrc file to your root.
Then any instances of babel (build, runtime, testing, etc) will reference that.
https://babeljs.io/docs/usage/babelrc/
You can even add specific config options per-environment.
In case anyone stumbles upon this. The 'experimental' option in babel has been deprecated. Your 'babelhook.js' should now read:
// This file is required in mocha.opts
// The only purpose of this file is to ensure
// the babel transpiler is activated prior to any
// test code, and using the same babel options
require("babel/register")({
stage: 1
});