Can you provide a module by another name? - javascript

In our application we use our own logging framework. Some of the external modules we use uses the debug module, which logs directly to the console.
I would like to wrap these logging calls, so that they are piped through our own framework.
I didn't want to override/hook console.log, because that's what our logging framework uses internally (and it feels dirty). So my next idea was to somehow instruct Node to load my own code when another module uses require('debug'). Is that possible? And, if so, how? Or is there maybe a better solution altogether?

For the time being, we just forked debug and we're now simply using our fork in our project.
We adjusted the fork to make use of our own logging solution. Integrating it was pretty easy, due to the support of GitHub URLs in package.json.

Related

How to compile Javascript console application locally?

I am a beginner in Javascript, I decided to practice Javascript by problem solving using it, I found an online judge that accepts Javascript V8 4.8.0 code.
So, I searched online to get that version of Javascript V8 on my machine, but I couldn't find any easy way, All the pages were explaining how to build it, and it seems to be a process that I don't need to go through.
Is there an easy way to compile and run command line apps written in Javascript on my machine?
Note: I don't want to use node.js because I tried using it's I/O and
as a beginner I think it is complex in some way.
Update: I found that package manager pbox.me which provides a version of V8 JavaScript Engine and I managed to install it.
Yet another problem appeared: whenever I try to run a js file writing d8 myfile.js in command line nothing happens as if it is an empty program, knowing that I tryied to d8.exe file and it is working, and I made sure the PATH is inserted in the environment variables.
What am I doing wrong?
The easiest way to get started with JavaScript is probably to use it in a browser. You can type simple things directly into the browser's JavaScript console (check the menu); or you can embed your code in a simple HTML document.
If you want, you can even pretty easily implement the readline()/print() functions, so you can pretend to be doing stdin/stdout based I/O: just read from an array of strings, and send output to console.log (or create DOM nodes if you want to be fancy and/or learn how to generate dynamic website content by hand).
Side note: V8 4.8 is severely outdated, don't use it to execute code you haven't written yourself.

How to unextend Extendables framework, keeping just the logger and Jasmine test functionalities

I'm using a nice framework for Adobe ExtendScript called Extendables. I forked the project here: https://github.com/daluu/Extendables
A problem though is that in some ways using the framework is worse than not because the framework extends javascript objects with more functionality. And on an initial review of the code files, they seem rather interdependent such that it will take some work to uncouple the strict dependencies to make it optional/configurable to load only what you need and skip the rest in case of issues with particular features (i.e. you can just not load/include what you don't use - I don't think that's currently possible, although I might be mistaken). See the issue tracker in my project for details but in general the issues encountered using the full framework is failure of try/catch blocks and object iteration includes unintented properties.
For me, I'd just like at a minimum to make all functionality optional and just load the logger and Jasmine test framework as those are the only two feature/modules that I really use with Extendables. I don't care for the extensions to strings, files, object, arrays, etc.
As I'm a novice in javascript/ExtendScript, and this is not a trivially simple javascript framework, I could use suggestions on how to decouple the dependencies so that every module (baring it's dependendencies) can be optionally loaded, and where there are dependencies we can group into sets as in you can load or not load this set of features.
Sorry that I can't include code snippets as its too much to post, you can find it in my Github fork.
Not sure if this is best StackExchange site to post but starting here.
You should be able to extract the log module from this file:
https://github.com/daluu/Extendables/blob/master/core-packages/logging/lib/index.jsx
Try to use it like this (not tested):
#include "core-packages/logging/lib/index.jsx"
Log.debug("Log this");
You might need to adjust some things in there e.g. Folder.extendables does not exist in ExtendScript. Also exports.Log at the end will throw an error.

Are there any reasons NOT to use angular-loader?

I've worked with Angular for a little bit, but I keep managing to learn something new - today, I installed the angular-seed project in order to give my development a little kick in the pants. I ran into the index-async file and learned about the angular-loader - which I hadn't used before.
I found this question, as well: What is angular-loader.js for?
It looks as though the index-async file is using a script loader in addition to the angular module loader, which makes sense. However, I've never used this method before. (In my company, we've used RequireJS to load angular modules before, and so I can understand why something like this would be easier and less cumbersome.) Yet, it also seems that I could use the loader without a third-party script loader - I could just include all of my app files, in any order, before the loader is called, without having to worry about the dependencies.
In short - when should I use angular-loader? More importantly, is there any reason NOT to use it all the time?
Well, you don't have to use it if you don't need it.
From the Angular Docs, you use it:
If you are loading multiple script files containing Angular modules, you can load them asynchronously and in any order as long as you load this file first. Often the contents of this file are copy&pasted into the index.html to avoid even the initial request to angular-loader.min.js. See angular-seed for an example of usage.
The reason behind is to optimize the loading time on the client-side - only load the currently needed module for the user, particularly if you have a reasonably huge app.

Can Q.js be used without node.js and require?

I am trying to use the latest q.js to add promises to my ajax calls. I don't use node.js in any way shape or form. I downloaded the latest from https://github.com/kriskowal/q and include just the q.js. In my console I see in some places:
ReferenceError: require is not defined
I don't know if this is a deal breaker or not, but I don't use requirejs either. I am playing around with a single page app, and while I use jquery for ajax, I plan to remove that dependency for either my own simple implementation or another smaller library. I want to use q.js, and also use bootstrap.js and will possibly use flatiron-director, that's about it. So I don't use npm, or any of that other stuff since my app is so simple. With only a couple JS files, I don't see any reason to add that complexity.
So is that error a problem and q.js won't work without require.js (or node.js)? I thought I read it's stand alone, but when I see errors like this and it seems as if it depends on some other JS libraries it has me doubting if I can use it or not.
The V2 branch of this project uses require.js. The V1 branch does not use require.js.
https://github.com/kriskowal/q/tree/v1

Can I compile loaded handlebars templates?

Is there a way for me to compile Handlebars templates that are loaded as individual script tags with a src attribute? For example, if I have
<script id="index-template" type="text/x-handlebars-template" src="templates/index.hbs">
I know I can precompile them, but I cannot introduce this step as it requires npm + my company doesn't use it.
I tried to find some way to use jQuery to get the source of the loaded file, but couldn't figure it out. Also, I know I can use AJAX, but I'm looking for another solution.
Precompilation requires Node.js. If you can't have it installed on every developer's machine, you may create a service for others to use (I can't find any existing one).
If you want to compile templates run-time, you can get them using AJAX. If the file have already been downloaded, it will be taken from cache. It is still an async operation, so you may use the same syntax for processing the result.
Funnily enough, I just wrote a Handlebars precompile bundler today to use with the asp.net optimization library. It takes advantage of Jurassic, as is explained here.
I have yet to put it in a github repo, but it will be once I make it slightly more generic. I will edit the post and point to the repo later tonight if anyone is interested.
It does take dependencies on Jurassic as well as System.Web.Optimization 1.1.

Categories

Resources