SimulationFiles.prototype._createHostJsFile = function()

in src/server/sim-files.js [123:215]


SimulationFiles.prototype._createHostJsFile = function (simulationFilePath, hostType, scriptTypes, pluginList) {
    var outputFile = this.getHostJsFile(hostType, simulationFilePath);
    var jsonFile = path.join(simulationFilePath, hostType + '.json');

    // See if we already have created our output file, and it is up-to-date with all its dependencies. However, if the
    // list of plugins has changed, or the directory where a plugin's simulation definition lives has changed, we need
    // to force a refresh.
    if (fs.existsSync(outputFile) && validatePlugins(hostType, pluginList, simulationFilePath)) {
        log.log('Creating ' + hostType + '.js: Existing file found and is up-to-date.');
        this._builtOnce[hostType] = true;
        return Promise.resolve(pluginList);
    }

    var filePath = this._hostTypeJsFile[hostType];
    log.log('Creating ' + hostType + '.js');

    var scriptDefs = createScriptDefs(hostType, scriptTypes);

    var b = browserify({
        paths: getBrowserifySearchPaths(hostType),
        debug: true,
        exports: false, // needed to prevent browserify to override hasExports option by set it to true
        hasExports: false
    });

    b.transform(function (file) {
        if (file === filePath) {
            var data = '';
            return through(function (buf, encoding, cb) {
                data += buf;
                cb();
            }, function (cb) {
                data = scriptDefs.reduce(function (previousData, scriptDef) {
                    return previousData.replace(scriptDef.comment, scriptDef.code.join(',\n'));
                }, data);
                this.push(data);
                cb();
            });
        } else {
            // No-op for other files
            return through(function (chunk, encoding, cb) {
                cb(null, chunk);
            });
        }
    });

    b.add(filePath);

    // Include common modules
    getCommonModules(hostType).forEach(function (module) {
        b.require(module.file, { expose: module.name });
    });

    var pluginTemplate = '\'%PLUGINID%\': require(\'%EXPOSEID%\')';
    Object.keys(pluginList).forEach(function (pluginId) {
        var pluginPath = pluginList[pluginId];
        scriptDefs.forEach(function (scriptDef) {
            var pluginScriptFile = path.join(pluginPath, scriptDef.fileName);
            if (fs.existsSync(pluginScriptFile)) {
                var exposeId = scriptDef.exposeId.replace(/%PLUGINID%/g, pluginId);
                scriptDef.code.push(pluginTemplate
                    .replace(/%PLUGINID%/g, pluginId)
                    .replace(/%EXPOSEID%/g, exposeId));
                b.require(pluginScriptFile, { expose: exposeId });
            }
        });
    });

    var fileInfo = {};
    b.on('file', function (file) {
        fileInfo[file] = new Date(fs.statSync(file).mtime).getTime();
    });

    var outputFileStream = fs.createWriteStream(outputFile);

    return new Promise((resolve, reject) => {
        outputFileStream.on('finish', function () {
            this._builtOnce[hostType] = true;
            fs.writeFileSync(jsonFile, JSON.stringify({ plugins: pluginList, files: fileInfo }));
            resolve(pluginList);
        }.bind(this));
        outputFileStream.on('error', function (error) {
            reject(error);
        });
    
        let bundle = b.bundle();
        bundle.on('error', function (error) {
            reject(error);
        });
    
        bundle.pipe(outputFileStream);
    });
};