JSDev: A small tool used to optimize the JavaScript development process

Source: Internet
Author: User
Tags echo command

JSDev is a rare gadget recently developed by Douglas Crockford. His general idea is to pre-process JavaScript and selectively convert comments into corresponding code. The annotation block processed by JSDev has the following two forms:

 
 
  1. /*<tag> <stuff>*/ 
  2. /*<tag>(<condition>) <stuff>*/ 

Then you can specify the tags enabled by JSDev. Then, JSDev will change this annotation to the corresponding code, and these annotations will not work normally in JavaScript, after JSMin and other tools are compressed, all of them will be removed, which facilitates JavaScript development.

In the beginning, old Dao only provided the C-language version of the program. A few days later, he provided the JavaScript version. I thought it became interesting and I had to spend two days, I want to see if idea can be used in future development projects. Next we will take the C program as an example to illustrate the usage of JSDev, and then talk about how to use it in node development or front-end development through the JavaScript version. This article assumes that you work in Unix-like systems (Linux/FreeBSD/Mac OS) and have some knowledge of command line and node programming.

C language version and JSDev usage

Clone my fork from github and compile the C source code into an executable file.

 
 
  1. Git clone https://github.com/yuest/jsdev.git # clone source code on github to local
  2. Cd jsdev
  3. Gcc jsdev. c # compile the C version
  4. Sudo mv a. out/usr/local/bin/jsdev # Move the compiled executable file to the directory under $ PATH

Now we can use jsdev to run the program. However, if you enter jsdev in the command line and press enter, there is nothing. In the past, the old saying only made the core part of this tool, but did not make any effort on the software interface, therefore, no command usage prompt is displayed. As a pre-processor, it only eats JavaScript code from stdin, and then pulls the processed code from stdout. As for file operations, you can use the pipeline feature of unix command lines.

To popularize this, we need to put files, such as in. js when stdin is input to the jsdev program, use the less than symbol <, such as jsdev <in. js, and the output is bigger than the symbol>: jsdev <in. js> out. js. To try jsdev, you only need to use jsdev <in. js to output the result to the screen for easy display, or you can even use the echo command to test simple single-line code. For example, echo "/* tag 'stuff' */" | jsdev tag

Only run jsdev <in. js> out. the js command does not apply to in. js has any changes. We need to call the jsdev command with tag as the parameter to specify the tag to be enabled. Multiple spaces are separated by spaces. For example, if the in. js code is as follows:

 
 
  1. /*hello console.log('Hello, World!')*/ 
  2. /*hi console.log('Hi, there.')*/ 

Then jsdev <in. js hello will output the following code:

 
 
  1. {console.log('Hello, World!')}  
  2. /*hi console.log('Hi, there.')*/ 

While jsdev <in. js hello hi is:

 
 
  1. {console.log('Hello, World!')}  
  2. {console.log('Hi, there.')} 

Curly braces are added to avoid association with other parts of the program. The main problem is that JavaScript has the design of automatically inserting semicolons at the end of a line. If the context is separated without curly braces, it may be associated with the front and back rows, such as function calls in the previous row. In fact, curly braces combine multiple statements into one statement. The most common difference is that they are used after the if statement accepts only the keywords of one statement. Curly braces in JavaScript do not generate a namespace.

You can specify a function name for the tag, so the comment block specified by this tag will be a bit like JSONP by this function name wrap), as shown in. js:

 
 
  1. /*log some_variable*/ 

Run the command jsdev <in. js log: console. log to output:

 
 
  1. {console.log(some_variable);} 

This can be used to print some variable values in the current State during code debugging, which is more convenient.

You can also specify conditions for the comment block, as shown in. js below:

 
 
  1. var name = 'Yuest' 
  2. /*hello console.log('Hello, World!')*/ 
  3. /*hello(name === 'Celine') console.log('Hello, Celine!')*/ 

Run the command jsdev <in. js hello output:

 
 
  1. var name = 'Yuest' 
  2. {console.log('Hello, World!')}  
  3. if (name === 'Celine') {console.log('Hello, Celine!')} 

If you run this code, it is clear that only Hello, World! Will be output !.

The specified functions and conditions can also be used at the same time, for example:

 
 
  1. var name = 'Yuest' 
  2. /*log 'Hello, World!'*/ 
  3. /*log(name === 'Ling') 'Hello, Ling!'*/ 

Run the command jsdev <in. js log: console. log to output:

 
 
  1. var name = 'Yuest' 
  2. {console.log('Hello, World!');}  
  3. if (name === 'Ling') {console.log('Hello, Ling!');} 

It is almost the same as the previous one, but the comments are much shorter. The above is everything about how to use JSDev. In fact, the most important form is/* <tag> <stuff> */. We can simply use this form to achieve the same purpose:

 
 
  1. var name = 'Yuest' 
  2. /*hello console.log('Hello, World!')*/ 
  3. /*hello if (name === 'Ling') {console.log('Hello, Ling!')}*/ 

The above Code uses the command jsdev <in. js hello output:

 
 
  1. var name = 'Yuest' 
  2. {console.log('Hello, World!')}  
  3. {if (name === 'Ling') {console.log('Hello, Ling!')}} 

Therefore, it can be seen that specifying function names and conditions for tags is only simplified.

By the way, I forgot to run the jsdev command to accept a-comment parameter to add comments to the output file header. For example, for the above in. js file, run the following command:

 
 
  1. jsdev <in.js hello -comment "Author: Yuest" -comment "This is a temporary file generated by JSDev" 

You will get:

 
 
  1. // Author: Yuest  
  2. // This is a temporary file generated by JSDev  
  3. var name = 'Yuest' 
  4. {console.log('Hello, World!')}  
  5. {if (name === 'Ling') {console.log('Hello, Ling!')}} 

Well, the above is really about the usage of JSDev, and about how to make good use of it, his user needs a little imagination. For example, you can write some bash scripts or something. The following describes the JavaScript version of JSDev and the methods I have come up with for using the node project.

JavaScript version and node usage

The old-mentioned jsdev. js defines only one JSDEV function and accepts three parameters. The first parameter is the source code to be processed, and the second and third parameters are two arrays, respectively specifying tags and comments. If the last command above is processed using the JavaScript version, it should be:

 
 
  1. JSDEV( input  
  2.      , ['hello']  
  3.      , ['Author: Yuest', 'This is a temporary file generated by JSDev']) 

My fork has added exports. JSDEV = JSDEV to node and publish to npm. You only need to add the jsdev dependency to npm install jsdev or package. json, and add the JSDEV dependency to the source code.

 
 
  1. var JSDEV = require('jsdev').JSDEV 

You can use the JavaScript version of JSDEV () function.

Because node can modify module. constructor. prototype. _ compile to modify the file to be loaded, it is very suitable to apply the JSDev Preprocessor. My idea is to describe the comments under which tags should be opened in a file. It should also be described in its own annotations. If the program loads jsdev and calls its replaceRequire method require ('jsdev '). replaceRequire (). Then, if the file to be entered by require contains comments such as // @ jsdev tag1 tag2, the comments block specified by tag1 and tag2 will be enabled with jsdev.

Specify the rule for enabling tags to write as follows:

 
 
  1. //@jsdev(test,production) tag1 tag2  
  2. //@jsdev(development) tag3 

The above annotation line indicates that the tag1 tag2 annotation block of this file is enabled when the environment variable NODE_ENV = test or NODE_ENV = production is enabled, and the tag3 annotation block is enabled when NODE_ENV = development. Without the brackets after jsdev, the default value is development. Therefore, you can write

 
 
  1. //@jsdev(test,production) tag1 tag2  
  2. //@jsdev tag3 

For example, a. js:

 
 
  1. require('jsdev').replaceRequire()  
  2. require('./b') 

B. js:

 
 
  1. //@jsdev(test,production) hello  
  2. //@jsdev hi  
  3. /*hello console.log('hello')*/ 
  4. /*hi console.log('hi')*/ 

Then Running node a. js will output hi, while running NODE_ENV = test will output hello

In addition, the same is true for front-end development. With the replaceStatic () function, you can replace the static middleware of connect/express. In this way, the JavaScript files returned to the browser through static middleware can be preprocessed. The rules for enabling tags in the file are the same as those previously written. Note that you must call replaceStatic () before using connect. static (root ().

For more examples, refer to the test I wrote. In the exported code,./runtests, of course, make sure that the npm dependency has been installed, that is, run npm install in the directory) to run the test. I am here to provide the usage of this very common tool. It depends on the user's imagination to make good use of its environment and set the tag.

If you have any shortcomings, you are welcome to testify. You can go to github to raise issue.

Original article: http://club.cnodejs.org/topic/4f3b65a1b43c3c846a05518c

Related Article

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.