esm-cjs-lexer
TypeScript icon, indicating that this package has built-in type declarations

0.11.2 • Public • Published

esm-cjs-lexer

A commonjs module lexer written in Rust for detecting the module.exports of a commonjs module. The lexer is powered by swc, compiled to WebAssembly.

Usage

esm-cjs-lexer currently only supports Node.js environment. You can install it via npm CLI:

npm i esm-cjs-lexer

esm-cjs-lexer provides a parse function that detects the module.exports of a commonjs module. The function returns an object with two properties: exports and reexports. The exports property is an array of the exported names, and the reexports property is an array of the reexported modules.

const { parse } = require("esm-cjs-lexer");

// named exports by assignment
// exports: ["a", "b", "c", "__esModule", "foo"]
const { exports } = parse("index.cjs", `
  exports.a = "a";
  module.exports.b = "b";
  Object.defineProperty(exports, "c", { value: 1 });
  Object.defineProperty(module.exports, "__esModule", { value: true })
  const key = "foo"
  Object.defineProperty(exports, key, { value: "e" });
`);

// reexports
// reexports: ["./lib"]
const { reexports } = parse("index.cjs", `
  module.exports = require("./lib");
`);

// object exports(spread syntax supported)
// exports: ["foo", "baz"]
// reexports: ["./lib"]
const { exports, reexports } = parse("index.cjs", `
  const foo = "bar"
  const obj = { baz: 123 }
  module.exports = { foo, ...obj, ...require("./lib") };
`);

// if expression
// exports: ["foo", "cjs"]
const { exports } = parse("index.cjs", `
  module.exports.a = "a";
  if (true) {
    exports.foo = "bar";
  }
  const mtype = "cjs";
  if (mtype === "cjs") {
    exports.cjs = true;
  } else {
    exports.esm = true;
  }
  if (false) {
    exports.unreachable = true;
  }
`);

// condition exports by checking if `process.env.NODE_ENV` equals to `nodeEnv` option
// reexports: ["./index.development.js"]
const { reexports } = parse("index.cjs", `
  if (process.env.NODE_ENV === "development") {
    module.exports = require("./index.development.js")
  } else {
    module.exports = require("./index.production.js")
  }
`, { nodeEnv: "development" });

// block&IIFE
// exports: ["foo", "baz", "__esModule"]
const { exports } = parse("index.cjs", `
  {
    exports.foo = 'bar'
  }
  (function () {
    exports.baz = 'qux'
    if (true) {
      return
    }
    exports.unreachable = true
  })();
  exports.__esModule = true
`);

// UMD format
// exports: ["foo"]
const { exports } = parse("index.cjs", `
  (function (global, factory) {
    typeof exports === "object" && typeof module !== "undefined" ? factory(exports) :
    typeof define === "function" && define.amd ? define(["exports"], factory) :
    (factory((global.MMDParser = global.MMDParser || {})));
  }(this, function (exports) {
    exports.foo = "bar";
  }))
`);

// exports by calling a function
// exports: ["foo"]
const { exports } = parse("index.cjs", `
  function module() {
    return { foo: "bar" }
  }
  module.exports = module()
`);

// annotated export names for ESM import
// exports: ["foo", "bar"]
const { exports } = parse("lib.cjs", `
0 && (module.exports = {
 foo,
 bar,
})
`);

// call reexports
// reexports: ["./lib()"]
const { reexports } = parse("index.cjs", `
  module.exports = require("./lib")()
`);
// apply call reexports
// exports: ["foo"]
const { exports } = parse("lib.cjs", `
  module.exports = function() {
    return { foo: "bar" }
  }
`, { callMode: true });

The parse function has the following types definition:

export function parse(
  specifier: string,
  code: string,
  options? {
    nodeEnv?: 'development' | 'production',
    callMode?: boolean,
  }
): {
  exports: string[],
  reexports: string[],
};

Development Setup

You will need rust 1.56+ and wasm-pack.

Build

wasm-pack build --target nodejs

Run tests

cargo test --all

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.11.2285latest

Version History

VersionDownloads (Last 7 Days)Published
0.11.2285
0.11.11
0.11.00
0.10.11
0.10.0635
0.9.13
0.9.00
0.8.10
0.8.00
0.7.50
0.7.40
0.7.30
0.7.20
0.7.10
0.7.00
0.6.20
0.6.10

Package Sidebar

Install

npm i esm-cjs-lexer

Weekly Downloads

898

Version

0.11.2

License

MIT

Unpacked Size

866 kB

Total Files

6

Last publish

Collaborators

  • ije