onGenerateChunk (modules, chunk, outputOptions, config, externalImports) {
        let files = Object.keys(chunk.modules).map(filePath => {
            let file = modules[filePath];
            return file.index + ':' + file.code;
        });  

        let entryIndex = modules[chunk.facadeModuleId].index;
        let { format } = outputOptions;
        let plugins = config.plugins || [];

        if (chunk.isDynamicEntry) {
            return `
                ${format === 'amd'? 'define(function (require, exports) {' : ''}
                ${createExternalImports(chunk, outputOptions, externalImports)}
                (function (global) {
                    global.__nollup_dynamic_require_callback("${chunk.fileName}", ${entryIndex}, {${files}});
                })(typeof globalThis !== 'undefined'? globalThis : (
                typeof self !== 'undefined' ? self : this
                ));
                ${format === 'amd'? '});' : ''}
            `;
        } else {
            return [
                format === 'amd'? 'define(function (require, exports) {' : '',
                createExternalImports(chunk, outputOptions, externalImports),
            ` ${(chunk.exports.length > 0 && (format === 'es' || format === 'amd'))? 'var __nollup_entry_exports = ' : ''}
                (function (modules, __nollup__global__) {

                function getRelativePath (from, to) {
                    from = from === '.'? [] : from.split('/');
                    to = to.split('/');
    
                    var commonLength = from.length;
                    for (var i = 0; i < from.length; i++) {
                        if (from[i] !== to[i]) {
                            commonLength = i;
                            break;
                        }
                    } 

                    if (from.length - commonLength === 0) {
                        return './' + to.slice(commonLength).join('/')
                    }

                    return from.slice(commonLength)
                    .map(() => '..')
                    .concat(to.slice(commonLength))
                    .join('/');
                }

                var instances = {};
                if (!__nollup__global__.__nollup__chunks__) __nollup__global__.__nollup__chunks__ = {};
                var chunks = __nollup__global__.__nollup__chunks__;

                var create_module = function (number) {

                    var module = {
                        id: number,
                        dependencies: [],
                        dynamicDependencies: [],
                        exports: {},
                        invalidate: false,
                        __resolved: false,
                        __resolving: false
                    };

                    instances[number] = module;

                    ${callNollupModuleInit(/** @type {NollupPlugin[]} */(plugins))}

                    var localRequire = function (dep) {
                        ${format === 'cjs'? `
                            if (typeof dep === 'string') {
                                return require(dep);
                            }

                            if (!modules[dep]) {
                                throw new Error([
                                    'Module not found: ' + dep,
                                    '- Module doesn\\'t exist in bundle.'
                                ].join('\\n'));
                            }
                        `: `
                            if (typeof dep === 'string' || !modules[dep]) {
                                throw new Error([
                                    'Module not found: ' + dep,
                                    '- Did you call "require" using a string?',
                                    '- Check if you\\'re using untransformed CommonJS.',
                                    '- If called with an id, module doesn\\'t exist in bundle.'
                                ].join('\\n'));
                            }
                        `}
                        return _require(module, dep);
                    };

                    ${format === 'cjs'? `
                        for (var prop in require) {
                            localRequire[prop] = require[prop];
                        }
                    ` : ''}
                    
                    localRequire.dynamic = function (file, entryIndex) {
                        return new Promise(function (resolve) {
                            var relative_file = getRelativePath('${path.dirname(chunk.fileName)}', file);

                            var cb = () => {
                                // Each main overrides the dynamic callback so they all have different chunk references
                                let chunk = chunks[file];
                                let id = chunk.entry;

                                for (var key in chunk.modules) {
                                    modules[key] = chunk.modules[key];
                                }
                                if (instances[number].dynamicDependencies.indexOf(id) === -1) {
                                    instances[number].dynamicDependencies.push(id);
                                }

                                resolve(_require(module, id));
                            };

                            // If the chunk already statically included this module, use that instead.
                            if (instances[entryIndex]) {
                                chunks[file] = { entry: entryIndex };
                                cb();
                                return;                                
                            }

                            if (chunks[file]) {
                                cb();
                            } else {
                                ${format === 'es'? (`
                                    ${this.context.liveBindings === 'with-scope'? `
                                        return fetch(file).then(res => {
                                            return res.text();
                                        }).then(res => {
                                            eval(res);
                                            cb();
                                        });
                                    ` : `
                                    return import(relative_file).then(cb);
                                    `}
                                    
                                `) : ''}
                                ${format === 'cjs'? (`
                                    return Promise.resolve(require(relative_file)).then(cb);
                                `) : ''}
                                ${format === 'amd'? (`
                                    return new Promise(function (resolve) { require([relative_file], resolve)}).then(cb);
                                `) : ''}
                            }
                        });
                    };


                    modules[number](function (dep) {
                        if (!instances[dep] || instances[dep].invalidate) {
                            create_module(dep);
                        }

                        if (instances[number].dependencies.indexOf(dep) === -1) {
                            instances[number].dependencies.push(dep);
                        }

                        return true;
                    }, function (dep) {
                        return get_exports(module, dep);
                    }, function(binder, impl) {
                        module.__binder = binder;
                        module.__impl = impl;
                    }, function (arg1, arg2) {
                        var bindings = {};
                        if (typeof arg1 === 'object') {
                            bindings = arg1;
                        } else {
                            bindings[arg1] = arg2;
                        }

                        for (var prop in bindings) {
                            ${this.context.liveBindings? `
                            if (!module.exports.hasOwnProperty(prop) || prop === 'default') {
                                Object.defineProperty(module.exports, prop, {
                                    get: bindings[prop],
                                    enumerable: true,
                                    configurable: true
                                });
                            ` : `
                            if (module.exports[prop] !== bindings[prop]()) {
                                module.exports[prop] = bindings[prop]();
                            `}

                                Object.keys(instances).forEach(key => {
                                    if (instances[key].dependencies.indexOf(number) > -1) {
                                        instances[key].__binder();
                                    }
                                })
                            }
                        }
                    }, localRequire, module, module, __nollup__global__);

                    // Initially this will bind nothing, unless
                    // the module has been replaced by HMR
                    module.__binder();
                };

                var get_exports = function (parent, number) {
                    return instances[number].exports;
                };

                var resolve_module = function (parent, number) {
                    var module = instances[number];

                    if (module.__resolved) {
                        return;
                    }

                    module.__resolving = true;

                    var executeModuleImpl = function (module) {
                        ${callNollupModuleWrap(/** @type {NollupPlugin[]} */ (plugins), `
                            module.__resolved = true;
                            module.__impl();
                        `)}
                    };

                    module.dependencies.forEach((dep) => {
                        if (!instances[dep].__resolved && !instances[dep].__resolving) {
                            resolve_module(module, dep);
                        }
                    });

                    // Make sure module wasn't resolved as a result of circular dep.
                    if (!module.__resolved) {
                        executeModuleImpl(module);                  
                    }
                };

                var _require = function (parent, number) {
                    if (!instances[number] || instances[number].invalidate) {
                        create_module(number);
                    }

                    resolve_module(parent, number);
                    return instances[number].exports;
                };

                if (!__nollup__global__.__nollup_dynamic_require_callback) {
                    __nollup__global__.__nollup_dynamic_require_callback = function (file, chunk_entry_module, chunk_modules) {
                        chunks[file] = { entry: chunk_entry_module, modules: chunk_modules };
                    };
                }

                ${callNollupBundleInit(/** @type {NollupPlugin[]} */ (plugins))}

                ${format === 'cjs'? `
                    var result = _require(null, ${entryIndex});
                    var result_keys = Object.keys(result);
                    if (result_keys.length === 1 && result_keys[0] === 'default') {
                        module.exports = result.default;
                    } else {
                        module.exports = result;
                    }
                `: `
                    return _require(null, ${entryIndex});
                `}
            })({
                    `,
                    files.join(','),
                    `
            }, typeof globalThis !== 'undefined'? globalThis : (
            typeof self !== 'undefined' ? self : this
            ));

            ${format === 'amd'? (chunk.exports.length === 1 && chunk.exports[0] === 'default'? 'return __nollup_entry_exports.default;' : (
                chunk.exports.map(declaration => {
                    if (declaration === 'default') {
                        return 'exports["default"] = __nollup_entry_exports.default;'
                    } else {
                        return `exports.${declaration} = __nollup_entry_exports.${declaration};`
                    }
                }).join('\n') + '\nObject.defineProperty(exports, "__esModule", { value: true });'
            )) + '\n});' : ''}

            ${format === 'es'? chunk.exports.map(declaration => {
                if (declaration === 'default') {
                    return 'export default __nollup_entry_exports.default;' 
                } else {
                    return `export var ${declaration} = __nollup_entry_exports.${declaration};`
                }
            }).join('\n') : ''}
                    `,
            ].join('\n');
        }
    }
@param {Object<string, NollupOutputModule>} modules 
@param {RollupOutputChunk} chunk 
@param {RollupOutputOptions} outputOptions 
@param {RollupConfigContainer} config 
@param {Array<NollupInternalModuleImport>} externalImports
@return {string}