text: (window.webpackJsonp=window.webpackJsonp||[]).push([[74],{220:function(e,t,n){"use strict";n.r(t);var a=n(0),s=Object(a.a)({},(function(){var e=this,t=e.$createElement,n=e._self._c||t;return n("div",{staticClass:"content"},[e._m(0),e._v(" "),e._m(1),e._v(" "),e._m(2),e._m(3),e._v(" "),e._m(4),e._m(5),e._v(" "),n("p",[e._v("loader是文件加载器，能够加载资源文件，并对这些文件进行一些处理，诸如编译、压缩等，最终一起打包到指定的文件中")]),e._v(" "),e._m(6),e._v(" "),e._m(7),e._v(" "),n("p",[e._v("在webpack运行的生命周期中会广播出许多事件，plugin可以监听这些事件，在合适的时机通过webpack提供的API改变输出结果。")]),e._v(" "),n("h3",{attrs:{id:"loader和plugin的区别-详解"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#loader和plugin的区别-详解"}},[e._v("#")]),e._v(" loader和plugin的区别 "),n("a",{attrs:{href:"https://www.jb51.net/article/148719.htm",target:"_blank",rel:"noopener noreferrer"}},[e._v("详解"),n("OutboundLink")],1)]),e._v(" "),n("p",[e._v("对于loader，它是一个转换器，将A文件进行编译形成B文件，这里操作的是文件，比如将A.scss转换为A.css，单纯的文件转换过程")]),e._v(" "),n("p",[e._v("plugin是一个扩展器，它丰富了webpack本身，针对是loader结束后，webpack打包的整个过程，它并不直接操作文件，而是基于事件机制工作，会监听webpack打包过程中的某些节点，执行广泛的任务")]),e._v(" "),e._m(8),n("p",[e._v("使用该plugin后，执行的顺序：")]),e._v(" "),n("p",[e._v("webpack启动后，在读取配置的过程中会执行new MyPlugin(options)初始化一个MyPlugin获取其实例\n在初始化compiler对象后，就会通过compiler.plugin(事件名称，回调函数)监听到webpack广播出来的事件\n并且可以通过compiler对象去操作webpack")])])}),[function(){var e=this.$createElement,t=this._self._c||e;return t("h2",{attrs:{id:"webpack"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#webpack"}},[this._v("#")]),this._v(" webpack")])},function(){var e=this.$createElement,t=this._self._c||e;return t("h3",{attrs:{id:"webpack的常见配置"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#webpack的常见配置"}},[this._v("#")]),this._v(" webpack的常见配置")])},function(){var e=this.$createElement,t=this._self._c||e;return t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[this._v('const webpack = require("webpack");\nconst path = require("path");\nconst HtmlWebpackPlugin = require("html-webpack-plugin");\nmodule.exports = {\n    // 入口文件\n    entry: {\n        app: path.join(__dirname, "../src/js/index.js")\n    },\n    // 输出文件\n    output: {\n        filename: "[name].bundle.js",\n        path: path.resolve(__dirname, "dist"),\n        publicPath: "/"\n    },\n    // loader配置\n    module: {\n        rules: [\n            {\n                test: /\\.scss/,\n                use: [\n                    "style-loader",\n                    "css-loader"\n                ]\n            }\n            ......\n        ]\n    },\n    // plugins配置\n    plugins: [\n        // 重新创建html文件\n        new HtmlWebpackPlugin({\n            title: "首页",\n            filename: "index.html",\n            template: path.resolve(__dirname, "../src/index.html")\n        })\n        ......\n    ]\n}\n')])])])},function(){var e=this.$createElement,t=this._self._c||e;return t("h3",{attrs:{id:"webpack的打包原理"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#webpack的打包原理"}},[this._v("#")]),this._v(" webpack的打包原理")])},function(){var e=this.$createElement,t=this._self._c||e;return t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[this._v("1. 识别入口文件\n2. 通过逐层识别模块依赖(Commonjs、amd或者es6的import，webpack都会对其进行分析，来获取代码的依赖)\n3. webpack做的就是分析代码，转换代码，编译代码，输出代码\n4. 最终形成打包后的代码\n")])])])},function(){var e=this.$createElement,t=this._self._c||e;return t("h3",{attrs:{id:"什么是loader"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#什么是loader"}},[this._v("#")]),this._v(" 什么是loader")])},function(){var e=this.$createElement,t=this._self._c||e;return t("ol",[t("li",[this._v("处理一个文件可以使用多个loader，loader的执行顺序和配置中的顺序是相反的，即最后一个loader最先执行，第一个loader最后执行")]),this._v(" "),t("li",[this._v("第一个执行的loader接收源文件内容作为参数，其它loader接收前一个执行的loader的返回值作为参数，最后执行的loader会返回此模块的JavaScript源码")])])},function(){var e=this.$createElement,t=this._self._c||e;return t("h3",{attrs:{id:"什么是plugin"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#什么是plugin"}},[this._v("#")]),this._v(" 什么是plugin")])},function(){var e=this.$createElement,t=this._self._c||e;return t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[this._v('class MyPlugin{\n    constructor(options){\n        console.log("MyPlugin constructor:", options);\n    }\n    apply(compiler){\n        compiler.plugin("compilation", compilation => {\n            console.log("MyPlugin");\n        });\n    }\n}\nmodule.exports = MyPlugin;\n \n \nwebpack.config.js配置：\nmodule.exports = {\n    ...\n    plugins: [\n        new MyPlugin({param: "my plugin"})\n    ]\n}\n')])])])}],!1,null,null,null);t.default=s.exports}}]);