{"id":469,"date":"2022-08-29T22:43:17","date_gmt":"2022-08-29T14:43:17","guid":{"rendered":"http:\/\/gjweb.top\/?p=469"},"modified":"2022-08-29T22:43:18","modified_gmt":"2022-08-29T14:43:18","slug":"02-webpack-%e5%9f%ba%e7%a1%80%e6%a6%82%e5%bf%b5%e5%8f%8a%e7%94%9f%e4%ba%a7%e6%a8%a1%e5%bc%8f%e7%9a%84%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=469","title":{"rendered":"02. webpack \u57fa\u7840\u6982\u5ff5\u53ca\u751f\u4ea7\u6a21\u5f0f\u7684\u914d\u7f6e"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>js \u68c0\u67e5 Eslint<\/li><li>webpack \u517c\u5bb9\u5904\u7406 eslint<\/li><li>webpack \u538b\u7f29 html \u548c JS<\/li><li>\u751f\u4ea7\u73af\u5883\u914d\u7f6e<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-js-\u68c0\u67e5-eslint\">1. JS \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>\"eslintConfig\":{<br> &nbsp; &nbsp;\"extends\":\"airbnb-base\"<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 EslintPlugin = require(\"eslint-webpack-plugin\")<br>plugins:&#91;<br> &nbsp; &nbsp;new EslintPlugin({<br> &nbsp; &nbsp; &nbsp; &nbsp;fix:true \/\/ \u81ea\u52a8\u4fee\u590d\u4e0d\u5408\u7406\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=\"2-webpack-\u517c\u5bb9\u5904\u7406-js\">2. webpack \u517c\u5bb9\u5904\u7406 js<\/h2>\n\n\n\n<p>\u65b9\u5f0f\u4e00\uff1a\u5168\u90e8\u517c\u5bb9\uff08\u7f3a\u70b9\uff1a\u4f1a\u5c06\u6240\u6709\u517c\u5bb9\u4ee3\u7801\u5f15\u5165\uff0c\u4f1a\u589e\u5927\u6587\u4ef6\u4f53\u79ef\uff09<\/p>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add babel-loader @babel\/core @babel\/preset-env @babel\/polyfill -D<\/code><\/pre>\n\n\n\n<p>\u5728 webpack.config.js \u4e2d\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module:{<br> &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp;  {test:\/\\.js$\/,loader:\"@babel\/polyfill\"}<br> &nbsp;  ]<br>}<\/code><\/pre>\n\n\n\n<p>\u65b9\u5f0f\u4e8c\uff1a\u6309\u9700\u52a0\u8f7d\uff08\u63a8\u8350\uff09<\/p>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add babel-loader @babel\/core @babel\/preset-env core-js<\/code><\/pre>\n\n\n\n<p>\u5728 webpack.config.js \u4e2d\u914d\u7f6e<\/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:\/\\.js$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exclude:\/node_modules\/<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader:\"babel-loader\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      presets:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;          &#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;              \"@babel\/preset-env\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;              {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                  useBuiltIns:\"usage\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                  coresjs:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                      version:3<br> &nbsp; &nbsp; &nbsp;                      },<br> &nbsp; &nbsp; &nbsp;                      targets:{ \/\/ \u6307\u5b9a\u517c\u5bb9\u5230\u54ea\u4e2a\u7248\u672c\u7684\u6d4f\u89c8\u5668<br> &nbsp; &nbsp; &nbsp;                          chrome:\"60\",<br> &nbsp; &nbsp; &nbsp;                          ie:\"9\"<br> &nbsp; &nbsp; &nbsp;                      }<br> &nbsp; &nbsp; &nbsp;                  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;          ]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      ]<br> &nbsp; &nbsp; &nbsp;      }<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  ]<br> &nbsp; &nbsp;<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-webpack-\u538b\u7f29-html-\u548c-js\">3.webpack \u538b\u7f29 html \u548c js<\/h2>\n\n\n\n<p>\u5728webpack.config.js \u4e2d <strong>mode:&#8217;production&#8217;<\/strong> \u6a21\u5f0f\uff0c \u4f1a\u9ed8\u8ba4\u8fdb\u884c JS \u7684\u6253\u5305\u538b\u7f29\uff0c<\/p>\n\n\n\n<p>\u4e0b\u9762\u793a\u4f8b\u538b\u7f29html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plugins:&#91;<br> &nbsp; &nbsp;new HtmlWebpackPlugin({<br> &nbsp; &nbsp; &nbsp; &nbsp;template:\".\/src\/index.html\",<br> &nbsp; &nbsp; &nbsp; &nbsp;collapseWhitespace\uff1atrue, \/\/ \u79fb\u9664\u7a7a\u683c<br> &nbsp; &nbsp; &nbsp; &nbsp;removeComments:true, \/\/\u79fb\u9664\u6ce8\u91ca<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-css-\u517c\u5bb9\u5904\u7406\">4. css \u517c\u5bb9\u5904\u7406<\/h2>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add postcss-loader postcss-preset-env -D<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e package.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"browsersList\":{<br> &nbsp; &nbsp;\"development\":&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp;\"lase 1 chrome version\",<br> &nbsp; &nbsp; &nbsp; &nbsp;\"lase 1 firefox version\",<br> &nbsp; &nbsp; &nbsp; &nbsp;\"lase 1 safari version\"<br> &nbsp;  ],<br> &nbsp; &nbsp;\"production\":&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp;\"&gt;0.2%\",<br> &nbsp; &nbsp; &nbsp; &nbsp;\"not dead\",<br> &nbsp; &nbsp; &nbsp; &nbsp;\"not op_mini all\"<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>module:{<br> &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp;  {test:\/\\.css$\/,use:&#91;\"style-loader\",\"css-loader\",\"postcss-loader\"]}<br> &nbsp;  ]<br>}<br>plugins:&#91;<br> &nbsp; &nbsp;require(\"postcss-preset-env\")<br>]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-webpack-\u642d\u5efa\u5f00\u53d1\u751f\u4ea7\u73af\u5883\">5. webpack \u642d\u5efa\u5f00\u53d1\u751f\u4ea7\u73af\u5883<\/h2>\n\n\n\n<p>package.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \"eslintConfig\": {<br> &nbsp; &nbsp;\"extends\": \"airbnb-base\"<br>  },<br> &nbsp;\"browserslist\": {<br> &nbsp; &nbsp;\"development\": &#91;<br> &nbsp; &nbsp; &nbsp;\"last 1 chrome version\",<br> &nbsp; &nbsp; &nbsp;\"last 1 firefox version\",<br> &nbsp; &nbsp; &nbsp;\"last 1 safari version\"<br> &nbsp;  ],<br> &nbsp; &nbsp;\"production\": &#91;<br> &nbsp; &nbsp; &nbsp;\"&gt;0.2%\",<br> &nbsp; &nbsp; &nbsp;\"not dead\",<br> &nbsp; &nbsp; &nbsp;\"not op_mini all\"<br> &nbsp;  ]<br>  }<\/code><\/pre>\n\n\n\n<p>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>const MiniCssExtractPlugin = require(\"mini-css-extract-plugin\")<br>const OptimizeCssAssetsWebpackPlugin = require(\"optimize-css-assets-webpack-plugin\")<br>const ESLintPlugin = require(\"eslint-webpack-plugin\")<br>const cssLoader = &#91;<br> &nbsp;\/\/ \"style-loader\",<br> &nbsp;MiniCssExtractPlugin.loader,<br>  {<br> &nbsp; &nbsp;loader:\"css-loader\",<br> &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp;esModule:false<br> &nbsp;  },<br>  },<br> &nbsp;\"postcss-loader\"<br>\u200b<br>]<br>module.exports = {<br> &nbsp;entry:\".\/src\/js\/index.js\",<br> &nbsp;output:{<br> &nbsp; &nbsp;filename:\"js\/index.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;...cssLoader]<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.less$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;use:&#91;...cssLoader,\"less-loader\"]<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.scss$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;use:&#91;...cssLoader,\"sass-loader\"]<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;loader:'html-withimg-loader'<br> &nbsp; &nbsp;  },<br> &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;test:\/\\.js$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;exclude:\/node_modules\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;loader:\"babel-loader\",<br> &nbsp; &nbsp; &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;presets:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"@babel\/preset-env\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;useBuiltIns:'usage',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;corejs:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;version:3<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;targets:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;chrome:\"60\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ie:\"9\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<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;exclude:\/\\.(png|jpg|html|js|css|less|scss)$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp;loader:'file-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp;options:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputPath:'media'<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; &nbsp; &nbsp;collapseWhitespace:true,<br> &nbsp; &nbsp; &nbsp;removeComments:true<br> &nbsp; &nbsp; &nbsp;<br> &nbsp;  }),<br> &nbsp; &nbsp;new MiniCssExtractPlugin({<br> &nbsp; &nbsp; &nbsp;filename:\"css\/index.css\"<br> &nbsp;  }),<br> &nbsp; &nbsp;new OptimizeCssAssetsWebpackPlugin(),<br> &nbsp; &nbsp;new ESLintPlugin({<br> &nbsp; &nbsp; &nbsp;fix:true<br> &nbsp;  }),<br> &nbsp; &nbsp;require(\"postcss-preset-env\")<br>  ],<br> &nbsp;mode:\"production\",<br> &nbsp;devServer:{<br> &nbsp; &nbsp;static:resolve(__dirname,\"dist\"),<br> &nbsp; &nbsp;open:true,<br> &nbsp; &nbsp;compress:true,<br> &nbsp; &nbsp;port:3000<br>  }<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>js \u68c0\u67e5 Eslint webpack \u517c\u5bb9\u5904\u7406 eslint webpack \u538b\u7f29 html \u548c JS \u751f [&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-469","post","type-post","status-publish","format-standard","hentry","category-webpack5"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/469","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=469"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/469\/revisions"}],"predecessor-version":[{"id":470,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/469\/revisions\/470"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}