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
Table of contents Uninstall and install samba Cre...
We usually use routing in vue projects, and vue-r...
English: A link tag will automatically complete h...
background Search the keyword .htaccess cache in ...
1. Window -> preferences to open the eclipse p...
The img tag in XHTML is so-called self-closing, w...
The CSS counter attribute is supported by almost ...
Building web pages that comply with Web standards ...
Table of contents 1. Problematic SQL statements S...
Today I will share with you how to write a player...
First, let’s take a look at a CSS carousel animat...
Table of contents Preface Background data splicin...
After the previous two chapters, do you have a ne...
1. Space rules Whitespace within HTML code is usu...
Related articles: Install Docker using yum under ...