Comparing version 0.8.2 to 0.9.0
22
index.js
@@ -5,5 +5,5 @@ const {createContext} = require("./lib/context"); | ||
function transform(options) { | ||
async function transform(options) { | ||
const context = createContext(options); | ||
const analyzer = createAnalyzer(context); | ||
const analyzer = await createAnalyzer(context); | ||
@@ -23,17 +23,17 @@ try { | ||
) { | ||
return Promise.resolve({ | ||
return { | ||
code: context.code, | ||
map: null, | ||
isTouched: false | ||
}); | ||
}; | ||
} | ||
return createWriter(context).write() | ||
.then(() => ({ | ||
code: context.s.toString(), | ||
map: options.sourceMap && context.s.generateMap({hires: true}), | ||
isTouched: true, | ||
context: context | ||
})); | ||
await createWriter(context).write(); | ||
return { | ||
code: context.s.toString(), | ||
map: options.sourceMap && context.s.generateMap({hires: true}), | ||
isTouched: true, | ||
context: context | ||
}; | ||
} | ||
module.exports = {transform}; |
@@ -1,3 +0,1 @@ | ||
const {walk} = require("estree-walker"); | ||
const isReference = require("is-reference"); | ||
const { | ||
@@ -13,3 +11,7 @@ getDeclareImport, | ||
function createAnalyzer(context) { | ||
async function createAnalyzer(context) { | ||
const [{walk}, {default: isReference}] = await Promise.all([ | ||
import("estree-walker"), | ||
import("is-reference") | ||
]); | ||
context.requireNodes = []; | ||
@@ -16,0 +18,0 @@ context.moduleNodes = []; |
@@ -403,2 +403,3 @@ function createExportWriter(context) { | ||
let assignment = 0; | ||
let childAssignment = 0; | ||
for (const node of nodes) { | ||
@@ -414,2 +415,4 @@ if (node.exported) { | ||
assignment++; | ||
} else { | ||
childAssignment++; | ||
} | ||
@@ -423,3 +426,6 @@ } else if ( | ||
} | ||
if (init === 1) { | ||
// FIXME: find a way to detect whether the init statement is the first access to the variable | ||
// so we can also use writeDefaultExportDeclare when nodes.length > 1 | ||
// https://github.com/eight04/cjs-es/issues/28 | ||
if (init === 1 && nodes.length === 1) { | ||
for (const node of nodes) { | ||
@@ -433,10 +439,11 @@ if (node.exported && node.exported.statement && !node.exported.name) { | ||
} else { | ||
const topIndex = nodes.reduce( | ||
(r, n) => n.rootPos < r ? n.rootPos : r, | ||
Infinity | ||
); | ||
// NOTE: we can't actually use topIndex... the script may access _module_exports_ indirectly through function call. | ||
// const topIndex = nodes.reduce( | ||
// (r, n) => n.rootPos < r ? n.rootPos : r, | ||
// Infinity | ||
// ); | ||
const kind = assignment ? "let" : "const"; | ||
const defaultValue = !assignment || context.needDefaultObject ? " = {}" : ""; | ||
const defaultValue = assignment + childAssignment === nodes.length && !context.needDefaultObject ? "" : " = {}"; | ||
context.s.appendRight( | ||
topIndex, | ||
0, | ||
`${kind} _module_exports_${defaultValue};\nexport {_module_exports_ as default};\n` | ||
@@ -443,0 +450,0 @@ ); |
@@ -1,2 +0,2 @@ | ||
const {attachScopes} = require("rollup-pluginutils"); | ||
const {attachScopes} = require("@rollup/pluginutils"); | ||
@@ -3,0 +3,0 @@ const RX_DEFAULT = /.*?\/\/.*\bdefault\b/y; |
{ | ||
"name": "cjs-es", | ||
"version": "0.8.2", | ||
"version": "0.9.0", | ||
"description": "Transform CommonJS module into ES module.", | ||
@@ -18,3 +18,3 @@ "keywords": [ | ||
"scripts": { | ||
"test": "eslint **/*.js --cache && c8 --reporter=lcov mocha", | ||
"test": "eslint . --cache && c8 --reporter=lcov mocha -b", | ||
"preversion": "npm test", | ||
@@ -27,14 +27,14 @@ "postversion": "git push --follow-tags && npm publish" | ||
"devDependencies": { | ||
"acorn": "^6.1.1", | ||
"c8": "^5.0.1", | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.1.4", | ||
"sinon": "^7.3.2" | ||
"acorn": "^8.8.0", | ||
"c8": "^7.12.0", | ||
"eslint": "^8.21.0", | ||
"mocha": "^10.0.0", | ||
"sinon": "^14.0.0" | ||
}, | ||
"dependencies": { | ||
"estree-walker": "^0.6.1", | ||
"is-reference": "^1.1.2", | ||
"magic-string": "^0.25.2", | ||
"rollup-pluginutils": "^2.8.1" | ||
"@rollup/pluginutils": "^4.2.1", | ||
"estree-walker": "^3.0.1", | ||
"is-reference": "^3.0.0", | ||
"magic-string": "^0.26.2" | ||
} | ||
} |
cjs-es | ||
====== | ||
[![Build Status](https://travis-ci.com/eight04/cjs-es.svg?branch=master)](https://travis-ci.com/eight04/cjs-es) | ||
[![.github/workflows/build.yml](https://github.com/eight04/cjs-es/actions/workflows/build.yml/badge.svg)](https://github.com/eight04/cjs-es/actions/workflows/build.yml) | ||
[![codecov](https://codecov.io/gh/eight04/cjs-es/branch/master/graph/badge.svg)](https://codecov.io/gh/eight04/cjs-es) | ||
@@ -31,3 +31,3 @@ [![install size](https://packagephobia.now.sh/badge?p=cjs-es)](https://packagephobia.now.sh/result?p=cjs-es) | ||
`; | ||
transform({code, parse}) | ||
transform({code, ast: parse(code, {ecmaVersion: "latest"})}) | ||
.then(result => { | ||
@@ -291,3 +291,3 @@ console.log(result.code); | ||
exportStyle?: String | async () => String, | ||
nested?: Boolean, | ||
nested?: Boolean = false, | ||
warn?: (message: String, pos: Number) => void | ||
@@ -312,2 +312,4 @@ }) | ||
* `nested` - By default, only top-level nodes are analyzed and transformed. To analyze the entire tree, set this to true. | ||
* `warn` - the transformer uses `warn` function to emit a warning. If `warn` is not set then the transformer will print the message to the console using `console.error`. | ||
@@ -336,2 +338,7 @@ | ||
* 0.9.0 (Aug 8, 2022) | ||
- Bump dependencies. | ||
- Fix: always put module wrapper at the top. | ||
* 0.8.2 (Jul 2, 2019) | ||
@@ -338,0 +345,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
1364
476
54587
+ Added@rollup/pluginutils@^4.2.1
+ Added@rollup/pluginutils@4.2.1(transitive)
+ Addedestree-walker@2.0.23.0.3(transitive)
+ Addedis-reference@3.0.2(transitive)
+ Addedmagic-string@0.26.7(transitive)
+ Addedpicomatch@2.3.1(transitive)
- Removedrollup-pluginutils@^2.8.1
- Removedestree-walker@0.6.1(transitive)
- Removedis-reference@1.2.1(transitive)
- Removedmagic-string@0.25.9(transitive)
- Removedrollup-pluginutils@2.8.2(transitive)
Updatedestree-walker@^3.0.1
Updatedis-reference@^3.0.0
Updatedmagic-string@^0.26.2