"Mobile web Front end efficient development" Note 3--Code Check task

Source: Internet
Author: User

During the development of a project, a unified code style is important for project collaboration and maintainability, so you can use some plug-ins for code-style checking.

The source file in this example contains two categories: the Sass file and the JavaScript file with the ECMAScript 6 specification. In gulp, the Gulp-eslint and Gulp-sass-lint plug-ins are used to detect them separately.

1. In the Code inspection task, using the open source JavaScript validation tool eslint for processing, before performing the task requires the global installation of Eslint, the command is as follows:

NPM Install Eslint–g

2. Execute the Eslint initialization command in the project directory and create the Eslint configuration file with the following command:

Eslint–init

3. Proceed to the Eslint Project Initialization Wizard, first select the configuration method, here Select "Use a popular style guide", 8.6 is shown.

4. Select the preset mode, where "standard" is selected and 8.7 is shown.

Figure 8.6 Choosing a configuration mode Figure 8.7 Choosing a preset mode

5. Select the storage file type for the configuration content, as shown in 8.8.

Figure 8.8 Selecting the storage file type for the configuration content

At the end of the Eslint project configuration, a file named ". Eslintrc" is generated in the directory. The next step is to introduce the plugin Gulp-eslint installation. First install the plugin in the project, the command is as follows:

NPM installgulp-eslint–save-Dev in the Gulpfile.js file, add a task to check the JavaScript code:varEslint = require ("Gulp-eslint");//Introducing the Eslint pluginGulp.task ("Eslint", function () {//Defining code Review TasksGULP.SRC ("Src/**/*.js")//perform a code check on all JS files under src. Pipe (Eslint ({//Perform checksUSEESLINTRC:true                           //using the. ESLINTRC configuration file}). Pipe (Eslint.format ())//Output Check Results. Pipe (Eslint.failaftererror)//Terminate Gulp task when current code check fails});

The example above simply describes how to perform a code check with the Eslint plugin in gulp. For more eslint configuration items and plugin documentation, see http://eslint.org/.

Note: Eslint provides two ways to terminate tasks: Eslint.failaftererror and Eslint.failonerror. The former waits until all the checks have been executed before terminating, while the latter terminates immediately when an error occurs.

Next, we introduce a code check for the Sass file using the Gulp-sass-lint plugin. First, install the plugin:

NPM Installgulp-sass-lint--save-Dev adds a task to the gulpfile.js to check the Sass file for code as follows:varSasslint =require ("Gulp-sass-lint");//Introducing the Sasslint pluginGulp.task ("Sasslint", function () {//Defining Sasslint TasksGULP.SRC ("Theme/**/*.scss"). Pipe (Sasslint ())//execute code check with Sasslint plugin. Pipe (Sasslint.format ())//Output Check Results. Pipe (Sasslint.failonerror ())//terminates the Gulp task when an error occurs});

This section briefly describes how to use the Gulp plug-in to check the code. For further usage of eslint and sasslint, please refer to the corresponding documentation.

More information about:

"Mobile web Front end efficient development" Note 3--Code Check task

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.