Comparing version 0.4.1 to 0.4.2
@@ -69,2 +69,3 @@ "use strict"; | ||
} | ||
// 🚧-②: At present, "ancestors" contains only one depth of "MemberExpression" | ||
if (/Program,VariableDeclaration,VariableDeclarator,(MemberExpression,)?CallExpression$/.test(ances)) { | ||
@@ -71,0 +72,0 @@ // const foo = require('foo') |
import { Analyzed } from './analyze'; | ||
import { AcornNode } from './types'; | ||
export interface ExportsRuntime { | ||
polyfill: string; | ||
exportDefault?: { | ||
node: AcornNode; | ||
nodes: AcornNode[]; | ||
name: string; | ||
@@ -8,0 +7,0 @@ statement: string; |
@@ -5,3 +5,2 @@ "use strict"; | ||
function generateExport(analyzed) { | ||
var _a; | ||
if (!analyzed.exports.length) { | ||
@@ -11,10 +10,9 @@ return null; | ||
let exportDefault; | ||
const moduleExports = (_a = [...analyzed.exports] | ||
// If there are multiple module.exports in one file, we need to get the last one | ||
.reverse() | ||
.find(exp => exp.token.left === 'module')) === null || _a === void 0 ? void 0 : _a.node; | ||
if (moduleExports) { | ||
const moduleExports = analyzed.exports | ||
.filter(exp => exp.token.left === 'module') | ||
.map(exp => exp.node); | ||
if (moduleExports.length) { | ||
const name = '__CJS__export_default__'; | ||
exportDefault = { | ||
node: moduleExports, | ||
nodes: moduleExports, | ||
name, | ||
@@ -37,5 +35,4 @@ statement: `export { ${name} as default }` | ||
} | ||
`; | ||
`.trim(); | ||
return { | ||
polyfill: `const module = { exports: {} }; const exports = module.exports;`, | ||
exportDefault, | ||
@@ -46,5 +43,1 @@ exportMembers, | ||
exports.generateExport = generateExport; | ||
function removeDuplicationExports(exports) { | ||
// If there are multiple module.exports in one file, we need to get the last one | ||
// If there are multiple exports.foo in one file, we need to get the last one | ||
} |
@@ -16,3 +16,3 @@ "use strict"; | ||
}; | ||
const importName = `__CJS__promotion__import__${count++}__`; | ||
const importName = `__CJS__import__${count++}__`; | ||
// TODO: Dynamic require id, e.g. require('path/' + filename) | ||
@@ -28,3 +28,6 @@ let requireId; | ||
if (!requireId && !functionScopeNode) { | ||
throw new Error(`Not supported statement: ${analyzed.code.slice(node.start, node.end)}`); | ||
const codeSnippets = analyzed.code.slice(node.start, node.end); | ||
throw new Error(`The following require statement cannot be converted. | ||
-> ${codeSnippets} | ||
${'^'.repeat(codeSnippets.length)}`); | ||
} | ||
@@ -40,3 +43,4 @@ if (topScopeNode) { | ||
const VariableDeclarator = topScopeNode.declarations[0]; | ||
const { /* Left */ id, /* Right */ init } = VariableDeclarator; | ||
const { /* L-V */ id, /* R-V */ init } = VariableDeclarator; | ||
// Left value | ||
let LV; | ||
@@ -52,2 +56,9 @@ if (id.type === 'Identifier') { | ||
} | ||
else { | ||
throw new Error(`Unknown VariableDeclarator.id.type(L-V): ${id.type}`); | ||
} | ||
const LV_str = (spe) => typeof LV === 'object' | ||
? LV.map(e => e.key === e.value ? e.key : `${e.key} ${spe} ${e.value}`).join(', ') | ||
: ''; | ||
// Right value | ||
if (init.type === 'CallExpression') { | ||
@@ -59,40 +70,41 @@ if (typeof LV === 'string') { | ||
else { | ||
const str = LV | ||
.map(e => e.key === e.value ? e.key : `${e.key} as ${e.value}`) | ||
.join(', '); | ||
// const { parse } = require('acorn') | ||
impt.importee = `import { ${str} } from '${requireId}'`; | ||
impt.importee = `import { ${LV_str('as')} } from '${requireId}'`; | ||
} | ||
} | ||
else if (init.type === 'MemberExpression') { | ||
const members = ancestors | ||
.filter(an => an.type === 'MemberExpression') | ||
.map(an => an.property.name); | ||
// 🚧-② | ||
const onlyOneMember = ancestors.find(an => an.type === 'MemberExpression').property.name; | ||
const importDefault = onlyOneMember === 'default'; | ||
if (typeof LV === 'string') { | ||
if (members.length === 1) { | ||
if (members[0] === 'default') { | ||
// const acorn = require('acorn').default | ||
impt.importee = `import ${LV} from '${requireId}'`; | ||
} | ||
else { | ||
impt.importee = members[0] === LV | ||
// const parse = require('acorn').parse | ||
? `import { ${LV} } from '${requireId}'` | ||
// const parse2 = require('acorn').parse | ||
: `import { ${members[0]} as ${LV} } from '${requireId}'`; | ||
} | ||
if (importDefault) { | ||
// const foo = require('foo').default | ||
impt.importee = `import ${LV} from '${requireId}'`; | ||
} | ||
else { | ||
impt.importee = `import * as ${importName} from '${requireId}'`; | ||
// const bar = require('id').foo.bar | ||
impt.declaration = `const ${LV} = ${importName}.${members.join('.')}`; | ||
impt.importee = onlyOneMember === LV | ||
// const bar = require('foo').bar | ||
? `import { ${LV} } from '${requireId}'` | ||
// const barAlias = require('foo').bar | ||
: `import { ${onlyOneMember} as ${LV} } from '${requireId}'`; | ||
} | ||
} | ||
else { | ||
impt.importee = `import * as ${importName} from '${requireId}'`; | ||
// const { bar } = require('id').foo | ||
impt.declaration = `const { ${LV.join(', ')} } = ${importName}.${members.join('.')}`; | ||
if (importDefault) { | ||
// const { member1, member2 } = require('foo').default | ||
impt.importee = `import ${importName} from '${requireId}'`; | ||
} | ||
else { | ||
// const { member1, member2 } = require('foo').bar | ||
impt.importee = `import { ${onlyOneMember} as ${importName} } from '${requireId}'`; | ||
} | ||
impt.declaration = `const { ${LV_str(':')} } = ${importName}`; | ||
} | ||
} | ||
else { | ||
throw new Error(`Unknown VariableDeclarator.init.type(R-V): ${id.init}`); | ||
} | ||
break; | ||
default: | ||
throw new Error(`Unknown TopScopeType: ${topScopeNode}`); | ||
} | ||
@@ -99,0 +111,0 @@ } |
@@ -53,7 +53,11 @@ "use strict"; | ||
if (exportRuntime) { | ||
let moduleRuntime = 'const module = { exports: {} }; const exports = module.exports;'; | ||
if (exportRuntime.exportDefault) { | ||
const { start } = exportRuntime.exportDefault.node; | ||
ms.appendRight(start, `const ${exportRuntime.exportDefault.name} = `); | ||
const { nodes, name } = exportRuntime.exportDefault; | ||
moduleRuntime += ` let ${name} = undefined;`; | ||
for (const node of nodes) { | ||
ms.appendLeft(node.start, `${name} = `); | ||
} | ||
} | ||
const polyfill = ['/* export-runtime-S */', exportRuntime.polyfill, '/* export-runtime-E */'].join(' '); | ||
const polyfill = ['/* export-runtime-S */', moduleRuntime, '/* export-runtime-E */'].join(' '); | ||
const _exports = [ | ||
@@ -60,0 +64,0 @@ '/* export-statement-S */', |
{ | ||
"name": "cjs-esm", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "Another CommonJs transform ESModule lib.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
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
19213
430