Comparing version 1.0.1 to 1.1.0
62
index.js
@@ -63,5 +63,8 @@ module.exports.runTransform = runTransform; | ||
} | ||
case "import*": { | ||
case "import*": | ||
case "importDefault": { | ||
const { identifier, moduleName } = token; | ||
return `const ${identifier} = require(${moduleName})`; | ||
return `const ${identifier} = require(${moduleName})${ | ||
token.isDefaultImport ? ".default" : "" | ||
}`; | ||
} | ||
@@ -72,3 +75,3 @@ case "awaitImport": { | ||
case "export": { | ||
exportBuffer.items.push([token.identifier]); | ||
exportBuffer.items.push(token.modules); | ||
return ""; | ||
@@ -195,5 +198,5 @@ } | ||
// export const IDENTIFIER = ... | ||
// export function IDENTIFIER(...) ... | ||
// export class IDENTIFIER ... | ||
// export [default] const IDENTIFIER = ... | ||
// export [default] function IDENTIFIER(...) ... | ||
// export [default] class IDENTIFIER ... | ||
// export { ... } >> handleReExport() | ||
@@ -203,3 +206,3 @@ // export { ... } from "MODULE" >> handleReExport() | ||
LOOKING_FOR = "export pattern"; | ||
const skipStart = pos + "export ".length; | ||
let skipStart = pos + "export ".length; | ||
@@ -213,2 +216,9 @@ if (str.indexWithin("{", skipStart, 5, false) !== -1) { | ||
LOOKING_FOR = "identifier type (function|class|const) for export"; | ||
let isDefaultExport = false; | ||
if (str.indexWithin("default ", skipStart, DISTANCE, false) !== -1) { | ||
skipStart += 9; // 8 === "default ".length; | ||
isDefaultExport = true; | ||
} | ||
const typeEnd = str.indexWithin(" ", skipStart, 9); | ||
@@ -228,4 +238,9 @@ // 9 === "function".length + 1 | ||
const end = pos + 6; // 6 == "export".length; | ||
const end = pos + 6 + (isDefaultExport ? 8 : 0); | ||
// 6 == "export".length, 8 === "default ".length; | ||
const modules = isDefaultExport | ||
? ["default", str.slice(identifierStart, identifierEnd + 1)] | ||
: [str.slice(identifierStart, identifierEnd + 1)]; | ||
start = end + 1; | ||
@@ -236,3 +251,3 @@ return { | ||
end, | ||
identifier: str.slice(identifierStart, identifierEnd + 1) | ||
modules | ||
}; | ||
@@ -244,3 +259,7 @@ } | ||
LOOKING_FOR = "import name for import*"; | ||
const identifierStart = str.indexWithin("* as ", pos + 7, DISTANCE) + 5; | ||
let identifierStart = str.indexWithin("* as ", pos + 7, DISTANCE, false); | ||
if (identifierStart === -1) { | ||
return handleDefaultImport(); | ||
} | ||
identifierStart += 5; | ||
// 7 === "import ".length, 5 === "* as ".length | ||
@@ -269,2 +288,25 @@ const identifierEnd = str.indexWithin( | ||
// import IDENTIFIER from "MODULE" | ||
function handleDefaultImport() { | ||
LOOKING_FOR = "import name for default import"; | ||
const identifierStart = pos + 7; // 7 === "import ".length | ||
const identifierEnd = str.indexWithin(" ", identifierStart, DISTANCE); | ||
LOOKING_FOR = "name of imported module for import*"; | ||
let moduleStart = | ||
str.indexWithin("from ", identifierEnd + 1) + "from".length; | ||
moduleStart = str.indexWithin(quote, moduleStart + 1); | ||
const moduleEnd = str.indexWithin(quote, moduleStart + 1, lenModuleName); | ||
start = moduleEnd + 1; | ||
return { | ||
type: "importDefault", | ||
start: pos, | ||
end: moduleEnd, | ||
isDefaultImport: true, | ||
identifier: str.slice(identifierStart, identifierEnd), | ||
moduleName: str.slice(moduleStart, moduleEnd + 1) | ||
}; | ||
} | ||
// export { ... } from "..." | export { ... } | ||
@@ -271,0 +313,0 @@ function handleReExport() { |
{ | ||
"name": "esm-to-cjs", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Transform ESM to Common JS for present NodeJS, without any junk wrappers or useless renaming", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -98,5 +98,6 @@ ## esm-to-cjs | ||
- No default exports and imports! Please use named exports only. | ||
- `import * as foo from "bar";` is converted to `const foo = require("bar");`. Not sure if this is what everyone wants. I did it as per my project requirements. | ||
- No `export *` syntax. | ||
- Also, `import foo from "bar";` is converted to `const foo = require("bar").default;"`. | ||
- No support for `export *` syntax. | ||
- No mixing of default import, named imports and `import *` in same statement. | ||
@@ -103,0 +104,0 @@ ## Packages |
17787
341
197