Comparing version 0.1.0 to 0.2.0
42
index.js
@@ -155,3 +155,3 @@ const {walk} = require("estree-walker"); | ||
function transformExportAssign({s, node}) { | ||
function transformExportAssign({s, node, exportStyle, code}) { | ||
const exported = getExportInfo(node); | ||
@@ -184,3 +184,5 @@ if (!exported) { | ||
} else { | ||
if (exported.value.type !== "ObjectExpression") { | ||
const rx = /.*?\/\/.*\bdefault\b/y; | ||
rx.lastIndex = node.start; | ||
if (exported.value.type !== "ObjectExpression" || exportStyle() === "default" || rx.test(code)) { | ||
// module.exports = ... | ||
@@ -254,3 +256,3 @@ s.overwrite( | ||
function transformImportDeclare({s, node, code}) { | ||
function transformImportDeclare({s, node, code, importStyle}) { | ||
const declared = getDeclareImport(node); | ||
@@ -262,17 +264,17 @@ if (!declared) { | ||
// const foo = require("foo") | ||
const rx = /.+\/\/.+\b(all|import\b.\ball)\b/y; | ||
const rx = /.*?\/\/.*\bdefault\b/y; | ||
rx.lastIndex = declared.required.end; | ||
if (rx.test(code)) { | ||
// import all | ||
if (rx.test(code) || importStyle(declared.required.value) === "default") { | ||
// import default | ||
s.overwrite( | ||
node.start, | ||
declared.left.start, | ||
"import * as " | ||
"import " | ||
); | ||
} else { | ||
// import default | ||
// import named | ||
s.overwrite( | ||
node.start, | ||
declared.left.start, | ||
"import " | ||
"import * as " | ||
); | ||
@@ -323,16 +325,20 @@ } | ||
function transform({parse, code, sourceMap = false} = {}) { | ||
function makeCallable(func) { | ||
if (typeof func === "function") { | ||
return func; | ||
} | ||
return () => func; | ||
} | ||
function transform({parse, code, sourceMap = false, importStyle = "named", exportStyle = "named"} = {}) { | ||
importStyle = makeCallable(importStyle); | ||
exportStyle = makeCallable(exportStyle); | ||
const s = new MagicString(code); | ||
let ast; | ||
try { | ||
ast = parse(code); | ||
} catch (err) { | ||
return; | ||
} | ||
const ast = parse(code); | ||
walk(ast, {enter(node, parent) { | ||
if (node.type === "VariableDeclaration" && parent.type === "Program") { | ||
transformImportDeclare({s, node, code}); | ||
transformImportDeclare({s, node, importStyle, code}); | ||
transformExportDeclare({s, node}); | ||
} else if (node.type === "AssignmentExpression" && parent.topLevel) { | ||
transformExportAssign({s, node}); | ||
transformExportAssign({s, node, exportStyle, code}); | ||
} else if (node.type === "ExpressionStatement" && parent.type === "Program") { | ||
@@ -339,0 +345,0 @@ node.topLevel = true; |
{ | ||
"name": "cjs-es", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Transform CommonJS module into ES module.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
107
README.md
cjs-es | ||
====== | ||
[![Build Status](https://travis-ci.org/eight04/cjs-es.svg?branch=master)](https://travis-ci.org/eight04/cjs-es) | ||
Transform CommonJS module into ES module. | ||
@@ -34,3 +36,3 @@ | ||
}); | ||
/* code -> ` | ||
/* code == ` | ||
function foo() {} | ||
@@ -40,9 +42,9 @@ function bar() {} | ||
export {bar}; | ||
` | ||
` */ | ||
``` | ||
Import problem | ||
-------------- | ||
Import style | ||
------------ | ||
There are two ways to transform the `require` statement when binding the module into one identifier: | ||
When binding the module into one identifier: | ||
@@ -53,26 +55,65 @@ ```js | ||
1. Prefer default import, which is the default behavior of the transformer: | ||
The transformer imports all members from the module by the default: | ||
```js | ||
import foo from "foo"; | ||
``` | ||
```js | ||
import * as foo from "foo"; | ||
``` | ||
2. Prefer named import: | ||
To import the default member, add `// default` comment: | ||
```js | ||
import * as foo from "foo"; | ||
``` | ||
You can switch to this behavior by specifying comment `// all` after `require`: | ||
```js | ||
const foo = require("foo"); // all | ||
``` | ||
```js | ||
const foo = require("foo"); // default | ||
``` | ||
Result: | ||
```js | ||
import foo from "foo"; | ||
``` | ||
The regex of import-all comment: | ||
Export style | ||
------------ | ||
If the `module.exports` is assigned with an object pattern: | ||
```js | ||
const foo = "foo"; | ||
const bar = "bar"; | ||
module.exports = { | ||
foo, | ||
bar | ||
}; | ||
``` | ||
The transformer converts it into named exports: | ||
```js | ||
const foo = "foo"; | ||
const bar = "bar"; | ||
export {foo}; | ||
export {bar}; | ||
``` | ||
```js | ||
/.+\/\/.+\b(all|import\b.\ball)\b/ | ||
``` | ||
If you like to export the entire object as the default member, you can use `// default` comment at the line of `module.exports`: | ||
```js | ||
const foo = "foo"; | ||
const bar = "bar"; | ||
module.exports = { // default | ||
foo, | ||
bar | ||
}; | ||
``` | ||
Result: | ||
```js | ||
const foo = "foo"; | ||
const bar = "bar"; | ||
export default { | ||
foo, | ||
bar | ||
}; | ||
``` | ||
API reference | ||
@@ -89,6 +130,13 @@ ------------- | ||
* `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` | ||
* `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` 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"` | ||
The result object has following members: | ||
@@ -102,4 +150,11 @@ | ||
* 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. |
@@ -10,6 +10,14 @@ /* eslint-env mocha */ | ||
it(dir, () => { | ||
const input = fs.readFileSync(`${__dirname}/cases/${dir}/input.js`, "utf8").replace(/\r/g, ""); | ||
const output = fs.readFileSync(`${__dirname}/cases/${dir}/output.js`, "utf8").replace(/\r/g, ""); | ||
const readFile = filename => { | ||
try { | ||
return fs.readFileSync(`${__dirname}/cases/${dir}/${filename}`, "utf8").replace(/\r/g, ""); | ||
} catch (err) { | ||
// pass | ||
} | ||
}; | ||
const options = JSON.parse(readFile("options.json") || "{}"); | ||
const input = readFile("input.js"); | ||
const output = readFile("output.js"); | ||
const actual = transform({code: input, parse}).code; | ||
const actual = transform(Object.assign({code: input, parse}, options)).code; | ||
assert.equal(actual, output); | ||
@@ -16,0 +24,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17160
41
462
156