{"id":481,"date":"2023-12-15T11:16:52","date_gmt":"2023-12-15T03:16:52","guid":{"rendered":"http:\/\/ericw.top\/?p=481"},"modified":"2023-12-15T15:39:40","modified_gmt":"2023-12-15T07:39:40","slug":"%e6%90%ad%e5%bb%ba%e5%bc%80%e5%8f%91%e7%8e%af%e5%a2%83","status":"publish","type":"post","link":"http:\/\/ericw.top\/?p=481","title":{"rendered":"\u642d\u5efa\u5f00\u53d1\u73af\u5883"},"content":{"rendered":"<h1 id=\"toc-0\">\u642d\u5efa\u5f00\u53d1\u73af\u5883<\/h1>\n<p><strong>\u6539\u4ee3\u7801\uff0c\u9700\u8981\u91cd\u65b0\u6253\u5305\u624d\u80fd\u8fd0\u884c\u67e5\u770b\uff0c\u6548\u7387\u5f88\u4f4e<\/strong><\/p>\n<p><strong>\u5f00\u53d1\u73af\u5883\uff1a<\/strong>\u914d\u7f6ewebpack-dev-server \u5feb\u901f\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f<\/p>\n<p><strong>\u4f5c\u7528\uff1a<\/strong>\u542f\u52a8Web\u670d\u52a1\uff0c<strong>\u81ea\u52a8<\/strong>\u68c0\u6d4b\u4ee3\u7801\u53d8\u5316\uff0c<strong>\u70ed\u66f4\u65b0<\/strong>\u5230\u7f51\u9875<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>dist \u76ee\u5f55\u548c\u6253\u5305\u5185\u5bb9\u662f\u5728<strong>\u5185\u5b58<\/strong>\u91cc\uff08\u66f4\u65b0\u5feb\uff09<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p>1\u3001<strong>\u4e0b\u8f7d<\/strong> <code class=\"prettyprint\" >webpack-dev-server <\/code>\u8f6f\u4ef6\u5305\u5230\u5f53\u524d\u9879\u76ee<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm i webpack-dev-server --save-dev<\/code><\/pre>\n<p>2\u3001\u8bbe\u7f6e\u6a21\u5f0f\u4e3a<strong>\u5f00\u53d1\u6a21\u5f0f<\/strong>\uff0c\u5e76\u914d\u7f6e<strong>\u81ea\u5b9a\u4e49\u547d\u4ee4<\/strong><\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">\/\/...\n\nmodule.exports = {\n    \/\/...\n    mode:&#039;development&#039;\n}<\/code><\/pre>\n<pre><code class=\"prettyprint\"  class=\"language-js\">&quot;scripts&quot;:{\n    &quot;build&quot;:&quot;webpack&quot;,\n    &quot;dev&quot;:&quot;webpack serve --open&quot;\n}<\/code><\/pre>\n<p>3\u3001\u4f7f\u7528<code class=\"prettyprint\" > npm run dev<\/code> \u6765<strong>\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668<\/strong>\uff0c\u8bd5\u8bd5<strong>\u70ed\u66f4\u65b0<\/strong>\u6548\u679c<\/p>\n<h2 id=\"toc-1\">\u6253\u5305\u6a21\u5f0f<\/h2>\n<p><strong>\u6253\u5305\u6a21\u5f0f\uff1a<\/strong>\u544a\u77e5 Webpack \u4f7f\u7528\u76f8\u5e94\u6a21\u5f0f\u7684\u5185\u7f6e\u4f18\u5316<br \/>\n<strong>\u8bbe\u7f6e\uff1a<\/strong><br \/>\n<strong>\u65b9\u5f0f1\uff1a<\/strong>\u5728 <code class=\"prettyprint\" >webpack.config.js <\/code>\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e <code class=\"prettyprint\" >mode<\/code> \u9009\u9879<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">module.exports = {\n    \/\/...\n    mode:&#039;production&#039;\n};<\/code><\/pre>\n<p><strong>\u65b9\u5f0f2\uff1a<\/strong>\u5728<code class=\"prettyprint\" > package.json<\/code> \u547d\u4ee4\u884c\u8bbe\u7f6e <code class=\"prettyprint\" >mode<\/code> \u53c2\u6570<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-json\">&quot;scipts&quot;:{\n    &quot;build&quot;:&quot;webpack --mode=production&quot;\n    &quot;dev&quot;:&quot;webpack serve --mode=development&quot;\n}<\/code><\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u547d\u4ee4\u884c\u8bbe\u7f6e\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\uff0c\u63a8\u8350\u7528\u547d\u4ee4\u884c\u8bbe\u7f6e<\/p>\n<h2 id=\"toc-2\">\u6253\u5305\u6a21\u5f0f\u7684\u5e94\u7528<\/h2>\n<p><strong>\u9700\u6c42\uff1a<\/strong>\u5728\u5f00\u53d1\u6a21\u5f0f\u4e0b\u7528 style-loader \u5185\u5d4c\u66f4\u5feb\uff0c\u5728\u751f\u4ea7\u6a21\u5f0f\u4e0b\u63d0\u53d6 css \u4ee3\u7801<br \/>\n<strong>\u65b9\u68481\uff1a<\/strong><code class=\"prettyprint\" >webpack.config.js<\/code> \u914d\u7f6e\u5bfc\u51fa\u51fd\u6570\uff0c\u4f46\u662f\u5c40\u9650\u6027\u5927\uff08\u53ea\u63a5\u53d7 2 \u79cd\u6a21\u5f0f\uff09<br \/>\n<strong>\u65b9\u68482\uff1a<\/strong>\u501f\u52a9 <code class=\"prettyprint\" >cross-env<\/code> \uff08\u8de8\u5e73\u53f0\u901a\u7528\uff09\u5305\u547d\u4ee4\uff0c\u8bbe\u7f6e\u53c2\u6570\u533a\u5206\u73af\u5883<br \/>\n\u6b65\u9aa4\uff1a<br \/>\n1\u3001\u4e0b\u8f7d <code class=\"prettyprint\" >cross-env<\/code> \u8f6f\u4ef6\u5305\u5230\u5f53\u524d\u9879\u76ee<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">npm i cross-env --save-dev<\/code><\/pre>\n<p>2\u3001\u914d\u7f6e\u81ea\u5b9a\u4e49\u547d\u4ee4\uff0c\u4f20\u5165\u53c2\u6570\u540d\u548c\u503c\uff08\u4f1a\u7ed1\u5b9a\u5230 <code class=\"prettyprint\" >process.env<\/code> \u5bf9\u8c61\u4e0b\uff09<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-js\">&quot;scipts&quot;:{\n     &quot;test&quot;:&quot;echo \\&quot;Error:no test specified\\&quot; &amp;&amp; exit 1&quot;,\n     &quot;build&quot;:&quot;cross-env NODE_ENV=production webpack --mode=production&quot;,\n     &quot;dev&quot;:&quot;cross-env NODE_ENV=development webpack serve --open --mode=development&quot;\n}<\/code><\/pre>\n<p>3\u3001\u5728 <code class=\"prettyprint\" >webpack.config.js<\/code> \u533a\u5206\u4e0d\u540c\u73af\u5883\u4f7f\u7528\u4e0d\u540c\u914d\u7f6e<br \/>\n4\u3001\u91cd\u65b0\u6253\u5305\u89c2\u5bdf\u4e24\u79cd\u914d\u7f6e\u533a\u522b<\/p>\n<p><strong>\u65b9\u68483\uff1a<\/strong>\u914d\u7f6e\u4e0d\u540c\u7684 webpack.config.js \uff08\u9002\u7528\u591a\u79cd\u6a21\u5f0f\u5dee\u5f02\u6027\u8f83\u5927\u60c5\u51b5\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u642d\u5efa\u5f00\u53d1\u73af\u5883 \u6539\u4ee3\u7801\uff0c\u9700\u8981\u91cd\u65b0\u6253\u5305\u624d\u80fd\u8fd0\u884c\u67e5\u770b\uff0c\u6548\u7387\u5f88\u4f4e \u5f00\u53d1\u73af\u5883\uff1a\u914d\u7f6ewebpack-dev-server  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":482,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,5],"tags":[],"_links":{"self":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/481"}],"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=481"}],"version-history":[{"count":2,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/481\/revisions"}],"predecessor-version":[{"id":499,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/481\/revisions\/499"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/media\/482"}],"wp:attachment":[{"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}