{"id":467,"date":"2022-08-28T20:47:09","date_gmt":"2022-08-28T12:47:09","guid":{"rendered":"http:\/\/gjweb.top\/?p=467"},"modified":"2022-08-28T20:47:10","modified_gmt":"2022-08-28T12:47:10","slug":"webpack%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5%e5%8f%8a%e5%bc%80%e5%8f%91%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=467","title":{"rendered":"webpack\u57fa\u672c\u6982\u5ff5\u53ca\u5f00\u53d1\u914d\u7f6e"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>\u57fa\u672c\u6982\u5ff5<\/li><li>webpack \u914d\u7f6e\u6846\u67b6<\/li><li>\u89e3\u6790 css \u6837\u5f0f<\/li><li>\u89e3\u6790 less \/ sass \u6587\u4ef6<\/li><li>webpack \u6253\u5305\u56fe\u7247\u8d44\u6e90<\/li><li>webpack \u6253\u5305\u5176\u4ed6\u8d44\u6e90<\/li><li>webpack \u914d\u7f6e devServer<\/li><li>webpack \u5355\u72ec\u6253\u5305css\u6587\u4ef6<\/li><li>webpack \u538b\u7f29 css \u6587\u4ef6<\/li><li>webpack \u68c0\u67e5 eslint<\/li><li>\u6784\u5efa\u57fa\u7840 webpack \u5f00\u53d1\u914d\u7f6e<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-\u57fa\u672c\u6982\u5ff5\">1. \u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>entry \u5165\u53e3<\/li><li>output \u51fa\u53e3<ul><li>filname \u6253\u5305\u540e\u7684 js \u8def\u5f84\u548c\u6587\u4ef6\u540d<\/li><li>path \u6253\u5305\u540e\u6574\u4f53\u8f93\u51fa\u7684\u6587\u4ef6\u5939<\/li><\/ul><\/li><li>loader \u4f7f\u7528 webpack \u80fd\u5904\u7406\u7684\u975e JS \u6587\u4ef6<\/li><li>plugin \u6267\u884c loader \u5904\u7406\u4e0d\u4e86\u7684\u529f\u80fd<\/li><li>mode<ul><li>process.NODE_ENV === development \u5f00\u53d1\u6a21\u5f0f<\/li><li>process.MODE_ENV === production \u751f\u4ea7\u6a21\u5f0f<\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-webpack-\u914d\u7f6e\u6846\u67b6\">2. webpack \u914d\u7f6e\u6846\u67b6<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install webpack webpack-cli -g<br>npm install webpack webpack-cli -D<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>const {resolve} = require(\"path\")<br>module.exports = {<br> &nbsp; &nbsp;entry:\"\", \/\/ \u5165\u53e3\u6587\u4ef6<br> &nbsp; &nbsp;output:{<br> &nbsp; &nbsp; &nbsp; &nbsp;filename:\"\",<br> &nbsp; &nbsp; &nbsp; &nbsp;path:resolve(__dirname,\"dist\")<br> &nbsp;  },<br> &nbsp; &nbsp;module:{<br> &nbsp; &nbsp; &nbsp; &nbsp;rules:{}<br> &nbsp;  },<br> &nbsp; &nbsp;plugins:&#91;],<br> &nbsp; &nbsp;mode:'development\"<br> &nbsp; &nbsp;mode:\"production\"<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-\u89e3\u6790css\u6837\u5f0f\">3. \u89e3\u6790css\u6837\u5f0f<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add style-loader css-loader -D<\/code><\/pre>\n\n\n\n<p>webpack.config.js \u4e2d\u6dfb\u52a0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.export = {<br> &nbsp; &nbsp;entry:'.\/src\/js\/index.js',<br> &nbsp; &nbsp;output:{<br> &nbsp; &nbsp; &nbsp; &nbsp;filename:'js\/index.js',<br> &nbsp; &nbsp; &nbsp; &nbsp;path:resolve(__dirname,\"dist\")<br> &nbsp;  },<br> &nbsp; &nbsp;module:{<br> &nbsp; &nbsp; &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {test:\/\\.css$\/,use:&#91;\"style-loader\",\"css-loader\"]}<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-\u89e3\u6790-less-sass-\u6837\u5f0f\">4. \u89e3\u6790 less \/ sass \u6837\u5f0f<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add style-loader css-loader less less-loader sass sass-loader -D<\/code><\/pre>\n\n\n\n<p>webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module:{<br> &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp;  {test:\/\\.less$\/,use:&#91;\"style-loader\",\"css-loader\",\"less-loader\"]},<br> &nbsp; &nbsp; &nbsp;  {test:\/\\.sass$\/,use:&#91;\"style-loader\",\"css-loader\",\"sass-loader\"]}<br> &nbsp;  ]<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-\u6253\u5305\u56fe\u7247\u8d44\u6e90\">5. \u6253\u5305\u56fe\u7247\u8d44\u6e90<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add url-loader html-withimg-webpack file-loader -D<\/code><\/pre>\n\n\n\n<p>webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module:{<br> &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.(png|jpg)$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader:\"url-loader\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;potions:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;limit:10 * 1024 , \/\/ \u5c0f\u4e8e10KB\u7684\u56fe\u7247\u8f6c\u4e3abase64<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;esModule:false<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\\/html$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader:\"html-withimg-loader\"<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  ]<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-webpack-\u6253\u5305\u5176\u4ed6\u8d44\u6e90\">6. webpack \u6253\u5305\u5176\u4ed6\u8d44\u6e90<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yarn add file-loader -D<\/pre>\n\n\n\n<p>webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">module:{<br> &nbsp; &nbsp;rules:[<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exculde:\/\\.(png|jpg|html|js|css|less|scss)$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader:\"file-loader\"<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  ]<br>}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-webpack-\u914d\u7f6e-devserver\">7.webpack \u914d\u7f6e devServer<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn  add webpack-dev-server -D<\/code><\/pre>\n\n\n\n<p>webpack.config.js \u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports={<br> &nbsp; &nbsp;entry:\"\",<br> &nbsp; &nbsp;output:{<br> &nbsp; &nbsp; &nbsp; &nbsp;filename:\"\",<br> &nbsp; &nbsp; &nbsp; &nbsp;path:resolve(__dirname,\"dist\")<br> &nbsp;  },<br> &nbsp; &nbsp;module:{<br> &nbsp; &nbsp; &nbsp; &nbsp;rules:&#91;]<br> &nbsp;  },<br> &nbsp; &nbsp;plugins:&#91;],<br> &nbsp; &nbsp;mode:\"development\",<br> &nbsp; &nbsp;devServer:{<br> &nbsp; &nbsp; &nbsp; &nbsp;staitc:reslove(__dirname,\"dist\"), \/\/ dist \u6307\u7684\u662f\u6253\u5305\u7684\u6587\u4ef6\u5939\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp;open:true, \/\/ \u6253\u5f00\u65b0\u7a97\u53e3<br> &nbsp; &nbsp; &nbsp; &nbsp;compress:true, \/\/ \u5f00\u542fgzip\u538b\u7f29<br> &nbsp; &nbsp; &nbsp; &nbsp;port:3000, \/\/ \u7aef\u53e3\u53f7<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx webpack-dev-server<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-webpack-\u63d0\u53d6-css-\u4e3a\u5355\u72ec\u6587\u4ef6\">8. webpack \u63d0\u53d6 css \u4e3a\u5355\u72ec\u6587\u4ef6<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add mini-css-extract-plugin -D<\/code><\/pre>\n\n\n\n<p>webpack.config.js \u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const MiniCssExtractPlugin = require(\"mini-css-extract-plugin\")<br>module:{<br> &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp;  {test:\/\\.css$\/,use:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MiniCssExtractPlugin.loader,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"css-loader\"<br> &nbsp; &nbsp; &nbsp;  ]}<br> &nbsp;  ]<br>},<br>plugins:&#91;<br> &nbsp; &nbsp;new MiniCssExtractPlugin({<br> &nbsp; &nbsp; &nbsp; &nbsp;filename:\"css\/index.css\" \/\/ \u6253\u5305\u540e\u7684 css \u8def\u5f84\u548c\u6587\u4ef6\u540d<br> &nbsp;  })<br>]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-webpack-\u538b\u7f29-css-\u6587\u4ef6\">9. webpack \u538b\u7f29 css \u6587\u4ef6<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add optimize-css-assets-webpack-plugin<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &nbsp;OptimizeCssAssetsWebpackPlugin = require(optimize-css-assets-webpack-plugin)<br>plugins:&#91;<br> &nbsp; &nbsp;new OptimizeCssAssetsWebpackPlugin()<br>]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-webpack-\u68c0\u67e5-eslint\">10. webpack \u68c0\u67e5 eslint<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add eslint eslint-config-airbnb-base eslint-plugin-import eslint-webpack-plugin -D<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e package.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{<br> &nbsp; &nbsp;\"eslintConfig\":{<br> &nbsp; &nbsp; &nbsp; &nbsp;\"extends\":\"airdnd-base<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const EslintWebpackPlugin = require(\"eslint-webpack-plugin\")<br>plugin:&#91;<br> &nbsp; &nbsp;new EslintWebpackPlugin({<br> &nbsp; &nbsp; &nbsp; &nbsp;fix:true, \/\/ \u81ea\u52a8\u4fee\u590d\u4e0d\u7b26\u5408\u89c4\u8303\u7684\u5199\u6cd5<br> &nbsp;  })<br>]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"11-webpack-\u914d\u7f6e\u57fa\u672c\u7684\u751f\u4ea7\u73af\u5883\">11. webpack \u914d\u7f6e\u57fa\u672c\u7684\u751f\u4ea7\u73af\u5883<\/h2>\n\n\n\n<p>\u5b89\u88c5\u5168\u90e8\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add webpack webpack-cli webpack-dev-serevr less less-loader sass sass-loader style-loader css-loader url-loader file-loader html-withimg-loader html-webpack-plugin mini-css-extract-plugin optimize-css-assets-webpack-plugin eslint-config-airbnb-base eslint-plugin-import eslint-webpack-plugin -D<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const HtmlWebpackPlugin = require(\"html-webpack-plugin\");<br>const {resolve} = require(\"path\");<br>module.exports = {<br> &nbsp;entry:\".\/src\/js\/index.js\",<br> &nbsp;output:{<br> &nbsp; &nbsp;filename:\"js\/dist.js\",<br> &nbsp; &nbsp;path:resolve(__dirname,\".\/dist\")<br>  },<br> &nbsp;module:{<br> &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.css$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;use:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"style-loader\",<br> &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader:'css-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;esModule:false,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.less$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;use:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'style-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader:'css-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;esModule:false,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"less-loader\"<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.(png|jpg)$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;loader:'url-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;limit:10*1024,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;esModule:false,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputPath:\"img\"<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.html$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;use:&#91;\"html-withimg-loader\"]<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;exclude:\/\\.(js|html|css|less|html|png|jpg)$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;loader:'file-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputPath:\"other\"<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp;  }<br> &nbsp;  ]<br>  },<br> &nbsp;plugins:&#91;<br> &nbsp; &nbsp;new HtmlWebpackPlugin({<br> &nbsp; &nbsp; &nbsp;template:\".\/src\/index.html\"<br> &nbsp;  })<br>  ],<br> &nbsp;mode:'development',<br> &nbsp;devServer:{<br> &nbsp; &nbsp;static:resolve(__dirname,\"dist\"),<br> &nbsp; &nbsp;compress:true,<br> &nbsp; &nbsp;port:3000,<br> &nbsp; &nbsp;open:true<br>  }<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u6982\u5ff5 webpack \u914d\u7f6e\u6846\u67b6 \u89e3\u6790 css \u6837\u5f0f \u89e3\u6790 less \/ sass \u6587\u4ef6 webpack \u6253 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-467","post","type-post","status-publish","format-standard","hentry","category-webpack5"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=467"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/467\/revisions"}],"predecessor-version":[{"id":468,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/467\/revisions\/468"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}