{"id":44644,"date":"2025-06-20T07:31:56","date_gmt":"2025-06-19T23:31:56","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44644.html"},"modified":"2025-06-20T07:31:56","modified_gmt":"2025-06-19T23:31:56","slug":"webpack%e7%bb%a7%e7%bb%ad%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44644.html","title":{"rendered":"webpack\u7ee7\u7eed\u5b66\u4e60"},"content":{"rendered":"<h4>\u8ba4\u8bc6PostCSS\u5de5\u5177<\/h4>\n<p>PostCSS\u662f\u4e00\u4e2a\u901a\u8fc7JS\u6765\u8f6c\u6362\u6837\u5f0f\u7684\u5de5\u5177&#xff0c;\u8fd9\u4e2a\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u4e00\u4e9bCSS\u7684\u8f6c\u6362\u548c\u9002\u914d&#xff0c;\u6bd4\u5982\u81ea\u52a8\u6dfb\u52a0\u6d4f\u89c8\u5668\u524d\u7f00&#xff0c;css\u6837\u5f0f\u7684\u91cd\u7f6e<\/p>\n<p>\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u9700\u8981\u501f\u52a9\u4e8ePostCSS\u5bf9\u5e94\u7684\u63d2\u4ef6<\/p>\n<p>\u81ea\u52a8\u6dfb\u52a0\u6d4f\u89c8\u5668\u524d\u7f00\u9700\u8981&#xff1a;<\/p>\n<p>npm install autoprefixer <\/p>\n<h4>\u5355\u72ec\u7684postcss\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\u8fdb\u884c\u7ba1\u7406<\/p>\n<p>\u5728\u6839\u76ee\u5f55\u4e0b\u521b\u5efapostcss.config.js<\/p>\n<p>module.exports &#061; {<br \/>\n    plugins:[<br \/>\n        require(&#034;autoprefixer&#034;)<br \/>\n    ],<br \/>\n} <\/p>\n<h4>postcss-preset-env<\/h4>\n<p>\u5728\u914d\u7f6epostcss-loader\u7684\u65f6\u5019&#xff0c;\u6211\u4eec\u914d\u7f6e\u63d2\u4ef6\u5e76\u4e0d\u9700\u8981\u4f7f\u7528autoprefixer&#xff0c;\u53ef\u4ee5\u4f7f\u7528postcss-preset-env<\/p>\n<p>preset\u662f\u9884\u8bbe\u7684\u610f\u601d<\/p>\n<p>postcss-preset-env\u4e5f\u662f\u4e00\u4e2apostcss\u7684\u63d2\u4ef6<\/p>\n<p>\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5c06\u4e00\u4e9b\u73b0\u4ee3\u7684css\u7279\u6027\u8f6c\u6210\u5927\u591a\u6570\u6d4f\u89c8\u5668\u8ba4\u8bc6\u7684css&#xff0c;\u5e76\u4e14\u4f1a\u6839\u636e\u76ee\u6807\u6d4f\u89c8\u5668\u6216\u8005\u8fd0\u884c\u65f6\u73af\u5883\u6dfb\u52a0\u6240\u9700\u7684polyfill<\/p>\n<p>\u4f1a\u81ea\u52a8\u5e2e\u52a9\u6211\u4eec\u6dfb\u52a0autoprefixer&#xff08;\u76f8\u5f53\u4e8e\u5df2\u7ecf\u5185\u7f6e\u4e86autoprefixer&#xff09;<\/p>\n<p>\u9996\u5148\u5b89\u88c5postcss-preset-env<\/p>\n<p>npm install postcss-preset-env -D <\/p>\n<p>\u4e4b\u540e\u6211\u4eec\u4fee\u6539\u6389\u4e4b\u524d\u7684autoprefixer<\/p>\n<h4>Webpack\u6253\u5305\u56fe\u7247-JS-Vue<\/h4>\n<p>css\u6d4f\u89c8\u5668\u524d\u7f00 \u5728\u73b0\u4ee3\u7684\u524d\u7aef\u5f00\u53d1\u6a21\u5f0f\u4e2d&#xff0c;\u4e0d\u9700\u8981\u624b\u52a8\u7684\u6dfb\u52a0\u6d4f\u89c8\u5668\u524d\u7f00<\/p>\n<h4>\u52a0\u8f7d\u56fe\u7247\u6848\u4f8b\u51c6\u5907<\/h4>\n<p>npm install file-loader <\/p>\n<p>\u5e38\u89c1\u7684\u4f7f\u7528\u56fe\u7247\u7684\u65b9\u5f0f\u662f\u4e24\u79cd&#xff1a;<\/p>\n<p>img\u5143\u7d20&#xff0c;\u8bbe\u7f6esrc\u5c5e\u6027<\/p>\n<p>\u5176\u4ed6\u5143\u7d20&#xff0c;\u8bbe\u7f6ebackground-image\u7684css\u5c5e\u6027<\/p>\n<h4>asset module type<\/h4>\n<p>\u5728Webpack5\u4e4b\u524d&#xff0c;\u52a0\u8f7d\u8fd9\u4e9b\u8d44\u6e90\u9700\u8981\u4f7f\u7528\u4e00\u4e9bloader&#xff0c;\u6bd4\u5982\u8bf4raw-loader\u3001ul-loader\u3001file-loader<\/p>\n<p>\u5728Webpack5\u5f00\u59cb&#xff0c;\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8d44\u6e90\u6a21\u5757\u7c7b\u578b\u6765\u66ff\u4ee3\u4e0a\u9762\u7684\u8fd9\u4e9bloader<\/p>\n<h5>\u8d44\u6e90\u6a21\u5757\u7c7b\u578b<\/h5>\n<p>asset\/resource\u53d1\u9001\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u5e76\u5bfc\u51faURL&#xff08;\u4e4b\u524d\u7528file-loader\u5b9e\u73b0&#xff09;<\/p>\n<p>asset\/inline\u5bfc\u51fa\u4e00\u4e2a\u8d44\u6e90\u7684data URL&#xff08;\u4e4b\u524d\u7528url-loader\u5b9e\u73b0&#xff09;<\/p>\n<p>asset\/source\u5bfc\u51fa\u8d44\u6e90\u7684\u6e90\u4ee3\u7801&#xff08;\u4e4b\u524d\u7528raw-loader\u5b9e\u73b0&#xff09;<\/p>\n<p>asset\u5728\u5bfc\u51fa\u4e00\u4e2adata URL\u548c\u53d1\u9001\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e4b\u95f4\u81ea\u52a8\u9009\u62e9&#xff08;\u4e4b\u524d\u7528url-loader\u5e76\u4e14\u914d\u7f6e\u8d44\u6e90\u4f53\u79ef\u9650\u5236\u5b9e\u73b0&#xff09;<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>module.exports &#061; {<br \/>\n  \/\/ \u8bbe\u7f6e\u6253\u5305\u7684\u6a21\u5f0f<br \/>\n  mode: &#039;development&#039;,<\/p>\n<p>  \/\/ \u8bbe\u7f6e\u5165\u53e3\u6587\u4ef6<br \/>\n  entry: &#039;.\/src\/index.js&#039;,<\/p>\n<p>  \/\/ \u8bbe\u7f6e\u8f93\u51fa<br \/>\n  output: {<br \/>\n    filename: &#039;bundle.js&#039;,<br \/>\n    path: path.resolve(__dirname, &#039;dist&#039;)<br \/>\n  },<\/p>\n<p>  \/\/ \u914d\u7f6e\u6a21\u5757\u89c4\u5219<br \/>\n  module: {<br \/>\n    rules: [<br \/>\n      {<br \/>\n        test: \/\\\\.css$\/,<br \/>\n        use: [<br \/>\n          &#034;style-loader&#034;,<br \/>\n          &#034;css-loader&#034;,<br \/>\n          &#034;postcss-loader&#034;<br \/>\n        ]<br \/>\n      },{<br \/>\n        test: \/\\\\.less$\/,<br \/>\n        use:[ &#034;style-loader&#034;,&#034;css-loader&#034;,&#034;less-loader&#034;]<br \/>\n      },<br \/>\n      {<br \/>\n        test:\/\\\\.(png|jpe?g|svg|gif)$\/,<br \/>\n        type:&#034;asset&#034;<br \/>\n      }<br \/>\n    ]<br \/>\n  }<br \/>\n}; <\/p>\n<p>\u66f4\u6539\u914d\u7f6e\u6587\u4ef6\u6210\u8fd9\u4e2a\u6837\u5b50&#xff0c;\u7136\u540e&#xff1a;<\/p>\n<p>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html lang&#061;&#034;en&#034;&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset&#061;&#034;UTF-8&#034;&gt;<br \/>\n    &lt;meta name&#061;&#034;viewport&#034; content&#061;&#034;width&#061;device-width, initial-scale&#061;1.0&#034;&gt;<br \/>\n    &lt;title&gt;Image Demo&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n    &lt;script src&#061;&#034;..\/dist\/bundle.js&#034;&gt;&lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n\/\/ \u5bfc\u5165\u56fe\u7247<br \/>\nimport imgSrc from &#039;.\/img\/sss.png&#039;;<\/p>\n<p>\/\/ \u521b\u5efaimg\u5143\u7d20<br \/>\nconst imgEl &#061; document.createElement(&#039;img&#039;);<\/p>\n<p>\/\/ \u8bbe\u7f6esrc\u5c5e\u6027<br \/>\nimgEl.src &#061; imgSrc;<\/p>\n<p>\/\/ \u5c06\u56fe\u7247\u6dfb\u52a0\u5230body\u4e2d<br \/>\ndocument.body.appendChild(imgEl); <\/p>\n<p>\u6253\u5305\u540e\u663e\u793a&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"741\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619233154-68549dea18a33.png\" width=\"618\" \/><\/p>\n<p>\u6253\u5305\u56fe\u7247\u6709\u4e0d\u540c\u7684\u65b9\u5f0f&#xff1a;<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>module.exports &#061; {<br \/>\n  \/\/ \u8bbe\u7f6e\u6253\u5305\u7684\u6a21\u5f0f<br \/>\n  mode: &#039;development&#039;,<\/p>\n<p>  \/\/ \u8bbe\u7f6e\u5165\u53e3\u6587\u4ef6<br \/>\n  entry: &#039;.\/src\/component\/cpns.js&#039;,<\/p>\n<p>  \/\/ \u8bbe\u7f6e\u8f93\u51fa<br \/>\n  output: {<br \/>\n    filename: &#039;bundle.js&#039;,<br \/>\n    path: path.resolve(__dirname, &#039;dist&#039;)<br \/>\n  },<\/p>\n<p>  \/\/ \u914d\u7f6e\u6a21\u5757\u89c4\u5219<br \/>\n  module: {<br \/>\n    rules: [<br \/>\n      {<br \/>\n        test: \/\\\\.css$\/,<br \/>\n        use: [<br \/>\n          &#034;style-loader&#034;,<br \/>\n          &#034;css-loader&#034;,<br \/>\n          &#034;postcss-loader&#034;<br \/>\n        ]<br \/>\n      },{<br \/>\n        test: \/\\\\.less$\/,<br \/>\n        use:[ &#034;style-loader&#034;,&#034;css-loader&#034;,&#034;less-loader&#034;]<br \/>\n      },<br \/>\n      {<br \/>\n        test:\/\\\\.(png|jpe?g|svg|gif)$\/,<br \/>\n        \/\/\u6253\u5305\u4e24\u5f20\u56fe\u7247&#xff0c;\u5e76\u4e14\u8fd9\u4e24\u5f20\u56fe\u7247\u6709\u81ea\u5df1\u7684\u5730\u5740&#xff0c;\u5c06\u5730\u5740\u8bbe\u7f6e\u5230img\/bgi\u4e2d<br \/>\n        type:&#034;asset\/resource&#034;<br \/>\n        \/\/\u5c06\u56fe\u7247\u8fdb\u884cbase64\u7684\u7f16\u7801&#xff0c;\u5e76\u4e14\u76f4\u63a5\u7f16\u7801\u540e\u7684\u6e90\u7801\u653e\u5230\u6253\u5305\u7684js\u6587\u4ef6\u4e2d<br \/>\n        \/\/type:&#034;asset\/inline&#034;<br \/>\n      }<br \/>\n    ]<br \/>\n  }<br \/>\n}; <\/p>\n<p>\u4e0a\u9762\u90a3\u79cd\u65b9\u5f0f\u7684\u7f3a\u70b9\u5c31\u662f\u591a\u56fe\u7247\u52a0\u8f7d\u8981\u4e24\u6b21\u7f51\u7edc\u8bf7\u6c42<\/p>\n<p>\u4e0b\u9762\u8fd9\u79cd\u65b9\u5f0f\u4e5f\u6709\u7f3a\u70b9&#xff0c;\u7f3a\u70b9\u662f\u9020\u6210js\u7684\u6587\u4ef6\u975e\u5e38\u5927&#xff0c;\u4e0b\u8f7djs\u6587\u4ef6\u672c\u8eab\u6d88\u8017\u65f6\u95f4\u975e\u5e38\u957f&#xff0c;\u4e0b\u8f7d\u548c\u89e3\u6790\u7684\u6267\u884c\u65f6\u95f4\u90fd\u5f88\u957f<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u8981\u8fdb\u884c\u5408\u7406\u7684\u89c4\u8303<\/p>\n<p>\u5bf9\u4e8e\u5c0f\u4e00\u70b9\u7684\u56fe\u7247&#xff0c;\u53ef\u4ee5\u8fdb\u884cbase64\u7f16\u7801<\/p>\n<p>\u5bf9\u4e8e\u5927\u4e00\u70b9\u7684\u56fe\u7247&#xff0c;\u5355\u72ec\u7684\u56fe\u7247\u6253\u5305&#xff0c;\u5f62\u6210url\u5730\u5740&#xff0c;\u5355\u72ec\u7684\u8bf7\u6c42\u8fd9\u4e2aurl\u56fe\u7247<\/p>\n<h5>\u81ea\u5b9a\u4e49\u6587\u4ef6\u8f93\u51fa\u8def\u5f84\u548c\u6587\u4ef6\u540d<\/h5>\n<p>\u65b9\u5f0f\u4e00&#xff1a;\u4fee\u6539output&#xff0c;\u6dfb\u52a0assetModuleFinename\u5c5e\u6027<\/p>\n<p>\u65b9\u5f0f\u4e8c&#xff1a;\u5728Rule\u4e2d&#xff0c;\u6dfb\u52a0\u4e00\u4e2agenerator\u5c5e\u6027&#xff0c;\u5e76\u4e14\u8bbe\u7f6efilename<\/p>\n<p>\u6709\u51e0\u4e2a\u5e38\u7528\u7684placeholder&#xff1a;<\/p>\n<p>ext&#xff08;\u5904\u7406\u6587\u4ef6\u7684\u6269\u5c55\u540d&#xff09;\u3001name&#xff08;\u5904\u7406\u6587\u4ef6\u7684\u540d\u79f0&#xff09;\u3001hash&#xff08;\u6587\u4ef6\u7684\u5185\u5bb9&#xff0c;\u4f7f\u7528MD4\u7684\u6563\u5217\u51fd\u6570\u5904\u7406&#xff0c;\u751f\u6210\u7684\u4e00\u4e2a128\u4f4d\u7684hash\u503c&#xff0c;32\u4e2a\u5341\u516d\u8fdb\u5236&#xff09;<\/p>\n<h5>url-loader\u7684limit\u6548\u679c<\/h5>\n<p>\u5728\u5f00\u53d1\u4e2d\u6211\u4eec\u5f80\u5f80\u662f\u5c0f\u7684\u56fe\u7247\u9700\u8981\u8f6c\u6362&#xff0c;\u4f46\u662f\u5927\u7684\u56fe\u7247\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u56fe\u7247<\/p>\n<p>\u5c0f\u7684\u56fe\u7247\u8f6c\u6362base64\u4e4b\u540e\u53ef\u4ee5\u548c\u9875\u9762\u4e00\u8d77\u8bf7\u6c42&#xff0c;\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8bf7\u6c42\u8fc7\u7a0b<\/p>\n<p>\u5927\u7684\u56fe\u7247\u4e5f\u8fdb\u884c\u8f6c\u6362&#xff0c;\u53cd\u800c\u4f1a\u5f71\u54cd\u9875\u9762\u7684\u8bf7\u6c42\u901f\u5ea6<\/p>\n<p>\u6211\u4eec\u8981\u4e24\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0<\/p>\n<p>\u4e00&#xff1a;\u5c06type\u4fee\u6539\u4e3aasset<\/p>\n<p>\u4e8c&#xff1a;\u6dfb\u52a0\u4e00\u4e2aparser\u5c5e\u6027&#xff0c;\u5236\u5b9adataUrl\u7684\u6761\u4ef6&#xff0c;\u6dfb\u52a0maxsize\u5c5e\u6027<\/p>\n<p>const path &#061; require(&#039;path&#039;);<\/p>\n<p>module.exports &#061; {<br \/>\n  \/\/ \u8bbe\u7f6e\u6253\u5305\u7684\u6a21\u5f0f<br \/>\n  mode: &#039;development&#039;,<\/p>\n<p>  \/\/ \u8bbe\u7f6e\u5165\u53e3\u6587\u4ef6<br \/>\n  entry: &#039;.\/src\/component\/cpns.js&#039;,<\/p>\n<p>  \/\/ \u8bbe\u7f6e\u8f93\u51fa<br \/>\n  output: {<br \/>\n    filename: &#039;bundle.js&#039;,<br \/>\n    path: path.resolve(__dirname, &#039;dist&#039;),<br \/>\n    \/\/ assetModuleFilename: &#039;abc.png&#039;<br \/>\n  },<\/p>\n<p>  \/\/ \u914d\u7f6e\u6a21\u5757\u89c4\u5219<br \/>\n  module: {<br \/>\n    rules: [<br \/>\n      {<br \/>\n        test: \/\\\\.css$\/,<br \/>\n        use: [<br \/>\n          &#034;style-loader&#034;,<br \/>\n          &#034;css-loader&#034;,<br \/>\n          &#034;postcss-loader&#034;<br \/>\n        ]<br \/>\n      },{<br \/>\n        test: \/\\\\.less$\/,<br \/>\n        use:[ &#034;style-loader&#034;,&#034;css-loader&#034;,&#034;less-loader&#034;]<br \/>\n      },<br \/>\n      {<br \/>\n        test:\/\\\\.(png|jpe?g|svg|gif)$\/,<br \/>\n        type: &#039;asset\/resource&#039;,<br \/>\n        \/\/\u6253\u5305\u4e24\u5f20\u56fe\u7247&#xff0c;\u5e76\u4e14\u8fd9\u4e24\u5f20\u56fe,<br \/>\n        \/\/\u5c06\u56fe\u7247\u8fdb\u884cbase64\u7684\u7f16\u7801&#xff0c;\u5e76\u4e14\u76f4\u63a5\u7f16\u7801\u540e\u7684\u6e90\u7801\u653e\u5230\u6253\u5305\u7684js\u6587\u4ef6\u4e2d<br \/>\n        \/\/type:&#034;asset\/inline&#034;<br \/>\n        parser:{<br \/>\n          dataUrlCondition:{<br \/>\n            maxSize: 60 * 1024<br \/>\n          }<br \/>\n        },<br \/>\n        generator:{<br \/>\n          \/\/\u4e0d\u5199\u6b7b&#xff0c;\u4f7f\u7528\u5360\u4f4d\u7b26<br \/>\n          \/\/name:\u6307\u5411\u539f\u6765\u7684\u56fe\u7247\u540d\u79f0<br \/>\n          \/\/ext&#xff1a;\u6269\u5c55\u540d<br \/>\n          \/\/hash:webpack\u751f\u6210\u7684hash\u503c<br \/>\n          filename:&#034;img\/[name]_[hash:8][ext]&#034;<br \/>\n        }<br \/>\n      }<br \/>\n    ]<br \/>\n  }<br \/>\n}; <\/p>\n<p>\u8fd9\u6837\u8fdb\u884c\u6253\u5305&#xff0c;\u6253\u5305\u7ed3\u675f\u4e4b\u540e\u4f1a\u5728\u524d\u9762\u751f\u6210\u4e00\u4e2aimg\u6587\u4ef6\u5939<\/p>\n<h4>babel<\/h4>\n<p>\u5f00\u53d1\u4e2d\u60f3\u8981\u4f7f\u7528es6&#043;\u7684\u8bed\u6cd5&#xff0c;\u60f3\u8981\u4f7f\u7528typescript&#xff0c;\u5f00\u53d1React\u9879\u76ee&#xff0c;\u90fd\u662f\u79bb\u4e0d\u5f00babel\u7684<\/p>\n<p>\u6240\u4ee5\u5b66\u4e60babel\u5bf9\u4e8e\u6211\u4eec\u7406\u89e3\u4ee3\u7801\u7f16\u5199\u5230\u7ebf\u4e0a\u7684\u8f6c\u53d8\u8fc7\u7a0b\u5f88\u91cd\u8981<\/p>\n<p>babel\u662f\u4e00\u4e2a\u5de5\u5177\u94fe&#xff0c;\u4e3b\u8981\u7528\u4e8e\u65e7\u6d4f\u89c8\u5668\u6216\u8005\u73af\u5883\u4e2d\u5c06ECMAScript2015&#043;\u4ee3\u7801\u8f6c\u6362\u4e3a\u5411\u540e\u517c\u5bb9\u7248\u672c\u7684JS&#xff0c;\u5305\u62ec\u8bed\u6cd5\u8f6c\u6362\u548c\u6e90\u4ee3\u7801\u8f6c\u6362\u7b49<\/p>\n<h4>babel\u547d\u4ee4\u884c\u7684\u4f7f\u7528<\/h4>\n<p>babel\u672c\u8eab\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u5de5\u5177&#xff0c;\u4e0d\u548cwebpack\u7b49\u6784\u5efa\u5de5\u5177\u914d\u7f6e\u6765\u5355\u72ec\u4f7f\u7528<\/p>\n<p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5728\u547d\u4ee4\u884c\u5c1d\u8bd5\u4f7f\u7528babel&#xff0c;\u9700\u8981\u5b89\u88c5\u4e0b\u9762\u7684\u5e93&#xff1a;<\/p>\n<p>&#064;babel\/core&#xff1a;babel\u7684\u6838\u5fc3\u4ee3\u7801&#xff0c;\u5fc5\u987b\u5b89\u88c5<\/p>\n<p>&#064;babel\/cli&#xff1a;\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528babel<\/p>\n<p>npm install &#064;babel\/cli &#064;babel\/core -D <\/p>\n<p>\u4f7f\u7528babel\u6765\u5904\u7406\u6211\u4eec\u7684\u6e90\u4ee3\u7801<\/p>\n<p>src\u662f\u6e90\u6587\u4ef6\u7684\u76ee\u5f55<\/p>\n<p>&#8211;out-dir\u6307\u5b9a\u8981\u8f93\u51fa\u7684\u6587\u4ef6\u5939dist<\/p>\n<h4>\u63d2\u4ef6\u7684\u4f7f\u7528<\/h4>\n<p>\u6211\u4eec\u9700\u8981\u8f6c\u6362\u7bad\u5934\u51fd\u6570&#xff0c;\u5c31\u53ef\u4ee5\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u8f6c\u6362\u76f8\u5173\u7684\u63d2\u4ef6<\/p>\n<p>npm install &#064;babel\/plugin-transform-arrow-functions -D<br \/>\nnpx babel src &#8211;out-dir dist &#8211;plugins&#061;&#064;babel\/plugin-transform-arrow-functions <\/p>\n<p>\u67e5\u770b\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u6211\u4eec\u4f1a\u53d1\u73b0const\u6ca1\u6709\u8f6c\u6210var<\/p>\n<p>plugin-transform-arrow-function\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u6837\u7684\u529f\u80fd<\/p>\n<p>\u6211\u4eec\u9700\u8981\u4f7f\u7528plugin-transform-block-scoping\u6765\u5b8c\u6210\u8fd9\u6837\u7684\u529f\u80fd<\/p>\n<p>npm install &#064;babel\/plugin-transform-block-scoping -D<br \/>\nnpx babel src &#8211;out-dir &#8211;plugins&#061;&#064;babel\/plugin-transform-block-scoping,&#064;babel\/plugin-transform-arrow-functions <\/p>\n<p>\u5f53\u8981\u8f6c\u6362\u7684\u5185\u5bb9\u8fc7\u591a\u7684\u65f6\u5019&#xff0c;\u4e00\u4e2a\u4e00\u4e2a\u8bbe\u7f6e\u5c31\u6bd4\u8f83\u9ebb\u70e6\u4e86&#xff0c;\u53ef\u4ee5\u7528\u9884\u8bbe<\/p>\n<p>\u5b89\u88c5&#064;babel\/preset-env\u9884\u8bbe<\/p>\n<p>npm install &#064;babel\/preset-env -D<br \/>\nmodule.exports &#061; {<br \/>\nplugins: [<br \/>\n    &#034;babel\/plugin-transform-arrow-functions&#034;,<br \/>\n    &#034;babel\/plugin-transform-block-scoping&#034;,<br \/>\n],<br \/>\npresets:[<br \/>\n    &#034;&#064;babel\/preset-env&#034;<br \/>\n]<br \/>\n} <\/p>\n<p>\u5728\u63d0\u4f9b\u4e86preset\u4e4b\u540e&#xff0c;webpack\u5c31\u4f1a\u6839\u636e\u6211\u4eec\u7684\u9884\u8bbe\u6765\u52a0\u8f7d\u5bf9\u5e94\u7684\u63d2\u4ef6\u5217\u8868&#xff0c;\u5e76\u4e14\u5c06\u5176\u5ddd\u7b2c\u4e00\u7ed9babel<\/p>\n<p>\u5e38\u89c1\u7684\u9884\u8bbe\u6709&#xff1a;<\/p>\n<p>env<\/p>\n<p>React<\/p>\n<p>TypeScript<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"373\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619233154-68549deae715a.jpg\" width=\"661\" \/>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf26\u6b21\u3002webpack\u76f8\u5173\u63d2\u4ef6\u7684\u4ecb\u7ecd_webpack\u6253\u5305\u56fe\u7247<\/p>\n","protected":false},"author":2,"featured_media":44642,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3099,87,371],"topic":[],"class_list":["post-44644","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-webpack","tag-87","tag-371"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>webpack\u7ee7\u7eed\u5b66\u4e60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/44644.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"webpack\u7ee7\u7eed\u5b66\u4e60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf26\u6b21\u3002webpack\u76f8\u5173\u63d2\u4ef6\u7684\u4ecb\u7ecd_webpack\u6253\u5305\u56fe\u7247\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44644.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-19T23:31:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619233154-68549dea18a33.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44644.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44644.html\",\"name\":\"webpack\u7ee7\u7eed\u5b66\u4e60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-19T23:31:56+00:00\",\"dateModified\":\"2025-06-19T23:31:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44644.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44644.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44644.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"webpack\u7ee7\u7eed\u5b66\u4e60\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"webpack\u7ee7\u7eed\u5b66\u4e60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/44644.html","og_locale":"zh_CN","og_type":"article","og_title":"webpack\u7ee7\u7eed\u5b66\u4e60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.4k\u6b21\uff0c\u70b9\u8d5e26\u6b21\uff0c\u6536\u85cf26\u6b21\u3002webpack\u76f8\u5173\u63d2\u4ef6\u7684\u4ecb\u7ecd_webpack\u6253\u5305\u56fe\u7247","og_url":"https:\/\/www.wsisp.com\/helps\/44644.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-19T23:31:56+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619233154-68549dea18a33.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44644.html","url":"https:\/\/www.wsisp.com\/helps\/44644.html","name":"webpack\u7ee7\u7eed\u5b66\u4e60 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-19T23:31:56+00:00","dateModified":"2025-06-19T23:31:56+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44644.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44644.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44644.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"webpack\u7ee7\u7eed\u5b66\u4e60"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=44644"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44644\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44642"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44644"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}