1. Introduction I have been studying the principles of The functions implemented by this plugin are relatively simple:
2. Webpack construction process and plugin principle2.1 Webpack build process The main construction process of
If 2.2 Plugin Principle A // Define a plugin class MyPlugin { // Constructor to receive plugin configuration options constructor(options) { // Get configuration items and initialize the plugin} // When the plugin is installed, apply is called and passed to the compiler apply(compiler) { // Get exclusive access to comolier, you can listen to event hooks // Function development... } } 2.3 compiler and compilation objects The two most commonly used objects in the development of
3. Plugin Development3.1 Project Directory The functions implemented by this plug-in are relatively simple, and the file directory is not complicated. First, create an empty folder // remove-console-Webpack-plugin ├─src │ └─index.js ├─.gitignore ├─package.json └─README.md 3.2 Plugin CodeThe plug-in code logic is not complicated, there are several main points:
class RemoveConsoleWebpackPlugin { // The constructor accepts configuration parameters constructor(options) { let include = options && options.include; let removed = ['log']; // default clearing methodif (include) { if (!Array.isArray(include)) { console.error('options.include must be an Array.'); } else if (include.includes('*')) { // Passing in * means clearing all consoles removed = Object.keys(console).filter(fn => { return typeof console[fn] === 'function'; }) } else { removed = include; // Overwrite according to the incoming configuration} } this.removed = removed; } // Webpack will call the apply method of the plugin instance and pass in the compiler object apply(compiler) { // js resource code processing function let assetsHandler = (assets, compilation) => { let removedStr = this.removed.reduce((a, b) => (a + '|' + b)); let reDict = { 1: [RegExp(`\\.console\\.(${removedStr})\\(\\)`, 'g'), ''], 2: [RegExp(`\\.console\\.(${removedStr})\\(`, 'g'), ';('], 3: [RegExp(`console\\.(${removedStr})\\(\\)`, 'g'), ''], 4: [RegExp(`console\\.(${removedStr})\\(`, 'g'), '('] } Object.entries(assets).forEach(([filename, source]) => { // Match js file if (/\.js$/.test(filename)) { // File content before processing let outputContent = source.source(); Object.keys(reDict).forEach(i => { let [re, s] = reDict[i]; outputContent = outputContent.replace(re, s); }) compilation.assets[filename] = { // Return file content source: () => { return outputContent }, // Return file size size: () => { return Buffer.byteLength(outputContent, 'utf8') } } } }) } /** * Listen to the event through compiler.hooks.compilation.tap * Get the compilation object in the callback method */ compiler.hooks.compilation.tap('RemoveConsoleWebpackPlugin', compilation => { // Webpack 5 if (compilation.hooks.processAssets) { compilation.hooks.processAssets.tap( { name: 'RemoveConsoleWebpackPlugin' }, assets => assetsHandler(assets, compilation) ); } else if (compilation.hooks.optimizeAssets) { // Webpack 4 compilation.hooks.optimizeAssets.tap( 'RemoveConsoleWebpackPlugin', assets => assetsHandler(assets, compilation) ); } }) } } // export Plugin module.exports = RemoveConsoleWebpackPlugin; 4. Publish to npm If you want others to use your plugin, you need to publish it to First, register an account on After logging in, you can use Before publishing, check whether the
When everything is ready, switch to the directory where the plugin is located and run After uploading successfully, search on 5. ConclusionThis concludes the article about writing a Webpack plugin in 80 lines of code and publishing it to npm. For more information about publishing Webpack plugins to npm, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to install Odoo12 development environment on Windows 10
>>: Summary of Mysql common benchmark commands
The fastest way to experience the latest version ...
Migration is unavoidable in many cases. Hardware ...
It is very convenient to connect to a remote serv...
The first time I installed MySQL on my virtual ma...
When the user's home directory becomes larger...
Table of contents Manual backup Timer backup Manu...
1. Problem Description When starting MYSQL, a pro...
Table of contents Stabilization Introduction Anti...
Table of contents 1. Test Data 2. The inconvenien...
Front-end is a tough job, not only because techno...
A brief description of environment variable confi...
1. Start the Docker container Start a new Docker ...
This article shares the shell script of mysql5.6....
Preface As a DBA, you will often encounter some M...
The essence of a flat website structure is simpli...