Home >Web Front-end >HTML Tutorial >How to clean expired hash files packaged by webpack on the server
The content of this article is about how to clean up the expired hash files packaged by webpack on the server. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Nowadays, front-end projects basically use webpack to package code, and the file names (except html files) are hashed, which can remove the browser cache. But another problem will arise, that is, a large number of expired (will not be used) hash files will accumulate on the server. These files are mainly js and css files, because basically every build will generate new js and css files. If these files are not cleared, they will occupy a large amount of server storage space.
1. Read the hash value set of the css file from the html file;
2. Then based on these hash value sets, the hash is not in this set Delete the css files within;
3. Read the hash value set of the js file from the html file;
4. Then based on these hash value sets, put those that are not dynamically loaded, And js files whose hash is not in this collection are deleted; (Dynamic loading of js refers to dynamic-imports)
5. Read the hash value set of dynamically loaded js files from js files that do not belong to dynamic loading;
6. Then based on these hash value sets, delete the js files that are dynamically loaded and the hash is not in this set;
7. From the remaining html, css, js files Read the hash value set of other static resource files such as pictures and fonts;
8. Then based on these hash value set, delete the static resource files whose hash is not in this set.
Based on the above idea, I encapsulated an npm package: sclean.
Mainly has the following functions:
1. Back up server files, because deletion is a very dangerous operation, so you will back it up before clearing expired files. Of course, you can also back up manually;
2. Restore server files to a state that was previously backed up ( Used for recovery operations after clearing errors);
3. Perform clearing operations according to configuration, such as customizing the target directory (dist, build), html file (php, jsp), hash length (32, 8) wait.
Installation
npm install sclean -g
Perform cleanup operation
sclean
The above is the detailed content of How to clean expired hash files packaged by webpack on the server. For more information, please follow other related articles on the PHP Chinese website!