首页 文章

React构建多个文件

提问于
浏览
0

有没有办法在webpack构建期间如何将React / Redux应用程序拆分为模块?假设我有一些与用户相关的组件以及与发票相关的其他组件 . 我想webpack构建users.js和invoices.js,我可以导入到index.html,因为我可能会更改一些与用户相关的组件,我只是在 生产环境 服务器上交换users.js,而不会触及invoices.js .

是否可以实现这种模块化?非常感谢任何答案

1 回答

  • 1

    What you are looking for is multiple entry points

    在此示例中,您有两个(HTML)页面用户和发票 . 您想为每个页面创建单独的包 . 除此之外,您还要创建一个共享包,其中包含两个页面中使用的所有模块(假设共有许多/大模块) . 这些页面还使用Code Splitting按需加载较少使用的部分功能 .

    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"
            })
        ]
    }
    

    这里是webpack本身更详细的example

相关问题