简体   繁体   English

如何理解webpack块生成块js文件以响应动态路由?

[英]How to understand how webpack chunk generating chunk js files for react dynamic routing?

I want to split my react javascript source code into small chunks files in order to do dynamic routing. 我想将我的react javascript源代码拆分为小块文件,以便进行动态路由。

In order to understand how dynamic routing work, I cloned react-router source code from https://github.com/ReactTraining/react-router . 为了了解动态路由的工作原理,我从https://github.com/ReactTraining/react-router克隆了react-router源代码。 And run webpack command under examples. 并在示例下运行webpack命令。 I can see that many files got generated under build directory as below: 我可以看到在构建目录下生成了许多文件,如下所示:

$ webpack
Hash: 5c9ad6c3ccf9f6bc5fcb
Version: webpack 1.13.3
Time: 4959ms
                               Asset     Size  Chunks             Chunk Names
                         19.chunk.js  1.42 kB      19  [emitted]  
                     active-links.js  9.91 kB       0  [emitted]  active-links
                        auth-flow.js  19.4 kB       2  [emitted]  auth-flow
auth-flow-async-with-query-params.js  10.4 kB       3  [emitted]  auth-flow-async-with-query-params
            auth-with-shared-root.js  21.9 kB       4  [emitted]  auth-with-shared-root
                          5.chunk.js  1.84 kB       5  [emitted]  
                          6.chunk.js  1.39 kB       6  [emitted]  
                          7.chunk.js  5.83 kB       7  [emitted]  
                          8.chunk.js  1.52 kB       8  [emitted]  
                          9.chunk.js  2.39 kB       9  [emitted]  
                         10.chunk.js  2.35 kB      10  [emitted]  
                         11.chunk.js  1.42 kB      11  [emitted]  
                         12.chunk.js  1.44 kB      12  [emitted]  
                      breadcrumbs.js  37.3 kB      13  [emitted]  breadcrumbs
            confirming-navigation.js  9.29 kB      14  [emitted]  confirming-navigation
                 dynamic-segments.js  7.25 kB      15  [emitted]  dynamic-segments
                        huge-apps.js  33.7 kB      16  [emitted]  huge-apps
                         17.chunk.js  3.56 kB      17  [emitted]  
                         18.chunk.js  5.21 kB      18  [emitted]  
                       animations.js   222 kB       1  [emitted]  animations
                         20.chunk.js  3.95 kB      20  [emitted]  
                         21.chunk.js  7.59 kB      21  [emitted]

From above output, you can see there are many chunk files got generated. 从上面的输出中,您可以看到生成了许多块文件。 In the examples/webpack.config.js file, I can see below configuration: 在examples / webpack.config.js文件中,我可以看到以下配置:

output: {
    path: __dirname + '/__build__',
    filename: '[name].js',
    chunkFilename: '[id].chunk.js',
    publicPath: '/__build__/'
  },

Is this the only place to configure chunk files? 这是配置块文件的唯一地方吗? What is the logic to split these chunk files? 拆分这些块文件的逻辑是什么? When I change the router, how does the server know which chunk file need to be downloaded? 当我更换路由器时,服务器如何知道需要下载哪个块文件?

When I change the router, how does the server know which chunk file need to be downloaded? 当我更换路由器时,服务器如何知道需要下载哪个块文件?

Not exactly sure about the specific chunkfiles, but Webpack compiles all of your js files into one file. 不确定具体的块文件,但是Webpack将所有js文件编译为一个文件。 Your index.html (in the same folder) is where your React components are rendered and also has a reference to that one file (bundle.js). 您的index.html(在同一文件夹中)是您的React组件的呈现位置,并且还具有对该文件(bundle.js)的引用。

From the servers perspective, when you go to your site, depending on how your routes are set up, you are always sent to your '/' route, which will then utilize your client side routing via react-router. 从服务器的角度来看,当您转到站点时,根据路由的设置方式,始终将您发送到“ /”路由,然后该路由将通过react-router利用客户端路由。

I found these resources really helpful in learning about react-router / client vs server side routing. 我发现这些资源对于学习反应路由器/客户端与服务器端路由非常有帮助。 Hope this is helpful. 希望这会有所帮助。

https://github.com/reactjs/react-router-tutorial React-router urls don't work when refreshing or writting manually https://github.com/reactjs/react-router-tutorial 手动刷新或手动编写时,React-router url不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM