InCorrect Path for Rest client in Haxe - haxe

I am a newbie to Haxe. I created the below code:
import restclient.RestClient;
/**
* ...
* #author Sriram
*/
class HaxeTest
{
static public function main() {
trace("Hello, world!");
var result = RestClient.get(
"SOME_REST_GET_API");
trace(result);
}
}
I wanted to run the below command:
haxe --js main-javascript.js --main HaxeTest
But I get the below error:
HaxeTest.hx:1: characters 8-29 : Type not found : restclient.RestClient
HaxeTest.hx:10: characters 22-32 : Type not found : RestClient
I have installed the rest-client using haxelib install rest-client
Kindly assist.

Installing a library doesn't directly mean it's used in compilation of the project.
You should add -lib rest-client to actually tell the compiler that you want to use this library
haxe --js main-javascript.js --main HaxeTest -lib rest-client

Related

How to use gulp-load-plugins with TypeScript?

I found no examples for gulp-load-plugins in TypeScript. Unfortunately,
my TypeScript is too poor to understand, what should to do from #type/gulp-load-plugins
comments.
I tried:
import * as _gulpPlugins from 'gulp-load-plugins';
const gulpPlugins: IGulpPlugins = _gulpPlugins();
return gulp.src(sourceFilesGlobSelections)
.pipe(gulpPlugins.pug())
// ...
It makes 4 warnings from Webpack (I dont understand where number such 75:13-25 refers; .pipe(gulpPlugins.pug()) is on 50 row):
WARNING in ../node_modules/gulp-load-plugins/index.js 75:13-25
Critical dependency: the request of a dependency is an expression
# ./TaskManagers/MarkupPreprocessingHelper.ts
WARNING in ../node_modules/gulp-load-plugins/index.js 81:48-63
Critical dependency: the request of a dependency is an expression
# ./TaskManagers/MarkupPreprocessingHelper.ts
WARNING in ../node_modules/gulp-load-plugins/index.js 117:40-55
Critical dependency: the request of a dependency is an expression
# ./TaskManagers/MarkupPreprocessingHelper.ts
WARNING in ../node_modules/gulp-load-plugins/index.js 122:51-66
Critical dependency: the request of a dependency is an expression
# ./TaskManagers/MarkupPreprocessingHelper.ts
It was told in #types/gulp-load-plugins:
/**
* Extend this interface to use Gulp plugins in your gulpfile.js
*/
interface IGulpPlugins {
}
I tried:
interface IGulpPlugins {
pug: () => NodeJS.ReadWriteStream;
}
It also defined:
declare module 'gulp-load-plugins' {
interface IOptions {
// ...
}
interface IPluginNameMappings {
[npmPackageName: string]: string
}
/** Loads in any gulp plugins and attaches them to an object, freeing you up from having to manually require each gulp plugin. */
function gulpLoadPlugins<T extends IGulpPlugins>(options?: IOptions): T;
// ...
}
It looks like maybe I should use gulpLoadPlugins instead of interface extending ...
Is is all that I understand with my current TypeScirpt proficiency, but it not enough to understand how to use gulp-load-plugins in TypeScript.
There is the working example in gulp-load-plugins-tests.ts:
import * as gulp from 'gulp';
import gulpConcat = require('gulp-concat');
import gulpLoadPlugins = require('gulp-load-plugins');
interface GulpPlugins extends IGulpPlugins {
concat: typeof gulpConcat;
}
gulp.task('taskName', () => {
gulp.src('*.*')
.pipe(plugins.concat('concatenated.js'))
.pipe(gulp.dest('output'));
});
About Critical dependency: the request of a dependency is an expression: do not bundle Node.js dependencies for the webpack projects those target is Node.js (not browser). webpack-node-externals will tell webpack do not bundle Node.js libraries, however it's still possible to import and use them as usual.

fuse.js as a dependency in an aurelia app

I've been trying, without success, to add fuse.js as a dependency to my aurelia-based app.
The app was initialized with the au tool, and uses TypeScript as the main language.
In aurelia_project/aurelia.json, I've tried adding the dependency in the following formats, under build.bundles[vendor-bundle].dependencies:
"fuse.js"
{
"name": "fuse.js",
"path": "../node_modules/fuse.js/dist/fuse"
}
{
"name": "fuse.js",
"path": "../node_modules/fuse.js/dist/",
"main": "fuse"
}
Neither has given me the library available at runtime (always undefined when imported by import * as Fuse from 'fuse.js';, and the two first have given me errors when building.
How can I add fuse.js as a dependency to an aurelia app?
I use fuse.js in my app as a value converter, my structure is webpack and dotnet core,
npm install fuse.js --save
add fuse.js in my vendor part of webpack.config.vendor file
npm install #types/fuse
I created a value converter like :
import { FuseOptions } from 'fuse.js';
import * as Fuse from 'fuse.js';
export class FuseValueConverter {
toView(value: Array<any>, options: FuseOptions, criteria: string) {
console.log(value);
if (!options || !criteria)
return value || [];
const fuse = new Fuse(value, options);
return fuse.search(criteria);
}
}
and then I caleed it in my view models just like any other value converter in Aurelia
Is it not that aurelia does not use extension (.js in your case) when importing?
Your json file is also incorrectly formatted.

Compiling Haxe without a main entry point

I am trying to compile a Haxe class without defining an entry point by using a build hxml file.
My folder structure looks like the following:
root
|
___src
|___Test.hx
|
___build.hxml
Test.hx has the following contents:
package foo;
class BarLib
{
public function new() {}
public function test() {
return "Hello from BarLib!";
}
}
build.hxml looks like this:
-cp src
--macro "include('foo')"
-js test.js
I then run haxe build.hxml from the root folder which creates the file test.js, but its contents is pretty much empty:
// Generated by Haxe 3.3.0
(function () { "use strict";
})();
It seems not to be able to locate the package foo.
What am I doing wrong?
You declare Test.hx to be part of the foo package, however, it's placed in a folder named src. If you move it to src/foo, Haxe produces the following output:
// Generated by Haxe 3.3.0
(function () { "use strict";
var foo_BarLib = function() {
};
foo_BarLib.prototype = {
test: function() {
return "Hello from BarLib!";
}
};
})();
It's also a bit unusual to have a file named Test.hx that doesn't actually define a type named Test. BarLib.hx might be a better name.
You can also use haxe -cp src foo.Test to compile a single class (and its references) without an entry point

Typescript and NodeJS : ReferenceError : Module is not defined

I am making a NodeJs console application with NodeJS Tools for Visual Studio (http://nodejstools.codeplex.com/) with the Typescript template.
Here is basicaly my code :
app.ts :
/// <reference path="Module/Module.ts" />
var foo = new Module.ModuleClass();
foo.foo();
Module/Module.ts :
module Module {
export class ModuleClass {
foo() {
console.log('Hello World');
}
}
}
The compiler run without trouble but on runtime, NodeJS can't find the module. Here is the error :
var foo = new Module.ModuleClass();
^
ReferenceError : Module is not defined
I tired a lot of thing about that problem (using or not the /// but I can't find any solution.
Excuse me for my English, I am not a native speaker.
Thanks in advance !
You need to export Module in Module/Module.ts
export module Module { ...
In app.ts, you need to require('./Module/Module')
import M = require('./Module/Module');
var foo = new M.Module.ModuleClass();
foo.foo();
You need to do this because this node.js uses the commonjs module system, which typescript supports via its "external" modules feature and a compiler arg --module commonjs

Import Haxe modules into a node.js script

Using node.js and Haxe, is there any way to write a function that generates a node.js modules from a Haxe file, and then returns the generated module? I'm started writing node.js modules using Haxe, and I need a way to import the modules more easily.
function requireHaxe(variableToRequire, haxeFileLocation){
//generate a JavaScript module from the Haxe file, and then return the generated JavaScript module
}
Consider this
//Haxenode.hx
class Haxenode {
#:expose("hello")
public static function hello(){
return "hello";
}
}
#:expose("hello") part is to put something in module.exports.
Now launch
haxe -js haxenode.js -dce no Haxenode
Now you can use haxenode.js in nodejs
var haxenode = require('./haxenode.js');
var hello = haxenode.hello;
So, this combined together is an answer to your question:
var cp = require('child_process');
function requireHaxe(haxeClassPath,cb){
//generate a JavaScript module from the Haxe file, and then return the generated JavaScript module
cp.exec('haxe -js haxenode.js -dce no ' + haxeClassPath,function(err){
if (err){
cb(err); return;
}
cb(null,require('./haxenode.js'));
});
}
Mind that output filename is a stub.
But don't do that - better to compile haxe as build step (with all necessary compile options) and then use regular require at runtime.

Resources