Pre-processing JavaScript with Node JS - node.js

Is it possible to intercept Node JS loading *.js files in order to pre-process them?
I want to be able to extend a standard JavaScript file with a special-syntax section that needs to be replaced with valid JavaScript on-the-fly as Node JS loads the file.
Or is there already a solution for this?

Is it possible to intercept Node JS loading *.js files in order to
pre-process them?
Why do you want to do so when files are being loaded?
Pre-processing should be done during build-time. Look for a task runner like GruntJS or Gulp, and implement that pre-processing during build-time and load processed files during run-time providing your own build task.

Related

Angular2 deploying to production environment questions

Some questions to put angular2 web project to production environment
We do development on lite server but what is best for production? Is is some other server module of nodejs? Technically we can have any server (apache, tomcat, etc).
How should we do source code management for below context.
browser must include js files so project should have js files when deployed
In standard java project we just commit .java files and uses jenkins (may be other tools) to compile and make the deploy-able structure
Should we follow same strategy here? i.e. don't commit compiled js files and deploy using some node compiler which takes ts files and compiles it to js
What is the best way to minify/obfuscate the js files
I know a way using outDir and outFile with grump but I don't want every files tobe included in one minified file because it kills the concept of lazy loading
Is there a way to minify and obfuscate js files on compile time only?
What enableProdMode() do? How it is different than not using it?
Here are some answers to your questions:
Angular2 applications only consist of static files so they can be serve by any static Web servers or server applications that can define static folders (Express, ...)
Regarding source code management, you must have a packaging phase to optimize the application loading (gater files, uglify, ...). Your source code must contain your TypeScript files (or JS files if using ES5 or ES6). Such packaging can be done using Gulp for example. Your Jenkins server will be able to checkout the source code, build it and execute tests.
In fact, when not using the outFile property of the TypeScript compiler, you won't be able to gather all the JS compiled files into a single one since anonymous modules will be created within each JS files.
See this question for more details of this:
How do I actually deploy an Angular 2 + Typescript + systemjs app?
Regarding prod mode, here is an extract of the documentation:
Disable Angular's development mode, which turns off assertions and other checks within the framework.
One important assertion this disables verifies that a change detection pass does not result in additional changes to any bindings (also known as unidirectional data flow).

Minimize Node js application .js files?

We have developed a desktop application using node webkit and it works fine. My only doubt is that, do we need to perform minification on the .js files written as part of node js server component. We usually perform minification on the javascript written mainly for UI view to reduce payload during loading of related javascripts of the HTML and also to hide coding information in the scripts so that its hard to modify.
So do we need to perform similar kind of concatenation and minification process on the node js server side .js files and then share the node webkit executable to the Customer. Without minification of node js files, the application works perfectly fine.
So, going back to my question -- Do we need to perform javascripts concatenation and minification for node js application?
Minification is generally to save bandwidth when downloading script files over the internet, so there isn't any real point to minifying your node.js files on your server if they aren't served anywhere.
I really doubt your server's storage needs to save a few kilobytes.

using requirejs without main file nor config()

I'm building an app that will contain many js (jquery) modules (files) using the following setup;
The build is run using Grunt task runner.
I use handlebars templates, then generate the html from *.hbs files.
During the build, I uglify all the js files into one minified file.
I call the minified file in my application <script src="assets/js/app.min.js"></script>
Now, I want to use requirejs to organize my code and adhere to the AMD specifications..
But I got 3 problems with this approach:
I need to have 1 single minified file for all the js; this keeps my code harder to "decode" thus to copy (since it is mixed with other dependencies; jquery, modernizer..) and also helps avoid extra http requests if scripts are called individually.. However, requirejs needs a main file to initialize the app and inside that main file It calls the other required files.. I don't have such configuration.
Many of the dependencies I'm using are in a bower package (that I don't include in the distribution); this makes it not possible to call those files using the suggested requirejs method.
I'm using jquery on this with some 3rd party plugins that don't call define(); to make them work I have to use shim config which again rises the problem #2!
So how am I supposed to take advantage of requirejs while keeping my code in modules and well organized?
Sorry for the long question and thanks for reading :)
ps: Feel free to change the title of the question as I couldn't find a better one!
I finally opted for AngularJS as it adheres to my setup above and allows me to split my app into manageable small modules.
I have also the possibility to use the ease of jQuery (even though it is not a best practice among angular community) and much more.

Combine and minify js file using nodejs dynamically

I am new to nodejs and grunt.
I was going through the less-middleware and was very impressed with the way it automatically creates the css files. I now want the same for js files also. Let me briefly explain -
User makes a request to site.min.js
The routes automatically captures the particular request.
Nodejs creates a new combined and minified js file based on the input parameters if the file is not already present.
Return the combined file.
I feel grunt is not the right way to go about it since every time the js files are updated I will have to commit new version of the min file to the repo. I want some dynamic mechanism of creating the minified version.
Also not that i am trying to deploy my code on heroku.
Grunt will compile your js files during the build. I'm not aware of a runtime pull-and-minify for javascript.

Get a JSON response and save to file using require.js

Im trying to make a plugin for require.js that allows me to call an external api, convert the json response and save it to a file.
Problems:
Im not sure if I am writing the plugin correctly
I cant seem to use node filesystem - though i am using r.js
I am hoping to do this on build, so that the file is ready before concat method happens (putting all files into one)
Is this even possible? Should I use a grunt task instead?
Any pointers or examples or tutorials or anything would be really useful.
In the end I used, https://npmjs.org/package/grunt-curl.
Was alot easier, and just modified the file a bit to wrap the response in define();
It allows the files to be downloaded on build and required later in the app

Resources