0.1.3 • Published 6 years ago

cjs-hoist v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

cjs-hoist

Build Status

Transform CommonJS module, hoisting require and exports statements to top-level.

Usage

const {parse} = require("acorn");
const {transform} = require("cjs-hoist");
const {code} = transform({
  parse,
  code: `
if (foo) {
  const bar = require("bar");
}
`
});
/* code -> `
const _require_bar_ = require("bar");
if (foo) {
  const bar = _require_bar_;
}
`

API reference

This module exports following members.

  • transform: A function which can convert CJS module synax into ES module syntax.

transform(options?: object): TransformResult object

options has following members:

  • parse: function. A parser function which can parse JavaScript code into ESTree.
  • code: string. The JavaScript source code.
  • sourceMap?: boolean. If true then generate the source map. Default: false
  • ignoreDynamicRequire?: boolean. If true then the dynamic require (i.e. Promise.resolve(require("..."))) is ignored. Default: true

The result object has following members:

  • code: string. The result JavaScript code.
  • map?: object. The source map object generated by magicString.generateMap.
  • isTouched: boolean. If false then the code isn't changed.

Changelog

  • 0.1.3 (Apr 26, 2018)

    • Dependency change: drop estraverse, ecma-variable-scope; add estree-walker, rollup-pluginutils, is-reference.
  • 0.1.2 (Apr 26, 2018)

    • Fix: require-in-module.
  • 0.1.1 (Apr 26, 2018)

    • Add: isTouched prop.
  • 0.1.0 (Apr 26, 2018)

    • Initial release.