2019-05-07 13:12:54 +00:00
|
|
|
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
|
2016-05-06 04:52:36 +00:00
|
|
|
// Node module: loopback-boot
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-05-08 21:48:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-13 01:03:35 +00:00
|
|
|
var boot = require('../');
|
2016-04-27 22:55:57 +00:00
|
|
|
var async = require('async');
|
2015-03-13 01:03:35 +00:00
|
|
|
var exportBrowserifyToFile = require('./helpers/browserify').exportToSandbox;
|
2019-10-03 12:37:59 +00:00
|
|
|
var packageFilter = require('./helpers/browserify').packageFilter;
|
2015-03-13 01:03:35 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
var browserify = require('browserify');
|
|
|
|
var sandbox = require('./helpers/sandbox');
|
|
|
|
var vm = require('vm');
|
|
|
|
var createBrowserLikeContext = require('./helpers/browser').createContext;
|
|
|
|
var printContextLogs = require('./helpers/browser').printContextLogs;
|
|
|
|
|
|
|
|
describe('browser support for multiple apps', function() {
|
|
|
|
this.timeout(60000); // 60s to give browserify enough time to finish
|
|
|
|
|
|
|
|
beforeEach(sandbox.reset);
|
|
|
|
|
|
|
|
it('has API for bundling and booting multiple apps', function(done) {
|
|
|
|
var app1Dir = path.resolve(__dirname, './fixtures/browser-app');
|
|
|
|
var app2Dir = path.resolve(__dirname, './fixtures/browser-app-2');
|
|
|
|
|
|
|
|
var apps = [
|
|
|
|
{
|
|
|
|
appDir: app1Dir,
|
|
|
|
appFile: './app.js',
|
2016-03-31 11:29:39 +00:00
|
|
|
moduleName: 'browser-app',
|
2015-03-13 01:03:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
appDir: app2Dir,
|
|
|
|
appFile: './app.js',
|
|
|
|
moduleName: 'browser-app2',
|
2016-03-31 11:29:39 +00:00
|
|
|
appId: 'browserApp2',
|
|
|
|
},
|
2015-03-13 01:03:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
browserifyTestApps(apps, function(err, bundlePath) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
var bundledApps = executeBundledApps(bundlePath, apps, function(err) {
|
|
|
|
var app1 = bundledApps.defaultApp;
|
|
|
|
var app2 = bundledApps.browserApp2;
|
2015-03-13 01:03:35 +00:00
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
expect(app1.settings).to.have.property('custom-key', 'custom-value');
|
|
|
|
expect(Object.keys(app1.models)).to.include('Customer');
|
|
|
|
expect(Object.keys(app1.models)).to.not.include('Robot');
|
|
|
|
expect(app1.models.Customer.settings).to.have.property('_customized',
|
|
|
|
'Customer');
|
2015-03-13 01:03:35 +00:00
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
expect(Object.keys(app2.models)).to.include('Robot');
|
|
|
|
expect(Object.keys(app2.models)).to.not.include('Customer');
|
2015-03-13 01:03:35 +00:00
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-03-13 01:03:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function browserifyTestApps(apps, next) {
|
|
|
|
var b = browserify({
|
2016-03-31 11:29:39 +00:00
|
|
|
debug: true,
|
2016-04-27 22:55:57 +00:00
|
|
|
basedir: path.resolve(__dirname, './fixtures'),
|
2019-10-03 12:37:59 +00:00
|
|
|
packageFilter,
|
2015-03-13 01:03:35 +00:00
|
|
|
});
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
var bundles = [];
|
2015-03-13 01:03:35 +00:00
|
|
|
for (var i in apps) {
|
|
|
|
var appDir = apps[i].appDir;
|
|
|
|
var appFile = apps[i].appFile;
|
|
|
|
var moduleName = apps[i].moduleName;
|
|
|
|
var appId = apps[i].appId;
|
|
|
|
|
|
|
|
appFile = path.join(appDir, appFile);
|
2017-05-08 21:48:48 +00:00
|
|
|
b.require(appFile, {expose: moduleName});
|
2015-03-13 01:03:35 +00:00
|
|
|
|
|
|
|
var opts = appDir;
|
|
|
|
if (appId) {
|
|
|
|
opts = {
|
|
|
|
appId: appId,
|
2016-03-31 11:29:39 +00:00
|
|
|
appRootDir: appDir,
|
2015-03-13 01:03:35 +00:00
|
|
|
};
|
|
|
|
}
|
2016-04-27 22:55:57 +00:00
|
|
|
bundles.push(opts);
|
2015-03-13 01:03:35 +00:00
|
|
|
}
|
2016-04-27 22:55:57 +00:00
|
|
|
async.eachSeries(bundles, function(opts, done) {
|
|
|
|
boot.compileToBrowserify(opts, b, done);
|
|
|
|
}, function(err) {
|
|
|
|
exportBrowserifyToFile(b, 'browser-app-bundle.js', next);
|
|
|
|
});
|
2015-03-13 01:03:35 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function executeBundledApps(bundlePath, apps, done) {
|
2015-03-13 01:03:35 +00:00
|
|
|
var code = fs.readFileSync(bundlePath);
|
|
|
|
var context = createBrowserLikeContext();
|
|
|
|
vm.runInContext(code, context, bundlePath);
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
var ids = [];
|
2015-03-13 01:03:35 +00:00
|
|
|
var script = 'var apps = {};\n';
|
|
|
|
for (var i in apps) {
|
|
|
|
var moduleName = apps[i].moduleName;
|
|
|
|
var id = apps[i].appId || 'defaultApp';
|
2016-04-27 22:55:57 +00:00
|
|
|
ids.push(id);
|
2015-03-13 01:03:35 +00:00
|
|
|
script += 'apps.' + id + ' = require("' + moduleName + '");\n';
|
|
|
|
}
|
|
|
|
script += 'apps;\n';
|
|
|
|
|
|
|
|
var appsInContext = vm.runInContext(script, context);
|
2016-04-27 22:55:57 +00:00
|
|
|
async.each(ids, function(id, done) {
|
|
|
|
appsInContext[id].once('booted', function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}, function(err) {
|
|
|
|
printContextLogs(context);
|
|
|
|
done(err, appsInContext);
|
|
|
|
});
|
2015-03-13 01:03:35 +00:00
|
|
|
|
|
|
|
return appsInContext;
|
|
|
|
}
|