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:
- 'Use strict ';
- Const react = require ('react ');
- Class MYTEST extends react. Component {
- Constructor (props ){
- // Noinspection JSAnnotator
- Super (props );
- }
- Render (){
- Return (
- <Div> gulp react, jsx to js and es6 to es5. </div>
- )
- }
- }
Next, create the main character in the project root directory --Gulpfile. jsWrite the following code:
- Var gulp = require ('gulp ');
- Var react = require ('Gulp-react ');
- Var babel = require ('Gulp-babel ');
- Var less = require ('Gulp-less ');
- Var sass = require ('Gulp-sass ');
- Gulp. task ("default", function (){
- Return gulp. src ('./src/jsxAndEs6.js') // file to be converted
- . Pipe (react () // convert jsx code
- . Pipe (babel ({
- Presets: ['babel-preset-es2015 '] // plug-in array
- }) // Es6 to es5
- . Pipe (gulp. dest ('./dest'); // output the converted file here
- })
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:
- @ Color: # aaa;
- . Container {
- Color: @ color;
- }
Create an sass file again. The Code is as follows:
- $ FontStack: Helvetica, sans-serif;
- $ PrimaryColor: #333;
- Body {
- Font-family: $ fontStack;
- Color: $ primaryColor;
- }
Next, return to our gulpfile. js and add the tasks that convert sass and less:
- Var gulp = require ('gulp ');
- Var react = require ('Gulp-react ');
- Var babel = require ('Gulp-babel ');
- Var less = require ('Gulp-less ');
- Var sass = require ('Gulp-sass ');
- Gulp. task ('less ', function (){
- Return gulp. src ('./src/Less_style.less ')
- . Pipe (less ())
- . Pipe (gulp. dest ('./dest '));
- });
- Gulp. task ('sass ', function (){
- Return gulp. src ('./src/Sass_style.scss ')
- . Pipe (sass ())
- . Pipe (gulp. dest ('./dest '))
- })
- Gulp. task ("default", ['less ', 'sass'], function (){
- Return gulp. src ('./src/jsxAndEs6.js') // file to be converted
- . Pipe (react () // convert jsx code
- . Pipe (babel ({
- Presets: ['babel-preset-es2015 '] // plug-in array
- }) // Es6 to es5
- . Pipe (gulp. dest ('./dest'); // output the converted file here
- });
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 ~~