CoffeeScript Compile On Windows With NodeJS - command-line

I have installed nodejs, installed coffee using npm and registered the environment variables and am now ready to compile my CoffeeScript into JavaScript for the first time.
I am running the following command in c:\MyCoffeeScriptProject\
coffee --compile --output js/
What happens next is that I get the interactive window and nothing in the "js" folder.
coffee>
I was expecting all .coffee files to be compiled into the "js" folder as .js files.

You didn't tell it which files to compile.
Usage: coffee [options] path/to/script.coffee [args]
Append a single period to the end for the current directory.
coffee --compile --output js/ .
It's easier to read if you rearrange it though.
coffee --output js/ --compile .
compiles all coffee files in . to js/
A common usage pattern is to have a src folder.
coffee --output js/ --compile src/

c:/parent>coffee --output output --compile src
Where "output" is blank folder and "src" folder contains coffee files.
Execute the command at level of src.

Related

Is there a way to set up Babel to continuously transpile and/or minify a src folder to a compiled folder?

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.

BabelJS: Doesn't find all .js files in directory

Babel doesn't find all of my .js/.es6 files in my directory.
I have this directory structure:
src/
assets/
sample/
models.es6
scripts/
playground.es6
If I run babel src --out-dir dist --source-maps --copy-files --presets env, it only transpiles /src/assets/sample/models.es6 and doesnt go through src/scripts/playground.es6.
What am I doing wrong?
Looking forward to your response!
You can do like below :
babel src/** --out-dir lib
more at official doc
Compile Directories
Compile the entire src directory and output it to the lib directory. You may use --out-dir or -d. This doesn’t overwrite any other files or directories in lib.
if you still stuck, you can use gulp or grunt or webpack to load/transpile mupltiple directives from different locations.
Hope it helps
I found the problem. It has barely to do with Babel.
Inside the src/assets/** is my Realm database sample.realm (https://realm.io). The file itself doesnt cause the problem. But if you open the sample.realm file with Realm Studio on MacOSX, a file called sample.realm.note gets created. This file causes babel to not exit the transpile task.

Coffeescript basic compiling error

Just getting started with Coffeescript and have installed it correctly , however having problems getting basic compilation to work
I have 2 folders names 'src' and 'js'
I create a simple .coffee file in the src folder called test.coffee
In the parent folder I open a terminal window and type the following
coffee -wc src -o js
This SHOULD automatically compile any .coffee files in the src folder and put in the js folder but I always get an error
File not found: –wc.coffee
What am I doing wrong?
Coffee is picky about parameter order.
Usage: coffee [options] path/to/script.coffee -- [args]
As you see, you have to specify all options before the script (or directory) you want to compile:
coffee -w -c -o js src
or
coffee -wco js src
According to the usage examples on coffeescript.org, the "watch" functionality is for files, not directories.
Try dropping the -w.

CoffeeScript - compile all .coffee files in current directory and all sub-directories

What is the easiest way to compile all .coffee files in the current directory and all sub-directories?
you can do so with the integrated coffee shell tool:
coffee --output lib --compile src
compiles a directory tree of .coffee files in src into a parallel tree of .js files in lib.
Check http://coffeescript.org/#usage for more details
coffee -c .
Thanks #TrevorBurnham
coffee --watch --compile .
or
coffee -wc .
Either of these commands will run forever, watching for *.coffee files in the current directory, and compiling those *.coffee files into *.js JavaScript files whenever the *.coffee files are changed.
If you want the *.js files to be generated into some other directory, just add --output or -o, like this:
coffee --watch --output lib --compile src
or
coffee -w -o lib -c src
If you are using *nix systems:
find -name "*.coffee" -exec coffee -c {} \;
and you may also consider using Guard: https://github.com/guard/guard-coffeescript

How to watch and recompile coffee scripts as they change?

Some time ago I used to do
coffee --bare --output . --watch --compile .
This watches the .coffee files in the current dir and recompiles them as they change.
Now, using 1.2.0 of coffeescript this does not seem to work any more. I'm presented with an error:
File not found: --watch.coffee
and the usage documentation for coffee seems rather light.
I'd skip the --output ., as it seems to be unnecessary if you're already in the same directory, and go straight with
coffee -bcw *.coffee
Otherwise, this is a duplicate of Compile CoffeeScript on Save?
BTW, you can also go:
coffee -bcw .
which will not only save a few keystrokes, but also scan subdirectories to compile .coffee files.