Gulp pitfall series (4) -- code conversion of gulp, gulp

Source: Internet
Author: User

Gulp pitfall series (4) -- code conversion of gulp, gulp
Finally, I entered the topic. It was just to convert Sass and SCSS code into CSS that I suddenly jumped from the learning path of Sass to the big pitfall of gulp.

Of course, gulp can not only convert Sass, but here we will mention the following conversion:

First, create a new folder, and then proceed with the same method as before, in the folder Npm init, Npm install -- save-dev gulpAfter installing gulp, we also need to install several plug-ins for code conversion. The relationship is as follows:
Jsx Code Conversion Npm install gulp-react
Es6 Code Conversion  Npm install gulp-babel, npm install babel-preset-es2015 (es6 is also called es2015)
Less Code Conversion Npm install gulp-less
Sass Code Conversion Npm install gulp-sass
  Tips: If the npm installation is slow or the card is in progress, you can switch to cnpm.     First, the conversion between jsx and es6:Okay. After the preparation is complete, we will first create a src directory, create a js file under the directory, and write some jsx and es6 code:
  1. 'Use strict ';
  2. Const react = require ('react ');
  3. Class MYTEST extends react. Component {
  4. Constructor (props ){
  5. // Noinspection JSAnnotator
  6. Super (props );
  7. }
  8. Render (){
  9. Return (
  10. <Div> gulp react, jsx to js and es6 to es5. </div>
  11. )
  12. }
  13. }

Next, create the main character in the project root directory --Gulpfile. jsWrite the following code:

 
  1. Var gulp = require ('gulp ');
  2. Var react = require ('Gulp-react ');
  3. Var babel = require ('Gulp-babel ');
  4. Var less = require ('Gulp-less ');
  5. Var sass = require ('Gulp-sass ');
  6. Gulp. task ("default", function (){
  7. Return gulp. src ('./src/jsxAndEs6.js') // file to be converted
  8. . Pipe (react () // convert jsx code
  9. . Pipe (babel ({
  10. Presets: ['babel-preset-es2015 '] // plug-in array
  11. }) // Es6 to es5
  12. . Pipe (gulp. dest ('./dest'); // output the converted file here
  13. })

Note: When importing data to the database here, we didn't introduce the babel-preset-es2015 we installed with npm, because it exists as a plug-in parameter for babel conversion, which can be called directly in babel.

 

Next, run the gulp command and you will find that there is an additional js file under the dest directory of the directory, and the converted code is opened, which is terrible:


Here, the conversion between jsx and es6 is done.



Next are Less and Sass:

First, create a less file in the src directory. The Code is as follows:

 
  1. @ Color: # aaa;
  2. . Container {
  3. Color: @ color;
  4. }

Create an sass file again. The Code is as follows:

  1. $ FontStack: Helvetica, sans-serif;
  2. $ PrimaryColor: #333;
  3. Body {
  4. Font-family: $ fontStack;
  5. Color: $ primaryColor;
  6. }


Next, return to our gulpfile. js and add the tasks that convert sass and less:

 
  1. Var gulp = require ('gulp ');
  2. Var react = require ('Gulp-react ');
  3. Var babel = require ('Gulp-babel ');
  4. Var less = require ('Gulp-less ');
  5. Var sass = require ('Gulp-sass ');
  6. Gulp. task ('less ', function (){
  7. Return gulp. src ('./src/Less_style.less ')
  8. . Pipe (less ())
  9. . Pipe (gulp. dest ('./dest '));
  10. });
  11. Gulp. task ('sass ', function (){
  12. Return gulp. src ('./src/Sass_style.scss ')
  13. . Pipe (sass ())
  14. . Pipe (gulp. dest ('./dest '))
  15. })
  16. Gulp. task ("default", ['less ', 'sass'], function (){
  17. Return gulp. src ('./src/jsxAndEs6.js') // file to be converted
  18. . Pipe (react () // convert jsx code
  19. . Pipe (babel ({
  20. Presets: ['babel-preset-es2015 '] // plug-in array
  21. }) // Es6 to es5
  22. . Pipe (gulp. dest ('./dest'); // output the converted file here
  23. });

 

Note: after a new task is written, it must be introduced to default. Otherwise, it will not be executed.

Next, execute the gulp command on the command line and you will find that there are more compiled css files in the dest folder. The effect is as follows:

 

Success ~~


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.