简体   繁体   English

Webpack加载代码动态拆分包

[英]Webpack loading code split bundles on the fly

So I want to split my application code into 3 bundles using webpack. 所以我想使用webpack将我的应用程序代码分成3个包。 I'm able to get my framework bundle loading and working, but I can't get the other two bundles to load on the fly. 我能够让我的框架包加载和工作,但我无法动态加载其他两个包。 Heres my webpack config: 继承我的webpack配置:

var fs      = require('fs'),
    path    = require('path'),
    webpack = require('webpack'),
    apps = fs.readdirSync('./app');
    appModuleRegex = new RegExp("^(" + apps.join('|') + ")/(.*)$");

module.exports = {
    context: 'app',
    entry: {
        framework: 'framework/app',
        operations: 'operations/app',
        platform: 'platform/app'
    },
    output: {
        path: '/web/dist/',
        filename: '[name].entry.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /\.test.js$/,
                loader: 'babel-loader'
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            }
        ]
    },
    resolveLoader: {
        root: path.join(process.cwd(), "node_modules")
    },
    resolve: {
        modulesDirectories: ['node_modules', 'bower_components'],
    },
    plugins: [
        new webpack.NormalModuleReplacementPlugin(appModuleRegex, function(result) {
            result.request = result.request.replace(appModuleRegex, "/web/app/$1/src/$2");
        }),
        new webpack.NormalModuleReplacementPlugin(/fetch/, function(result) {
            console.log("TRANSFORMING fetch");
            result.request = 'isomorphic-fetch';
        }),
        // new webpack.optimize.CommonsChunkPlugin("commons.js")
    ],
    devServer: {
        contentBase: '/web/dist/'
    }
}

And basically I'm using react router to try and load the other app bundles if the URL matches them: 基本上我正在使用react路由器尝试加载其他应用程序包,如果URL匹配它们:

class NotFound extends React.Component {
    render () {
        return null;
    }

    componentDidMount () {
        var path = this.context.router.getCurrentPath(),
            appName = path.split('/')[1];

        if (appName === 'operations') {
            require('./operations.entry.js');
        }
        else if (appName === 'platform') {
            require('./platform.entry.js');
        }
    }
}

But webpack doesn't seem to like this pattern: 但webpack似乎不喜欢这种模式:

weave_1 | ERROR in ./app/framework/src/app.js
weave_1 | Module not found: Error: Cannot resolve 'file' or 'directory' ./operations.entry.js in /web/app/framework/src
weave_1 |  @ ./app/framework/src/app.js 131:16-45
weave_1 | 
weave_1 | ERROR in ./app/framework/src/app.js
weave_1 | Module not found: Error: Cannot resolve 'file' or 'directory' ./platform.entry.js in /web/app/framework/src
weave_1 |  @ ./app/framework/src/app.js 136:16-43

However I can see in my dist directory this structure: 但是我可以在我的dist目录中看到这个结构:

css/                 
index.html           
media/               
platform.entry.js
framework.entry.js   
js/                  
operations.entry.js

So I would think it would be fine. 所以我认为没关系。 index.html is loading the framework.entry.js code. index.html正在加载framework.entry.js代码。 Any ideas? 有任何想法吗?

So the hacky way I ended up doing this was by just injecting the other bundles on the fly: 所以我最终做到这一点的黑客方式就是直接注入其他捆绑包:

class NotFound extends React.Component {
    render () {
        return null;
    }

    componentDidMount () {
        var path = this.context.router.getCurrentPath(),
            splitPath = path.split('/'),
            appName = splitPath[1];

         (function(d, script) {
             script = d.createElement('script');
             script.type = 'text/javascript';
             script.async = true;
             script.src = appName + '.bundle.js';
             d.getElementsByTagName('head')[0].appendChild(script);
         }(document));
    }
}

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

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