Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var debug = require('debug')('loopback:boot:compiler');
var Module = require('module');
var _ = require('lodash');
var g = require('strong-globalize')();
var requireNodeOrEsModule = require('./require');

var FILE_EXTENSION_JSON = '.json';

Expand Down Expand Up @@ -589,7 +590,7 @@ function resolveMiddlewarePath(rootDir, middleware, config) {

// Try to require the module and check if <module>.<fragment> is a valid
// function
var m = require(sourceFile);
var m = requireNodeOrEsModule(sourceFile);
if (typeof m[fragment] === 'function') {
resolved.sourceFile = sourceFile;
return resolved;
Expand Down Expand Up @@ -778,7 +779,7 @@ function loadMixins(sourceFiles, normalization) {
meta.name = name;
if (utils.fileExistsSync(metafile)) {
// May overwrite name, not sourceFile
_.extend(meta, require(metafile));
_.extend(meta, requireNodeOrEsModule(metafile));
}
meta.sourceFile = filepath;
mixinInstructions[meta.name] = meta;
Expand Down
15 changes: 8 additions & 7 deletions lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var async = require('async');
var path = require('path');
var format = require('util').format;
var g = require('strong-globalize')();
var requireNodeOrEsModule = require('./require');

/**
* Execute bootstrap instructions gathered by `boot.compile`.
Expand Down Expand Up @@ -211,7 +212,7 @@ function defineMixins(app, instructions) {
if (!modelBuilder.mixins || !mixins.length) return;

mixins.forEach(function(obj) {
var mixin = require(obj.sourceFile);
var mixin = requireNodeOrEsModule(obj.sourceFile);

if (typeof mixin === 'function' || mixin.prototype instanceof BaseClass) {
debug('Defining mixin %s', obj.name);
Expand Down Expand Up @@ -244,7 +245,7 @@ function defineModels(app, instructions) {
model = registry.createModel(data.definition);
if (data.sourceFile) {
debug('Loading customization script %s', data.sourceFile);
var code = require(data.sourceFile);
var code = requireNodeOrEsModule(data.sourceFile);
if (typeof code === 'function') {
debug('Customizing model %s', name);
code(model);
Expand Down Expand Up @@ -288,12 +289,12 @@ function runScripts(app, list, callback) {
list.forEach(function(filepath) {
debug('Requiring script %s', filepath);
try {
var exports = require(filepath);
if (typeof exports === 'function') {
var bootFn = requireNodeOrEsModule(filepath);
if (typeof bootFn === 'function') {
debug('Exported function detected %s', filepath);
functions.push({
path: filepath,
func: exports,
func: bootFn,
});
}
} catch (err) {
Expand Down Expand Up @@ -340,7 +341,7 @@ function setupMiddleware(app, instructions) {
middleware.forEach(function(data) {
debug('Configuring middleware %j%s', data.sourceFile,
data.fragment ? ('#' + data.fragment) : '');
var factory = require(data.sourceFile);
var factory = requireNodeOrEsModule(data.sourceFile);
if (data.fragment) {
factory = factory[data.fragment].bind(factory);
}
Expand Down Expand Up @@ -429,7 +430,7 @@ function getUpdatedConfigObject(app, config, opts) {
function setupComponents(app, instructions) {
instructions.components.forEach(function(data) {
debug('Configuring component %j', data.sourceFile);
var configFn = require(data.sourceFile);
var configFn = requireNodeOrEsModule(data.sourceFile);
var opts = {
useEnvVars: true,
};
Expand Down
9 changes: 9 additions & 0 deletions lib/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright IBM Corp. 2015,2017. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = function requireNodeOrEsModule(sourceFile) {
var exports = require(sourceFile);
return exports && exports.__esModule ? exports.default : exports;
};
12 changes: 9 additions & 3 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,15 @@ describe('browser support', function() {
expect(Object.keys(app.models)).to.include('Customer');
expect(app.models.Customer.settings)
.to.have.property('_customized', 'Customer');
expect(Object.keys(app.models)).to.include('ProductUmd');
expect(app.models.ProductUmd.settings)
.to.have.property('_customized', 'UMD');

// configured in fixtures/browser-app/component-config.json
// and fixtures/browser-app/components/dummy-component.js
// configured in fixtures/browser-app/component-config.json,
// fixtures/browser-app/components/dummy-component.js and
// fixtures/browser-app/components/dummy-component-umd.js
expect(app.dummyComponentOptions).to.eql({ option: 'value' });
expect(app.dummyComponentUmdOptions).to.eql({ option: 'valueUmd' });

done();
});
Expand All @@ -79,8 +84,9 @@ describe('browser support', function() {

var modelBuilder = app.registry.modelBuilder;
var registry = modelBuilder.mixins.mixins;
expect(Object.keys(registry)).to.eql(['TimeStamps']);
expect(Object.keys(registry)).to.eql(['AuditedUmd', 'TimeStamps']);
expect(app.models.Customer.timeStampsMixin).to.eql(true);
expect(app.models.ProductUmd.auditedMixin).to.eql(true);

done();
});
Expand Down
3 changes: 3 additions & 0 deletions test/executor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ describe('executor', function() {
'barStarted',
'barFinished',
'barSyncExecuted',
'umdLoaded',
]);
done();
}, 10);
Expand All @@ -294,6 +295,7 @@ describe('executor', function() {
'barStarted',
'barFinished',
'barSyncExecuted',
'umdLoaded',
]);
done();
});
Expand Down Expand Up @@ -838,6 +840,7 @@ describe('executor', function() {
.end(function(err, res) {
if (err) return done(err);
expect(res.headers.names).to.equal('custom-middleware');
expect(res.headers.umd).to.equal('success');
done();
});
});
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/browser-app/component-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"./components/dummy-component": {
"option": "value"
},
"./components/dummy-component-umd": {
"option": "valueUmd"
}
}
11 changes: 11 additions & 0 deletions test/fixtures/browser-app/components/dummy-component-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright IBM Corp. 2015,2017. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = {
default: function(app, options) {
app.dummyComponentUmdOptions = options;
},
};
Object.defineProperty(module.exports, '__esModule', { value: true });
11 changes: 11 additions & 0 deletions test/fixtures/browser-app/mixins/audited-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright IBM Corp. 2014,2017. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = {
default: function(Model, options) {
Model.auditedMixin = true;
},
};
Object.defineProperty(module.exports, '__esModule', { value: true });
3 changes: 3 additions & 0 deletions test/fixtures/browser-app/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
},
"Customer": {
"dataSource": "db"
},
"ProductUmd": {
"dataSource": "db"
}
}
11 changes: 11 additions & 0 deletions test/fixtures/browser-app/models/product-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright IBM Corp. 2014,2017. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = {
default: function(ProductUmd) {
ProductUmd.settings._customized = 'UMD';
},
};
Object.defineProperty(module.exports, '__esModule', { value: true });
5 changes: 5 additions & 0 deletions test/fixtures/browser-app/models/product-umd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "ProductUmd",
"base": "User",
"mixins": {"AuditedUmd": {} }
}
11 changes: 11 additions & 0 deletions test/fixtures/simple-app/boot/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright IBM Corp. 2014,2017. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = {
default: function(app) {
process.bootFlags.push('umdLoaded');
},
};
Object.defineProperty(module.exports, '__esModule', { value: true });
5 changes: 4 additions & 1 deletion test/fixtures/simple-app/middleware.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"initial": {
"../../helpers/push-name-middleware": {
"params": "custom-middleware"
}
},
"../../helpers/set-umd-middleware": {
"params": "success"
}
}
}
14 changes: 14 additions & 0 deletions test/helpers/set-umd-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

module.exports = {
default: function(value) {
return function(req, res, next) {
res.setHeader('umd', value);
next();
};
},
};
Object.defineProperty(module.exports, '__esModule', { value: true });