@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.0.16 to 0.0.17
@@ -95,2 +95,11 @@ import CommonJsTransformer from '../../src/transformers/CommonJsTransformer' | ||
}) | ||
it('Should not override current export', async () => { | ||
const input = ` | ||
exports.a = "a"; | ||
exports.b = "b"; | ||
export { hello as a }; | ||
export default "yoyo"; | ||
` | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
}) | ||
it('Should export nested expression', async () => { | ||
@@ -97,0 +106,0 @@ const input = ` |
@@ -20,4 +20,5 @@ import TypeScript from 'typescript'; | ||
private addModuleToScope; | ||
private getAllExportedNames; | ||
private getAllCommonJsExportedNames; | ||
private getAllEsmExportedNames; | ||
private convertToEsmExport; | ||
} |
@@ -359,3 +359,3 @@ "use strict"; | ||
} | ||
getAllExportedNames(sourceFile) { | ||
getAllCommonJsExportedNames(sourceFile) { | ||
const exportedNames = new Set(); | ||
@@ -390,4 +390,22 @@ const visit = (node) => { | ||
} | ||
getAllEsmExportedNames(sourceFile) { | ||
const list = []; | ||
for (const statement of sourceFile.statements) { | ||
if (typescript_1.default.isExportAssignment(statement)) { | ||
list.push(KEYNAME.default); | ||
} | ||
else if (typescript_1.default.isExportDeclaration(statement) && | ||
statement.exportClause && | ||
typescript_1.default.isNamedExports(statement.exportClause) && | ||
statement.exportClause.elements) { | ||
for (const element of statement.exportClause.elements) { | ||
list.push(element.name.text); | ||
} | ||
} | ||
} | ||
return list; | ||
} | ||
convertToEsmExport(sourceFile) { | ||
const exportedNames = this.getAllExportedNames(sourceFile); | ||
const esmExportedNames = this.getAllEsmExportedNames(sourceFile); | ||
const exportedNames = this.getAllCommonJsExportedNames(sourceFile).filter((name) => !esmExportedNames.includes(name)); | ||
const moduleExportStatement = typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ | ||
@@ -411,3 +429,5 @@ typescript_1.default.factory.createVariableDeclaration(KEYNAME.exports, undefined, undefined, typescript_1.default.factory.createObjectLiteralExpression()), | ||
} | ||
if (!exportedNames.includes(KEYNAME.default)) { | ||
// If there is no default export already | ||
if (!exportedNames.includes(KEYNAME.default) && | ||
!esmExportedNames.includes(KEYNAME.default)) { | ||
statements.push(typescript_1.default.factory.createExportAssignment(undefined, undefined, undefined, typescript_1.default.factory.createIdentifier(KEYNAME.exports))); | ||
@@ -414,0 +434,0 @@ } |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -620,3 +620,5 @@ import TypeScript from 'typescript' | ||
} | ||
private getAllExportedNames(sourceFile: TypeScript.SourceFile): string[] { | ||
private getAllCommonJsExportedNames( | ||
sourceFile: TypeScript.SourceFile, | ||
): string[] { | ||
const exportedNames: Set<string> = new Set() | ||
@@ -659,6 +661,27 @@ const visit = ( | ||
} | ||
private getAllEsmExportedNames(sourceFile: TypeScript.SourceFile): string[] { | ||
const list: string[] = [] | ||
for (const statement of sourceFile.statements) { | ||
if (TypeScript.isExportAssignment(statement)) { | ||
list.push(KEYNAME.default) | ||
} else if ( | ||
TypeScript.isExportDeclaration(statement) && | ||
statement.exportClause && | ||
TypeScript.isNamedExports(statement.exportClause) && | ||
statement.exportClause.elements | ||
) { | ||
for (const element of statement.exportClause.elements) { | ||
list.push(element.name.text) | ||
} | ||
} | ||
} | ||
return list | ||
} | ||
private convertToEsmExport( | ||
sourceFile: TypeScript.SourceFile, | ||
): TypeScript.SourceFile { | ||
const exportedNames = this.getAllExportedNames(sourceFile) | ||
const esmExportedNames = this.getAllEsmExportedNames(sourceFile) | ||
const exportedNames = this.getAllCommonJsExportedNames(sourceFile).filter( | ||
(name) => !esmExportedNames.includes(name), | ||
) | ||
const moduleExportStatement = TypeScript.factory.createVariableStatement( | ||
@@ -689,3 +712,2 @@ undefined, | ||
) | ||
const statements = [moduleExportStatement, ...sourceFile.statements] | ||
@@ -731,3 +753,7 @@ if (exportedNames.length) { | ||
} | ||
if (!exportedNames.includes(KEYNAME.default)) { | ||
// If there is no default export already | ||
if ( | ||
!exportedNames.includes(KEYNAME.default) && | ||
!esmExportedNames.includes(KEYNAME.default) | ||
) { | ||
statements.push( | ||
@@ -734,0 +760,0 @@ TypeScript.factory.createExportAssignment( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
150090
2745