Package Exports
- cjs-es
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cjs-es) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
cjs-es
Transform CommonJS module into ES module.
Features
Lightweight.
Prefer named import/export when possible.
Only support the syntax that is interchangeable between mjs and js.
Convert in-place. It only converts:
- top-level
require
declaration (const foo = require("foo")
), - top-level
module.exports
,exports
assignment (module.exports = ...
/const foo = exports.foo = ...
), - dynamic-require expression (
Promise.resolve(require("foo"))
).
There are more samples under
test/cases
folder.- top-level
Usage
const {parse} = require("acorn");
const {transform} = require("cjs-es");
const {code} = transform({
parse,
code: `
function foo() {}
function bar() {}
module.exports = {foo, bar};
`
});
/* code == `
function foo() {}
function bar() {}
export {foo};
export {bar};
` */
Import style
When binding the module into one identifier:
const foo = require("foo");
The transformer imports all members from the module by the default:
import * as foo from "foo";
To import the default member, add // default
comment:
const foo = require("foo"); // default
Result:
import foo from "foo";
Export style
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};
If you like to export the entire object as the default member, you can use // default
comment at the line of module.exports
:
const foo = "foo";
const bar = "bar";
module.exports = { // default
foo,
bar
};
Result:
const foo = "foo";
const bar = "bar";
export default {
foo,
bar
};
API reference
This module exports following members.
transform
: A function which can convert CJS module synax into ES module syntax.
transform(options?: object): TransformResult object
options
has following members:
parse
:function
. A parser function which can parse JavaScript code into ESTree.code
:string
. The JavaScript source code.sourceMap?
:boolean
. If true then generate the source map. Default:false
importStyle?
:string
orfunction -> 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 ofrequire("module-id")
.
exportStyle?
:string
orfunction -> string
. The result must be"named"
or"default"
. Default:"named"
The result object has following members:
code
: string. The result JavaScript code.map?
: object. The source map object generated bymagicString.generateMap
.
Changelog
0.2.0 (Apr 26, 2018)
- Change: don't suppress parse error.
- Change: remove
// all
comment. - Add: importStyle, exportStyle option.
- Add: use
// default
to change import/export style.
0.1.0 (Apr 25, 2018)
- Initial release.