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
beforerequire
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