npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@esm.sh/cjs-module-lexer

v1.0.1

Published

A lexer for detecting the `module.exports` of a CJS module.

Downloads

159

Readme

cjs-module-lexer

A lexer for detecting the module.exports of a CJS module, written in Rust.

Usage

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

npm i @esm.sh/cjs-module-lexer

cjs-module-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.sh/cjs-module-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