Use coffeescript to compile the method summary of the node. js project, node. jscoffeescript

Source: Internet
Author: User

Use coffeescript to compile the method summary of the node. js project, node. jscoffeescript

Node. js writes applications based on JavaScript. JavaScript is my main development language. CoffeeScript is a programming language compiled into JavaScript. In fact, the CoffeeScript language is flexible to use because of its one-to-one translation into JavaScript. There are also many ways to introduce it into the project. Here, I will use coffeescript to compile the node. js Project Method for a summary.

Run the coffeescript project directly using the coffee command
Generally speaking of coffeescript, it naturally comes to mind that it is a javascript younger brother and cannot be separated from the js shadow. In fact, you can regard it as an independent language. We all know that after the coffee-script package is installed globally on the node platform, you can use the coffee command to enter the interactive interface of coffeescript, which is also called repl. If your project is completely written in coffee, it's easy to simply use the coffee command for your portal script. For example, your portal script is named "app. coffee ", then execute:

Copy codeThe Code is as follows:
Coffee app. coffee

Note that the coffee extension here cannot be omitted.

This method should be the most "official" way to use coffeescript. Simple, direct! In addition, once you use a coffee file as the project portal, the entire project will be compatible with both coffee and js. You can use any require js or coffee file and module in the project, or even any require coffee file in the js file of the project. In addition, no extension is required when you reference coffee or js files, as long as the names in the previous sections do not conflict with each other.

The biggest problem with this method is that, if it is used as a module, it can only be used for coffee projects; if it is used as an application, the runtime environment must install coffee-script. After all, coffeescript is still a niche language. It is a pity that it lost js users as a module.

Another possible drawback is performance. After all, there is only a js engine in node, and the coffee code needs to be compiled into js before running. This process takes a little time, although the compilation speed from coffee to js is actually quite fast. However, this should not be a big problem. In general, require is written at the top of the file, that is, when the application is started, it is angry that all the require files are require, when require is used, coffee is compiled into js and put into the js engine. the time consumed by compilation is concentrated when the application is started, almost no new coffee of require will be encountered during runtime. The most common use case of node is the web server, which is even better.

Reference coffeescript in javascript Projects
Coffee-script in npm can be installed both globally and as a module of the project. What is the significance of coffee-script as a module of the project? In fact, a coffeescript compiler is added to the project. This project can compile the coffee file at any time during runtime.

You must reference the coffee file as easily as in the first method. No problem. Just register it. If your project portal file is app. js, you only need to add the following sentence at the beginning of the file:

Copy codeThe Code is as follows:
Require ('coffee-script/register ');

Then you can freely request the coffee file in the project.

In essence, this method is no different from the first method, except that coffee-script is not installed globally, so your module can exist independently. As an application, you do not need to install coffee-script in the environment.

Disadvantages: I think the biggest problem is that it is easy to make the code messy. js and coffee may be used in a while, of course, the first method may also be like this, but I started it with coffee and it should not have written js ...... In short, I think it is better to unify the language of a project (unfortunately, I mainly use this method in a project that has written a general structure using js, I want to use coffee to get swollen ......)

Performance problems are the same as the first method.

Orthodox method-compile
When it comes to compilation, we feel like we are back to the era of C or Java. Indeed, as a compilation language, it is right to compile and run it again. C has gcc, java has javac, and coprocessor has coffee-c.

It is very easy to compile a coprocessor file. For example, to edit the app. coffee file, run the following command in the current directory of the file:

Copy codeThe Code is as follows:
Coffee-c app. coffee

A file named app. js appears in the current directory. This command can also be applied to directories. For example, if you put all the coffee source files in the project under the src directory, execute:

Copy codeThe Code is as follows:
Coffee-c src

All coffee source files under the src directory and Its subdirectories are compiled into js files and put in the same directory as the source files.

However, for large projects, it is not good to put the source files and the compilation result files together. Specify an output directory:

Copy codeThe Code is as follows:
Coffee-c-o outputs src

The Parameter order of this command is a bit strange. This is defined in coffee's help:

Copy codeThe Code is as follows:
Coffee [options] path/to/script. coffee -- [args]

Note that all options are between coffee and the file path. The final args uses the target file as the parameter passed during script execution. That is to say, all the options can be placed between coffee and the file name. The-c option is independent and does not have its own parameter. It only indicates that the file provided at the end of the command is compiled, so it can be written as follows:

Copy codeThe Code is as follows:
Coffee-o outputs-c src

If you want to add another option to prevent the compilation result from being surrounded by the self-executed function body, that is:

Copy codeThe Code is as follows:
Coffee-o outputs-c-B src

If you want to compile all the source files into a target file named out. js, it is:

Copy codeThe Code is as follows:
Coffee-o outputs-c-j out src

It would be annoying to execute commands like this for every code change. The coffee command has an option-w which can be automatically compiled to monitor changes to the source file:

Copy codeThe Code is as follows:
Coffee-o outputs-c-w src

For large projects, it is best to determine the compilation method in advance so that all developers only need one instruction to complete all compilation tasks, which requires automatic construction.

Offee provides an automated build tool, cake, just like make in the c world. However, as mentioned on the official website, cake is a very simple building system. In fact, the cake function is to execute a script named cakefile, And the cakefile script is written in coffeescript. This script only provides very limited built-in functions, such as tasks, for declaring a command and its corresponding descriptions and execution functions. Others are writing a pure node project. To complete compilation, either use the fs module of node to output the string compiled by the coffee module, or use the child_process module to execute shell commands. In fact, the goal of cake construction is not necessarily coffee, because it actually executes a node script and can handle any automated tasks.

In addition, there are some better third-party automated building tools that can also complete automatic coffee compilation, such as the famous Grunt and domestic fekit.

This orthodox compilation method may seem the most reliable and should be favored by old programmers. It allows the team to form a fixed development model. In addition, the compiled project becomes a pure js project. No additional dependency is required for running the project directly as an application or being referenced by other projects as a module. In addition, compilation is not required during the runtime, so there is no performance problem caused by compilation.

The disadvantage is that it is too troublesome. If you want to create a project that is not very big, it will take half a day to create a cakefile or configure grunt, which is not worth it.

Through the summary above, writing the node. js project using coffeescript can be very simple. I hope you can use coffee quickly. At the same time, I hope the above content will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.