es-get-exports

0.1.1 • Public • Published

version Build Status

ECMAScript 2015 getExports

Retrieve a list of export names from an ECMAScript 2015 module file in Node.js, without evaluating the module.

Usage

Given the following module in 'my/module.js':

export function hello() {
    return "Hello!";
}
 
const YES_CONSTANT = 'YES';
const NO_CONSTANT = 'NO';
 
export {YES_CONSTANT, NO_CONSTANT};
export default 42;

This code with retrieve the names of all exports:

import {getExports} from 'es-get-exports';
 
getExports('./my/module', {base: __dirpath})
    .then((list) => {
        console.log(list); // ['hello', 'YES_CONSTANT', 'NO_CONSTANT', 'default']
    });
 

Note: This version will only work for Harmony ECMAScript modules. CommonJS modules will yield an empty list.

API

function getExports(module:string, {base: string, recursive: boolean})
                  : Promise<(string|object)[]>

Returns a standard Promise containing a list of strings (may contain object placeholders iff the recursive option is false.

Options

  • base : The relative directory path for resolving the module file. When relative to the caller module's directory, simply assign this to __dirpath.
  • recursive : [default is true] whether to resolve import * from statements recursively. If false, a placeholder object {allFrom: string, toResolve = true} is added to the list and no further resolution is made.

License

MIT

Package Sidebar

Install

npm i es-get-exports

Weekly Downloads

1

Version

0.1.1

License

MIT

Last publish

Collaborators

  • enet4