just create stand alone JavaScript
That is something you can already do.
Main (keep BANano_Ready empty!) :
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#IgnoreWarnings: 16, 10, 14, 15
#End Region
Sub Process_Globals
Private BANano As BANano 'ignore
End Sub
Sub AppStart (Form1 As Form, Args() As String)
' you can change some output params here
BANano.Initialize("BANano", "BANanoTest",1)
BANano.Header.Title="BANano"
BANano.JAVASCRIPT_NAME = "myCode.js"
' set the Transpiler Switches to this:
BANano.TranspilerOptions.MergeAllCSSFiles = False
BANano.TranspilerOptions.MergeAllJavascriptFiles = False
BANano.TranspilerOptions.RemoveDeadCode = False
BANano.TranspilerOptions.ShowWarningDeadCode = False
' start the build
BANano.Build(File.DirApp)
#if release
ExitApplication
#end if
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
' HERE STARTS YOUR APP ' KEEP IT EMPTY!
Sub BANano_Ready()
End Sub
Whatever code you want to be transpiled to javascript, e.g. here a class MyClass:
Sub Class_Globals
Private Counter As Int
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
Counter = 0
End Sub
public Sub AddToCounter(value As Int)
Counter = Counter + value
End Sub
public Sub GetCounter() As Int
Return
End Sub
Now Build() to transpile. In the scripts folder you can find the transpiled code myCode.js.
Create some HTML file, and include bananocore.js and the transpiled code as <script> (+whatever javascript/css files you would need) and start using it.
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<script type="application/javascript" src="bananocore.js"></script>
<script type="application/javascript" src="myCode.js"></script>
<script>
var myClass = new banano_teststandalonebanano_myclass();
myClass.initialize();
myClass.addtocounter(1000);
console.log(myClass.getcounter());
</script>
</body>
</html>
Note: all methods are lowercased, as B4J is not strict about the case.
You can do some cleanup in the myCode.js file if it bothers you (although the impact is so minimal I would not even spend time on it) e.g. by removing the following:
//var _banano_teststandalonebanano = new banano_teststandalonebanano(); ' <---- REMOVE
/* App */
function banano_teststandalonebanano_myclass() {
var _B = this;
_B.__1 = 0;
_B.initialize = function() {
_B.__1 = 0;
};
_B.addtocounter = function(__2) {
_B.__1 = _B.__1 + __2;
};
_B.getcounter = function() {
return _B.__1;
};
}
/* ' <---- REMOVE
function banano_teststandalonebanano() {
var _B;
this.banano_ready = function() {
if (_B == null) _B = this;
};
}
window.addEventListener('online', function() {
if (typeof _banano_teststandalonebanano['banano_online'] === "function") {
_banano_teststandalonebanano.banano_online();
}
});
window.addEventListener('offline', function() {
if (typeof _banano_teststandalonebanano['banano_offline'] === "function") {
_banano_teststandalonebanano.banano_offline();
}
});
var BANversion = 1631864437171;
window.document.addEventListener("readystatechange", BANLoadChecker, true);
function BANLoadChecker() {
if (window.document.readyState == "complete") {
_banano_teststandalonebanano.banano_ready();
}
}
*/
Result:
The next version of BANano will hava a new Transpiler Option:
BANano.TranspilerOptions.DisableShortenVariableNames = True
which will allow e.g. global variables to retain their true name (counter) instead of a shortened one (__1) so one will be able to this:
console.log(myClass.counter);
The cleaned up code would then look something like this:
function banano_teststandalonebanano_myclass() {
var _B = this;
_B.counter = 0;
_B.initialize = function() {
_B.counter = 0;
};
_B.addtocounter = function(value) {
_B.counter = _B.counter + value;
};
_B.getcounter = function() {
return _B.counter;
};
}
The suggested method BANano.GetJavaScript("...") is just not possible, as BANano does so much more than transpile line by line. Many things are only possible in the B4J syntax and equally other ones only in JavaScript which means N lines in B4J can result in a complete other set of N lines in Javascript. Temporary variables (and even methods) have to be injected, variables/methods have to be aware of the existence of other variables/methods, etc... to make stuff possible.
Using BANano to 'learn' JavaScript is not a good idea. One should follow a course or something if one wants to learn JS. The whole purpose of BANano is that one does not have to do this and can just use the familiar B4J syntax. The way you write B4X code may even be confusing when you look at how it is done in JavaScript.
But the above example shows that it IS possible to use BANano transpiled code "outside" with other e.g. templates.
Alwaysbusy