{"id":1034,"date":"2022-08-06T10:42:00","date_gmt":"2022-08-06T02:42:00","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=1034"},"modified":"2022-07-27T17:38:22","modified_gmt":"2022-07-27T09:38:22","slug":"webpack%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0-web%e5%b7%a5%e7%a8%8b%e5%8c%961","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=1034","title":{"rendered":"webpack\u5b66\u4e60\u7b14\u8bb0-web\u5de5\u7a0b\u5316(1)"},"content":{"rendered":"\n<p>\u73b0\u4ee3\u610f\u4e49\u4e0a\u7684web\u5de5\u7a0b\u5316\uff0c\u5305\u62ec\u6a21\u5757\u5316\uff0c\u7ec4\u4ef6\u5316\uff0c\u81ea\u52a8\u5316\uff0c\u89c4\u8303\u5316\u3002\u5728\u9879\u76ee\u7684\u542f\u52a8\u4e4b\u524d\uff0c\u6211\u4eec\u505a\u597d\u8fd9\u4e00\u7cfb\u5217\u7684\u5de5\u7a0b\u5316\uff0c\u90a3\u4e48\u9879\u76ee\u7684\u6700\u7ec8\u7684\u8d28\u91cf\u4f1a\u5f88\u9ad8\u7684\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u8bf4\u4e00\u4e0b\u6a21\u5757\u5316\uff0c\u6a21\u5757\u5316\u5c31\u662f\u5c06\u4e00\u4e2a\u5927\u5c0f\u62c6\u5206\u7a0b\u76f8\u4e92\u4f9d\u8d56\u7684\u5c0f\u6587\u4ef6\uff0c\u5728\u8fdb\u884c\u7edf\u4e00\u7684\u62fc\u88c5\u548c\u52a0\u8f7d\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u65b9\u4fbf\u4e86\u591a\u4eba\u5408\u4f5c\u5f00\u53d1\uff0c\u63d0\u9ad8\u4e86\u9879\u76ee\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u6269\u5c55\u6027\u3002<\/p>\n\n\n\n<p><strong>JavaScript \u6a21\u5757\u5316\u5927\u6982\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\uff1a<\/strong><\/p>\n\n\n\n<p><strong>&nbsp; 1\u3001\u65e0\u6a21\u5757\u5316<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;2\u3001CommonJS \u89c4\u8303\uff08CJS\u89c4\u8303\uff0c\u9002\u7528\u4e8e\u670d\u52a1\u7aef\uff09<\/strong><\/p>\n\n\n\n<p class=\"has-small-font-size\">\u901a\u8fc7 &nbsp;require() \u52a0\u8f7d\u9700\u8981\u7684\u6a21\u5757, \u8fd9\u4e2a\u65b9\u6cd5\u662f\u4e00\u4e2a\u5168\u5c40\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7 exports \/ module.exports \u5bfc\u51fa\u8981\u66b4\u9732\u7684\u63a5\u53e3\u3002<\/p>\n\n\n\n<p><strong>3\u3001 AMD\u89c4\u8303<\/strong><\/p>\n\n\n\n<p class=\"has-small-font-size\">&nbsp; &nbsp; AMD\u89c4\u8303\u662f\u5f02\u6b65\u52a0\u8f7d\u7684\uff0c\u6a21\u5757\u7684\u52a0\u8f7d\u90fd\u4e0d\u5f71\u54cd\u540e\u8fb9\u7684\u8bed\u53e5\u6267\u884c\uff0c\u6240\u6709\u4e00\u6765\u8fd9\u4e2a\u6a21\u5757\u7684\u8bed\u53e5\uff0c\u90fd\u5b9a\u4e49\u5728\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4e2d\u3002<\/p>\n\n\n\n<p class=\"has-small-font-size\">&nbsp; &nbsp; AMD\u89c4\u8303\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a API\uff1a require([module], callback) &nbsp; \u3001 define(id, [depends], callback)\uff0c\u901a\u8fc7 define \u5b9a\u4e49\u4e00\u4e2a\u6a21\u5757\uff0c\u901a\u8fc7 require \u52a0\u8f7d\u4e00\u4e2a\u6a21\u5757\u3002<\/p>\n\n\n\n<p class=\"has-small-font-size\">&nbsp; &nbsp; \u4f7f\u7528 require.js \u7684\u65f6\u5019\u5fc5\u987b\u63d0\u524d\u52a0\u8f7d\u5b8c\u6240\u6709\u7684\u6a21\u5757\uff0c\u7136\u540e\u624d\u53ef\u4ee5\u4f7f\u7528\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u4ed6\u7684\u4e00\u4e2a\u7f3a\u70b9\u5c31\u66b4\u9732\u51fa\u6765\u4e86\u3002\u90a3\u5c31\u662f \u2018 \u4e0d\u80fd\u6309\u9700\u52a0\u8f7d\uff0c\u5982\u679c\u6a21\u5757\u8fc7\u591a\uff0c\u52a0\u8f7d\u7b49\u5f85\u65f6\u95f4\u4f1a\u5f88\u957f \u2019\u3002<\/p>\n\n\n\n<p><strong>4\u3001CMD\u89c4\u8303<\/strong><\/p>\n\n\n\n<p class=\"has-small-font-size\">&nbsp; &nbsp; \u5b9e\u73b0\u6b64\u89c4\u8303\u7684\u662f sea.js \uff0c\u548c require.js \u76f8\u4f3c\uff0c\u6bcf\u4e2a js \u6587\u4ef6\u90fd\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u6bd4 AMD \u7684\u4f18\u70b9\u5728\u4e8e\uff0c\u5b83\u80fd\u591f\u6309\u9700\u52a0\u8f7d\uff0c\u4e0d\u8fc7\u5bf9\u4e8e\u6d4f\u89c8\u5668\u7aef\u6765\u8bf4\uff0c\u6709\u53ef\u80fd\u4f1a\u9020\u6210\u5c0f\u5c0f\u7684\u5ef6\u8fdf\u3002\u56e0\u4e3a\u4f60\u662f\u5728\u9700\u8981\u7684\u65f6\u5019\u5728\u8fdb\u884c\u52a0\u8f7d\u7684\uff0c\u6240\u4ee5\u5728\u7b49\u5f85\u52a0\u8f7d\u7684\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u4f1a\u6709\u4e00\u5b9a\u7684\u5ef6\u8fdf\u7684\u3002\u8fd9\u4e00\u70b9\u662f\u4e0d\u540c\u4e8e AMD \u7684\u3002<\/p>\n\n\n\n<p class=\"has-small-font-size\">&nbsp; &nbsp; \u8bf4\u767d\u4e86\u5c31\u662f\uff0c AMD \u4f1a\u63d0\u524d\u52a0\u8f7d\u6a21\u5757\uff0cCMD \u4f1a\u5ef6\u8fdf\u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n\n\n\n<p><strong>5\u3001ES6\u6a21\u5757\u5316<\/strong><\/p>\n\n\n\n<p class=\"has-small-font-size\">&nbsp; &nbsp; ES6 \u7684\u6a21\u5757\u5316\u662f\u771f\u6b63\u7684\u89c4\u8303\uff0cES6 \u4e2d\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 export \u5bfc\u51fa\u6a21\u5757\uff0c\u901a\u8fc7 import \u5f15\u5165\u6a21\u5757\u3002\u7531\u4e8e\u73b0\u5728\u6d4f\u89c8\u5668\u5bf9\u6a21\u5757\u7684\u652f\u6301\u5ea6\u95ee\u9898\uff0c\u6240\u4ee5\u5927\u90e8\u5206\u4f7f\u7528 babelJS \u6216\u8005 Traceur \u5c06 ES6 \u4ee3\u7801\u8f6c\u5316\u4e3a\u517c\u5bb9 ES5 \u7248\u672c\u7684 js\u4ee3\u7801\u3002<\/p>\n\n\n\n<p class=\"has-small-font-size\">&nbsp; &nbsp; \u6211\u8fd8\u662f\u6bd4\u8f83\u63a8\u5d07\u4f7f\u7528 ES6 \u7684\u8bed\u6cd5\u8fdb\u884c\u4ee3\u7801\u7684\u7f16\u5199\uff0c\u52a0\u8f7d\u6a21\u5757\u7684\u65f6\u5019\uff0c\u6bcf\u4e2a\u6a21\u5757\u53ea\u52a0\u8f7d\u4e00\u6b21\uff0c\u7b2c\u4e8c\u6b21\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u4f1a\u4ece\u5185\u5b58\u4e2d\u76f4\u63a5\u8bfb\u53d6\uff0c\u8fd9\u6837\u5c31\u4f18\u5316\u4e86\u5f88\u591a\u3002\u6bcf\u4e00\u4e2a\u6a21\u5757\u5185\u58f0\u660e\u7684\u53d8\u91cf\u90fd\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u4f1a\u6c61\u67d3\u5168\u5c40\u4f5c\u7528\u57df\u3002\u4e00\u4e2a\u6a21\u5757\u4e5f\u53ef\u4ee5\u5bfc\u5165\u591a\u4e2a\u6a21\u5757\u3002<\/p>\n\n\n\n<p>\u6982\u5ff5\u6027\u7684\u5982\u4e0a\uff0c\u4e0b\u9762\u5206\u4eab\u4e2a\u6784\u5efa\u5de5\u5177\u53eb<a href=\"https:\/\/www.webpackjs.com\/concepts\/\" data-type=\"URL\" data-id=\"https:\/\/www.webpackjs.com\/concepts\/\">webpack<\/a>\uff0c<\/p>\n\n\n\n<h1>\u6982\u5ff5<\/h1>\n\n\n\n<p><a href=\"https:\/\/webpack.js.org\/concepts\" target=\"_blank\" rel=\"noreferrer noopener\">\u67e5\u770b\u539f\u6587<\/a>|<a href=\"https:\/\/github.com\/docschina\/webpack.js.org\/edit\/cn\/src\/content\/concepts\/index.md\">\u7f16\u8f91\u6b64\u9875<\/a><\/p>\n\n\n\n<p>\u672c\u8d28\u4e0a\uff0c<em>webpack<\/em>&nbsp;\u662f\u4e00\u4e2a\u73b0\u4ee3 JavaScript \u5e94\u7528\u7a0b\u5e8f\u7684<em>\u9759\u6001\u6a21\u5757\u6253\u5305\u5668(module bundler)<\/em>\u3002\u5f53 webpack \u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5b83\u4f1a\u9012\u5f52\u5730\u6784\u5efa\u4e00\u4e2a<em>\u4f9d\u8d56\u5173\u7cfb\u56fe(dependency graph)<\/em>\uff0c\u5176\u4e2d\u5305\u542b\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u7684\u6bcf\u4e2a\u6a21\u5757\uff0c\u7136\u540e\u5c06\u6240\u6709\u8fd9\u4e9b\u6a21\u5757\u6253\u5305\u6210\u4e00\u4e2a\u6216\u591a\u4e2a&nbsp;<em>bundle<\/em>\u3002\u53ef\u4ee5\u4ece<a href=\"https:\/\/www.webpackjs.com\/concepts\/modules\">\u8fd9\u91cc<\/a>\u4e86\u89e3\u66f4\u591a\u5173\u4e8e JavaScript \u6a21\u5757\u548c webpack \u6a21\u5757\u7684\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u4ece webpack v4.0.0 \u5f00\u59cb\uff0c\u53ef\u4ee5\u4e0d\u7528\u5f15\u5165\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002\u7136\u800c\uff0cwebpack \u4ecd\u7136\u8fd8\u662f<a href=\"https:\/\/www.webpackjs.com\/configuration\">\u9ad8\u5ea6\u53ef\u914d\u7f6e\u7684<\/a>\u3002\u5728\u5f00\u59cb\u524d\u4f60\u9700\u8981\u5148\u7406\u89e3\u56db\u4e2a<strong>\u6838\u5fc3\u6982\u5ff5<\/strong>\uff1a<\/p>\n\n\n\n<ul><li>\u5165\u53e3(entry)<\/li><li>\u8f93\u51fa(output)<\/li><li>loader<\/li><li>\u63d2\u4ef6(plugins)<\/li><\/ul>\n\n\n\n<h2>\u5165\u53e3(entry)<a href=\"https:\/\/www.webpackjs.com\/concepts\/#%E5%85%A5%E5%8F%A3-entry-\"><\/a><\/h2>\n\n\n\n<p><strong>\u5165\u53e3\u8d77\u70b9(entry point)<\/strong>\u6307\u793a webpack \u5e94\u8be5\u4f7f\u7528\u54ea\u4e2a\u6a21\u5757\uff0c\u6765\u4f5c\u4e3a\u6784\u5efa\u5176\u5185\u90e8<em>\u4f9d\u8d56\u56fe<\/em>\u7684\u5f00\u59cb\u3002\u8fdb\u5165\u5165\u53e3\u8d77\u70b9\u540e\uff0cwebpack \u4f1a\u627e\u51fa\u6709\u54ea\u4e9b\u6a21\u5757\u548c\u5e93\u662f\u5165\u53e3\u8d77\u70b9\uff08\u76f4\u63a5\u548c\u95f4\u63a5\uff09\u4f9d\u8d56\u7684\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u4f9d\u8d56\u9879\u968f\u5373\u88ab\u5904\u7406\uff0c\u6700\u540e\u8f93\u51fa\u5230\u79f0\u4e4b\u4e3a&nbsp;<em>bundles<\/em>&nbsp;\u7684\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06\u5728\u4e0b\u4e00\u7ae0\u8282\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u5728&nbsp;<a href=\"https:\/\/www.webpackjs.com\/configuration\">webpack \u914d\u7f6e<\/a>\u4e2d\u914d\u7f6e&nbsp;<code>entry<\/code>&nbsp;\u5c5e\u6027\uff0c\u6765\u6307\u5b9a\u4e00\u4e2a\u5165\u53e3\u8d77\u70b9\uff08\u6216\u591a\u4e2a\u5165\u53e3\u8d77\u70b9\uff09\u3002\u9ed8\u8ba4\u503c\u4e3a&nbsp;<code>.\/src<\/code>\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e2a&nbsp;<code>entry<\/code>&nbsp;\u914d\u7f6e\u7684\u6700\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p><strong>webpack.config.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {\n  entry: '.\/path\/to\/my\/entry\/file.js'\n};\n<\/code><\/pre>\n\n\n\n<h2><a href=\"https:\/\/www.webpackjs.com\/concepts\/#%E5%87%BA%E5%8F%A3-output-\"><\/a>\u51fa\u53e3(output)<a href=\"https:\/\/www.webpackjs.com\/concepts\/#%E5%87%BA%E5%8F%A3-output-\"><\/a><\/h2>\n\n\n\n<p><strong>output<\/strong>&nbsp;\u5c5e\u6027\u544a\u8bc9 webpack \u5728\u54ea\u91cc\u8f93\u51fa\u5b83\u6240\u521b\u5efa\u7684&nbsp;<em>bundles<\/em>\uff0c\u4ee5\u53ca\u5982\u4f55\u547d\u540d\u8fd9\u4e9b\u6587\u4ef6\uff0c\u9ed8\u8ba4\u503c\u4e3a&nbsp;<code>.\/dist<\/code>\u3002\u57fa\u672c\u4e0a\uff0c\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7ed3\u6784\uff0c\u90fd\u4f1a\u88ab\u7f16\u8bd1\u5230\u4f60\u6307\u5b9a\u7684\u8f93\u51fa\u8def\u5f84\u7684\u6587\u4ef6\u5939\u4e2d\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u914d\u7f6e\u4e2d\u6307\u5b9a\u4e00\u4e2a&nbsp;<code>output<\/code>&nbsp;\u5b57\u6bb5\uff0c\u6765\u914d\u7f6e\u8fd9\u4e9b\u5904\u7406\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<p><strong>webpack.config.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const path = require('path');\n\nmodule.exports = {\n  entry: '.\/path\/to\/my\/entry\/file.js',\n  output: {\n    path: path.resolve(__dirname, 'dist'),\n    filename: 'my-first-webpack.bundle.js'\n  }\n};\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7&nbsp;<code>output.filename<\/code>&nbsp;\u548c&nbsp;<code>output.path<\/code>&nbsp;\u5c5e\u6027\uff0c\u6765\u544a\u8bc9 webpack bundle \u7684\u540d\u79f0\uff0c\u4ee5\u53ca\u6211\u4eec\u60f3\u8981 bundle \u751f\u6210(emit)\u5230\u54ea\u91cc\u3002\u53ef\u80fd\u4f60\u60f3\u8981\u4e86\u89e3\u5728\u4ee3\u7801\u6700\u4e0a\u9762\u5bfc\u5165\u7684 path \u6a21\u5757\u662f\u4ec0\u4e48\uff0c\u5b83\u662f\u4e00\u4e2a&nbsp;<a href=\"https:\/\/nodejs.org\/api\/modules.html\">Node.js \u6838\u5fc3\u6a21\u5757<\/a>\uff0c\u7528\u4e8e\u64cd\u4f5c\u6587\u4ef6\u8def\u5f84\u3002\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u672f\u8bed<strong>\u751f\u6210(emitted \u6216 emit)<\/strong>\u8d2f\u7a7f\u4e86\u6211\u4eec\u6574\u4e2a\u6587\u6863\u548c<a href=\"https:\/\/www.webpackjs.com\/api\/plugins\">\u63d2\u4ef6 API<\/a>\u3002\u5b83\u662f\u201c\u751f\u4ea7(produced)\u201d\u6216\u201c\u91ca\u653e(discharged)\u201d\u7684\u7279\u6b8a\u672f\u8bed\u3002<code>output<\/code>&nbsp;\u5c5e\u6027\u8fd8\u6709<a href=\"https:\/\/www.webpackjs.com\/configuration\/output\">\u66f4\u591a\u53ef\u914d\u7f6e\u7684\u7279\u6027<\/a>\uff0c\u5982\u679c\u4f60\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e&nbsp;<code>output<\/code>&nbsp;\u5c5e\u6027\u7684\u6982\u5ff5\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<a href=\"https:\/\/www.webpackjs.com\/concepts\/output\">\u9605\u8bfb\u6982\u5ff5\u7ae0\u8282<\/a>\u6765\u4e86\u89e3\u66f4\u591a\u3002<\/p>\n\n\n\n<h2><a href=\"https:\/\/www.webpackjs.com\/concepts\/#loader\"><\/a>loader<a href=\"https:\/\/www.webpackjs.com\/concepts\/#loader\"><\/a><\/h2>\n\n\n\n<p><em>loader<\/em>&nbsp;\u8ba9 webpack \u80fd\u591f\u53bb\u5904\u7406\u90a3\u4e9b\u975e JavaScript \u6587\u4ef6\uff08webpack \u81ea\u8eab\u53ea\u7406\u89e3 JavaScript\uff09\u3002loader \u53ef\u4ee5\u5c06\u6240\u6709\u7c7b\u578b\u7684\u6587\u4ef6\u8f6c\u6362\u4e3a webpack \u80fd\u591f\u5904\u7406\u7684\u6709\u6548<a href=\"https:\/\/www.webpackjs.com\/concepts\/modules\">\u6a21\u5757<\/a>\uff0c\u7136\u540e\u4f60\u5c31\u53ef\u4ee5\u5229\u7528 webpack \u7684\u6253\u5305\u80fd\u529b\uff0c\u5bf9\u5b83\u4eec\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u672c\u8d28\u4e0a\uff0cwebpack loader \u5c06\u6240\u6709\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u8f6c\u6362\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u4f9d\u8d56\u56fe\uff08\u548c\u6700\u7ec8\u7684 bundle\uff09\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u7684\u6a21\u5757\u3002\u6ce8\u610f\uff0cloader \u80fd\u591f&nbsp;<code>import<\/code>&nbsp;\u5bfc\u5165\u4efb\u4f55\u7c7b\u578b\u7684\u6a21\u5757\uff08\u4f8b\u5982&nbsp;<code>.css<\/code>&nbsp;\u6587\u4ef6\uff09\uff0c\u8fd9\u662f webpack \u7279\u6709\u7684\u529f\u80fd\uff0c\u5176\u4ed6\u6253\u5305\u7a0b\u5e8f\u6216\u4efb\u52a1\u6267\u884c\u5668\u7684\u53ef\u80fd\u5e76\u4e0d\u652f\u6301\u3002\u6211\u4eec\u8ba4\u4e3a\u8fd9\u79cd\u8bed\u8a00\u6269\u5c55\u662f\u6709\u5f88\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u8fd9\u53ef\u4ee5\u4f7f\u5f00\u53d1\u4eba\u5458\u521b\u5efa\u51fa\u66f4\u51c6\u786e\u7684\u4f9d\u8d56\u5173\u7cfb\u56fe\u3002<\/p>\n\n\n\n<p>\u5728\u66f4\u9ad8\u5c42\u9762\uff0c\u5728 webpack \u7684\u914d\u7f6e\u4e2d&nbsp;<strong>loader<\/strong>&nbsp;\u6709\u4e24\u4e2a\u76ee\u6807\uff1a<\/p>\n\n\n\n<ol><li><code>test<\/code>&nbsp;\u5c5e\u6027\uff0c\u7528\u4e8e\u6807\u8bc6\u51fa\u5e94\u8be5\u88ab\u5bf9\u5e94\u7684 loader \u8fdb\u884c\u8f6c\u6362\u7684\u67d0\u4e2a\u6216\u67d0\u4e9b\u6587\u4ef6\u3002<\/li><li><code>use<\/code>&nbsp;\u5c5e\u6027\uff0c\u8868\u793a\u8fdb\u884c\u8f6c\u6362\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528\u54ea\u4e2a loader\u3002<\/li><\/ol>\n\n\n\n<p><strong>webpack.config.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const path = require('path');\n\nconst config = {\n  output: {\n    filename: 'my-first-webpack.bundle.js'\n  },\n  module: {\n    rules: &#91;\n      { test: \/\\.txt$\/, use: 'raw-loader' }\n    ]\n  }\n};\n\nmodule.exports = config;\n<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u914d\u7f6e\u4e2d\uff0c\u5bf9\u4e00\u4e2a\u5355\u72ec\u7684 module \u5bf9\u8c61\u5b9a\u4e49\u4e86&nbsp;<code>rules<\/code>&nbsp;\u5c5e\u6027\uff0c\u91cc\u9762\u5305\u542b\u4e24\u4e2a\u5fc5\u987b\u5c5e\u6027\uff1a<code>test<\/code>&nbsp;\u548c&nbsp;<code>use<\/code>\u3002\u8fd9\u544a\u8bc9 webpack \u7f16\u8bd1\u5668(compiler) \u5982\u4e0b\u4fe1\u606f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u201c\u563f\uff0cwebpack \u7f16\u8bd1\u5668\uff0c\u5f53\u4f60\u78b0\u5230\u300c\u5728&nbsp;<code>require()<\/code>\/<code>import<\/code>&nbsp;\u8bed\u53e5\u4e2d\u88ab\u89e3\u6790\u4e3a &#8216;.txt&#8217; \u7684\u8def\u5f84\u300d\u65f6\uff0c\u5728\u4f60\u5bf9\u5b83\u6253\u5305\u4e4b\u524d\uff0c\u5148<strong>\u4f7f\u7528<\/strong>&nbsp;<code>raw-loader<\/code>&nbsp;\u8f6c\u6362\u4e00\u4e0b\u3002\u201d<\/p><\/blockquote>\n\n\n\n<p>\u91cd\u8981\u7684\u662f\u8981\u8bb0\u5f97\uff0c<strong>\u5728 webpack \u914d\u7f6e\u4e2d\u5b9a\u4e49 loader \u65f6\uff0c\u8981\u5b9a\u4e49\u5728&nbsp;<code>module.rules<\/code>&nbsp;\u4e2d\uff0c\u800c\u4e0d\u662f&nbsp;<code>rules<\/code><\/strong>\u3002\u7136\u800c\uff0c\u5728\u5b9a\u4e49\u9519\u8bef\u65f6 webpack \u4f1a\u7ed9\u51fa\u4e25\u91cd\u7684\u8b66\u544a\u3002\u4e3a\u4e86\u4f7f\u4f60\u53d7\u76ca\u4e8e\u6b64\uff0c\u5982\u679c\u6ca1\u6709\u6309\u7167\u6b63\u786e\u65b9\u5f0f\u53bb\u505a\uff0cwebpack \u4f1a\u201c\u7ed9\u51fa\u4e25\u91cd\u7684\u8b66\u544a\u201d<\/p>\n\n\n\n<p>loader \u8fd8\u6709\u66f4\u591a\u6211\u4eec\u5c1a\u672a\u63d0\u5230\u7684\u5177\u4f53\u914d\u7f6e\u5c5e\u6027\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/www.webpackjs.com\/concepts\/loaders\">\u4e86\u89e3\u66f4\u591a\uff01<\/a><\/p>\n\n\n\n<h2><a href=\"https:\/\/www.webpackjs.com\/concepts\/#%E6%8F%92%E4%BB%B6-plugins-\"><\/a>\u63d2\u4ef6(plugins)<a href=\"https:\/\/www.webpackjs.com\/concepts\/#%E6%8F%92%E4%BB%B6-plugins-\"><\/a><\/h2>\n\n\n\n<p>loader \u88ab\u7528\u4e8e\u8f6c\u6362\u67d0\u4e9b\u7c7b\u578b\u7684\u6a21\u5757\uff0c\u800c\u63d2\u4ef6\u5219\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u8303\u56f4\u66f4\u5e7f\u7684\u4efb\u52a1\u3002\u63d2\u4ef6\u7684\u8303\u56f4\u5305\u62ec\uff0c\u4ece\u6253\u5305\u4f18\u5316\u548c\u538b\u7f29\uff0c\u4e00\u76f4\u5230\u91cd\u65b0\u5b9a\u4e49\u73af\u5883\u4e2d\u7684\u53d8\u91cf\u3002<a href=\"https:\/\/www.webpackjs.com\/api\/plugins\">\u63d2\u4ef6\u63a5\u53e3<\/a>\u529f\u80fd\u6781\u5176\u5f3a\u5927\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5404\u79cd\u5404\u6837\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u60f3\u8981\u4f7f\u7528\u4e00\u4e2a\u63d2\u4ef6\uff0c\u4f60\u53ea\u9700\u8981&nbsp;<code>require()<\/code>&nbsp;\u5b83\uff0c\u7136\u540e\u628a\u5b83\u6dfb\u52a0\u5230&nbsp;<code>plugins<\/code>&nbsp;\u6570\u7ec4\u4e2d\u3002\u591a\u6570\u63d2\u4ef6\u53ef\u4ee5\u901a\u8fc7\u9009\u9879(option)\u81ea\u5b9a\u4e49\u3002\u4f60\u4e5f\u53ef\u4ee5\u5728\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\u56e0\u4e3a\u4e0d\u540c\u76ee\u7684\u800c\u591a\u6b21\u4f7f\u7528\u540c\u4e00\u4e2a\u63d2\u4ef6\uff0c\u8fd9\u65f6\u9700\u8981\u901a\u8fc7\u4f7f\u7528&nbsp;<code>new<\/code>&nbsp;\u64cd\u4f5c\u7b26\u6765\u521b\u5efa\u5b83\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p><strong>webpack.config.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const HtmlWebpackPlugin = require('html-webpack-plugin'); \/\/ \u901a\u8fc7 npm \u5b89\u88c5\nconst webpack = require('webpack'); \/\/ \u7528\u4e8e\u8bbf\u95ee\u5185\u7f6e\u63d2\u4ef6\n\nconst config = {\n  module: {\n    rules: &#91;\n      { test: \/\\.txt$\/, use: 'raw-loader' }\n    ]\n  },\n  plugins: &#91;\n    new HtmlWebpackPlugin({template: '.\/src\/index.html'})\n  ]\n};\n\nmodule.exports = config;\n<\/code><\/pre>\n\n\n\n<p>webpack \u63d0\u4f9b\u8bb8\u591a\u5f00\u7bb1\u53ef\u7528\u7684\u63d2\u4ef6\uff01\u67e5\u9605\u6211\u4eec\u7684<a href=\"https:\/\/www.webpackjs.com\/plugins\">\u63d2\u4ef6\u5217\u8868<\/a>\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u5728 webpack \u914d\u7f6e\u4e2d\u4f7f\u7528\u63d2\u4ef6\u662f\u7b80\u5355\u76f4\u63a5\u7684\uff0c\u7136\u800c\u4e5f\u6709\u5f88\u591a\u503c\u5f97\u6211\u4eec\u8fdb\u4e00\u6b65\u63a2\u8ba8\u7684\u7528\u4f8b\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/www.webpackjs.com\/concepts\/plugins\">\u4e86\u89e3\u66f4\u591a\uff01<\/a><\/p>\n\n\n\n<h2><a href=\"https:\/\/www.webpackjs.com\/concepts\/#%E6%A8%A1%E5%BC%8F\"><\/a>\u6a21\u5f0f<a href=\"https:\/\/www.webpackjs.com\/concepts\/#%E6%A8%A1%E5%BC%8F\"><\/a><\/h2>\n\n\n\n<p>\u901a\u8fc7\u9009\u62e9&nbsp;<code>development<\/code>&nbsp;\u6216&nbsp;<code>production<\/code>&nbsp;\u4e4b\u4e2d\u7684\u4e00\u4e2a\uff0c\u6765\u8bbe\u7f6e&nbsp;<code>mode<\/code>&nbsp;\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u542f\u7528\u76f8\u5e94\u6a21\u5f0f\u4e0b\u7684 webpack \u5185\u7f6e\u7684\u4f18\u5316<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {\n  mode: 'production'\n};\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u4ee3\u610f\u4e49\u4e0a\u7684web\u5de5\u7a0b\u5316\uff0c\u5305\u62ec\u6a21\u5757\u5316\uff0c\u7ec4\u4ef6\u5316\uff0c\u81ea\u52a8\u5316\uff0c\u89c4\u8303\u5316\u3002\u5728\u9879\u76ee\u7684\u542f\u52a8\u4e4b\u524d\uff0c\u6211\u4eec\u505a\u597d\u8fd9\u4e00\u7cfb\u5217\u7684\u5de5\u7a0b\u5316\uff0c\u90a3\u4e48 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[44,1],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/1034"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1034"}],"version-history":[{"count":2,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/1034\/revisions"}],"predecessor-version":[{"id":1037,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/1034\/revisions\/1037"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1034"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}