Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Transform CommonJS module into ES module.
const foo = require("foo")
-> import * as foo from "foo";
.require
/exports
statement that is not top-level.Promise.resolve(require("foo"))
-> import("foo")
.There are more examples under test/cases
folder.
const {parse} = require("acorn");
const {transform} = require("cjs-es");
const code = `
function foo() {}
function bar() {}
module.exports = {foo, bar};
`;
transform({code, parse})
.then(result => {
console.log(result.code);
/* ->
function foo() {}
function bar() {}
export {foo};
export {bar};
*/
});
When binding the module into one identifier:
const foo = require("foo");
The transformer imports all members from the module by default:
import * as foo from "foo";
To import the default member, mark require()
as // default
:
const foo = require("foo"); // default
Result:
import foo from "foo";
Note that if the identifier is used as the callee of a function/new expression, it would be considered as the default member since the namespace is not callable.
If the module.exports
is assigned with an object pattern:
const foo = "foo";
const bar = "bar";
module.exports = {
foo,
bar
};
The transformer converts it into named exports:
const foo = "foo";
const bar = "bar";
export {foo};
export {bar};
To export the entire object as the default member, mark module.exports
as // default
:
const foo = "foo";
const bar = "bar";
module.exports = { // default
foo,
bar
};
Result:
const foo = "foo";
const bar = "bar";
export default {
foo,
bar
};
Also note that if you set exportStyle
to default
, all named exports would be merged into a namespace object:
const foo = "foo";
const bar = "bar";
exports.foo = foo;
exports.bar = bar;
Result:
const foo = "foo";
const bar = "bar";
let _exports_ = {};
_exports_.foo = foo;
_exports_.bar = bar;
export default _exports_;
If the require
/module
/exports
statement are nested, they would be hoisted.
if (foo) {
require("foo").foo();
}
Result:
import * as _require_foo_ from "foo";
if (foo) {
_require_foo_.foo();
}
if (foo) {
module.exports = () => "foo";
} else {
module.exports = () => "bar";
}
Result:
let _module_exports_;
export default _module_exports_;
if (foo) {
_module_exports_ = () => "foo";
} else {
_module_exports_ = () => "bar";
}
if (foo) {
exports.foo = () => "foo";
}
function test() {
exports.foo = () => "bar";
}
Result:
let _export_foo_;
export {_export_foo_ as foo};
if (foo) {
_export_foo_ = () => "foo";
}
function test() {
_export_foo_ = () => "bar";
}
ES6 lazy load import("...")
is async and return a promise. It is interchangeable with Promise.resolve(require("..."))
in CommonJS:
module.exports = () => {
return Promise.resolve(require("foo"));
};
Result:
export default () => {
return import("foo");
};
module.exports
/exports
at the same timeIt is not a good idea to put exports
everywhere, but it is a common pattern:
if (foo) {
exports = module.exports = () => "foo";
} else {
module.exports = exports = () => "bar";
}
exports.OK = "OK";
console.log(module.exports);
All module.export
and exports
would be converted into a single reference:
let _module_exports_;
export default _module_exports_;
if (foo) {
_module_exports_ = () => "foo";
} else {
_module_exports_ = () => "bar";
}
_module_exports_.OK = "OK";
console.log(_module_exports_);
module
aroundIt will generate a module wrapper in this case:
var define = require('amdefine')(module);
define(() => {});
Result:
const _module_ = {exports: {}};
import _require_amdefine_ from "amdefine";
var define = _require_amdefine_(_module_);
define(() => {});
export default _module_.exports;
This module exports following members.
transform
: A function which can convert CJS module synax into ES module syntax.options
has following members:
parse?
: function
. A parser function which can parse JavaScript code into ESTree.
code
: string
. The JavaScript source code.
ast?
: AST object. If set then ignore options.parse
.
sourceMap?
: boolean
. If true then generate the source map. Default: false
importStyle?
: string
or function -> string
. The result must be "named"
or "default"
. Default: "named"
When the value is a function, it recieves one argument:
moduleId
: string
. The module ID of require("module-id")
.exportStyle?
: string
or function -> string
. The result must be "named"
or "default"
. Default: "named"
nested?
: boolean
. If true then analyze the entire AST, otherwise only top-level nodes are visited. If there is no nested require/exports/dynamic import statements, it is safe to keep it off. Default: false
.
warn?
: function
. This function is called when the transformer emmits a warning. The arguments are:
message
: string
. Warning message.pos
: number
. The position of the current node.If undefined, the transformer would use global console.error
.
options.importStyle
and options.exportStyle
could be async, and the return value would be cached (for importStyle
, the value is cached for each moduleId
).
The result object has following members:
code
: string. The result JavaScript code.map?
: object. The source map object generated by magicString.generateMap
. If isTouched
or options.sourceMap
is false then the map is null.isTouched
: boolean. If false then the code is not changed.If an error is thrown during walking the AST, the error has a property pos
which points to the index of the current node.
0.6.0 (Sep 19, 2018)
[, foo]
.exports
and module.exports
to a single reference.0.5.0 (Jul 19, 2018)
0.4.9 (Jun 29, 2018)
0.4.8 (Jun 22, 2018)
0.4.7 (May 15, 2018)
0.4.6 (May 13, 2018)
0.4.5 (May 1, 2018)
0.4.4 (May 1, 2018)
0.4.3 (May 1, 2018)
options.warn
.require
.const foo = module.exports = ...
.0.4.2 (Apr 30, 2018)
0.4.1 (Apr 30, 2018)
0.4.0 (Apr 30, 2018)
options.importStyle
and options.exportStyle
are async now.transform
function is async now.options.hoist
, options.dynamicImport
.options.nested
.0.3.3 (Apr 29, 2018)
options.ast
.0.3.2 (Apr 28, 2018)
.node
property for tree-walk error.0.3.1 (Apr 28, 2018)
0.3.0 (Apr 27, 2018)
hoist
option.dynamicImport
option.0.2.2 (Apr 26, 2018)
isTouched
property.0.2.1 (Apr 26, 2018)
0.2.0 (Apr 26, 2018)
// all
comment.// default
to change import/export style.0.1.0 (Apr 25, 2018)
FAQs
Transform CommonJS module into ES module.
The npm package cjs-es receives a total of 1,162 weekly downloads. As such, cjs-es popularity was classified as popular.
We found that cjs-es demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.