


[Compilation and sharing] Some webpack interview questions (with answer analysis)
Talk about your understanding of Webpack
1.What is Webpack?
webpack is a static module packager. When webpack processes an application, it recursively builds a dependency graph that contains every module the application needs, and then packages these modules into one or Multiple bundles.
Webpack is like a production line. It must go through a series of processing processes (loaders) before the source files can be converted into output results. Each processing process on this production line has a single responsibility, and there are dependencies between multiple processes. Only after the current processing is completed can it be handed over to the next process for processing.
A plug-in is like a function inserted into the production line, which processes the resources on the production line at a specific time. Webpack will broadcast events during the running process. The plug-in only needs to listen to the events it cares about and can join the production line to change the operation of the production line.
2. Can you talk about the packaging process/building process
3.Can you talk about the optimization of front-end operation
The packaging process/packaging principle/building process of Webpack?
# The running process of webpack is a serial process, and its workflow is to connect various plug-ins in series.
Command line executionnpx webpack
Packaging command starts
1.Initialize compilation parameters
: Read and merge parameters from configuration files and shell commands
2.Start compilation
: Initialize the Compiler object according to the parameters obtained in the previous step, load all configured Plugins, and execute the run method of the object to start compilation.
3.Determine the entry
: Find all entry files according to the entry in the configuration
4.Compile module
: Trigger from the entry file and call all configured Loader pairs The module is translated, and then the modules that the module depends on are found, and then this step is recursed until all the entry-dependent files are translated.
5.Complete module compilation
: After using Loader to translate all modules in step 4, the final translated content of each module and the dependency graph between them are obtained.
6.Output resources
: According to the dependency graph, assemble Chunks containing multiple modules, then convert each Chunk into a separate file and add it to the output list, and determine the output according to the configuration The path and file name are output.
In the above process, Webpack
will broadcast specific events at specific points in time, and the plug-in will execute specific logic after listening to the events of interest.
Summary
- Initialization: Read and merge parameters from configuration files and shell commands, initialize Compiler instances according to parameters, load Plugin (register all configured plug-in), call the run method of the Compiler instance to start compilation.
Compiler compilation object controls the webpack life cycle and does not perform specific tasks, but only performs some scheduling work. For example, perform module creation, dependency collection, chunking, packaging, etc.
After calling run, a Compiltation instance is created. Each build will create a new Compiltation instance, which contains the basic information of the buildWebpack
Specific events will be broadcast at specific points in time, and the plug-in will execute specific logic after listening to the events of interest.
- Compilation: Triggered from entry, each Module is serially called to the corresponding Loader to translate the module, and then the modules that the module depends on are found and compiled recursively.
Start parsing the file to build the AST syntax tree from the entry specified in the configuration file (webpack.config.js)
- According to dependencies The graph is assembled into a chunk containing multiple modules, and each chunk is converted into a file for output.
Different entries generate different chunks, and dynamic import will also generate its own chunk
The role of loader in Webpack/ What is a loader?
Loader is webpack provides a mechanism to handle multiple file formats, because webpack only knows JS and JSON, so Loader is equivalent to a translator, converting other types of resources Perform preprocessing.
Used to convert the "source code" of the module.
The loader supports chain calls, and the order of calls is from right to left. **Each loader in the chain will process resources that have been processed before, and eventually become js code.
You can provide more capabilities to the JavaScript ecosystem through loader's preprocessing functions.
What are the common loaders?
- less-loader: Compile less files into css files
During development, we often use the less preprocessor to write css styles. Improve development efficiency
- css-loader: Turn the css file into a commonjs module and load it into js. The module content is the style string
- style-loader: Create a style tag and load the style in js Insert the resource into the tag and add the tag to the head to take effect
- ts-loader: Package and compile Typescript files
What does Plugin do? /What is Plugin
Plugin is more powerful, and its main purpose is to solve things that loader cannot achieve, such as packaging optimization and code compression.
After the Plugin is loaded, the functions defined by the plugin will be triggered at a certain point in the webpack build to help webpack do something. Implement functional extensions to webpack.
What are the common Plugins
- html-webpack-plugin processes html resources and creates an empty HTML by default, automatically Introduce all the resources of the packaged output (js/css)
- mini-css-extract-plugin The packaged css is in the js file. This plug-in can extract the css separately
- clean-webpack -plugin Every time you package, the CleanWebpackPlugin plug-in will automatically delete the last package.
What is the execution order (loading mechanism) of the Webpack plug-in?
The difference between Loader and Plugin in Webpack
Always say
webpack is like a production line, which needs to go through a series of processes Only after the process (loader) can the source file be converted into an output result. Each processing process on this production line has a single responsibility, and there are dependencies between multiple processes. Only after the current processing is completed can it be handed over to the next process for processing.
A plug-in is like a function inserted into the production line, which processes the resources on the production line at a specific time. Webpack will broadcast events during the running process. The plug-in only needs to listen to the events it cares about and can join the production line to change the operation of the production line.
Or use the previous summary to explain what Loader and Plugin are respectively
Running timing
1. The loader runs in the compilation phase
2. Plugins work throughout the entire cycle
Usage method
Loader: 1. Download 2. Use
Plugin: 1. Download 2 .Quote 3. What optimization methods have been done using
#Webpack? What optimization methods are there?
How to use webpack to optimize front-end performance? The question is about production environment optimization
How to improve the build speed of webpack? The question is about optimization of build speed
tree-shaking Delete unused code to optimize front-end performance/increase build speed
tree-shaking is A kind of Dead Code Elimination technology packaging based on the ES Module specification. During the packaging process, modules that have not been referenced in the project are detected and marked, and the modules that have not been referenced are deleted, which improves the build speed and reduces the program running time.
What should you pay attention to when using tree-shaking?
1. Default mode = production
, the tree-shaking
function is enabled by default in the production environment.
2. Module code needs to be written using ES6 specifications. ES6 module dependencies are deterministic and have nothing to do with runtime status.
3. Try not to write code with side effects. For example, you have written an immediate execution function, used external variables in the function, etc.
How to use webpack to optimize front-end performance?
- Code compression
On-demand loading
- Code split splitChunks - in the optimization configuration item Configuration
1. You can package the code in node__mudules separately into a chunk output (for example, using jqury?)
2. It will automatically analyze whether there are any common ones in the multi-entry chunk Files, if any, will be packaged into a separate chunk and will not be packaged repeatedly
- Use Dll for sub-packaging
Under normal circumstances node_module will be Package into a file
Use dll technology to package frameworks and libraries that are not frequently updated separately and generate a chunk
- Use routing to lazy load
All modules referenced by the import() function in the code will be packed into a separate package and placed in the directory where the chunk is stored. When the browser runs this line of code, it will automatically request this resource and implement asynchronous loading.
#How does Webpack configure compression code? What is compressed?
1. Configure the plug-in as a compressor for compression in the optimization configuration item.
2. Use this plug-in for compression in plugins
js compression: using terser-webpack-plugin
css compression: using optimize-css-assets -webpack-plugin plug-in
Removed console, comments, spaces, line breaks, unused css code, etc.
How to improve the build speed of webpack?
Idea 1: Reduce the files or codes that need to be built
- HMR (Hot Module Replacement) module hot replacement only rebuilds the changed modules – in the development environment
- Narrow the processing scope: Make reasonable use of these two attributes exclude: files that do not need to be processed and include: files that need to be processed
- When the babel cache is built for the second time, the previous cache will be read, only Rebuild the changed files
- Use Dll for subpackaging--> Subpackaging is convenient for on-demand loading
Normally node_module will be packaged into a file
Using dll technology, you can package those frameworks and libraries that are not frequently updated separately to generate a chunk
The project source code also needs to be split, and the packaged files can be divided according to routing--> How to implement routing lazy loading ? How to implement asynchronous loading of components in webpack?
Idea 2: Build in multiple processes
- Package thread-loader in multiple processes and place it before the time-consuming loader
Process startup and process communication have overhead, and the working time is relatively long, so multi-process packaging is required
[Related recommendations: javascript video tutorial, programming video】
The above is the detailed content of [Compilation and sharing] Some webpack interview questions (with answer analysis). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Vue is an excellent JavaScript framework that can help us quickly build interactive and efficient web applications. Vue3 is the latest version of Vue, which introduces many new features and functionality. Webpack is currently one of the most popular JavaScript module packagers and build tools, which can help us manage various resources in our projects. This article will introduce how to use Webpack to package and build Vue3 applications. 1. Install Webpack

As a programming language that has become very popular in recent years, Go language has become a hot spot for interviews in many companies and enterprises. For beginners of the Go language, how to answer relevant questions during the interview process is a question worth exploring. Here are five common Go language interview questions and answers for beginners’ reference. Please introduce how the garbage collection mechanism of Go language works? The garbage collection mechanism of the Go language is based on the mark-sweep algorithm and the three-color marking algorithm. When the memory space in the Go program is not enough, the Go garbage collector

Differences: 1. The startup speed of the webpack server is slower than that of Vite; because Vite does not require packaging when starting, there is no need to analyze module dependencies and compile, so the startup speed is very fast. 2. Vite hot update is faster than webpack; in terms of HRM of Vite, when the content of a certain module changes, just let the browser re-request the module. 3. Vite uses esbuild to pre-build dependencies, while webpack is based on node. 4. The ecology of Vite is not as good as webpack, and the loaders and plug-ins are not rich enough.

As a well-known programming learning website, php Chinese website has compiled some React interview questions for you to help front-end developers prepare and clear React interview obstacles.

This article summarizes some selected Web front-end interview questions worth collecting (with answers). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

With the continuous development of web development technology, front-end and back-end separation and modular development have become a widespread trend. PHP is a commonly used back-end language. When doing modular development, we need to use some tools to manage and package modules. Webpack is a very easy-to-use modular packaging tool. This article will introduce how to use PHP and webpack for modular development. 1. What is modular development? Modular development refers to decomposing a program into different independent modules. Each module has its own function.

Configuration method: 1. Use the import method to put the ES6 code into the packaged js code file; 2. Use the npm tool to install the babel-loader tool, the syntax is "npm install -D babel-loader @babel/core @babel/preset- env"; 3. Create the configuration file ".babelrc" of the babel tool and set the transcoding rules; 4. Configure the packaging rules in the webpack.config.js file.

This article will share with you 50 Angular interview questions that you must master. It will analyze these 50 interview questions from three parts: beginner, intermediate and advanced, and help you understand them thoroughly!
