“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
Preface ๐คบ
- Recently in build own component library, inside want to use
Gulp
The styles are packaged separately for the on-demand introduction of ๐ - Of course with
Webpack
Configuring plug-ins is also possible, just as a refresherGulp
! - about
Gulp
andWebpack
Refer to my previous article:Gulp and WebPack? โ ๏ธ - Because this article is relatively basic, the big guy can directly detour ๐, this article is intended to record the process of consolidating knowledge and using ๐โ๏ธ
Hands-on ๐คน came ๏ธ
Create a project ๐งฑ
- Execute the following commands separately before everything starts to make sure your computer is installed
node
andnpm
node --version
npm --version
Copy the code
- If no, see Installing Node.js and Configuring the Environment
- In the local empty folder
gulpBegin
Create agulpfile.js
emptyjs
file
mkdir gulpBegin
cd gulpBegin
echo test>gulpfile.js
Copy the code
- And then we’re going to do this
gulpfile.js
The file defines usgulp
The task
Gulp depends on installation ๐๏ธ
- Automatically created
package.json
Press enter (used to store configuration information and version number)
npm init
Copy the code
- The installation
gulp
Command line command
npm install -g gulp
Copy the code
- The installation
gulp
And writepackage.json
File dependencies
npm install --save-dev gulp
Copy the code
- The directory should look like this
GlupBegin โโ gulpfile.js โโ node_modules โโ package-lock.json โโ package.jsonCopy the code
Gulp use ๐
Gulp allows you to use your existing JavaScript knowledge to write gulpFiles, or use your experience with GulpFiles to write normal JavaScript code. While Gulp provides some utilities to simplify file system and command-line operations, the rest of the code you write is pure JavaScript code.
- According to the website
gulp
Can be usedJavaScript
Code to write, let’s write the first task
Exporting tasks ๐ฅฝ
- The way to define a build task is by exporting the function members
/* gulpfile.js */ function gulpFun() {console.log('hi gulp')} export.default =gulpFun // Export a default task memberCopy the code
- in
gulpfile.js
Command line of the directory where the cli is locatedgulp
Can be
- In the latest
gulp
“, it cancelled the synchronous task mode, and agreed that each of our tasks must be asynchronous tasks. When our tasks are completed, we need to call the callback function to identify the completion of this task - If the task is a named task, you need to execute it on the command line interface
gulp
+The name of the task
/* gulpfile.js */ function gulpFun(cb) {console.log('hi gulp') cb()} export.task1 =gulpFun // Export a task1 memberCopy the code
Composite tasks โ๏ธ
- Gulp provides two powerful combinative approaches to situations where there are multiple independent tasks
series()
ๅparallel()
series()
It will let the tasks go one by one
/* gulpfile.js */ const { series } = require('gulp'); Function gulpFun(cb) {// task 1 console.log('hi ') cb()} function gulpFun2(cb) {// task 2 console.log('hi ') cb()} Exports. default= Series (gulpFun,gulpFun2) // Export a default memberCopy the code
parallel()
It will synchronize the tasks
/* gulpfile.js */ const { parallel } = require('gulp'); Function gulpFun(cb) {// task 1 console.log('hi ') cb()} function gulpFun2(cb) {// task 2 console.log('hi ') cb()} Exports. default=parallel(gulpFun,gulpFun2) // Export a default memberCopy the code
You’ve already learned the basics of gulp, but the whole point of using gulp is to process files and reduce the amount of time we waste, so we need to use the GULp API to do it
API ๐ป gulp
gulp
theApi
There are a lot of but commonly used only a few, the rest of the useful to go to the document to find it
- The build process is mostly about pulling files
read
Come out and do someOperation transformation
Put themNext place
forprocessing
At the end of the processing of the documentswrite
To a designated place - The whole process is sequential and step by step like a pipe, so we can use it in the task
.pipe(write to stream)
To achieve data writing
src()
- Create a stream to read objects, which you can use as an entry to this file
dest()
- Create a stream for writing objects to the file system
For example
- Create one in the folder
scss
File testing
/* gulpfile.js */
const { src,dest,series } = require('gulp');
function gulpFun(cb) {
src('./styles/text.scss')
.pipe(dest('./lib/styles'))
cb()
}
exports.default=series(gulpFun)
Copy the code
- run
gulp
After that, the directory structure will look like this
GlupBegin โโ gulpfile.js โโ node_modules โโ lib โ โโ styles โ โโ text.scss โโ package-lock Styles โ โ text. SCSSCopy the code
- You can find
gulp
When reading thestyles/text.scss
After the./lib/styles
The file is automatically generated, which is the read and write stream
The gulp plug-in works with ๐ฅ
- Of course we have to deal with files and not just transfer files like this, we have to convert our files for example
Compress CSS
.Compression js
,rename
,CSS background image generates Sprite diagram
Wait, this time you need to use some plug-ins to assist. - in
gulp
There are also many plug-ins available in the official website, and you can visit them if you need to find themwebsite - Here are a few plugins to help build what I said above
css
Style to package - We assume that the
styles
There are three in the directoryscss
file
Styles โโ button.scss โโ icon.scss โโ indexCopy the code
- in
index.scss
In theimport
The above two SCSS styles
/* index.scss */
@import "button";
@import "icon";
Copy the code
- Next we just need to pack
index.scss
You can import the packaged file somewhere else - I’m using these four plugins, all of which have to be in
npm
Download to copy directly to for easy Postingpackage.json
performnpm install
You can
"DevDependencies" : {" gulp ":" ^ 4.0.2 ", "gulp - autoprefixer" : "^ 7.0.0", "gulp - clean - CSS" : "^ 4.2.0", "gulp - rename" : "^ 1.4.0", "gulp - sass" : "^ 4.0.2"}Copy the code
- in
gulpfile.js
Write the following code
/* gulpfile.js */ const { src,dest,series } = require('gulp'); const cleanCSS = require('gulp-clean-css'); const sass = require('gulp-sass'); const rename = require('gulp-rename'); const autoprefixer = require('gulp-autoprefixer'); Function gulpFun(cb) {SRC ('./styles/index.scss').pipe(sass()) // autoprefixer()) Pipe (cleanCSS()) // Compress the CSS file, } export.default =series(gulpFun).pipe(rename('xl-ui.css')).pipe(dest('./lib/styles'))Copy the code
- run
gulp
It can be packed in./lib/styles
seexl-ui.css
file - Simply put, use the documents you need to deal with
src()
Read and passThe introduction of
A variety ofThe plug-in
To carry outTo deal with
throughpipe
To do the pipe connection, let the whole processExecute in sequence
We just need to give it what it needs to do in each stepSpecification ok
And then after that,There is no need to do repetitive tedious operations
the - And then in addition to packing
index.scss
After I also want to put each individual componentscss
What about packing? - That’s what you do when you pack one, and that’s what you do when you pack two or three, and we just have to define the tasks that he’s going to do
- Create a component.json file to store the files we want to package
/* component.json */
{
"button": "./styles/button.scss",
"icon": "./styles/icon.scss"
}
Copy the code
- in
gulpfile.js
Write the following code
/* gulpfile.js */ const { src,dest,series } = require('gulp'); const cleanCSS = require('gulp-clean-css'); const sass = require('gulp-sass'); const rename = require('gulp-rename'); const autoprefixer = require('gulp-autoprefixer'); const components = require('./component.json') function gulpFun(cb) { src('./styles/index.scss') .pipe(sass()) Pipe (autoprefixer()) // Automatically process the browser prefixes according to the browser version to make CSS compatible. Pipe (cleanCSS()) // Compress the CSS file, Rename ('xl-ui.css')).pipe(dest('./lib/styles')) // write to this directory cb()} // pack a separate CSS file function GulpFun2 (cb) {object.keys (components).foreach (compName => {SRC ('./styles/${compName}.scss ') // Traverse the SCSS file to be converted Pipe (sass()) // convert sass to CSS. Pipe (autoprefixer()) // Automatically process the browser prefixes according to the browser version to make CSS compatible. Pipe (cleanCSS()) // compress the CSS file, Reduce the file size. The pipe (rename (` ${compName}. CSS `)) / / to file renaming, pipe (dest (. / lib/styles')); }) cb()} exports.default=series(gulpFun,gulpFun2)Copy the code
- run
gulp
It can be packed in./lib/styles
seexl-ui.css
Files and separate packagescss
file
GlupBegin โโ node_modules โโ component.json โโ gulpfile.js โโ lib โ โโ styles โ โโ button.css โ โโ icon Xl-ui.css โโ package-lock. โโ package.json โโ styles โโ button.scss โโ icon.scss โโ indexCopy the code
- After we unpack
lib
Foldercss
The file finds that each is indeed zipped and renamed separately - That’ll take care of our paperwork! Isn’t that convenient! After we set a list of tasks that need to be done, the next time we have a document that needs to be packaged, we simply execute a simple sentence
gulp
Command can be
Put it at the end ๐
- What this article says above is only to use
gulp
Package styles, if any, for a variety of modularjs
We still use it when we refer to each otherwebpack
Let’s deal with it. After allwebpack
Focusing on modular packaging, all files will be processed into a unifiedjs
file - for
gulp
There are also a lot of details if you are interested in you can go to the official website document to check, I believe you can understand the smart - If you think this article is helpful to your words might as well support yo ~~๐
Past highlights ๐
Basic configuration and packaging of Webpack with an introduction (I) โก๏ธ
Basic configuration and packaging of Webpack with an introduction (ii) ๐ฆ
Gulp and WebPack? โ ๏ธ
For convenience, I changed someone else’s wheel ๐