{"id":217755,"date":"2021-05-19T09:22:42","date_gmt":"2021-05-19T01:22:42","guid":{"rendered":"https:\/\/gulass.cn\/?p=217755"},"modified":"2021-05-04T10:21:46","modified_gmt":"2021-05-04T02:21:46","slug":"webpack-loader-plugin","status":"publish","type":"post","link":"https:\/\/gulass.cn\/webpack-loader-plugin.html","title":{"rendered":"Webpack\u4e2dLoader\u548cPlugin\u7684\u533a\u522b\u548c\u7f16\u5199\u601d\u8def"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u7531\u4e8ewebpack\u57fa\u4e8e\u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f\uff0c\u5728\u8fd0\u884c\u7684\u751f\u547d\u5468\u671f\u4e2d\u4f1a\u5e7f\u64ad\u51fa\u8bb8\u591a\u4e8b\u4ef6\uff0c\u63d2\u4ef6\u901a\u8fc7\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u5c31\u53ef\u4ee5\u5728\u7279\u5b9a\u7684\u9636\u6bb5\u6267\u884c\u81ea\u5df1\u7684\u63d2\u4ef6\u4efb\u52a1<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e00\u3001\u533a\u522b<\/strong><\/div>\n

\u524d\u9762\u4e24\u8282\u6211\u4eec\u6709\u63d0\u5230Loader\u4e0ePlugin\u5bf9\u5e94\u7684\u6982\u5ff5\uff0c\u5148\u6765\u56de\u987e\u4e0b
\n\"\"
\nloader \u662f\u6587\u4ef6\u52a0\u8f7d\u5668\uff0c\u80fd\u591f\u52a0\u8f7d\u8d44\u6e90\u6587\u4ef6\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u6587\u4ef6\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff0c\u8bf8\u5982\u7f16\u8bd1\u3001\u538b\u7f29\u7b49\uff0c\u6700\u7ec8\u4e00\u8d77\u6253\u5305\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u4e2d
\nplugin \u8d4b\u4e88\u4e86 webpack \u5404\u79cd\u7075\u6d3b\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6253\u5305\u4f18\u5316\u3001\u8d44\u6e90\u7ba1\u7406\u3001\u73af\u5883\u53d8\u91cf\u6ce8\u5165\u7b49\uff0c\u76ee\u7684\u662f\u89e3\u51b3 loader \u65e0\u6cd5\u5b9e\u73b0\u7684\u5176\u4ed6\u4e8b
\n\u4ece\u6574\u4e2a\u8fd0\u884c\u65f6\u673a\u4e0a\u6765\u770b\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a
\n\"\"
\n\u53ef\u4ee5\u770b\u5230\uff0c\u4e24\u8005\u5728\u8fd0\u884c\u65f6\u673a\u4e0a\u7684\u533a\u522b\uff1a<\/p>\n

loader \u8fd0\u884c\u5728\u6253\u5305\u6587\u4ef6\u4e4b\u524d
\nplugins \u5728\u6574\u4e2a\u7f16\u8bd1\u5468\u671f\u90fd\u8d77\u4f5c\u7528
\n\u5728Webpack \u8fd0\u884c\u7684\u751f\u547d\u5468\u671f\u4e2d\u4f1a\u5e7f\u64ad\u51fa\u8bb8\u591a\u4e8b\u4ef6\uff0cPlugin \u53ef\u4ee5\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u5728\u5408\u9002\u7684\u65f6\u673a\u901a\u8fc7Webpack\u63d0\u4f9b\u7684 API\u6539\u53d8\u8f93\u51fa\u7ed3\u679c<\/p>\n

\u5bf9\u4e8eloader\uff0c\u5b9e\u8d28\u662f\u4e00\u4e2a\u8f6c\u6362\u5668\uff0c\u5c06A\u6587\u4ef6\u8fdb\u884c\u7f16\u8bd1\u5f62\u6210B\u6587\u4ef6\uff0c\u64cd\u4f5c\u7684\u662f\u6587\u4ef6\uff0c\u6bd4\u5982\u5c06A.scss\u6216A.less\u8f6c\u53d8\u4e3aB.css\uff0c\u5355\u7eaf\u7684\u6587\u4ef6\u8f6c\u6362\u8fc7\u7a0b<\/p>\n

\u4e8c\u3001\u7f16\u5199loader<\/strong><\/div>\n

\u5728\u7f16\u5199 loader \u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u4e86\u89e3 loader \u7684\u672c\u8d28<\/p>\n

\u5176\u672c\u8d28\u4e3a\u51fd\u6570\uff0c\u51fd\u6570\u4e2d\u7684 this \u4f5c\u4e3a\u4e0a\u4e0b\u6587\u4f1a\u88ab webpack \u586b\u5145\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u80fd\u5c06 loader\u8bbe\u4e3a\u4e00\u4e2a\u7bad\u5934\u51fd\u6570<\/p>\n

\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u4e3a webpack \u4f20\u9012\u7ed9 loader \u7684\u6587\u4ef6\u6e90\u5185\u5bb9<\/p>\n

\u51fd\u6570\u4e2d this \u662f\u7531 webpack \u63d0\u4f9b\u7684\u5bf9\u8c61\uff0c\u80fd\u591f\u83b7\u53d6\u5f53\u524d loader \u6240\u9700\u8981\u7684\u5404\u79cd\u4fe1\u606f<\/p>\n

\u51fd\u6570\u4e2d\u6709\u5f02\u6b65\u64cd\u4f5c\u6216\u540c\u6b65\u64cd\u4f5c\uff0c\u5f02\u6b65\u64cd\u4f5c\u901a\u8fc7 this.callback\u8fd4\u56de\uff0c\u8fd4\u56de\u503c\u8981\u6c42\u4e3a string \u6216\u8005 Buffer<\/p>\n

\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\n\/\/ \u5bfc\u51fa\u4e00\u4e2a\u51fd\u6570\uff0csource\u4e3awebpack\u4f20\u9012\u7ed9loader\u7684\u6587\u4ef6\u6e90\u5185\u5bb9 \r\nmodule.exports = function(source) { \r\n    const content = doSomeThing2JsString(source); \r\n     \r\n    \/\/ \u5982\u679c loader \u914d\u7f6e\u4e86 options \u5bf9\u8c61\uff0c\u90a3\u4e48this.query\u5c06\u6307\u5411 options \r\n    const options = this.query; \r\n     \r\n    \/\/ \u53ef\u4ee5\u7528\u4f5c\u89e3\u6790\u5176\u4ed6\u6a21\u5757\u8def\u5f84\u7684\u4e0a\u4e0b\u6587 \r\n    console.log('this.context'); \r\n     \r\n    \/* \r\n     * this.callback \u53c2\u6570\uff1a \r\n     * error\uff1aError | null\uff0c\u5f53 loader \u51fa\u9519\u65f6\u5411\u5916\u629b\u51fa\u4e00\u4e2a error \r\n     * content\uff1aString | Buffer\uff0c\u7ecf\u8fc7 loader \u7f16\u8bd1\u540e\u9700\u8981\u5bfc\u51fa\u7684\u5185\u5bb9 \r\n     * sourceMap\uff1a\u4e3a\u65b9\u4fbf\u8c03\u8bd5\u751f\u6210\u7684\u7f16\u8bd1\u540e\u5185\u5bb9\u7684 source map \r\n     * ast\uff1a\u672c\u6b21\u7f16\u8bd1\u751f\u6210\u7684 AST \u9759\u6001\u8bed\u6cd5\u6811\uff0c\u4e4b\u540e\u6267\u884c\u7684 loader \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a AST\uff0c\u8fdb\u800c\u7701\u53bb\u91cd\u590d\u751f\u6210 AST \u7684\u8fc7\u7a0b \r\n     *\/ \r\n    this.callback(null, content); \/\/ \u5f02\u6b65 \r\n    return content; \/\/ \u540c\u6b65 \r\n} \r\n<\/pre>\n

\u4e00\u822c\u5728\u7f16\u5199loader\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4fdd\u6301\u529f\u80fd\u5355\u4e00\uff0c\u907f\u514d\u505a\u591a\u79cd\u529f\u80fd<\/p>\n

\u5982less\u6587\u4ef6\u8f6c\u6362\u6210 css\u6587\u4ef6\u4e5f\u4e0d\u662f\u4e00\u6b65\u5230\u4f4d\uff0c\u800c\u662f less-loader\u3001css-loader\u3001style-loader\u51e0\u4e2a loader\u7684\u94fe\u5f0f\u8c03\u7528\u624d\u80fd\u5b8c\u6210\u8f6c\u6362<\/p>\n

\u4e09\u3001\u7f16\u5199plugin<\/strong><\/div>\n

\u7531\u4e8ewebpack\u57fa\u4e8e\u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f\uff0c\u5728\u8fd0\u884c\u7684\u751f\u547d\u5468\u671f\u4e2d\u4f1a\u5e7f\u64ad\u51fa\u8bb8\u591a\u4e8b\u4ef6\uff0c\u63d2\u4ef6\u901a\u8fc7\u76d1\u542c\u8fd9\u4e9b\u4e8b\u4ef6\uff0c\u5c31\u53ef\u4ee5\u5728\u7279\u5b9a\u7684\u9636\u6bb5\u6267\u884c\u81ea\u5df1\u7684\u63d2\u4ef6\u4efb\u52a1<\/p>\n

\u5728\u4e4b\u524d\u4e5f\u4e86\u89e3\u8fc7\uff0cwebpack\u7f16\u8bd1\u4f1a\u521b\u5efa\u4e24\u4e2a\u6838\u5fc3\u5bf9\u8c61\uff1a<\/p>\n

compiler\uff1a\u5305\u542b\u4e86 webpack \u73af\u5883\u7684\u6240\u6709\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u5305\u62ec options\uff0cloader \u548c plugin\uff0c\u548c webpack \u6574\u4e2a\u751f\u547d\u5468\u671f\u76f8\u5173\u7684\u94a9\u5b50
\ncompilation\uff1a\u4f5c\u4e3a plugin \u5185\u7f6e\u4e8b\u4ef6\u56de\u8c03\u51fd\u6570\u7684\u53c2\u6570\uff0c\u5305\u542b\u4e86\u5f53\u524d\u7684\u6a21\u5757\u8d44\u6e90\u3001\u7f16\u8bd1\u751f\u6210\u8d44\u6e90\u3001\u53d8\u5316\u7684\u6587\u4ef6\u4ee5\u53ca\u88ab\u8ddf\u8e2a\u4f9d\u8d56\u7684\u72b6\u6001\u4fe1\u606f\u3002\u5f53\u68c0\u6d4b\u5230\u4e00\u4e2a\u6587\u4ef6\u53d8\u5316\uff0c\u4e00\u6b21\u65b0\u7684 Compilation \u5c06\u88ab\u521b\u5efa
\n\u5982\u679c\u81ea\u5df1\u8981\u5b9e\u73b0plugin\uff0c\u4e5f\u9700\u8981\u9075\u5faa\u4e00\u5b9a\u7684\u89c4\u8303\uff1a<\/p>\n

\u63d2\u4ef6\u5fc5\u987b\u662f\u4e00\u4e2a\u51fd\u6570\u6216\u8005\u662f\u4e00\u4e2a\u5305\u542b apply \u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u8fd9\u6837\u624d\u80fd\u8bbf\u95eecompiler\u5b9e\u4f8b
\n\u4f20\u7ed9\u6bcf\u4e2a\u63d2\u4ef6\u7684 compiler \u548c compilation \u5bf9\u8c61\u90fd\u662f\u540c\u4e00\u4e2a\u5f15\u7528\uff0c\u56e0\u6b64\u4e0d\u5efa\u8bae\u4fee\u6539
\n\u5f02\u6b65\u7684\u4e8b\u4ef6\u9700\u8981\u5728\u63d2\u4ef6\u5904\u7406\u5b8c\u4efb\u52a1\u65f6\u8c03\u7528\u56de\u8c03\u51fd\u6570\u901a\u77e5 Webpack \u8fdb\u5165\u4e0b\u4e00\u4e2a\u6d41\u7a0b\uff0c\u4e0d\u7136\u4f1a\u5361\u4f4f
\n\u5b9e\u73b0plugin\u7684\u6a21\u677f\u5982\u4e0b\uff1a<\/p>\n

\r\nclass MyPlugin { \r\n    \/\/ Webpack \u4f1a\u8c03\u7528 MyPlugin \u5b9e\u4f8b\u7684 apply \u65b9\u6cd5\u7ed9\u63d2\u4ef6\u5b9e\u4f8b\u4f20\u5165 compiler \u5bf9\u8c61 \r\n  apply (compiler) { \r\n    \/\/ \u627e\u5230\u5408\u9002\u7684\u4e8b\u4ef6\u94a9\u5b50\uff0c\u5b9e\u73b0\u81ea\u5df1\u7684\u63d2\u4ef6\u529f\u80fd \r\n    compiler.hooks.emit.tap('MyPlugin', compilation => { \r\n        \/\/ compilation: \u5f53\u524d\u6253\u5305\u6784\u5efa\u6d41\u7a0b\u7684\u4e0a\u4e0b\u6587 \r\n        console.log(compilation); \r\n         \r\n        \/\/ do something... \r\n    }) \r\n  } \r\n} \r\n<\/pre>\n

\u5728 emit \u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u4ee3\u8868\u6e90\u6587\u4ef6\u7684\u8f6c\u6362\u548c\u7ec4\u88c5\u5df2\u7ecf\u5b8c\u6210\uff0c\u53ef\u4ee5\u8bfb\u53d6\u5230\u6700\u7ec8\u5c06\u8f93\u51fa\u7684\u8d44\u6e90\u3001\u4ee3\u7801\u5757\u3001\u6a21\u5757\u53ca\u5176\u4f9d\u8d56\uff0c\u5e76\u4e14\u53ef\u4ee5\u4fee\u6539\u8f93\u51fa\u8d44\u6e90\u7684\u5185\u5bb9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u524d\u9762\u4e24\u8282\u6211\u4eec\u6709\u63d0\u5230Loader\u4e0ePlugin\u5bf9\u5e94\u7684\u6982\u5ff5\uff0c\u5148\u6765\u56de\u987e\u4e0b loader \u662f\u6587\u4ef6\u52a0\u8f7d\u5668\uff0c\u80fd\u591f\u52a0\u8f7d\u8d44\u6e90\u6587 […]<\/p>\n","protected":false},"author":1898,"featured_media":217760,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[680],"class_list":["post-217755","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-webpack"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/217755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=217755"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/217755\/revisions"}],"predecessor-version":[{"id":217761,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/217755\/revisions\/217761"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/217760"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=217755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=217755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=217755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}