{"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":"
\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 loader \u8fd0\u884c\u5728\u6253\u5305\u6587\u4ef6\u4e4b\u524d \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 |