1.0.1 • Published 5 years ago

require-esmodule v1.0.1

Weekly downloads
62
License
MIT
Repository
github
Last release
5 years ago

Build Status Coverage

require-esmodule

require a compiled es6 module and handle exports.default.

Install

$ npm i require-esmodule

Usage

// foo.js
Object.defineProperty(exports, '__esModule', {value: true})

exports.default = {
  foo: 'default-foo'
}

exports.foo = 'foo'
// bar.js
module.exports = {
  default: {
    bar: 'default-bar'
  },
  bar: 'bar'
}
// baz.js
module.exports = null
// qux.js
const {
  requireModule,
  getExports
} = require('require-esmodule')

console.log(requireModule('/path/to/foo').foo)         // 'default-foo'

console.log(requireModule('/path/to/foo', false).foo)  // 'foo'

console.log(requireModule('/path/to/bar').bar)         // 'bar'
// bar.js is not a es6 module

console.log(requireModule('/path/to/baz'))             // null

requireModule(id: string, requireDefault? : boolean = true)

  • id string ABSOLUTE path of the module
  • requireDefault? boolean=true whether should require export default. Defaults to true.

Returns any the module exports

requireDefault as false

const foo = requireModule('./foo', false)

is equivalent to:

import * as foo from './foo'

while

const foo = requireModule('./foo')

is equivalent to:

import foo from './foo'

The purpose of require-esmodule is to detect and make it easier to get the default exports of es modules, so the default value of requireDefault is set to true

getExports(exports: any, requireDefault?)

Detect and get the real exports from the return value of require(id)

License

MIT