cjs-esm
v0.4.3
Published
Another CommonJs transform ESModule lib.
Downloads
16
Readme
cjs-esm
Another CommonJs transform ESModule lib.
English | 简体中文
Usage
import cjs2esm from 'cjs-esm'
// or
// const cjs2esm = require('cjs-esm').default
const { code, map } = cjs2esm(`const fs = require('fs')`)
TODO
✅ Nested scope(function-scope) 🚧-🐞
function load(path) {
require(path);
}
↓
function load(path) {
import/*🚧-🐞*/(path).then(m => m.default || m);
}
❌ Dynamic require id
✅ require statement
// Top-level scope
const foo = require('foo').default
↓
import foo from 'foo';
const foo = require('foo')
↓
import * as foo from 'foo';
const foo = require('foo').bar
↓
import * as __CJS_import__0__ from 'foo'; const { bar: foo } = __CJS_import__0__;
// Non top-level scope
const foo = [{ bar: require('foo').bar }]
↓
import * as __CJS_import__0__ from 'foo'; const foo = [{ bar: __CJS_import__0__.bar }]
✅ exports statement
module.exports = fn() { };
↓
const __CJS__export_default__ = module.exports = fn() { };
export { __CJS__export_default__ as default }
exports.foo = 'foo';
↓
const __CJS__export_foo__ = (module.exports == null ? {} : module.exports).foo;
export { __CJS__export_foo__ as foo }