I would like to create a Jakefile which compiles some CoffeeScripts to install a NodeJS application.
How do you do that?
I tried with:
https://gist.github.com/1241827
but it's a weak approach, definitely not classy.
Any hints?
Approx snippet I have used:
var fs = require('fs')
var coffee = require('coffee-script')
// If you'd like to see compiled code..
// console.log(coffee.compile(fs.readFileSync('coffee.coffee')))
// ..otherwise
fs.writeFileSync('output.js', coffee.compile(fs.readFileSync('input.coffee')))
..assumes you have the coffee-script node module installed, of course.
Translated from this Cakefile of mine.
Related
I'm using gulp to uglify my files and it works fine for older Javascript.
There has already been a question about how to uglify an ES6-javascript file: how to uglify javascript classes?
That's because my code does not work (classes are ES5 or smth):
gulp.task('handleJs', () => {
gulp.src('src/frontend/xy/js/file.js')
.pipe(uglify());
}
The answer doesn't seem to be up-to-date anymore because uglify-js-harmony is deprecated (https://www.npmjs.com/package/uglify-js-harmony).
It says, I should use uglify-es (not -js) but I don't find a solution, how to use it with gulp? Which npm-packages do I really need for that and how does the code have to look like?
This is documented to a degree in the README for gulp-uglify. For clarity, I've slightly modified the example there to match your snippet.
You need to npm install the pump, gulp-uglify, and uglify-es packages first (along with any other packages your project needs. Then, you can setup your gulp task similar to as follows:
var uglifyjs = require('uglify-es');
var composer = require('gulp-uglify/composer');
var pump = require('pump');
var minify = composer(uglifyjs, console);
gulp.task('handleJs', function (cb) {
var options = {};
pump([
gulp.src('src/frontend/xy/js/file.js'),
minify(options),
gulp.dest('dist')
],
cb
);
});
I need to include a NodeJS library manually for our config, and I downloaded this library: https://github.com/squaremo/amqp.node/
and I tried to include the library with
var amqp = require("amqp.node-master");
I ended up getting exceptions for
Cannot find module './defs'
I look in connection.js and it has this at the beginning:
'use strict';
var defs = require('./defs');
var constants = defs.constants;
var frame = require('./frame');
var HEARTBEAT = frame.HEARTBEAT;
var Mux = require('./mux').Mux;
Looking at it, I don't see any defs folder or def.js in the library. Am I missing something?
If you look at their source, you'll see that def.js is generated during build.
That means you can't install it directly from GitHub; you need to install a version that ran make.
I am not familiar with commonjs conception, so my strategy might be basically wrong..
I want to use CryptoJS.HMAC() and CryptoJS.SHA256 in titanium
I have downloaded CryptoJS v3.1.2.zip from here.
then copy all files under Resources/CryptJS/ .
then I add this last line in core.js
return C;
}(Math));
module.export = CryptoJS; //add this line
now in my app.js
var CryptoJS = require('./CryptoJS/components/core');
CryptoJS.HMAC(CryptoJS.SHA256, dateStamp, testKey, { asBytes: true});
however it shows
undefined is not a function error when I call CryptoJS.HMAC()
Can I have help?
You find a commonJS version of Crypto Js on npm: https://www.npmjs.com/package/browserify-cryptojs
Maybe you got more luck with that!
Try adding an s to export, so it becomes:
module.exports = CryptoJS; //add this line
I have some idea for WebTorrent. I have some experience with JavaScript and jQuery, but I never use Node.js or Browserify. Could someone explain me how to use the following simple code?
var client = new WebTorrent()
var torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d'
client.add(torrentId, function (torrent) {
// Torrents can contain many files. Let's use the first.
var file = torrent.files[0]
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('body')
})
Should I attach a <script src="webtorrent.min.js"></script> and convert the code from above by Browserify (browserify app.js > bundle.js)? Or maybe I need to use npm install webtorrent and attach some Browserify code?
After using the code npm install webtorrent and just doing Ctrl + c + Ctrl + v, this example from above in <script> tag showed up player. Magic :) But I do something in my VM and now Chrome say that the require('webtorrent') is not definied.
It would be great if I could use WebTorrent as simple as jQuery.
With the code you posted, you don't need to require. If you use the standalone minified version, you can use use var client = WebTorrent()and start using it.
Here's an old example: http://jsfiddle.net/fr090taa/ when I answered this: How to use webtorrent.io
Don't use node/npm/browserify. Just use the minified version.
Check https://github.com/DiegoRBaquero/BTorrent (there's also a clean compiled version to view in JS instead of Coffee)
I'm a WebTorrent project contributor.
While working on many differente Node.js projects, I began having common code that I want to move out in a new Node.js package in order to not rewrite the same code multiple times. I've a new Node.js module and I'm using it in my projects using npm link.
Now, I'm a bit confused as to how to structure this common library in order to properly modularize it. This is what I have right now in my common library:
// "my-common-lib"'s app.js
module.exports = {
math: require("./math/mathLib"),
network: require("./network/networkLib")
};
--
//mathLib.js
exports.pi = 3.14;
This works, I can do the following in another node.js project:
var commonLibrary = require("my-common-lib");
var commonMath = commonLibrary.Math;
console.log("Pi: " + commonMath.pi);
While this solves the issue, I would prefer something similar to how lodash does it:
var commonMath = require("my-common-lib/math");
console.log("Awesome pi: " + commonMath.pi);
I can't quite figure out how lodash does it, and I would definitely like to avoid having a humongous main js file.
TL;DR I want to modularize a node.js module so I can require submodules (require("my-common-lib\myCommonMathLib")), how can I do this?
lodash does it with a dedicated modular build. Look at the ES6 build for example. Every "sub-project" has a dedicated module, in a dedicated '.js' file. The aggregating file (lodash.js) simply imports all other modules.
If you want the nice lib/module convention, simply have a your lib.js file (aggregator) at the top level, next to a directory by the same name where all internal modules are kept.
Another option for the require("lib") part is to have a "main": "lib.js" configuration in your package.json
If you want to use lodash/array for example, LoDash has an array.js file, with the following:
module.exports = {
'chunk': require('./array/chunk'),
'compact': require('./array/compact'),
So you can easily have math.js inside your main folder, which has something like:
module.exports = {
pi: 3.14
// OR
pi: require('./math/pi'); // and have file pi.js inside math folder
}
This way you can use it as a short:
var math = require('my-common-lib/math');
math.pi; // 3.14