global-modulize
Version:
Make it easy to import any file by using config file.
58 lines (43 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var CONFIG_FILE_NAME = '.gm.js';
var path = require('path');
var assert = require('assert');
var config = require(path.join(process.cwd(), CONFIG_FILE_NAME));
if (config.default) {
config = config.default;
}
assert(config);
assert(config.entries);
var root = config.root || '';
var entries = config.entries;
exports.default = function (name) {
// get full path
var FILE_PATH = path.join(process.cwd(), root, entries[name]);
// get module by `require`
var gmodule = require(FILE_PATH); // eslint-disable-line global-require
// if export using `export defualt`, use the `default` as module
if (gmodule.default) {
var _ret = function () {
// get the `default` property
var gmoduleDefault = gmodule.default;
// mount all properties to the `default` property
Object.keys(gmodule).forEach(function (property) {
// ignore `default` itself
if (property !== 'default') {
gmoduleDefault[property] = gmodule[property];
}
});
// the `default` property
return {
v: gmoduleDefault
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
// the module
return gmodule;
};