Webpack experience sharing
webpack is a module packager. Its main goal is to package JavaScript files together. The packaged files are used in the browser, but it can also be used for transformation, bundle or package. This article mainly shares Webpack with you. Some of my experiences, I hope it can help everyone.
Cache
Generate file name [name].[contenthash:8].js
, combined with max-age + cdn for caching.
Webpage loading
html
It used to be a fixed html, html -"Version number js file (typed through webpack)-"Determine the specific js through the version number . (It doesn’t make sense now that I think about it, although it is not so heavily coupled with the backend)
Now is the changed html, which is typed out through HtmlWebpackPlugin (generated by webpack, containing js with the version number)
Like this It also saves a serial time (pulling the version number file). At the same time, it is also convenient to do grayscale. For example, if you publish the requirements, let some users experience it first, then dynamically type out the html and send it to the background. (Fixing html is not so easy)
css separation
In Guanmai, there will be very few css changes, thanks to react-gm's improvement of class names. Since there are few changes, it is not bad to separate the css and use ExtractTextPlugin to separate the css.
After separation, the size of js is reduced and js is not blocked. At the same time, css and js can be pulled at the same time.
common
1 |
|
Looking at the packaged common code, you will find that the IDs and hashes of other modules will be typed in common, so that the commons file will change every time, making it difficult to cache.
Just provide two file lists, and you will find that webpack puts the id and hash in the manifest file. This way commons can be cached. btw, I didn’t see any introduction on the official website, let me know if you do.
1 |
|
Local development
Let’s talk about agents. Local services are started by themselves. There is no background service, so naturally they need to be agents somewhere. Available via devServer.proxy. You can also use an agent to go to the external network to check for bugs. It is all source code, so bug checking is very fast.
1 2 3 4 5 6 |
|
Packaging speed
DllPlugin
When the project becomes large, it will inevitably require many packages, resulting in a very long packaging time. The official recommended approach is to make files that do not change frequently into DLLs.
Our project will react
react-dom
prop-types
classnames
mobx
mobx-react
lodash
moment
big.js
Waiting for the call.
Online information all introduces that the file name of dll is [name]_[hash].dll.js, ours is [npm version]_dll.js npm version
is package.json version read Come in.
Why not use hash but npm version? We encountered a problem when thinking about secondary packaging. How to judge whether the dll needs to be repackaged? What to do if it is hash (please recommend a solution). If you use npm version, we will repackage as soon as the version changes. For example, if react is upgraded, we will have version + and repackage.
happypack
happypack greatly improves the speed of build. It can be packaged in multiple threads, and the cache also speeds up rebuild.
devtool
Use eval for development and source-map for production (used to troubleshoot display problems, sacrificing packaging speed, within an acceptable range)
babel-loader
Remember cacheDirectory
noParse && alias
Some libraries do not need to be parsed, the noParse configuration does not parse, and alias points to the x.min.js file.
tree shaking
It’s not used yet, it feels like it’s not the right time yet. There is a good article. Your Tree-Shaking has no use.
Compression
UglifyJsPlugin
At the beginning, I used webpack.optimize.UglifyJsPlugin, which was 1.x at that time. version, webpack is based on uglify-js@2.x.
Now webpack has come out separately, and it has many more functions. It supports cache and multi-core compression. I tried it and it was quite fast. webpack.optimize.UglifyJsPlugin is actually uglifyjs-webpack-plugin.
What makes me curious is that the official said it is based on uglify-es. But I think the dependency is still uglify-js. strangeness.
UglifyJsParallelPlugin
Before the official release, I used webpack-uglify-parallel, which supports multi-core. The speed is not much different from uglifyjs-webpack-plugin.
When I wrote the article, I looked through github and found that it was abandoned in July and integrated into the official website.
Others
Babel-minify-webpack-plugin from babel, stay on the sidelines.
Script cdn
Some js (gm-fetch babel-polyfill) has very few changes. Packing with webpack is a bit wasteful. It can be obtained through cdn and put in script form into the html document. .
Analysis
There are charts and very intuitive analysis. I prefer it.
webpack-monitor
The old one is webpack-bundle-analyzer
Related recommendations:
Web example code for using webpack to build front-end projects
Summary of webpack configuration methods
Detailed explanation of npm and webpack configuration methods in node.js
The above is the detailed content of Webpack experience sharing. 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

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

Using Jetty7 for Web Server Processing in JavaAPI Development With the development of the Internet, the Web server has become the core part of application development and is also the focus of many enterprises. In order to meet the growing business needs, many developers choose to use Jetty for web server development, and its flexibility and scalability are widely recognized. This article will introduce how to use Jetty7 in JavaAPI development for We

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

In the current era of rapid development of the Internet, PHP, as a server-side scripting language, is adopted by more and more developers. PHP has the advantages of being easy to learn, flexible, open source and free, and can quickly develop various websites and web applications. However, as a PHP developer, if you want to stand out in the fierce competition and write efficient and stable code, you also need to master the implementation skills and experience of various functions. First of all, reasonable planning of project architecture is the key to developing PHP applications. A good project structure can provide better code maintainability

The web is a global wide area network, also known as the World Wide Web, which is an application form of the Internet. The Web is an information system based on hypertext and hypermedia, which allows users to browse and obtain information by jumping between different web pages through hyperlinks. The basis of the Web is the Internet, which uses unified and standardized protocols and languages to enable data exchange and information sharing between different computers.

PHP belongs to the backend in web development. PHP is a server-side scripting language, mainly used to process server-side logic and generate dynamic web content. Compared with front-end technology, PHP is more used for back-end operations such as interacting with databases, processing user requests, and generating page content. Next, specific code examples will be used to illustrate the application of PHP in back-end development. First, let's look at a simple PHP code example for connecting to a database and querying data:
