博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从零开始的webpack生活-0x017:CustomPlugin自定义插件
阅读量:6959 次
发布时间:2019-06-27

本文共 2653 字,大约阅读时间需要 8 分钟。

0x001 概述

上一章讲的是其他一些常用的小插件,这一章讲的是自定义插件。

0x002 环境配置

$ mkdir 0x0016-other-plugin$ cd 0x0016-other-plugin$ npm init -y$ vim webpack.config.js// ./webpack.config.jsconst path = require('path');module.exports = {    entry : {        'index': ['./src/index.js'],    },    output: {        path    : path.join(__dirname, 'dist'),        filename: '[name].[chunkhash].js'    };

0x003 栗子1-最简单的插件

  1. 编写插件,这个插件会子安控制台输出一句hello plugin

    // ./CustomPlugin.jsfunction UpdatePackageVersionPlugin(options) {    console.log('hello plugin')}CustomPlugin.prototype.apply = function (compiler) {};module.exports = CustomPlugin;
  2. 引用该插件

    const path                     = require('path');var CustomPlugin = require('./CustomPlugin')module.exports = {    entry : {        'index': ['./src/index.js'],    },    output: {        path    : path.join(__dirname, 'dist'),        filename: '[name].bundle.js'    },    plugins: [        new CustomPlugin({options:true})    ]};
  3. 打包并查看控制台

    $ webpack# ↓↓↓↓↓↓↓↓↓↓↓插件输出hello plugin# ↑↑↑↑↑↑↑↑↑↑↑插件输出The compiler is starting to compile...The compiler is starting a new compilation...The compilation is starting to optimize files...The compilation is going to emit files...Hash: 8daa4edb5544a8f81c35Version: webpack 3.8.1Time: 58ms          Asset    Size  Chunks             Chunk Namesindex.bundle.js  2.6 kB       0  [emitted]  index   [0] multi ./src/index.js 28 bytes {0} [built]   [2] ./src/index.js 14 bytes {0} [built]

0x004 栗子2-偷偷添加资源

  1. 修改插件,这个插件会读取打包好的资源文件,并写入到filelist.md文件,保存到dist目录下。

function CustomPlugin(options) {    console.log('hello plugin')}CustomPlugin.prototype.apply = function (compiler) {    compiler.plugin('emit', function (compilation, callback) {        // Create a header string for the generated file:        var filelist = 'In this build:\n\n';        // Loop through all compiled assets,        // adding a new line item for each filename.        for (var filename in compilation.assets) {            filelist += ('- '+ filename +'\n');        }        // Insert this list into the webpack build as a new file asset:        compilation.assets['filelist.md'] = {            source: function() {                return filelist;            },            size: function() {                return filelist.length;            }        };        callback();    })};module.exports = CustomPlugin;
  1. 打包并查看文件

    $ webpack$ ls ./distfilelist.mdindex.bundle.js$ cat filelist.md// ./filelist.mdIn this build:- index.bundle.js
3. 更多配置请查阅[webpack关于自定义plugin章节][3]### 0x005 资源- [源代码][4]  [1]: https://segmentfault.com/a/1190000011976221  [2]: https://segmentfault.com/a/1190000011976221  [3]: https://webpack.js.org/contribute/writing-a-plugin/

转载地址:http://urmil.baihongyu.com/

你可能感兴趣的文章
vagrant up启动报错
查看>>
SVN版本控制图标未显示或显示异常解决方法
查看>>
KVC````valueForKeyPath
查看>>
ECharts实例(1)
查看>>
eclipse 配置git ssh登录
查看>>
安装MariaDB和Apache
查看>>
Tomcat项目部署—动态部署
查看>>
FastCGI 进程管理器(FPM)-配置
查看>>
Hello Word ~ v0.2.2 背单词软件发布 -- By WHYPRO
查看>>
文件上传显示保存到数据库实现类
查看>>
JQuery笔记
查看>>
JVMTI开发教程之一个简单的Agent
查看>>
struts2.0中struts.xml配置文件详解
查看>>
spring 事务管理——回滚之service层(事务控制层)
查看>>
Git学习笔记
查看>>
Developer Express 之 XtraReport报表预览控件PrintControl设置
查看>>
Httpclient 4.x文件上传
查看>>
SpringMVC 学习系列 (5) 之 数据绑定 -2
查看>>
04. Java NIO Buffer 缓冲区
查看>>
Java 锁与对象头
查看>>