{"id":362,"date":"2023-12-14T09:40:27","date_gmt":"2023-12-14T01:40:27","guid":{"rendered":"http:\/\/ericw.top\/?p=362"},"modified":"2023-12-15T09:42:27","modified_gmt":"2023-12-15T01:42:27","slug":"webpack-%e6%89%93%e5%8c%85%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"http:\/\/ericw.top\/?p=362","title":{"rendered":"Webpack  \u6253\u5305\u5de5\u5177"},"content":{"rendered":"<h1 id=\"toc-0\">Webpack \u6253\u5305\u5de5\u5177<\/h1>\n<h2 id=\"toc-1\">\u4f7f\u7528Webpack<\/h2>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n1\u3001\u65b0\u5efa\u5e76\u521d\u59cb\u5316\u9879\u76ee\uff0c\u7f16\u5199\u4e1a\u52a1\u6e90\u4ee3\u7801<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm i webpack webpack-cil --save-dev<\/code><\/pre>\n<p>2\u3001\u4e0b\u8f7d webpack webpack-cli \u5230\u5f53\u524d\u9879\u76ee\u4e2d\uff08\u7248\u672c\u72ec\u7acb\uff09\uff0c\u5e76\u914d\u7f6e\u5c40\u90e8\u81ea\u5b9a\u4e49\u547d\u4ee4<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">&quot;scripts&quot;:{\n    &quot;build&quot;:&quot;webpack&quot;\n},<\/code><\/pre>\n<p>3\u3001\u8fd0\u884c\u6253\u5305\u547d\u4ee4\uff0c\u81ea\u52a8\u4ea7\u751f dist \u5206\u53d1\u6587\u4ef6\u5939\uff08\u538b\u7f29\u548c\u4f18\u5316\u540e\uff0c\u7528\u4e8e\u6700\u7ec8\u8fd0\u884c\u7684\u4ee3\u7801\uff09<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm run build<\/code><\/pre>\n<h2 id=\"toc-2\">\u4fee\u6539Webpack\u6253\u5305\u5165\u53e3 \u51fa\u53e3<\/h2>\n<p><strong>Webpack \u914d\u7f6e\uff1a<\/strong>\u5f71\u54cd Webpack \u6253\u5305\u8fc7\u7a0b\u548c\u7ed3\u679c<br \/>\n<strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n1\u3001\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u65b0\u5efa <code class=\"prettyprint\" >webpack.config.js<\/code> \u914d\u7f6e\u6587\u4ef6<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">const path = require(&#039;path&#039;)\n\nmodule.exports = {\n    entry:path.resolve(__dirname,&quot;src\/login\/index.js&quot;),\n    output:{\n        path:path.resolve(__dirname,&#039;dist&#039;),\n        filename:&#039;.login\/index.js&#039;\n    }\n}<\/code><\/pre>\n<p>2\u3001\u5bfc\u51fa<strong>\u914d\u7f6e\u5bf9\u8c61<\/strong>\uff0c\u914d\u7f6e\u5165\u53e3\uff0c\u51fa\u53e3\u6587\u4ef6\u7684\u8def\u5f84<br \/>\n3\u3001\u91cd\u65b0\u6253\u5305\u89c2\u5bdf<br \/>\n<strong>\u6ce8\u610f\uff1a\u53ea\u6709\u548c\u5165\u53e3\u4ea7\u751f\u76f4\u63a5\/\u95f4\u63a5\u7684\u5f15\u5165\u5173\u7cfb\uff0c\u624d\u4f1a\u88ab\u6253\u5305<\/strong><\/p>\n<h2 id=\"toc-3\">\u6253\u5305Webpack<\/h2>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n1\u3001\u51c6\u5907\u7528\u6237\u767b\u5f55\u9875\u9762<br \/>\n2\u3001\u7f16\u5199\u6838\u5fc3 JS \u903b\u8f91\u4ee3\u7801<br \/>\n3\u3001\u6253\u5305\u5e76\u624b\u52a8\u590d\u5236\u7f51\u9875\u5230 dist \u4e0b\uff0c\u5f15\u5165\u6253\u5305\u540e\u7684 js\uff0c\u8fd0\u884c<br \/>\n<strong>\u6838\u5fc3\uff1aWebpack \u6253\u5305\u540e\u7684\u4ee3\u7801\uff0c\u4f5c\u7528\u5728\u524d\u7aef\u7f51\u9875\u4e2d\u4f7f\u7528<\/strong><\/p>\n<p><img decoding=\"async\" src=\"C:\\Users\\MI\\AppData\\Roaming\\Typora\\typora-user-images\\1702602487071.png\" alt=\"1702602487071\" \/><\/p>\n<h2 id=\"toc-4\">\u81ea\u52a8\u751f\u6210html\u6587\u4ef6<\/h2>\n<p><strong>\u63d2\u4ef6 html-webpack-plugin\uff1a<\/strong> \u5728 Webpack \u6253\u5305\u65f6\u751f\u6210 html \u6587\u4ef6<br \/>\n<strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n\u4e0b\u8f7d<code class=\"prettyprint\" > html-webpack-plugin<\/code> \u672c\u5730\u8f6f\u4ef6\u5305<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm i html-webpack-Plugin --save-dev<\/code><\/pre>\n<p>\u914d\u7f6e <code class=\"prettyprint\" >webpack.config.js<\/code> \u8ba9 Webpack \u62e5\u6709\u63d2\u4ef6\u529f\u80fd<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">const HtmlWebpackplugin = require(&#039;html-webpack-plugin&#039;)\n\nmodule.exports = {\n    \/\/...\n    plugins:[\n        new HtmlWebpackPlugin = require(&#039;hhtml-webpack-plugin&#039;)\n\n        module.exports = {\n        new HtmlWebpackPlugin({\n        template:&#039;.\/public\/login.html&#039;,\n        filename:&#039;.\/login\/index.html&#039;\n        })\n        }\n    ]\n}<\/code><\/pre>\n<p>\u91cd\u65b0<strong>\u6253\u5305<\/strong>\u89c2\u5bdf\u6548\u679c<\/p>\n<h2 id=\"toc-5\">\u6253\u5305CSS\u4ee3\u7801<\/h2>\n<p><strong>\u52a0\u8f7d\u5668 css-loader\uff1a<\/strong>\u89e3\u6790 css \u4ee3\u7801<br \/>\n<strong>\u52a0\u8f7d\u5668 style-loader\uff1a<\/strong>\u628a\u89e3\u6790\u540e\u7684 css \u4ee3\u7801\u63d2\u5165\u5230 DOM<br \/>\n<strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n1\u3001\u51c6\u5907 css \u6587\u4ef6\u4ee3\u7801\u5f15\u5165\u5230 src\/login\/index.js \u4e2d\uff08\u538b\u7f29\u8f6c\u8bd1\u5904\u7406\u7b49\uff09<br \/>\n2\u3001\u4e0b\u8f7d css-loader \u548c style-loader \u672c\u5730\u8f6f\u4ef6\u5305<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm i css-loader style-loader --save-dev<\/code><\/pre>\n<p>3\u3001\u914d\u7f6e webpack.config.js \u8ba9 Webpack \u62e5\u6709\u8be5\u52a0\u8f7d\u5668\u529f\u80fd<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">moduule.exports = {\n    \/\/...\n    module:{\n        rules:[\n            {\n                test:\/\\.css$\/i,\n                use:[&quot;style-loader&quot;,&quot;css-loader&quot;],\n            },\n        ],\n    },\n};<\/code><\/pre>\n<p>4\u3001\u6253\u5305\u540e\u89c2\u5bdf\u6548\u679c<\/p>\n<p><strong>\u6ce8\u610f\uff1aWebpack \u9ed8\u8ba4\u53ea\u8bc6\u522b js \u4ee3\u7801<\/strong><\/p>\n<h2 id=\"toc-6\">\u4f18\u5316 - \u63d0\u53d6CSS\u4ee3\u7801<\/h2>\n<p><strong>\u63d2\u4ef6 mini-css-extract-plugin\uff1a<\/strong>\u63d0\u53d6 css \u4ee3\u7801<br \/>\n<strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n1\u3001\u4e0b\u8f7d mini-css-extract-plugin \u672c\u5730\u8f6f\u4ef6\u5305<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm install --save-dev mini-css-extract-plugin<\/code><\/pre>\n<p>2\u3001\u914d\u7f6e webpack.config.js \u8ba9 Webpack \u62e5\u6709\u8be5\u63d2\u4ef6\u529f\u80fd<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">const MiniCssExtractPlugin = require(&#039;mini-css-extract-plugin&#039;)\n\nmodule.exports = {\n    \/\/...\n    module:{\n        rules:[\n            {\n                test:\/\\.css$\/i,\n                use:[MiniCssExtractPlugin.loader,&quot;css-loader&quot;],\n            },\n        ],\n    },\n    plugin:[\n        \/\/...\n        new MiniCssExtractPlugin()\n    ]\n};<\/code><\/pre>\n<p>3\u3001\u6253\u5305\u540e\u89c2\u5bdf\u6548\u679c<\/p>\n<p><strong>\u6ce8\u610f\uff1a\u4e0d\u80fd\u548c style-loader \u4e00\u8d77\u4f7f\u7528<\/strong><br \/>\n<strong>\u597d\u5904\uff1acss \u6587\u4ef6\u53ef\u4ee5\u88ab\u6d4f\u89c8\u5668\u7f13\u5b58\uff0c\u51cf\u5c11 js \u6587\u4ef6\u4f53\u79ef<\/strong><\/p>\n<h2 id=\"toc-7\">\u4f18\u5316 - \u538b\u7f29\u8fc7\u7a0b<\/h2>\n<p><strong>\u95ee\u9898\uff1acss \u4ee3\u7801\u63d0\u53d6\u540e\u6ca1\u6709\u538b\u7f29<\/strong><br \/>\n<strong>\u89e3\u51b3\uff1a<\/strong>\u4f7f\u7528 css-minimizer-webpack-plugin \u63d2\u4ef6<br \/>\n<strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n1\u3001\u4e0b\u8f7d css-minimizer-webpack-plugin \u672c\u5730\u8f6f\u4ef6\u5305<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm install css-minimizer-webpack-plugin --save-dev<\/code><\/pre>\n<p>2\u3001\u914d\u7f6e webpack.config.js \u8ba9 webpack \u62e5\u6709\u8be5\u529f\u80fd<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">const CssMinimizerPlugin = require(&quot;css-minimizer-webpack-plugin&quot;);\n\nmodule.exports = {\n    \/\/...\n    \/\/ \u4f18\u5316\n    optimizertion:{\n        \/\/\u6700\u5c0f\u5316\n        minimizer:[\n            \/\/\u5728webpack@5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u7528&#039;...&#039;\u8bed\u6cd5\u6765\u6269\u5c55\u73b0\u6709\u7684minimizer\n            &#039;...&#039;,\n            new CssMinimizerPlugin(),\n        ],\n    }\n};<\/code><\/pre>\n<p>3\u3001\u6253\u5305\u91cd\u65b0\u89c2\u5bdf<\/p>\n<h2 id=\"toc-8\">\u6253\u5305less\u4ee3\u7801<\/h2>\n<p><strong>\u52a0\u8f7d\u5668 less-loader\uff1a<\/strong>\u628a less \u4ee3\u7801\u7f16\u8bd1\u4e3a css \u4ee3\u7801<br \/>\n<strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n1\u3001\u65b0\u5efa less \u4ee3\u7801\uff08\u8bbe\u7f6e\u80cc\u666f\u56fe\uff09\u5e76\u5f15\u5165\u5230 src\/login\/index.js \u4e2d<br \/>\n2\u3001\u4e0b\u8f7d less \u548c less-loader \u672c\u5730\u8f6f\u4ef6\u5305<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm i less less-loader --save-dev<\/code><\/pre>\n<p>3\u3001\u914d\u7f6e webpack.config.js \u8ba9 Webpack \u62e5\u6709\u529f\u80fd<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">\/\/...\nmodule.exports = {\n    \/\/...\n    module:{\n        rules:[\n            \/\/...\n            {\n                test:\/\\.less$\/i,\n                use:[MiniCssExtractPlugin.loader,&quot;css-loader&quot;,&quot;less-lloader&quot;]\n            }\n        ]\n    }\n}<\/code><\/pre>\n<p>4\u3001\u6253\u5305\u540e\u89c2\u5bdf\u6548\u679c<\/p>\n<p><strong>\u6ce8\u610f\uff1aless-loader \u9700\u8981\u914d\u5408 less \u8f6f\u4ef6\u5305\u4f7f\u7528<\/strong><\/p>\n<h2 id=\"toc-9\">\u6253\u5305\u56fe\u7247<\/h2>\n<p><strong>\u8d44\u6e90\u6a21\u5757\uff1a<\/strong>Webpack5 \u5185\u7f6e\u8d44\u6e90\u6a21\u5757\uff08\u5b57\u4f53\uff0c\u56fe\u7247\u7b49\uff09\u6253\u5305\uff0c\u65e0\u9700\u989d\u5916 loader<br \/>\n<strong>\u6b65\u9aa4\uff1a<\/strong><br \/>\n<strong>\u914d\u7f6e webpack.config.js \u8ba9 Webpack \u62e5\u6709\u6253\u5305\u56fe\u7247\u529f\u80fd<\/strong><\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">module.exports = {\n    \/\/...\n    module:{\n        rules:[\n            \/\/...\n            {\n                test:\/\\.(png|jpg|jpge|gif)$\/i,\n                type:&#039;asset&#039;,\n                generator:{\n                    filename:&#039;assets\/[hash][ext][query]&#039;\n                }\n            }\n        ]\n    }\n}<\/code><\/pre>\n<ul>\n<li>\n<p>\u5360\u4f4d\u7b26 \u3010hash\u3011\u5bf9\u6a21\u5757\u5185\u5bb9\u505a\u7b97\u6cd5\u8ba1\u7b97\uff0c\u5f97\u5230\u6620\u5c04\u7684\u6570\u5b57\u5b57\u6bcd\u7ec4\u5408\u7684\u5b57\u7b26\u4e32<\/p>\n<\/li>\n<li>\n<p>\u5360\u4f4d\u7b26 \u3010ext\u3011\u4f7f\u7528\u5f53\u524d\u6a21\u5757\u539f\u672c\u7684\u5360\u4f4d\u7b26\uff0c\u4f8b\u5982\uff1a.png \/ .jpg \u7b49\u5b57\u7b26\u4e32<\/p>\n<\/li>\n<li>\n<p>\u5360\u4f4d\u7b26 \u3010query\u3011\u4fdd\u7559\u5f15\u5165\u6587\u4ef6\u65f6\u4ee3\u7801\u4e2d\u67e5\u8be2\u53c2\u6570\uff08\u53ea\u6709 URL \u4e0b\u751f\u6548\uff09<\/p>\n<p>\u6253\u5305\u540e\u89c2\u5bdf\u6548\u679c\u548c\u533a\u522b<br \/>\n<strong>\u6ce8\u610f\uff1a\u5224\u65ad\u4e34\u754c\u503c\u9ed8\u8ba4\u4e3a 8KB<\/strong><br \/>\n<strong>\u5927\u4e8e<\/strong> 8KB \u6587\u4ef6\uff1a<strong>\u53d1\u9001\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u5e76\u5bfc\u51fa URL \u5730\u5740<\/strong><br \/>\n<strong>\u5c0f\u4e8e<\/strong> 8KB \u6587\u4ef6\uff1a<strong>\u5bfc\u51fa\u4e00\u4e2a data URI\uff08base64\u5b57\u7b26\u4e32\uff09<\/strong><\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Webpack \u6253\u5305\u5de5\u5177 \u4f7f\u7528Webpack \u6b65\u9aa4\uff1a 1\u3001\u65b0\u5efa\u5e76\u521d\u59cb\u5316\u9879\u76ee\uff0c\u7f16\u5199\u4e1a\u52a1\u6e90\u4ee3\u7801 npm i web [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":479,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,11,5],"tags":[],"_links":{"self":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/362"}],"collection":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=362"}],"version-history":[{"count":2,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":480,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/362\/revisions\/480"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/media\/479"}],"wp:attachment":[{"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}