简体   繁体   English

如何了解React-Router项目中的Webpack配置

[英]How to understand the webpack config in react-router project

I am learning react-router dynamic routing from this link https://github.com/ReactTraining/react-router/blob/master/docs/guides/DynamicRouting.md . 我正在从此链接https://github.com/ReactTraining/react-router/blob/master/docs/guides/DynamicRouting.md学习反应路由器动态路由。 The huge-apps project is the one I am looking into. 我正在研究大型应用程序项目。 I cloned the react-router git repo from https://github.com/ReactTraining/react-router and followed the instruction to set it up. 我从https://github.com/ReactTraining/react-router克隆了react-router git repo,并按照说明进行了设置。 Everything works fine here. 这里一切都很好。 But I don't understand some parts of the configuration in webpack configuration under examples directory. 但是我不理解examples目录下webpack配置中配置的某些部分。

Below is the output of the webpack config: 以下是webpack配置的输出:

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

I can see that all the output files are put under /__build__ directory. 我可以看到所有输出文件都放在/__build__目录下。 In huge-apps/index.html file, I can see it load the javascript files as below: 在huge-apps / index.html文件中,我可以看到它加载了javascript文件,如下所示:

<script src="/__build__/shared.js"></script>
<script src="/__build__/huge-apps.js"></script>

But I couldn't find the __build__ directory under the entire react-router project. 但是我找不到整个react-router项目下的__build__目录。 And I couldn't find the shraed.js and huge-apps.js file either. 而且我也找不到shraed.jshuge-apps.js文件。 I am confused about where webpack put these files. 我对webpack将这些文件放在何处感到困惑。 From the inspect on browser I can see it loads the javascript files from http://localhost:8080/ build /huge-apps.js . 从浏览器上的检查中,我可以看到它从http:// localhost:8080 / build /huge-apps.js加载了javascript文件。 Are they in memory only? 他们只是在记忆中吗?

The React Router examples use webpackDevMiddleware to handle requests to __build__ resources, which serves files from in-memory. React Router示例使用webpackDevMiddleware来处理对__build__资源的请求, __build__资源用于存储内存中的文件。

From server.js : server.js

app.use(webpackDevMiddleware(webpack(WebpackConfig), {
  publicPath: '/__build__/',
  stats: {
    colors: true
  }
}))

If you are running webpack indevelopment mode all the file get loaded in memory. 如果您以开发模式运行webpack,则所有文件都会加载到内存中。 to create the file in you need to add " ExtractTextPlugin " plugin in webpack 要在其中创建文件,需要在webpack中添加“ ExtractTextPlugin ”插件

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

  plugins: [
     ....
    new ExtractTextPlugin("[name].css"),
   ...
  ],

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

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