{"id":610,"date":"2018-01-02T22:49:32","date_gmt":"2018-01-02T14:49:32","guid":{"rendered":"https:\/\/sora.sound.moe\/?p=610"},"modified":"2018-01-02T22:49:32","modified_gmt":"2018-01-02T14:49:32","slug":"webpack-%e5%ae%9e%e7%8e%b0-react-router-%e7%bb%84%e4%bb%b6-code-splitting","status":"publish","type":"post","link":"https:\/\/sora.sound.moe\/?p=610","title":{"rendered":"Webpack \u5b9e\u73b0 React-Router \u7ec4\u4ef6 Code Splitting"},"content":{"rendered":"<p><!--more--><\/p>\n<p>\u53c2\u8003\uff1a<a href=\"https:\/\/segmentfault.com\/a\/1190000011128817\">https:\/\/segmentfault.com\/a\/1190000011128817<\/a><\/p>\n<p>\u8fd9\u91cc\u53ea\u662f\u5bf9\u8fd9\u7bc7\u6587\u7ae0\u505a\u4e00\u4e2a\u8865\u5145\u3002<\/p>\n<p>\u5e94\u6211\u524d\u6587\u7684\u64cd\u4f5c\uff0c\u6211\u5f15\u5165\u4e86<a href=\"https:\/\/github.com\/timarney\/react-app-rewired\">react-app-rewired<\/a>\u00a0\u6765\u4fee\u6539 Webpack \u914d\u7f6e\u3002<\/p>\n<p>\u90a3\u4e48\u63a5\u4e0b\u6765\u7ee7\u7eed\u5f15\u5165\u4e0a\u9762\u6587\u7ae0\u63d0\u5230\u7684 babel-plugin-syntax-dynamic-import \u63d2\u4ef6\u3002<\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528 react-app-rewired \u7684\u00a0<span style=\"font-size: 1rem;\">injectBabelPlugin \u51fd\u6570\u5b8c\u6210\u3002<\/span><\/p>\n<p>\u6700\u540e\u7ed3\u679c\u5927\u6982\u662f\u8fd9\u6837\u3002<\/p>\n<pre class=\"lang:js decode:true \">const tsImportPluginFactory = require('ts-import-plugin')\nconst { getLoader, injectBabelPlugin } = require(\"react-app-rewired\");\nconst path = require('path');\n\nfunction resolve(dir) {\n    return path.join(__dirname, '.', dir)\n}\n\nmodule.exports = function override(config, env) {\n    config.resolve.alias = {\n        ...config.resolve.alias,\n        '@': resolve('src')\n    }\n\n    config = injectBabelPlugin('syntax-dynamic-import', config);\n\n    const tsLoader = getLoader(\n        config.module.rules,\n        rule =&gt;\n            rule.loader &amp;&amp;\n            typeof rule.loader === 'string' &amp;&amp;\n            rule.loader.includes('ts-loader')\n    );\n\n    tsLoader.options = {\n        getCustomTransformers: () =&gt; ({\n            before: [tsImportPluginFactory({\n                libraryName: 'antd',\n                libraryDirectory: 'es',\n                style: 'css',\n            })]\n        })\n    };\n\n    return config;\n}<\/pre>\n<p>\u7136\u540e\u5b89\u88c5 react-loadable \uff0c\u4fee\u6539\u8def\u7531\u8bbe\u7f6e\u3002<\/p>\n<p>\u672c\u6765\u662f\u8db3\u591f\u4f18\u96c5\u4e86\uff0c\u4f46\u662f\u7ec4\u4ef6\u52a0\u8f7d\u7684\u65f6\u5019\u6211\u4eec\u6700\u597d\u8fd8\u662f\u7ed9\u7528\u6237\u4e00\u70b9\u53cd\u9988\uff0c\u4e8e\u662f\u81ea\u7136\u60f3\u5230\u4e86\u5f15\u5165 Nprogress \u3002<\/p>\n<p>\u672c\u6765\u662f\u60f3\uff0c\u5982\u679c\u5728\u6bcf\u4e00\u4e2a\u52a8\u6001 import \u7684\u5730\u65b9\u90fd\u5728\u524d\u540e\u5e94\u7528 Nprogress\uff0c\u672a\u514d\u4f1a\u6709\u592a\u591a\u91cd\u590d\u4ee3\u7801\uff0c\u4e8e\u662f\u5199\u4e86\u4e0b\u9762\u7684\u51fd\u6570\u6765\u5c01\u88c5\u4e00\u4e0b<\/p>\n<pre class=\"lang:default decode:true \">const loadComponent = (path: string) =&gt; {\n  const promise = import(path);\n  nprogress.start();\n  promise.then(() =&gt; {\n    nprogress.done();\n  });\n  return promise;\n}<\/pre>\n<p>\u7136\u540e\u53d1\u73b0\uff0c\u4e0d\u8d77\u4f5c\u7528\uff0c\u63d0\u793a\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u7ec4\u4ef6\u3002<\/p>\n<p>\u76f4\u63a5\u7528import\u5f15\u5165\u53ef\u884c\uff0c\u8fd9\u4e2a\u5c01\u88c5\u4e86\u4e00\u5c42\u7684\u51fd\u6570\u4e0d\u884c\u3002<\/p>\n<p>\u5751\u4e86\u4e00\u4f1a\u513f\u4e4b\u540e\u627e\u5230\u5982\u4e0b\u63cf\u8ff0<\/p>\n<blockquote>\n<h3>Dynamic imports and webpack<\/h3>\n<p>Although the specification for\u00a0<code>import()<\/code>\u00a0supports a dynamic importing of modules in the browser runtime, webpack&#8217;s\u00a0<code>require.ensure()<\/code>\u00a0is not dynamic and requires a hardcoded string to work correctly. For more information see\u00a0<a href=\"https:\/\/webpack.github.io\/docs\/context.html#dynamic-requires\">webpack&#8217;s documentation<\/a>\u00a0on dynamic requires.<\/p><\/blockquote>\n<p>\u597d\u597d\u597d\u3002\u4e0d\u652f\u6301\u662f\u5427\u3002\u884c\u3002\u8d70\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u5751\u653e\u7740\uff0c\u4ee5\u540e\u518d\u770b\u770b\u548b\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"twentyseventeen-featured-image":false,"twentyseventeen-thumbnail-avatar":false},"uagb_author_info":{"display_name":"Sora","author_link":"https:\/\/sora.sound.moe\/?author=1"},"uagb_comment_info":0,"uagb_excerpt":null,"_links":{"self":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/610"}],"collection":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=610"}],"version-history":[{"count":0,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions"}],"wp:attachment":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}