morpha
Version:
A Web-IDE or Desktop-IDE creator.
89 lines (82 loc) • 3.12 kB
JavaScript
;
var _require = require('./WHITE_LIST'),
CORE_SREVICE_NAMES = _require.CORE_SREVICE_NAMES;
var _require2 = require('./constants'),
PREFIX = _require2.PREFIX;
var getMorphaConfig = require('./getMorphaConfig');
var fs = void 0;
var path = void 0;
var safeRequire = function safeRequire(moduleName) {
try {
return require(moduleName);
} catch (e) {
return null;
}
};
function getResolveFn(resolvePath, targetPack, packCwd) {
return function resolve(targetPath) {
if (!targetPath) throw new Error('Can not resolve package main path in ' + targetPack);
var realPath = path.join(targetPack, targetPath);
if (!fs.existsSync(realPath)) throw new Error('Can not resolve package path ' + realPath);
if (!resolvePath) return realPath;
// Resolve path for morpha-bootstrap
var relativePath = path.relative(packCwd, realPath);
if (/^node_modules/.test(relativePath)) {
return path.relative('node_modules', relativePath);
}
var res = path.relative(resolvePath, realPath);
return res[0] === '.' || res[0] === '/ ' ? res : './' + res;
};
}
module.exports = function loadPackages(packCwd, configs) {
var loadedPackages = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
// Do not load "fs" "path" packages if run in webpack browser
fs = safeRequire('fs');
path = safeRequire('path');
if (!fs) return {};
var isClient = configs.isClient,
resolvePath = configs.resolvePath,
entryConfig = configs.entryConfig;
var packages = {};
function loadPack(targetPack, packConfig) {
var result = void 0;
var resolve = getResolveFn(resolvePath, targetPack, packCwd);
var pack = packConfig || getMorphaConfig(targetPack);
// Loaded before
if (packages[pack.name]) return;
var isModulePath = pack.isModulePath;
if (isClient) {
if (!pack.clientBuildMain && !pack.clientMain) return;
result = {
name: pack.name,
main: isModulePath ? resolve(pack.clientBuildMain || pack.clientMain) : resolve(pack.clientMain),
dependencies: pack.clientDependencies || [],
styles: pack.styles ? pack.styles.map(function (stylePath) {
return resolve(stylePath);
}) : []
};
} else {
if (!pack.processBuildMain && !pack.processMain) return;
result = {
name: pack.name,
main: isModulePath ? resolve(pack.processBuildMain || pack.processMain) : resolve(pack.processMain),
dependencies: pack.processDependencies || []
};
}
if (result.dependencies.length > 0) {
result.dependencies.filter(function (dep) {
return !CORE_SREVICE_NAMES.includes(dep) && !packages[dep] && !loadedPackages[dep];
}).forEach(function (dep) {
if (pack.packages[dep]) {
loadPack(path.join(targetPack, pack.packages[dep]));
} else {
// todo load pre node_modules
loadPack(path.join(targetPack, 'node_modules', PREFIX + dep));
}
});
}
packages[pack.name] = result;
}
loadPack(packCwd, entryConfig);
return packages;
};