{"id":275408,"date":"2024-05-16T14:26:45","date_gmt":"2024-05-16T06:26:45","guid":{"rendered":""},"modified":"2024-05-16T14:26:51","modified_gmt":"2024-05-16T06:26:51","slug":"webpack-%e9%a1%b9%e7%9b%ae%e6%80%8e%e4%b9%88%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90-html-%e9%a1%b5%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/275408.html","title":{"rendered":"webpack \u9879\u76ee\u600e\u4e48\u81ea\u52a8\u751f\u6210 html \u9875\u9762"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200401\/fc6fe46e-0953-4b4e-be14-e3ce87cac45d.webp\" alt=\"webpack \u9879\u76ee\u600e\u4e48\u81ea\u52a8\u751f\u6210 html \u9875\u9762\" \/><\/p>\n<p><p>Webpack \u9879\u76ee\u53ef\u4ee5\u901a\u8fc7<strong>\u4f7f\u7528 HtmlWebpackPlugin \u63d2\u4ef6<\/strong>\u3001<strong>\u914d\u7f6e webpack.config.js \u6587\u4ef6<\/strong>\u6765\u81ea\u52a8\u751f\u6210 HTML \u9875\u9762\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u81ea\u52a8\u5f15\u5165\u751f\u6210\u7684 JavaScript \u548c CSS \u6587\u4ef6\uff0c\u8fd8\u63d0\u4f9b\u4e86\u6a21\u677f\u529f\u80fd\u3001\u538b\u7f29 HTML \u6587\u4ef6\u7b49\u591a\u79cd\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<p>HtmlWebpackPlugin \u662f\u4e00\u4e2a webpack \u63d2\u4ef6\uff0c\u5b83\u7b80\u5316\u4e86HTML\u6587\u4ef6\u7684\u521b\u5efa\uff0c\u4ee5\u4fbf\u4e3a\u4f60\u7684webpack\u5305\u63d0\u4f9b\u670d\u52a1\u3002\u8fd9\u4e2a\u63d2\u4ef6\u5c06\u4e3a\u4f60\u751f\u6210\u4e00\u4e2a HTML5 \u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528 script \u6807\u7b7e\u81ea\u52a8\u5f15\u5165\u4f60\u6240\u6709 webpack \u751f\u6210\u7684\u5305\u3002\u5982\u679c\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u751f\u6210\u4e86\u591a\u4e2a\u5305\uff08\u6bd4\u5982\uff0c\u4f7f\u7528\u4e86\u591a\u5165\u53e3\u70b9\u6216\u8005\u4f7f\u7528\u4e86\u4ee3\u7801\u5206\u5272\uff09\uff0cHtmlWebpackPlugin \u5c06\u4e3a\u4f60\u751f\u6210\u5305\u542b\u6240\u6709\u5fc5\u8981 script \u6807\u7b7e\u7684 HTML \u6587\u4ef6\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u677f\u6765\u5b9a\u5236\u751f\u6210\u7684 HTML \u9875\u9762\u7684\u5916\u89c2\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5 HtmlWebpackPlugin \u63d2\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5b89\u88c5 HtmlWebpackPlugin \u63d2\u4ef6\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7 npm \u6216\u8005 yarn \u6765\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">npm install html-webpack-plugin --save-dev<\/p>\n<h2><strong>\u6216\u8005\u4f7f\u7528 yarn<\/strong><\/h2>\n<p>yarn add html-webpack-plugin --dev<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u63d2\u4ef6\u4f5c\u4e3a\u5f00\u53d1\u4f9d\u8d56\u5b89\u88c5\uff0c\u56e0\u4e3a\u5b83\u4ec5\u5728\u5f00\u53d1\u548c\u6784\u5efa\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e webpack.config.js \u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728\u9879\u76ee\u7684 webpack.config.js \u6587\u4ef6\u4e2d\u5f15\u5165\u5e76\u914d\u7f6e HtmlWebpackPlugin\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const HtmlWebpackPlugin = require(&#039;html-webpack-plugin&#039;);<\/p>\n<p>module.exports = {<\/p>\n<p>  \/\/ \u5165\u53e3\u6587\u4ef6\u8bbe\u7f6e<\/p>\n<p>  entry: &#039;.\/src\/index.js&#039;,<\/p>\n<p>  \/\/ \u63d2\u4ef6\u914d\u7f6e<\/p>\n<p>  plugins: [<\/p>\n<p>    new HtmlWebpackPlugin({<\/p>\n<p>      title: &#039;My Awesome Application&#039;, \/\/ \u751f\u6210\u7684 HTML \u6587\u4ef6\u7684\u6807\u9898<\/p>\n<p>      template: &#039;.\/src\/template.html&#039;, \/\/ \u6307\u5b9a\u6a21\u677f\u6587\u4ef6<\/p>\n<p>    })<\/p>\n<p>  ],<\/p>\n<p>  \/\/ \u5176\u4ed6 webpack \u914d\u7f6e...<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u914d\u7f6e\u8be5\u63d2\u4ef6\u540e\uff0c\u6bcf\u6b21\u6784\u5efa\u65f6Webpack\u90fd\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a HTML \u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u4f1a\u5f15\u7528\u8f93\u51fa\u7684\u6240\u6709 JavaScript \u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6a21\u677f<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u7ec6\u81f4\u5730\u63a7\u5236\u751f\u6210\u7684 HTML \u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;&lt;%= htmlWebpackPlugin.options.title %&gt;&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;div id=&quot;app&quot;&gt;&lt;\/div&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6a21\u677f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 lodash \u6a21\u677f\u8bed\u6cd5\u6765\u8bbf\u95ee HtmlWebpackPlugin \u4e2d\u914d\u7f6e\u7684\u53d8\u91cf\u3002\u4f8b\u5982\uff0c<code>&lt;%= htmlWebpackPlugin.options.title %&gt;<\/code> \u4f1a\u88ab\u66ff\u6362\u6210\u914d\u7f6e\u4e2d\u6307\u5b9a\u7684\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fdb\u9636\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>HtmlWebpackPlugin \u8fd8\u652f\u6301\u8bb8\u591a\u5176\u4ed6\u914d\u7f6e\u9009\u9879\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>minify<\/strong>\uff1a\u63a7\u5236\u662f\u5426\u4ee5\u53ca\u5982\u4f55\u538b\u7f29\u751f\u6210\u7684 HTML \u6587\u4ef6\u3002<\/li>\n<li><strong>filename<\/strong>\uff1a\u6307\u5b9a\u8f93\u51fa\u7684 HTML \u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002<\/li>\n<li><strong>chunks<\/strong>\uff1a\u6307\u5b9a\u54ea\u4e9b JavaScript \u6587\u4ef6\u5305\u542b\u5728\u751f\u6210\u7684 HTML \u4e2d\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u914d\u7f6e\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u4f60\u7684\u5f00\u53d1\u548c\u6784\u5efa\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u5176\u4ed6\u63d2\u4ef6\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0cHtmlWebpackPlugin \u5f80\u5f80\u4e0d\u662f\u5b64\u7acb\u4f7f\u7528\u7684\u3002\u7ed3\u5408\u4f7f\u7528\u5982 CleanWebpackPlugin\uff08\u6e05\u7406\/build\u6587\u4ef6\u5939\uff09\u3001MiniCssExtractPlugin\uff08\u63d0\u53d6 CSS \u5230\u5355\u72ec\u6587\u4ef6\uff09\u7b49\u63d2\u4ef6\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u9879\u76ee\u7684\u6784\u5efa\u6548\u7387\u548c\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cWebpack \u9879\u76ee\u53ef\u4ee5\u8f7b\u677e\u81ea\u52a8\u751f\u6210 HTML \u9875\u9762\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u4f18\u5316\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728webpack\u9879\u76ee\u4e2d\u5b9e\u73b0\u81ea\u52a8\u751f\u6210html\u9875\u9762\uff1f<\/strong><\/p>\n<p>\u5728webpack\u9879\u76ee\u4e2d\u5b9e\u73b0\u81ea\u52a8\u751f\u6210html\u9875\u9762\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528html-webpack-plugin\u63d2\u4ef6\u3002\u9996\u5148\uff0c\u5728\u9879\u76ee\u4e2d\u5b89\u88c5html-webpack-plugin\u63d2\u4ef6\uff0c\u7136\u540e\u5728webpack\u914d\u7f6e\u6587\u4ef6\u4e2d\u5f15\u5165\u8be5\u63d2\u4ef6\u3002<\/p>\n<pre><code class=\"language-javascript\">const HtmlWebpackPlugin = require(&#039;html-webpack-plugin&#039;);\n\nmodule.exports = {\n  \/\/ \u5176\u4ed6\u914d\u7f6e\u9879...\n  plugins: [\n    new HtmlWebpackPlugin({\n      title: &#039;My App&#039;,  \/\/ \u8bbe\u7f6ehtml\u9875\u9762\u7684\u6807\u9898\n      template: &#039;path\/to\/template.html&#039;  \/\/ \u4f7f\u7528\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49html\u7684\u7ed3\u6784\n    })\n  ]\n}\n<\/code><\/pre>\n<p>\u901a\u8fc7\u914d\u7f6ehtml-webpack-plugin\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u751f\u6210\u7684html\u9875\u9762\u7684\u6807\u9898\u3001\u9009\u62e9\u4f7f\u7528\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5c06\u6253\u5305\u751f\u6210\u7684js\u6587\u4ef6\u81ea\u52a8\u5f15\u5165\u5230html\u4e2d\u3002<\/p>\n<p><strong>2. \u5728webpack\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u6839\u636e\u6a21\u677f\u52a8\u6001\u751f\u6210html\u9875\u9762\uff1f<\/strong><\/p>\n<p>\u5728webpack\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528html-webpack-plugin\u63d2\u4ef6\u7684\u6a21\u677f\u529f\u80fd\u6765\u52a8\u6001\u751f\u6210html\u9875\u9762\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2ahtml\u6a21\u677f\u6587\u4ef6\u3002\u5728\u6a21\u677f\u6587\u4ef6\u4e2d\u53ef\u4ee5\u4f7f\u7528\u6807\u8bb0(<code>&lt;%= %&gt;<\/code>\u683c\u5f0f)\u6765\u52a8\u6001\u66ff\u6362\u5185\u5bb9\u3002<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt;&lt;%= htmlWebpackPlugin.options.title %&gt;&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;div id=&quot;app&quot;&gt;&lt;\/div&gt;\n  &lt;script src=&quot;bundle.js&quot;&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u5728webpack.config.js\u4e2d\u8fdb\u884c\u914d\u7f6e:<\/p>\n<pre><code class=\"language-javascript\">const HtmlWebpackPlugin = require(&#039;html-webpack-plugin&#039;);\n\nmodule.exports = {\n  \/\/ \u5176\u4ed6\u914d\u7f6e\u9879...\n  plugins: [\n    new HtmlWebpackPlugin({\n      title: &#039;My App&#039;,\n      template: &#039;path\/to\/template.html&#039;\n    })\n  ]\n}\n<\/code><\/pre>\n<p>\u901a\u8fc7\u914d\u7f6ehtml-webpack-plugin\u63d2\u4ef6\u7684template\u9009\u9879\u4e3a\u6a21\u677f\u6587\u4ef6\u7684\u8def\u5f84\uff0cwebpack\u4f1a\u6839\u636e\u6a21\u677f\u81ea\u52a8\u751f\u6210html\u9875\u9762\uff0c\u5e76\u5c06\u6253\u5305\u751f\u6210\u7684js\u6587\u4ef6\u5f15\u5165\u5230html\u4e2d\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5728webpack\u9879\u76ee\u4e2d\u751f\u6210\u591a\u4e2ahtml\u9875\u9762\uff1f<\/strong><\/p>\n<p>\u60f3\u8981\u5728webpack\u9879\u76ee\u4e2d\u751f\u6210\u591a\u4e2ahtml\u9875\u9762\uff0c\u53ef\u4ee5\u901a\u8fc7html-webpack-plugin\u63d2\u4ef6\u7684\u591a\u6b21\u521b\u5efa\u5b9e\u4f8b\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u5728webpack\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u914d\u7f6e\u591a\u4e2ahtml-webpack-plugin\u793a\u4f8b\uff0c\u5e76\u8bbe\u7f6e\u4e0d\u540c\u7684\u6a21\u677f\u548c\u8f93\u51fa\u6587\u4ef6\u540d\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-javascript\">const HtmlWebpackPlugin = require(&#039;html-webpack-plugin&#039;);\nconst path = require(&#039;path&#039;);\n\nmodule.exports = {\n  \/\/ \u5176\u4ed6\u914d\u7f6e\u9879...\n  plugins: [\n    new HtmlWebpackPlugin({\n      title: &#039;Home&#039;,\n      template: path.resolve(__dirname, &#039;src\/templates\/home.html&#039;),\n      filename: &#039;home.html&#039;\n    }),\n    new HtmlWebpackPlugin({\n      title: &#039;About&#039;,\n      template: path.resolve(__dirname, &#039;src\/templates\/about.html&#039;),\n      filename: &#039;about.html&#039;\n    }),\n    \/\/ \u5176\u4ed6\u9875\u9762\u7684\u914d\u7f6e...\n  ]\n}\n<\/code><\/pre>\n<p>\u6bcf\u4e00\u4e2aHtmlWebpackPlugin\u5b9e\u4f8b\u90fd\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u7684html\u9875\u9762\u3002\u4f60\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5b9e\u4f8b\u4e2d\u8bbe\u7f6e\u4e0d\u540c\u7684\u6807\u9898\u3001\u6a21\u677f\u548c\u8f93\u51fa\u6587\u4ef6\u540d\uff0c\u4ee5\u5b9e\u73b0\u751f\u6210\u591a\u4e2ahtml\u9875\u9762\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Webpack \u9879\u76ee\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 HtmlWebpackPlugin \u63d2\u4ef6\u3001\u914d\u7f6e webpack.config [&hellip;]","protected":false},"author":3,"featured_media":275413,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275408"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=275408"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/275408\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/275413"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=275408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=275408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=275408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}