UNPKG

global-modulize

Version:

Make it easy to import any file by using config file.

46 lines (33 loc) 1.06 kB
const CONFIG_FILE_NAME = '.gm.js'; const path = require('path'); const assert = require('assert'); let config = require(path.join(process.cwd(), CONFIG_FILE_NAME)); if (config.default) { config = config.default; } assert(config); assert(config.entries); const root = config.root || ''; const entries = config.entries; export default (name) => { // get full path const FILE_PATH = path.join(process.cwd(), root, entries[name]); // get module by `require` const gmodule = require(FILE_PATH); // eslint-disable-line global-require // if export using `export defualt`, use the `default` as module if (gmodule.default) { // get the `default` property const gmoduleDefault = gmodule.default; // mount all properties to the `default` property Object.keys(gmodule).forEach(property => { // ignore `default` itself if (property !== 'default') { gmoduleDefault[property] = gmodule[property]; } }); // the `default` property return gmoduleDefault; } // the module return gmodule; };