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

c2es

v0.2.1

Published

`c2es` transforms your commonjs code into esm!

Downloads

37

Readme

c2es

c2es transforms your commonjs code into esm!

This package is still very experimental.
It converts commonjs code to esm but does not minify it.

input source code:

const { readFileSync } = require('fs');
const { join: j } = require('path');

module.exports.readme = readFileSync(j(process.cwd(), 'package.json'), 'utf-8');

output will be:

import * as $$require_a from 'node:fs';
import * as $$require_b from 'node:path';

var $$m = (m) => m.default || m;
var module = { exports: {} };
const { readFileSync } = $$m($$require_a);
const { join: j } = $$m($$require_b);

module.exports.readme = readFileSync(j(process.cwd(), 'package.json'), 'utf-8');

var $$default = module.exports;
var { readme: $$export_readme } = module.exports;

export { $$export_readme as readme, $$default as default };

Apis

Provide entry and output filename. Automatically compiles and outputs to the appropriate file

const { c2es } = require('c2es');

c2es('dist.js', 'dist.mjs', {
  dynamicImport: false,
  requirePrefix: '$$require_',
});

Or You can simply compile with cli

npx c2es dist.js --output dist.mjs

--output option must be provided.

Examples

  • Export Default

input:

function a() {}

function b() {}

function c() {}

module.exports = function () {};

module.exports.a = a;
module.exports.b = b;
module.exports.c = c;

output:

var module = { exports: {} };
function a() {}
function b() {}
function c() {}

module.exports = function () {};
module.exports.a = a;
module.exports.b = b;
module.exports.c = c;

var $$default = module.exports;
var { a: $$export_a, b: $$export_b, c: $$export_c } = module.exports;

export { $$export_a as a, $$export_b as b, $$export_c as c, $$default as default };
  • Dynamic Import

If your code has dynamic require, please add option --dynamic.

npx c2es input.js --output output.js --dynamic

tip: Prefix await before require

input:

const target = './main.js';
const m = require(target);

output:

var $$dynamic = async (m) => {
  if (global.require) {
    return require(m);
  }
  return $$m(await import(m));
};
var $$m = (m) => m.default || m;
var module = { exports: {} };
const target = './main.js';
const m = $$dynamic(target);
var $$default = module.exports;
var {} = module.exports;
export { $$default as default };

Dynamic import is not yet supported. You can get it to work by defining require().

  • Scope

input:

function a() {
  const { name: n, version: v } = JSON.parse(
    require('fs').readFileSync('./package.json', 'utf-8')
  );
  console.log(n, v);
}
a();

output:

import * as $$require_a from 'node:fs';
var $$m = (m) => m.default || m;
var module = { exports: {} };
function a() {
  const { name: n, version: v } = JSON.parse(
    $$m($$require_a).readFileSync('./package.json', 'utf-8')
  );
  console.log(n, v);
}
a();
var $$default = module.exports;
var {} = module.exports;
export { $$default as default };
  • require prefix

--require-prefix=<prefix>

npx c2es input.js --output output.js --require-prefix=_require_

input:

const { readFileSync } = require('fs');

console.log(readFileSync('./main.js', 'utf-8'));

output:

import * as _require_a from 'node:fs';
var $$m = (m) => m.default || m;
var module = { exports: {} };
const { readFileSync } = $$m(_require_a);

console.log(readFileSync('./main.js', 'utf-8'));
var $$default = module.exports;
var {} = module.exports;
export { $$default as default };

License

MIT