{"id":280,"date":"2021-08-12T20:37:33","date_gmt":"2021-08-12T12:37:33","guid":{"rendered":"http:\/\/gjweb.top\/?p=280"},"modified":"2021-08-23T21:15:26","modified_gmt":"2021-08-23T13:15:26","slug":"webpack%e6%a0%b8%e5%bf%83%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=280","title":{"rendered":"webpack\u6838\u5fc3\u6982\u5ff5"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>\u8d77\u6b65<\/li><li>entry \u7684\u7528\u6cd5<\/li><li>output \u7684\u7528\u6cd5<\/li><li>loaders \u7684\u7528\u6cd5<\/li><li>plugins \u7684\u7528\u6cd5<\/li><li>mode \u7684\u7528\u6cd5<\/li><li>\u6587\u4ef6\u76d1\u542c<\/li><li>\u6587\u4ef6\u70ed\u66f4\u65b0<\/li><li>\u81ea\u52a8\u5220\u9664dist\u6587\u4ef6\u5939<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8d77\u6b65<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>npm init -y \/\/\u6784\u5efa\u9879\u76ee\u6587\u4ef6<br>npm install webpack webpack-cli --save-dev<\/code><\/pre>\n\n\n\n<p>webpack \u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6\u4e3a webpack.config.js ; \u53ef\u4ee5\u901a\u8fc7 webpack &#8211;config \u6307\u5b9a\u914d\u7f6e\u6587\u4ef6 ; \u5e38\u89c4\u9879\u76ee\u6784\u5efa\u4f1a\u914d\u7f6e webpack.dev.js \u548c webpack.prod.js \u4e24\u4e2a\u6587\u4ef6.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-demo<br>--package.json<br>--src<br>----index.js (\u65b0\u5efa)<br>--webpack.config.js (\u65b0\u5efa)<\/code><\/pre>\n\n\n\n<p>\u5728 webpack.config.js \u4e2d\u5199\u5165\u914d\u7f6e\u9879<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'use strict'<br>const path = require('path');<br>module.exports = {<br>    entry:'.\/src\/index.js',<br>    output:{<br>        path:path.join(__dirname,'dist'),<br>        filename:'bundle.js'<br>    },<br>    mode:'procuction'<br>}<\/code><\/pre>\n\n\n\n<p>\u5728 package.json \u4e2d\u914d\u7f6e npm scripts<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"scripts\":{<br> &nbsp; &nbsp;\"build\":\"webpack\"<br>}<\/code><\/pre>\n\n\n\n<p>\u6253\u5305\u8fd0\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run build<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">entry \u7684\u7528\u6cd5<\/h2>\n\n\n\n<p>entry \u7528\u6765\u6307\u5b9a webpack \u7684\u6253\u5305\u5165\u53e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\u5355\u5165\u53e3*\/<br>module.exprots = {<br> &nbsp; &nbsp;entry:'.\/src\/index.js'<br>}<br>\/*\u591a\u5165\u53e3*\/<br>module.export = {<br> &nbsp; &nbsp;entry:{<br> &nbsp; &nbsp; &nbsp; &nbsp;app:\".\/src\/app.js\",<br> &nbsp; &nbsp; &nbsp; &nbsp;home:\".\/src\/home.js\"<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">output \u7684\u7528\u6cd5<\/h2>\n\n\n\n<p>output \u7528\u6765\u6307\u5b9a webpack \u6253\u5305\u7684\u8f93\u51fa\u5165\u53e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\u5355\u5165\u53e3\u914d\u7f6e*\/<br>module.export = {<br> &nbsp; &nbsp;entry:\".\/src\/index.js\",<br> &nbsp; &nbsp;output:{<br> &nbsp; &nbsp; &nbsp; &nbsp;filename:'bundle.js',<br> &nbsp; &nbsp; &nbsp; &nbsp;path:__dirname+\"\/dist\"<br> &nbsp;  }<br>}<br>\/*\u591a\u5165\u53e3\u914d\u7f6e*\/<br>module.export = {<br> &nbsp; &nbsp;entry:{<br> &nbsp; &nbsp; &nbsp; &nbsp;app:\".\/src\/app.js\",<br> &nbsp; &nbsp; &nbsp; &nbsp;home:\".\/src\/home.js\"<br> &nbsp;  },<br> &nbsp; &nbsp;output:{<br> &nbsp; &nbsp; &nbsp; &nbsp;filename:'&#91;name].js',<br> &nbsp; &nbsp; &nbsp; &nbsp;path:__dirname + \"\/dist\"<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">loaders \u7684\u7528\u6cd5<\/h2>\n\n\n\n<p>webpack \u672c\u8eab\u652f\u6301 JS \u548c JSON \u4e24\u79cd\u6587\u4ef6\u7c7b\u578b,\u53ef\u4ee5\u5b89\u88c5 Loaders\u4f9d\u8d56,\u53ef\u4ee5\u5c06\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u7f16\u8bd1\u6210\u7406\u60f3\u7684\u7ed3\u679c.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">\u540d\u79f0<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">babel-loader<\/td><td>\u8f6c\u6362 ES6\u3001ES7 \u7b49js\u65b0\u7279\u6027\u8bed\u6cd5<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">css-loader<\/td><td>\u652f\u6301.css\u6587\u4ef6\u7684\u52a0\u8f7d\u53ca\u89e3\u6790<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">less-loader<\/td><td>\u5c06 less \u6587\u4ef6 \u8f6c\u6362\u4e3acss<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">file-loader<\/td><td>\u8fdb\u884c\u56fe\u7247\u3001\u5b57\u4f53\u7b49\u7684\u6253\u5305<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">raw-loader<\/td><td>\u5c06\u6587\u4ef6\u4ee5\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u5bfc\u5165<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">thread-loader<\/td><td>\u591a\u8fdb\u7a0b\u6253\u5305 JS \u548c CSS<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {<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\">Mode \u7684\u7528\u6cd5<\/h2>\n\n\n\n<p>Mode \u7528\u6765\u6307\u5b9a\u5f53\u524d\u7684\u6784\u5efa\u73af\u5883\u662f: production\u3001development\u3001none<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">\u9009\u9879<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">development<\/td><td>\u8bbe\u7f6e process.env.NODE_ENV \u7684\u503c\u4e3a development<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">production<\/td><td>\u8bbe\u7f6e process.env.NODE_ENV \u7684\u503c\u4e3a production<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">none<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>module.export = {<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\">\u6587\u4ef6\u76d1\u542c<\/h2>\n\n\n\n<p>\u5728\u6587\u4ef6\u6253\u5305\u65f6\uff0c\u76d1\u542c\u6587\u4ef6\u53d8\u5316\u5b9e\u73b0\u6587\u4ef6\u91cd\u65b0\u6253\u5305 \uff08\u7f3a\u70b9\uff1a \u6587\u4ef6\u91cd\u65b0\u6253\u5305\u7f16\u8bd1\u540e\u9875\u9762\u4e0d\u4f1a\u81ea\u52a8\u5237\u65b0\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\u4e24\u79cd\u914d\u7f6e\u65b9\u5f0f 1.\u5728package.json\u4e2d\u914d\u7f6e*\/<br>\"script\":{<br> &nbsp; &nbsp;\"watch\":\"webpack --watch\"<br>}<br>\/*\u5728 webpack.config.js \u4e2d\u914d\u7f6e*\/<br>module.export = {<br> &nbsp; &nbsp;watch:true<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u6587\u4ef6\u70ed\u66f4\u65b0<\/h2>\n\n\n\n<p>\u5728\u5f00\u53d1\u6a21\u5f0f\u4e0b\uff0c\u76d1\u542c\u6587\u4ef6\u53d8\u5316\uff0c\u81ea\u52a8\u7f16\u8bd1\u5e76\u5237\u65b0\uff08\u6b64\u65f6\u7684\u7f16\u8bd1\u4e0d\u4f1a\u89e6\u53d1I\/O \uff0c\u800c\u662f\u76f4\u63a5\u4ece\u5185\u5b58\u4e2d\u8c03\u7528\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*\u5728 webpack.config.js \u4e2d*\/<br>const webpack = require(\"webpack\")<br>module.exports = {<br> &nbsp; &nbsp;mode:\"development\",<br> &nbsp; &nbsp;plugins:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp;new webpack.HotModuleReplacementPlugin()<br> &nbsp;  ]<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u52a8\u5220\u9664 dist \u6587\u4ef6\u5939<\/h2>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install clean-webpack-plugin --save-dev<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e webpack.config.js \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const {CleanWebpackPlugin} = require(\"clean-webpack-plugin\")\r\nmodule.esports = {\r\n    plugins:&#91;\r\n        new CleanWebpackPlugin()\r\n    ]\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8d77\u6b65 entry \u7684\u7528\u6cd5 output \u7684\u7528\u6cd5 loaders \u7684\u7528\u6cd5 plugins \u7684\u7528\u6cd5 mode \u7684\u7528 [&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-280","post","type-post","status-publish","format-standard","hentry","category-webpack"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/280","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=280"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/280\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}