This article will share with you 10 interview questions about webpack. Check for any gaps and fill them in. How many of these ten interview questions can you answer correctly? Can you get them all correct?
The interview questions are just an introduction, Quickly brushing the questions is just memorizing
(the interviewer will ask you, but he may not be very in-depth), think It still takes a lot of effort to understand deeply
;
Initialization parameters
: Parse the webpack configuration parameters, merge the parameters passed in by the shell and configured in the webpack.config.js file to form the final configuration result;
Start compiling
: Obtained from the previous step Initialize the compiler object with the parameters, register all configured plug-ins, the plug-in listens to the event nodes of the webpack build life cycle, reacts accordingly, and executes the run method of the object to start compilation;
Determine the entry
: From the configured entry, start parsing the file to build the AST syntax tree, find the dependencies, and go on recursively;
Compile module
: In the recursion, according to the file type and loader configuration, all configured loaders are called to convert the files, and then the modules that the module depends on are found, and this step is recursed until all the entry-dependent files have been processed by this step;
Complete module compilation and output
: After the recursion is completed, each file result is obtained, including each module and the dependencies between them, and code is generated based on the entry or subcontracting configuration. Block chunk;
Output completed
: Output all chunks to the file system;
In fact, I opened the express application
, added monitoring of webpack compilation, added a long websocket connection with the browser, and triggered webpack to compile when the file changes And after completion, will tell the browser through sokcet message to prepare to refresh
. In order to reduce the cost of refreshing, does not need to refresh the web page
, but refreshes a certain module
, webpack-dev-server can support hot update, and compare it through the hash value of the generated file For modules that need to be updated, the browser will perform hot replacement
Server
Client
1. webpack There are many ways to calculate hash in the ecosystem
hash
chunkhash
contenthash
hash represents the hash value generated in each webpack compilation. All files using this method have the same hash. Each build causes webpack to calculate a new hash. Chunkhash is formed based on the entry file and its associated chunk. Changes to a certain file will only affect the hash value of the chunk associated with it, but will not affect the contenthash creation of other files based on the file content. When the file content changes, the contenthash changes
2. Avoid the same random value
after
calculating the hash. The same random value may be generated because these files belong to the same chunk. You can mention a file to an independent chunk (such as putting it into entry)
<% HtmlWebpackPlugin.options.loading.html %>
. In html-webpack-plugin, you can inject script into it by configuring html attributesProvidePlugin
: Automatically load modules, replacing require and importhtml-webpack-plugin
Can automatically generate html code based on the template, and automatically reference css and js filesextract-text-webpack-plugin
Extract styles referenced in js files into css files DefinePlugin
Configure global variables at compile time, which is very useful for allowing different behaviors in development mode and release mode builds. HotModuleReplacementPlugin
Hot updateoptimize-css-assets-webpack-plugin
Duplicate css in different components can be quickly removedwebpack-bundle-analyzer
A webpack bundle file analysis tool that displays bundle files in the form of interactively zoomable treemaps. compression-webpack-plugin
The production environment can use gzip to compress JS and CSShappypack
: Accelerate code construction through a multi-process modelclean-wenpack-plugin
Clean up unused files under each package speed-measure-webpack-plugin
: You can see to U each The execution time of each Loader and Plugin (the time of the entire package, the time of each Plugin and Loader) webpack-bundle-analyzer
: Visualize the volume of Webpack output files (business components , Rely on third-party modulesfunction MyWebpackPlugin()( }; // prototype 上定义 apply 方法 MyWebpackPlugin.prototype.apply=function(){ // 指定一个事件函数挂载到webpack compiler.pluginCwebpacksEventHook"funcion (compiler)( console. log(“这是一个插件”); // 功能完成调用后webpack提供的回调函数 callback() })
file-loader
: Output the file to a folder, and reference the output through a relative URL in the code Fileurl-loader
: Similar to file-loader, but can inject the file content into the code in base64 when the file is very small. source-map-loader
: Load additional Source Map files to facilitate breakpoint debugging image-loader
: Load and Compress image filesbabel-loader
: Convert ES6 to ES5css-loader
: Load CSS, support modularization, Features such as compression and file importstyle-loader
: Inject CSS code into JavaScript and load CSS through DOM operations.eslint-loader
: Check JavaScript code through ESLintThe server sets the http cache header
(cache-control)that is, as splitChunk, because they are almost unchanged
Lazy loading
: Using import() method
, dynamically loaded files can be divided into independent chunks to get their own chunkhashKeep the hash value stable
: Changes in the compilation process and file intercommunication should try not to affect the calculation of hashes of other files. For the unstable issue of incremental digital IDs generated by lower versions of webpack, you can use hashedModuleIdsPlugin to generate based on file paths. Using webpack to optimize front-end performance means optimizing the output of webpack so that the final packaged result runs quickly and efficiently in the browser.
Compress code
: Remove redundant code, comments, simplify code writing, etc. You can use webpack's UglifyJsPlugin and ParallelUglifyPlugin to compress JS files, and use cssnano (css-loader?minimize) to compress cssUse CDN to accelerate
: During the build process, the css file will be compressed. Change the static resource path used to the corresponding path on the CDN. You can use webpack's output parameter and the publicPath parameter of each loader to modify the resource pathTree Shaking
: Delete the segments that will never be seen in the code. You can achieve this by appending the parameter --optimize-minimize when starting webpack. Code Splitting
: Divide the code into chunks according to routing dimensions or components, so that it can be loaded on demand. At the same time, you can make full use of the browser cacheto extract public third-party libraries
: SplitChunksPlugin plug-in to extract public modules. The browser cache can be used to cache these for a long time without frequent changes. The public codetree-shaking optimization
, which is a method of moving Except for more code, to optimize the packaging volume, is enabled by default in the production environment
. unnecessary code
when the code is not running
; es6 module
Specificationstatic analysis
, so correctly determine which modules are loaded during compilation
Original address: https://juejin.cn/post/7002839760792190989
Author: Front-end_Maverick_to Rhino
Related tutorial recommendations: "Webpack Getting Started Video Tutorial"
The above is the detailed content of 10 webpack interview questions, how many can you answer?. For more information, please follow other related articles on the PHP Chinese website!