{"id":284,"date":"2021-08-23T21:18:21","date_gmt":"2021-08-23T13:18:21","guid":{"rendered":"http:\/\/gjweb.top\/?p=284"},"modified":"2021-08-23T21:18:22","modified_gmt":"2021-08-23T13:18:22","slug":"webapck-%e5%8a%9f%e8%83%bd%e6%80%bb%e7%bb%93%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=284","title":{"rendered":"webapck \u529f\u80fd\u603b\u7ed3\uff08\u4e00\uff09"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>\u81ea\u52a8\u517c\u5bb9 css \u524d\u7f00 postcss-loader postcss-preset-env<\/li><li>\u9759\u6001\u8d44\u6e90\u5185\u8054 raw-loader<\/li><li>\u79fb\u52a8\u7aefcss PX \u81ea\u52a8\u8f6c\u6362\u4e3a rem<\/li><li>\u591a\u9875\u9762\u901a\u7528\u6253\u5305\u65b9\u6848<\/li><li>\u5feb\u901f\u5b9a\u4f4d\u6e90\u7801<\/li><li>\u57fa\u7840\u5305\u548c\u516c\u5171\u811a\u672c\u5206\u79bb<\/li><li>tree shaking (\u6447\u6811\u4f18\u5316)<\/li><li>scope hoisting<\/li><li>\u4ee3\u7801\u5206\u5272\u548c\u52a8\u6001import<\/li><li>webpack\u96c6\u6210eslint<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u52a8\u517c\u5bb9css\u524d\u7f00<\/h2>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install postcss-loader postcss-preset-env --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>module.exports = {<br> &nbsp; &nbsp;module:{<br> &nbsp; &nbsp; &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test: \/.less$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;use: &#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MiniCssExtractPlugin.loader,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'css-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'less-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader: 'postcss-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;options: {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;postcssOptions:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;plugins:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"postcss-preset-env\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ident: \"postcss\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  },<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ] <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e\u5b8c\u6210\u540e\u518d\u6839\u76ee\u5f55\u521b\u5efa .browserslistrc \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>last 2 version<br> &nbsp;  &gt; 1%<br> &nbsp;  iOS &gt;= 7<br> &nbsp;  Android &gt; 4.1<br> &nbsp;  Firefox &gt; 20<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u9759\u6001\u8d44\u6e90\u5185\u8054<\/h2>\n\n\n\n<p>\u9759\u6001\u8d44\u6e90\u5185\u8054\u6307\u7684\u662f\uff0c\u628a JS \u6216\u8005 css\\ \u56fe\u7247 \\ \u5b57\u4f53\u7b49\u4ee3\u7801\u5185\u5d4c\u5230HTML\u4e2d\u53bb\u3002\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u516c\u5171\u90e8\u5206\u4f7f\u7528<\/p>\n\n\n\n<p>\u4f18\u70b9\uff1a \u51cf\u5c11 HTTP \u7f51\u7edc\u8bf7\u6c42\u6570 \u3001 css\u5185\u655b\u907f\u514d\u9875\u9762\u95ea\u52a8<\/p>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install raw-loader@5.0.1 --save-dev<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e \u5728 HTML \u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;%= require(\"raw-loader!.\/xxx.html\")%&gt;<br>&lt;script&gt;<br>    &lt;%= require(\"raw-loader!babel-loader!..\/node_modules\/lib-flexible\/flexible.js\")%&gt;<br>&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u79fb\u52a8\u7aef CSS px \u81ea\u52a8\u8f6c\u6362\u4e3a rem<\/h2>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install px2rem-loader --save-dev<br>npm install lib-flexible --save<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {<br> &nbsp; &nbsp;modele:{<br> &nbsp; &nbsp; &nbsp; &nbsp;rules:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;test:\/.less$\/,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;use:&#91;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loader:'px2rem-loader',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;optins:{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;remUnit:75, \/\/ 1rem = 75rpx<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;remPrecesion:8 \/\/ px \u8f6c rem \u5c0f\u6570\u70b9\u4f4d\u6570<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  ]<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<p>\u5728 HTML\u9875\u9762\u4e2d\u9759\u6001\u5f15\u5165 lib-flexible<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;head&gt;<br>    &lt;script&gt;&lt;%=require('raw-loader!babel-loader!..\/node_modules\/lib-flexible\/flexible.js')%&gt;&lt;\/script&gt;<br>&lt;\/head&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u591a\u9875\u9762\u901a\u7528\u6253\u5305\u65b9\u6848<\/h2>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install glob html-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 glob = require(\"glob\")<br>const HtmlWebpackPlugin = require('html-webpack-plugin');<br>const setMPA = () =&gt; {<br>  const entry = {};<br>  const htmlWebpackPlugins = &#91;];<br>  const entryFiles = glob.sync(path.join(__dirname, '.\/src\/*\/index.js'))<br>  Object.keys(entryFiles).map(index =&gt; {<br>      const entryFile = entryFiles&#91;index];<br>      const match = entryFile.match(\/src\\\/(.*)\\\/index\\.js\/)<br>      const pageName = match &amp;&amp; match&#91;1];<br>      entry&#91;pageName] = entryFile;<br>      htmlWebpackPlugins.push(<br>        new HtmlWebpackPlugin({<br>          template: path.join(__dirname, `src\/${pageName}\/index.html`),<br>          filename: `${pageName}.html`,<br>          chunks: &#91;'vendors', pageName],<br>        })<br>      )<br>  })<br>  return {<br>    entry,<br>    htmlWebpackPlugins<br>  }<br>}<br>const { entry, htmlWebpackPlugins } = setMPA()<br>module.exports = {<br>    entry:entry,<br>    output: {<br>        path: path.join(__dirname, 'dist'),<br>        filename: '&#91;name]_&#91;chunkhash:8].js'<br>    },<br>    plugins:&#91;].concat(htmlWebpackPlugins)<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5feb\u901f\u5b9a\u4f4d\u6e90\u7801<\/h2>\n\n\n\n<p><strong>\u6ce8\u610f: \u5f00\u53d1\u73af\u5883\u5f00\u542f,\u7ebf\u4e0a\u73af\u5883\u9700\u8981\u5173\u95ed\u6b64\u529f\u80fd. \u907f\u514d\u5bfc\u81f4\u4e1a\u52a1\u4ee3\u7801\u7684\u6cc4\u9732<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528 sourcemap \u5b9a\u4f4d\u6e90\u7801<\/p>\n\n\n\n<p>source map \u5173\u952e\u5b57<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u5173\u952e\u5b57<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>eval<\/td><td>\u4f7f\u7528 eval \u5305\u88f9\u819c\u5757\u4ee3\u7801<\/td><\/tr><tr><td>source map<\/td><td>\u4ea7\u751f .map \u6587\u4ef6<\/td><\/tr><tr><td>cheap<\/td><td>\u4e0d\u5305\u542b\u5217\u4fe1\u606f<\/td><\/tr><tr><td>inline<\/td><td>\u5c06 .map \u4f5c\u4e3a DataURI \u5d4c\u5165, \u4e0d\u5355\u72ec\u751f\u6210.map\u6587\u4ef6<\/td><\/tr><tr><td>module<\/td><td>\u5305\u542b loader \u7684 sourcemap<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>source map \u7c7b\u578b<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>devtool<\/th><th>\u662f\u5426\u9002\u5408\u751f\u4ea7\u73af\u5883<\/th><th>\u53ef\u4ee5\u5b9a\u4f4d\u7684\u4ee3\u7801<\/th><\/tr><\/thead><tbody><tr><td>(none)<\/td><td>yes<\/td><td>\u6700\u7ec8\u8f93\u51fa\u7684\u4ee3\u7801<\/td><\/tr><tr><td>eval<\/td><td>no<\/td><td>webpack \u751f\u6210\u7684\u4ee3\u7801 (\u4e00\u4e2a\u4e2a\u7684\u6a21\u5757)<\/td><\/tr><tr><td>cheap-eval-source-map<\/td><td>no<\/td><td>\u7ecf\u8fc7 loader \u8f6c\u6362\u540e\u7684\u4ee3\u7801(\u53ea\u80fd\u770b\u5230\u884c)<\/td><\/tr><tr><td>cheap-module-eval-source-map<\/td><td>no<\/td><td>\u6e90\u4ee3\u7801(\u53ea\u80fd\u770b\u5230\u884c)<\/td><\/tr><tr><td>eval-source-map<\/td><td>no<\/td><td>\u6e90\u4ee3\u7801<\/td><\/tr><tr><td>cheap-source-map<\/td><td>yes<\/td><td>\u7ecf\u8fc7loader\u8f6c\u6362\u540e\u7684\u4ee3\u7801(\u53ea\u80fd\u770b\u5230\u884c)<\/td><\/tr><tr><td>cheap-cheap-source-map<\/td><td>yes<\/td><td>\u6e90\u4ee3\u7801(\u53ea\u80fd\u770b\u5230\u884c)<\/td><\/tr><tr><td>inline-cheap-source-map<\/td><td>no<\/td><td>\u7ecf\u8fc7loader\u8f6c\u6362\u540e\u7684\u4ee3\u7801(\u53ea\u80fd\u770b\u5230\u884c)<\/td><\/tr><tr><td>inlline-cheap-module-source-map<\/td><td>no<\/td><td>\u6e90\u4ee3\u7801(\u53ea\u770b\u5230\u884c)<\/td><\/tr><tr><td>source-map<\/td><td>yes<\/td><td>\u6e90\u4ee3\u7801<\/td><\/tr><tr><td>inline-source-map<\/td><td>no<\/td><td>\u6e90\u4ee3\u7801<\/td><\/tr><tr><td>hidden-source-map<\/td><td>yes<\/td><td>\u6e90\u4ee3\u7801<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u914d\u7f6e webpack.confing.js \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'use strict'<br>module.exports = {<br>    devtool:\"source-map\"<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u57fa\u7840\u5305\u548c\u516c\u5171\u811a\u672c\u5206\u79bb<\/h2>\n\n\n\n<p>webpack4\u5185\u7f6e\u4e86 SplitChunksPlugin \u66ff\u4ee3 webpack3\u4e2d\u7684 CommonsChunkPlugin\u63d2\u4ef6,\u4e5f\u53ef\u4ee5\u4f7f\u7528 html-webpack-externals-plugin\u8fdb\u884c\u57fa\u7840\u5305\u7684\u5206\u79bb<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">CommonsChunkPlugin<\/h3>\n\n\n\n<p>chunk\u53c2\u6570\u8bf4\u660e<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u53c2\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>.async<\/td><td>\u5f02\u6b65\u5f15\u5165\u7684\u5e93\u8fdb\u884c\u5206\u79bb(\u9ed8\u8ba4)<\/td><\/tr><tr><td>.initial<\/td><td>\u540c\u6b65\u5f15\u5165\u7684\u5e93\u8fdb\u884c\u5206\u79bb<\/td><\/tr><tr><td>.all<\/td><td>\u6240\u6709\u5f15\u5165\u7684\u5e93\u8fdb\u884c\u5206\u79bb(\u63a8\u8350)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">\u6253\u5305\u57fa\u7840\u5305\u5206\u79bb<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {<br>    \/\/ \u9700\u8981\u5728HtmlWebpackPlugin\u7684chunks\u4e2d\u52a0\u5165'vendors'<br>    optimization:{<br>        splitChunks:{<br>          cacheGroups:{<br>            commons:{<br>              test:\/(react|react-dom)\/,<br>              name:'vendors',<br>              chunks:'all'<br>            }<br>          }<br>        }<br>    }<br>}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6253\u5305\u5206\u79bb\u516c\u5171\u4ee3\u7801<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {<br>    \/\/ \u9700\u8981\u5728HtmlWebpackPlugin\u7684chunks\u4e2d\u52a0\u5165'commons'<br>    optimization:{<br>        splitChunks:{<br>            minSize:0, \/\/ \u9700\u8981\u5206\u79bb\u7684\u6587\u4ef6\u5927\u5c0f<br>            cacheGroups:{<br>                commons:{<br>                    name: 'commons',<br>                    chunks: 'all',<br>                    minChunks: 2, \/\/ \u6587\u4ef6\u5f15\u5165\u7684\u6570\u91cf<br>                }<br>            }<br>            <br>        }<br>    }<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">html-webpack-externals-plugin<\/h3>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install html-webpack-externals-plugin --save-dev<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const HtmlWebpackExternalsPlugin = require(\"html-webpack-externals-plugin\")<br>module.exports = {<br>    plugins:&#91;<br>        new HtmlWebpackExternalsPlugin({<br>          externals: &#91;<br>            {<br>              module: 'react',<br>              entry: 'https:\/\/now8.gtimg.com\/now\/lib\/16.2.0\/react.min.js',<br>              global: 'React',<br>            },<br>            {<br>              module: 'react-dom',<br>              entry: 'https:\/\/now8.gtimg.com\/now\/lib\/16.2.0\/react-dom.min.js',<br>              global: 'ReactDOM',<br>            },<br>          ],<br>        })<br>    ]<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">tree shaking (\u6447\u6811\u4f18\u5316)<\/h2>\n\n\n\n<p>\u4e00\u4e2a\u6a21\u5757\u53ef\u80fd\u6709\u591a\u4e2a\u65b9\u6cd5,\u53ea\u8981\u5176\u4e2d\u7684\u67d0\u4e2a\u65b9\u6cd5\u88ab\u4f7f\u7528,\u5219\u6574\u4e2a\u6587\u4ef6\u90fd\u4f1a\u88ab\u6253\u5305, \u4f7f\u7528 tree shaking \u53ea\u4f1a\u628a\u7528\u5230\u7684\u65b9\u6cd5\u8fdb\u884c\u6253\u5305, \u5176\u4ed6\u65b9\u6cd5\u4f1a\u88ab\u64e6\u9664. <strong>\uff08\u6ce8\u610f\uff1a\u5fc5\u987b\u662fES6\u7684\u8bed\u6cd5\uff09<\/strong><\/p>\n\n\n\n<p>webpack \u5728 production \u6a21\u5f0f\u4e0b\u81ea\u52a8\u5f00\u542f tree-shaking<\/p>\n\n\n\n<p>\u914d\u7f6e .babelrc \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{<br>    \"modules\":false<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">scope hoisting<\/h2>\n\n\n\n<p>\u73b0\u8c61\uff1a webpack\u5728\u6784\u5efa\u4ee3\u7801\u7684\u65f6\u5019\u662f\u4f1a\u5b58\u5728\u5927\u91cf\u7684\u95ed\u5305\u4ee3\u7801\uff0c \u8fd9\u4e9b\u95ed\u5305\u4f1a\u5bfc\u81f4\u6784\u5efa\u6587\u4ef6\u4f53\u79ef\u589e\u5927\uff0c\u51fd\u6570\u4f5c\u7528\u57df\u53d8\u591a\uff0c\u5185\u5b58\u5f00\u9500\u4e5f\u4f1a\u589e\u5927\u3002<\/p>\n\n\n\n<p>\u89e3\u51b3\uff1a\u4f7f\u7528scopr hoisting<\/p>\n\n\n\n<p>\u539f\u7406\uff1a\u5c06\u6240\u6709\u7684\u6a21\u5757\u4ee3\u7801\u6309\u7167\u5f15\u5165\u987a\u5e8f\u653e\u5728\u4e00\u4e2a\u51fd\u6570\u4f5c\u7528\u57df\u91cc\uff0c\u7136\u540e\u9002\u5f53\u7684\u91cd\u547d\u540d\u4e00\u4e9b\u53d8\u91cf\u4e00\u9632\u6b62\u53d8\u91cf\u540d\u51b2\u7a81\uff1b \u901a\u8fc7 scopr hoidting\u53ef\u4ee5\u51cf\u5c11\u51fd\u6570\u58f0\u660e\u4ee3\u7801\u548c\u5185\u5b58\u7684\u5f00\u9500 <strong>\uff08\u6ce8\u610f\uff1a\u5fc5\u987b\u662fES6\u7684\u8bed\u6cd5\uff09<\/strong><\/p>\n\n\n\n<p>\u4f7f\u7528\uff1awebpack4\u4ee5\u4e0a \u5728 production \u6a21\u5f0f\u4e0b\u81ea\u52a8\u5f00\u542f<\/p>\n\n\n\n<p>\u5728 webpack3 \u4e2d\u9700\u8981\u5728 webpack.config \u4e2d\u914d\u7f6e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {<br>    plugins:&#91;<br>        new webpack.optimize.ModuleConcatenationPlugin()<br>    ]<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801\u5206\u5272\u548c\u52a8\u6001 import<\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u8f83\u5927\u7684 web \u5e94\u7528\u6765\u8bb2\uff0c\u5c06\u6240\u6709\u7684\u4ee3\u7801\u90fd\u5728\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u662f\u4e0d\u591f\u6709\u6548\u7684\uff0c\u7279\u522b\u662f\u5f53\u67d0\u4e9b\u4ee3\u7801\u5feb\u662f\u5728\u67d0\u4e9b\u7279\u6b8a\u7684\u65f6\u5019\u624d\u4f1a\u88ab\u4f7f\u7528\u5230\u7684\u65f6\u5019\u3002webpack \u63d0\u4f9b\u4e86\u5c06\u4ee3\u7801\u5206\u5272\u6210 chunks\uff08\u8bed\u5757\uff09\uff0c\u5f53\u4ee3\u7801\u8fd0\u884c\u5230\u9700\u8981\u4ed6\u4eec\u7684\u65f6\u5019\u5728\u8fdb\u884c\u52a0\u8f7d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u9002\u7528\u7684\u573a\u666f<ul><li>\u62bd\u79bb\u76f8\u540c\u4ee3\u7801\u5230\u4e00\u4e2a\u5171\u4eab\u5757<\/li><li>\u811a\u672c\u61d2\u52a0\u8f7d, \u4f7f\u5f97\u521d\u59cb\u4e0b\u8f7d\u7684\u4ee3\u7801\u66f4\u5c0f<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u52a8\u6001import<\/h3>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install @babel\/plugin-syntax-dynamic-import --save-dev<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e .babelrc<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{<br>    \"plugins\":&#91;<br>        \"@babel\/plugin-syntax-dynamic-import\"<br>    ]<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">webpack\u96c6\u6210 eslint<\/h2>\n\n\n\n<p>webpack \u53ef\u4ee5\u4f7f\u7528\u6700\u5168\u7684 eslint-config-airbnb \u8fdb\u884c\u8bed\u6cd5\u89c1\u68c0\u67e5\uff0c\u4e5f\u53ef\u4f7f\u7528 eslint-config-ivweb \u505a\u8bed\u6cd5\u68c0\u67e5\uff1b\u524d\u8005\u7684\u8bed\u6cd5\u68c0\u67e5\u6700\u5168\uff0c\u4f53\u79ef\u4e5f\u76f8\u5bf9\u8f83\u5927\u3002\u540e\u8005\u662f\u7531\u817e\u8bafNOW \u56e2\u961f\u5b9a\u5236\u7684\u8bed\u6cd5\u89c4\u8303\uff0c\u8986\u76d6\u76f8\u5bf9\u8f83\u5c11\uff0c\u4f46\u4f53\u79ef\u4e5f\u76f8\u5bf9\u8f83\u5c0f\u3002<\/p>\n\n\n\n<p>\u5b89\u88c5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install eslint eslint-plugin-import eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y eslint-config-airbnb babel-eslint --save-dev<\/code><\/pre>\n\n\n\n<p>\u914d\u7f6e webpack.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {<br>    module:{<br>        rules:&#91;<br>            {<br>                test:\/\\.js$\/,<br>                use:&#91;<br>                    'babel-loader',<br>                    'eslint-loader'<br>                ]<br>            }<br>        ]<br>    }<br>}<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa .eslintrc.js \u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = {<br>    \"parser\":\"babel-eslint\",<br>    \"extends\":\"airbnb\", \/\/ \u7ee7\u627fairbnb\uff0c\u53ef\u4ee5\u591a\u9009&#91;\"xxx\",\"xxx\"]<br>    \"env\":{<br>        \"browser\":true,<br>        \"node\":true<br>    },<br>    \"rules\":{ \/\/ \u89c4\u5219\u81ea\u5b9a\u4e49<br>        \"indent\":&#91;'error',2]<br>    }<br>}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/feflow\/eslint-config-ivweb\/blob\/master\/docs\/RULE.md\">\u9644\u8d60\u817e\u8baf\u6574\u7406\u7684\u89c4\u5219\u5217\u8868<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u52a8\u517c\u5bb9 css \u524d\u7f00 postcss-loader postcss-preset-env \u9759\u6001\u8d44\u6e90\u5185\u8054 ra [&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-284","post","type-post","status-publish","format-standard","hentry","category-webpack"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/284","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=284"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/284\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}