2016-05-06 04:52:36 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// 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 fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var commondir = require('commondir');
|
2014-12-03 06:10:50 +00:00
|
|
|
var cloneDeep = require('lodash').cloneDeep;
|
2016-04-27 22:55:57 +00:00
|
|
|
var g = require('./globalize');
|
2014-06-03 12:08:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add boot instructions to a browserify bundler.
|
|
|
|
* @param {Object} instructions Boot instructions.
|
|
|
|
* @param {Object} bundler A browserify object created by `browserify()`.
|
|
|
|
*/
|
2016-04-27 22:55:57 +00:00
|
|
|
module.exports = function addInstructionsToBrowserify(context, bundler) {
|
|
|
|
addPlugins(bundler);
|
|
|
|
// bundlePluginScripts(context, bundler);
|
|
|
|
bundleModelScripts(context, bundler);
|
|
|
|
bundleMixinScripts(context, bundler);
|
|
|
|
bundleComponentScripts(context, bundler);
|
|
|
|
bundleOtherScripts(context, bundler);
|
|
|
|
bundleInstructions(context, bundler);
|
2014-06-03 12:08:34 +00:00
|
|
|
};
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function addPlugins(bundler) {
|
|
|
|
var dir = path.join(__dirname, './plugins');
|
|
|
|
var files = fs.readdirSync(dir);
|
|
|
|
files.forEach(function(f) {
|
|
|
|
bundler.require(path.join(dir, f),
|
2017-05-08 21:48:48 +00:00
|
|
|
{expose: './plugins/' + path.basename(f, '.js')});
|
2016-04-27 22:55:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bundleOtherScripts(context, bundler) {
|
|
|
|
var list = context.instructions.bootScripts;
|
|
|
|
addScriptsToBundle('boot', list, bundler);
|
|
|
|
}
|
|
|
|
|
|
|
|
function bundlePluginScripts(context, bundler) {
|
|
|
|
var list = context.instructions.pluginScripts;
|
|
|
|
addScriptsToBundle('plugins', list, bundler);
|
2014-06-13 11:14:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function bundleModelScripts(context, bundler) {
|
|
|
|
bundleSourceFiles(context, 'models', bundler);
|
2015-01-06 13:00:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function bundleMixinScripts(context, bundler) {
|
|
|
|
bundleSourceFiles(context, 'mixins', bundler);
|
2015-04-14 09:02:09 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function bundleComponentScripts(context, bundler) {
|
|
|
|
bundleSourceFiles(context, 'components', bundler);
|
2015-01-06 13:00:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function bundleSourceFiles(context, type, bundler) {
|
|
|
|
var files = context.instructions[type]
|
2014-06-13 11:14:43 +00:00
|
|
|
.map(function(m) { return m.sourceFile; })
|
|
|
|
.filter(function(f) { return !!f; });
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
var instructionToFileMapping = context.instructions[type]
|
2014-06-13 11:14:43 +00:00
|
|
|
.map(function(m) { return files.indexOf(m.sourceFile); });
|
|
|
|
|
2015-01-06 13:00:26 +00:00
|
|
|
addScriptsToBundle(type, files, bundler);
|
2014-06-13 11:14:43 +00:00
|
|
|
|
|
|
|
// Update `sourceFile` properties with the new paths
|
2015-01-06 13:00:26 +00:00
|
|
|
instructionToFileMapping.forEach(function(fileIx, sourceIx) {
|
2014-06-13 11:14:43 +00:00
|
|
|
if (fileIx === -1) return;
|
2016-04-27 22:55:57 +00:00
|
|
|
context.instructions[type][sourceIx].sourceFile = files[fileIx];
|
2014-06-13 11:14:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addScriptsToBundle(name, list, bundler) {
|
|
|
|
if (!list.length) return;
|
2014-06-03 12:08:34 +00:00
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
var root = commondir(list.map(path.dirname));
|
2014-06-03 12:08:34 +00:00
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
for (var ix in list) {
|
|
|
|
var filepath = list[ix];
|
2014-06-03 12:08:34 +00:00
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
// Build a short unique id that does not expose too much
|
|
|
|
// information about the file system, but still preserves
|
|
|
|
// useful information about where is the file coming from.
|
|
|
|
var fileid = 'loopback-boot#' + name + '#' + path.relative(root, filepath);
|
2014-06-03 12:08:34 +00:00
|
|
|
|
2014-06-13 11:14:43 +00:00
|
|
|
// Add the file to the bundle.
|
2017-05-08 21:48:48 +00:00
|
|
|
bundler.require(filepath, {expose: fileid});
|
2014-06-03 12:08:34 +00:00
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
// Rewrite the context entry with the new id that will be
|
2014-06-13 11:14:43 +00:00
|
|
|
// used to load the file via `require(fileid)`.
|
|
|
|
list[ix] = fileid;
|
2014-06-03 12:08:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 22:55:57 +00:00
|
|
|
function bundleInstructions(context, bundler) {
|
|
|
|
var instructions = cloneDeep(context.instructions);
|
2014-11-12 15:56:01 +00:00
|
|
|
|
|
|
|
var hasMiddleware = instructions.middleware.phases.length ||
|
|
|
|
instructions.middleware.middleware.length;
|
|
|
|
if (hasMiddleware) {
|
2016-08-04 16:05:24 +00:00
|
|
|
g.warn(
|
|
|
|
'Discarding {{middleware}} instructions,' +
|
2018-07-26 16:03:12 +00:00
|
|
|
' {{loopback}} client does not support {{middleware}}.'
|
|
|
|
);
|
2014-11-12 15:56:01 +00:00
|
|
|
}
|
|
|
|
delete instructions.middleware;
|
|
|
|
|
2014-06-03 12:08:34 +00:00
|
|
|
var instructionsString = JSON.stringify(instructions, null, 2);
|
|
|
|
|
|
|
|
/* The following code does not work due to a bug in browserify
|
|
|
|
* https://github.com/substack/node-browserify/issues/771
|
|
|
|
var instructionsStream = require('resumer')()
|
|
|
|
.queue(instructionsString);
|
|
|
|
instructionsStream.path = 'boot-instructions';
|
|
|
|
b.require(instructionsStream, { expose: 'loopback-boot#instructions' });
|
|
|
|
*/
|
|
|
|
|
2015-03-13 01:03:35 +00:00
|
|
|
var instructionId = 'instructions';
|
|
|
|
// Create an unique instruction identifier using the application ID.
|
|
|
|
// This is only useful when multiple loopback applications are being bundled
|
|
|
|
// together.
|
|
|
|
if (instructions.appId)
|
|
|
|
instructionId += '-' + instructions.appId;
|
|
|
|
|
2014-06-03 12:08:34 +00:00
|
|
|
// Write the instructions to a file in our node_modules folder.
|
|
|
|
// The location should not really matter as long as it is .gitignore-ed
|
|
|
|
var instructionsFile = path.resolve(__dirname,
|
2015-03-13 01:03:35 +00:00
|
|
|
'..', 'generated-' + instructionId + '.json');
|
2014-06-03 12:08:34 +00:00
|
|
|
fs.writeFileSync(instructionsFile, instructionsString, 'utf-8');
|
2015-03-13 01:03:35 +00:00
|
|
|
|
|
|
|
var moduleName = 'loopback-boot#' + instructionId;
|
2017-05-08 21:48:48 +00:00
|
|
|
bundler.require(instructionsFile, {expose: moduleName});
|
2014-06-03 12:08:34 +00:00
|
|
|
}
|