{"id":285,"date":"2021-08-23T21:19:14","date_gmt":"2021-08-23T13:19:14","guid":{"rendered":"http:\/\/gjweb.top\/?p=285"},"modified":"2021-08-23T21:19:14","modified_gmt":"2021-08-23T13:19:14","slug":"webpack-%e5%8a%9f%e8%83%bd%e6%80%bb%e7%bb%93-%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=285","title":{"rendered":"webpack \u529f\u80fd\u603b\u7ed3 (\u4e8c)"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>webpack \u6253\u5305\u5e93\u548c\u57fa\u7840\u5e93<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">webpack \u6253\u5305\u5e93\u548c\u57fa\u7840\u5e93<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7684\u5b9e\u4f8b\uff0c\u4ee5\u6c42\u548c\u4e3a\u4f8b\u8fdb\u884c\u6253\u5305\uff0c\u4f1a\u901a\u8fc7 terser-webpack-plugin \u63d2\u4ef6\u8fdb\u884c\u4ee3\u7801\u7684\u538b\u7f29\uff0c\u8bbe\u7f6e\u5165\u53e3\u6587\u4ef6\uff0c\u5e76\u53d1\u5e03\u5230npm\u4e0a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa src \/ index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function num(a,b){<br> &nbsp; &nbsp;return a+b<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u200b<br>const TerserPlugin = require(\"terser-webpack-plugin\")<br>module.exports &nbsp;= {<br> &nbsp; &nbsp;mode:\"none\",<br> &nbsp; &nbsp;entry:{<br> &nbsp; &nbsp; &nbsp; &nbsp;\"large-number\":\".\/src\/index.js\",<br> &nbsp; &nbsp; &nbsp; &nbsp;\"large-number.min\":\".\/src\/index.js\"<br> &nbsp;  },<br> &nbsp; &nbsp;output:{<br> &nbsp; &nbsp; &nbsp; &nbsp;filename:'&#91;name].js',<br> &nbsp; &nbsp; &nbsp; &nbsp;library:'largeNumber',<br> &nbsp; &nbsp; &nbsp; &nbsp;libraryTarget:'umd',<br> &nbsp; &nbsp; &nbsp; &nbsp;libraryExport:'default'<br> &nbsp;  },<br> &nbsp; &nbsp;optimization:{<br> &nbsp; &nbsp; &nbsp; &nbsp;minimize:true,<br> &nbsp; &nbsp; &nbsp; &nbsp;minimizer:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new TerserPlugin({<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;include:\/\\.min\\.js$\/<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  })<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp;  }<br>}<br>\u200b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u4ee3\u7801 \u4f7f\u7528\u4e86 <strong>terser-webpack-plugin<\/strong> \u63d2\u4ef6\uff0c\u5355\u72ec\u5bf9 min.js \u6587\u4ef6\u8fdb\u884c\u4e86\u4ee3\u7801\u538b\u7f29\uff0c\u6240\u4ee5\u8981\u5b89\u88c5\u8be5\u63d2\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install terser-webpack-plugin --save-dev<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u914d\u7f6e\u5165\u53e3\u6587\u4ef6\uff0c\u914d\u7f6epackage.json<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{<br> &nbsp; &nbsp;\"main\":\"index.js\"<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6839\u76ee\u5f55\u521b\u5efa index.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if(process.env.NODE_ENV === \"production\"){<br> &nbsp; &nbsp;module.exports = require(\".\/dist\/large-number.min.js\")<br>}else{<br> &nbsp; &nbsp;module.exports = require(\".\/dist\/large-number.js\")<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6253\u5305\u5e76\u53d1\u5e03\u5230NPM<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run build<br>npm publish<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>webpack \u6253\u5305\u5e93\u548c\u57fa\u7840\u5e93 webpack \u6253\u5305\u5e93\u548c\u57fa\u7840\u5e93 \u4e0b\u9762\u7684\u5b9e\u4f8b\uff0c\u4ee5\u6c42\u548c\u4e3a\u4f8b\u8fdb\u884c\u6253\u5305\uff0c\u4f1a\u901a\u8fc7 ter [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-webpack"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/285","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=285"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/285\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}