1. Official explanation
In essence, webpack is a static modular packaging tool for current JavaScript applications. (This sentence can be summarized from two points: modules and packaging )
Let’s talk about the concepts of modules and packaging !
2. Front-end modularization
3. How to understand packaging?
Comparison between webpack and grunt/gulp.
The core of grunt/gulp is Task.
1. You can configure a series of tasks and define the transactions to be processed by the tasks (such as ES6, ts conversion, image compression, scss to css conversion)
2. Then let grunt/gulp execute these tasks in sequence, and automate the entire process
to see a gulp task.
1. The following task is to convert all js files under src into ES5 syntax.
2. And finally output to the dist folder.
When to use grunt/gulp?
1. The project module dependencies are very simple, and the concept of modularization is not even used.
2. Just use grunt/gulp for simple merging and compression.
3. If the entire project uses modular management and the interdependence is very strong, we can use webpack.
What is the difference between grunt/gulp and webpack?
1. grunt/gulp puts more emphasis on the automation of front-end processes, and modularization is not its core.
2. Webpack places more emphasis on modular development management, and functions such as file compression, merging, and preprocessing are its included functions.
( wepack must rely on the node environment for normal operation, and the node environment must be required for normal execution. Use the npm tool to manage various dependent packages in node)
1. File and folder analysis
The following is the code in the mathUtils.js file and the main.js file: (CommonJS modularization specification, CommonJS is the standard of modularization, nodejs is the implementation of CommodJS (modularization))
2. Command
webpack ./src/main.js ./dist/bundle.js (package the main.js file into a bundle.js file)
Description: In the same way, you can also use the ES6 modular specification
3. Create a webpack.config.js file to simplify the packaging command
(map the packaging command to packaging entry and exit ).
The code in this file:
entry: For the packaged entry
output: We need to obtain the path in the packaged exit
description: If you want to use node, you must rely on the package.json file
After running npm install [email protected] --save-dev , the dependencies are added as follows
4. Mapping webpack commands to npm run
In addition to mapping webpack to the entrance and exit , you can also map webpack commands to npm run for some operations (needs to be modified in the **"script"** script tag in package.json). .
1. What is loader?
Now let's think about what webpack is used for?
2. Loader usage process
1)
Preparation work for css file processing:
1. In the src directory, create a css file, create a normal.css file in it
2. Reorganize the directory structure of the file and put the scattered js files in a js folder
3. The code in normal.css is very simple, that is, set the body to red
4. But the style in normal.css will not take effect at this time, because it is not referenced, and webpack cannot find it, because we only have one entry, and webpack will search for other dependent files starting from the entry.
5. At this time we must reference it in main.js, the entry file
After that we need to import the corresponding loader for use!
Step 1 : Install the loader you need to use through npm
(npm install --save-dev css-loader) (npm install --save-dev style-loader)
In the official website of webpack, find the following method of using loader for styles:
Step 2 : Make configuration instructions under the modules keyword in webpack.config.js
: css-loader is only responsible for loading css files , and is not responsible for embedding specific css styles into documents.
At this time, we also need a style-loader help us process
Note: style-loader needs to be placed in front of css-loader.
2) Less file processing
step 1 : Install the corresponding loader (note: less is also installed here, because webpack will use less to compile the less file). Command: npm install --save-dev less-loader less
Step 2 : Modify the corresponding configuration file (in webpack.config.js) and add a rules option for processing .less files. as follows:
3) Image file processing
step 1 : Add two images to the project (one is less than 8kb, the other is greater than 8kb)
step 2 : first consider referencing the image in the css style, as follows
Step 3 : Modify the corresponding configuration file (in webpack.config.js) and add a rules option for processing image files. as follows:
Step 4 : An error was found after packaging, because images larger than 8kb will be processed through file-loader , but there is no file-loader in our project. (You need to install file-loader , command npm install --save-dev file-loader). After installation and packaging, you will find that there is an additional image file in the dist folder.
Description:
Found that webpack automatically generated a very long name for us
1. This is a 32-bit hash value to prevent name duplication.
2. However, in actual development, there may be certain requirements for the name of the packaged image
. Therefore, we can add the following options in options:
1. img: the folder to which the file is to be packaged.
2. name: Get the original name of the picture and put it at this location
3. Hash8: In order to prevent image name conflicts, hash is still used, but only 8 bits are retained.
4. ext: Use the original extension of the image as follows:
You also need to configure and modify the path used by the image later
. 1. By default, webpack will return the generated path to the user.
2. However, the entire program is packaged in the dist folder, so here you need to add another dist/ to the path.
as follows:
In summary, after packaging, the image file looks like this
4), ES6 to ES5 babel
Step 2: Import Vue in main.js (import Vue from 'vue') as follows
Step 3: Hang p on the vue instance in index.html, as follows
Step 4: An error is found after packaging. We need to specify that the vue we use is the runtime-compiler version.
Specific operations: You need to add resolve to webpack and take an alias ( alias ), as follows:
Step 1: Hang p on the vue instance in index.html
Step 2: Import the APP component into the main.js file , register the APP in the Vue instance , and use the component APP in the Vue template ( componentization )
Step 3: Create the APP.vue file and separate the template of the vue page from the js code and css code , as follows
Step 4: Configure the corresponding loader of vue,
Modify the configuration file of webpack.config.js:
1. Understand plugin
2. Webpack-Add copyright information Plugin usage
3. Package html plugin
4. js compression Plugin
. Webpack provides an optional local development server . This local server is built based on node.js and uses the express framework internally. It can achieve what we want by allowing the browser to automatically refresh and display the modified results .
However, it is a separate module. You need to install it before using it in webpck. Command: (npm install --save-dev [email protected])
devserver is also an option in webpack . The option itself can be set as follows property :
1. contentBase: Which folder provides local services. The default is the root folder. We need to fill in ./dist here.
2. port: port number
3. inline: page refresh in real time
4. historyApiFallback: In the SPA page, the history mode
webpack.config.js file configuration that relies on HTML5 is modified as follows:
The –open parameter means opening the browser directly
In addition,
below we want to separate the webpack configuration files: that is, separate the things needed for development and the things used for publishing ( compilation ). as follows:
1. What does CLI mean?
2. Prerequisites for using Vue CLI - Node (Node needs to be installed)
However, to use Node, npm must be involved.
What is NPM?
3. Use Vue CLI to
install the Vue scaffolding
npm install -g @vue/cli
Note: The version installed above is the Vue CLI3 version. If you want to initialize the project according to the Vue CLI2 method, it is not possible
to initialize the Vue CLI2 project
vue init webpack my -project