2019-05-07 13:12:54 +00:00
|
|
|
// Copyright IBM Corp. 2014,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';
|
|
|
|
|
2014-06-03 12:08:34 +00:00
|
|
|
var boot = require('../');
|
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;
|
2014-06-03 12:08:34 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
2014-11-13 14:31:35 +00:00
|
|
|
var expect = require('chai').expect;
|
2014-06-03 12:08:34 +00:00
|
|
|
var browserify = require('browserify');
|
|
|
|
var sandbox = require('./helpers/sandbox');
|
|
|
|
var vm = require('vm');
|
2015-03-13 01:03:35 +00:00
|
|
|
var createBrowserLikeContext = require('./helpers/browser').createContext;
|
|
|
|
var printContextLogs = require('./helpers/browser').printContextLogs;
|
2014-06-03 12:08:34 +00:00
|
|
|
|
2014-10-21 06:21:40 +00:00
|
|
|
var compileStrategies = {
|
2018-07-23 18:26:47 +00:00
|
|
|
default: function(appDir) {
|
2014-10-21 06:21:40 +00:00
|
|
|
var b = browserify({
|
|
|
|
basedir: appDir,
|
2016-03-31 11:29:39 +00:00
|
|
|
debug: true,
|
2019-10-03 12:37:59 +00:00
|
|
|
packageFilter,
|
2014-10-21 06:21:40 +00:00
|
|
|
});
|
2017-05-08 21:48:48 +00:00
|
|
|
b.require('./app.js', {expose: 'browser-app'});
|
2014-10-21 06:21:40 +00:00
|
|
|
return b;
|
|
|
|
},
|
|
|
|
|
2018-07-23 18:26:47 +00:00
|
|
|
coffee: function(appDir) {
|
2014-10-21 06:21:40 +00:00
|
|
|
var b = browserify({
|
|
|
|
basedir: appDir,
|
|
|
|
extensions: ['.coffee'],
|
2016-03-31 11:29:39 +00:00
|
|
|
debug: true,
|
2019-10-03 12:37:59 +00:00
|
|
|
packageFilter,
|
2014-10-21 06:21:40 +00:00
|
|
|
});
|
|
|
|
b.transform('coffeeify');
|
|
|
|
|
2017-05-08 21:48:48 +00:00
|
|
|
b.require('./app.coffee', {expose: 'browser-app'});
|
2014-10-21 06:21:40 +00:00
|
|
|
return b;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2014-06-03 12:08:34 +00:00
|
|
|
describe('browser support', function() {
|
2014-08-18 12:35:19 +00:00
|
|
|
this.timeout(60000); // 60s to give browserify enough time to finish
|
|
|
|
|
2014-08-19 07:26:57 +00:00
|
|
|
beforeEach(sandbox.reset);
|
|
|
|
|
2014-06-03 12:08:34 +00:00
|
|
|
it('has API for bundling and executing boot instructions', function(done) {
|
|
|
|
var appDir = path.resolve(__dirname, './fixtures/browser-app');
|
|
|
|
|
|
|
|
browserifyTestApp(appDir, function(err, bundlePath) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
var app = executeBundledApp(bundlePath, function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
// configured in fixtures/browser-app/boot/configure.js
|
|
|
|
expect(app.settings).to.have.property('custom-key', 'custom-value');
|
|
|
|
expect(Object.keys(app.models)).to.include('Customer');
|
2018-07-23 18:26:47 +00:00
|
|
|
expect(app.models.Customer.settings).to.have.property(
|
|
|
|
'_customized',
|
|
|
|
'Customer'
|
|
|
|
);
|
2016-04-27 22:55:57 +00:00
|
|
|
|
|
|
|
// configured in fixtures/browser-app/component-config.json
|
|
|
|
// and fixtures/browser-app/components/dummy-component.js
|
2017-05-08 21:48:48 +00:00
|
|
|
expect(app.dummyComponentOptions).to.eql({option: 'value'});
|
2016-04-27 22:55:57 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-06-03 12:08:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-14 09:02:09 +00:00
|
|
|
it('loads mixins', function(done) {
|
|
|
|
var appDir = path.resolve(__dirname, './fixtures/browser-app');
|
|
|
|
var options = {
|
2016-03-31 11:29:39 +00:00
|
|
|
appRootDir: appDir,
|
2015-04-14 09:02:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
browserifyTestApp(options, function(err, bundlePath) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
var app = executeBundledApp(bundlePath, function(err) {
|
|
|
|
var modelBuilder = app.registry.modelBuilder;
|
|
|
|
var registry = modelBuilder.mixins.mixins;
|
|
|
|
expect(Object.keys(registry)).to.eql(['TimeStamps']);
|
|
|
|
expect(app.models.Customer.timeStampsMixin).to.eql(true);
|
2015-04-14 09:02:09 +00:00
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
done();
|
|
|
|
});
|
2015-04-14 09:02:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-10-21 06:21:40 +00:00
|
|
|
it('supports coffee-script files', function(done) {
|
|
|
|
// add coffee-script to require.extensions
|
2018-07-23 18:26:47 +00:00
|
|
|
require('coffeescript/register');
|
2014-10-21 06:21:40 +00:00
|
|
|
|
|
|
|
var appDir = path.resolve(__dirname, './fixtures/coffee-app');
|
|
|
|
|
|
|
|
browserifyTestApp(appDir, 'coffee', function(err, bundlePath) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
var app = executeBundledApp(bundlePath, function(err) {
|
|
|
|
// configured in fixtures/browser-app/boot/configure.coffee
|
|
|
|
expect(app.settings).to.have.property('custom-key', 'custom-value');
|
|
|
|
expect(Object.keys(app.models)).to.include('Customer');
|
2018-07-23 18:26:47 +00:00
|
|
|
expect(app.models.Customer.settings).to.have.property(
|
|
|
|
'_customized',
|
|
|
|
'Customer'
|
|
|
|
);
|
2016-04-27 22:55:57 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-10-21 06:21:40 +00:00
|
|
|
});
|
2014-06-03 12:08:34 +00:00
|
|
|
});
|
2014-10-21 06:21:40 +00:00
|
|
|
});
|
|
|
|
|
2015-04-14 09:02:09 +00:00
|
|
|
function browserifyTestApp(options, strategy, next) {
|
2014-11-13 14:54:59 +00:00
|
|
|
// set default args
|
2018-07-23 18:26:47 +00:00
|
|
|
if (typeof strategy === 'function' && !next) {
|
2014-10-21 06:21:40 +00:00
|
|
|
next = strategy;
|
|
|
|
strategy = undefined;
|
|
|
|
}
|
2018-07-23 18:26:47 +00:00
|
|
|
if (!strategy) strategy = 'default';
|
2014-10-21 06:21:40 +00:00
|
|
|
|
2018-07-23 18:26:47 +00:00
|
|
|
var appDir = typeof options === 'object' ? options.appRootDir : options;
|
2014-10-21 06:21:40 +00:00
|
|
|
var b = compileStrategies[strategy](appDir);
|
2014-06-03 12:08:34 +00:00
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
boot.compileToBrowserify(options, b, function(err) {
|
|
|
|
exportBrowserifyToFile(b, 'browser-app-bundle.js', next);
|
|
|
|
});
|
2014-06-03 12:08:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function executeBundledApp(bundlePath, done) {
|
2014-06-03 12:08:34 +00:00
|
|
|
var code = fs.readFileSync(bundlePath);
|
|
|
|
var context = createBrowserLikeContext();
|
|
|
|
vm.runInContext(code, context, bundlePath);
|
|
|
|
var app = vm.runInContext('require("browser-app")', context);
|
2016-04-27 22:55:57 +00:00
|
|
|
app.once('booted', function(err) {
|
|
|
|
printContextLogs(context);
|
|
|
|
done(err, app);
|
|
|
|
});
|
2014-06-03 12:08:34 +00:00
|
|
|
return app;
|
|
|
|
}
|