简体   繁体   English

React构建多个文件

[英]React build multiple files

Is there any way how to split React/Redux application into modules during webpack build? 有什么方法可以在webpack构建过程中将React / Redux应用程序拆分为模块吗? Lets say I have some set of components related to user and others related to invoices. 可以说我有一些与用户有关的组件,而其他与发票有关的组件。 I would like webpack to build users.js and invoices.js which I could import to index.html and as I might change some user related components I would just swap users.js on production server leaving invoices.js untouched. 我希望webpack构建可以导入到index.html的users.js和invoices.js,并且由于我可能会更改一些与用户相关的组件,所以我只需要在生产服务器上交换users.js,而不会影响invoices.js。

Is such modularity posssible to implement? 这样的模块化可能实现吗? Many thanks for answers of any kind 非常感谢您的各种回答

What you are looking for is multiple entry points 您正在寻找的是多个入口点

In this example you have two (HTML) pages users and invoices. 在此示例中,您有两个(HTML)页面用户和发票。 You want to create individual bundles for each page. 您要为每个页面创建单独的捆绑包。 In addition to this you want to create a shared bundle that contains all modules used in both pages (assuming there are many/big modules in common). 除此之外,您还想创建一个共享包,其中包含两个页面中使用的所有模块(假设有很多相同的大模块)。 The pages also use Code Splitting to load a less used part of the features on demand. 这些页面还使用代码拆分功能按需加载功能较少的部分。

var path = require("path");
var webpack= require("webpack");
module.exports = {
    entry: {
        users: "./users",
        invoices: "./invoices"
    },
    output: {
        path: path.join(__dirname, "js"),
        filename: "[name].bundle.js",
        chunkFilename: "[id].chunk.js"
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            filename: "commons.js",
            name: "commons"
        })
    ]
}

here is an more detailed example from webpack itself 这是来自webpack本身的更详细的示例

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

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