@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.0.32 to 0.1.0
@@ -14,8 +14,4 @@ import TypeScript from 'typescript'; | ||
private convertForwards; | ||
private oldconvertToEsmExport; | ||
private stripModule; | ||
private stripWildcardExports; | ||
private requireTopScope; | ||
private createSyntheticDefaultExport; | ||
private addModuleToScope; | ||
private getAllCommonJsExportedNames; | ||
@@ -22,0 +18,0 @@ private getAllEsmExportedNames; |
@@ -29,14 +29,2 @@ "use strict"; | ||
const convertedForwards = this.convertForwards(withoutDefineProperty); | ||
/* | ||
const withoutWildcardExports = this.stripWildcardExports( | ||
withoutDefineProperty, | ||
) | ||
const requireInTopScope = this.requireTopScope(withoutWildcardExports) | ||
const esmExport = this.convertToEsmExport(requireInTopScope) | ||
const withEsmImport = this.convertToEsmImport(esmExport) | ||
const withSyntheticDefaultExport = this.createSyntheticDefaultExport( | ||
withEsmImport, | ||
) | ||
return this.addModuleToScope(withSyntheticDefaultExport) | ||
*/ | ||
const requireInTopScope = this.requireTopScope(convertedForwards); | ||
@@ -190,43 +178,2 @@ const withEsmExport = this.convertToEsmExport(requireInTopScope); | ||
} | ||
// exports.hello = something -> export { something as hello } | ||
oldconvertToEsmExport(sourceFile) { | ||
const newTopStatements = []; | ||
const newBottomStatements = []; | ||
const visit = (node) => { | ||
// exports.something = something; | ||
if (typescript_1.default.isPropertyAccessExpression(node) && | ||
typescript_1.default.isIdentifier(node.name) && | ||
typescript_1.default.isIdentifier(node.expression) && | ||
node.expression.text === KEYNAME.exports) { | ||
if (node.name.text in this.exportsVariableMap) { | ||
return typescript_1.default.factory.createIdentifier(this.exportsVariableMap[node.name.text]); | ||
} | ||
else { | ||
const newIdentifierName = this.generateUniqueName(); | ||
this.exportsVariableMap[node.name.text] = newIdentifierName; | ||
const identifier = typescript_1.default.factory.createIdentifier(newIdentifierName); | ||
newTopStatements.push(typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ | ||
typescript_1.default.factory.createVariableDeclaration(newIdentifierName, undefined, undefined, undefined), | ||
], typescript_1.default.NodeFlags.Let))); | ||
// exports.default = something; | ||
if (node.name.text === 'default') { | ||
newBottomStatements.push(typescript_1.default.factory.createExportAssignment(undefined, undefined, undefined, identifier)); | ||
} | ||
else { | ||
newBottomStatements.push(typescript_1.default.factory.createExportDeclaration(undefined, undefined, false, typescript_1.default.factory.createNamedExports([ | ||
typescript_1.default.factory.createExportSpecifier(newIdentifierName, node.name), | ||
]))); | ||
} | ||
return identifier; | ||
} | ||
} | ||
return typescript_1.default.visitEachChild(node, visit, this.context); | ||
}; | ||
const changedSourceFile = typescript_1.default.visitNode(sourceFile, visit); | ||
return typescript_1.default.factory.updateSourceFile(changedSourceFile, [ | ||
...newTopStatements, | ||
...changedSourceFile.statements, | ||
...newBottomStatements, | ||
]); | ||
} | ||
// module.exports -> exports | ||
@@ -246,36 +193,2 @@ stripModule(sourceFile) { | ||
} | ||
// exports = hello -> export.default = hello | ||
// exports = { a: 'a' } -> exports.a = 'a'; | ||
stripWildcardExports(sourceFile) { | ||
const visit = (node) => { | ||
// exports = SOMETHING | ||
if (typescript_1.default.isExpressionStatement(node) && | ||
typescript_1.default.isBinaryExpression(node.expression) && | ||
node.expression.operatorToken.kind === | ||
typescript_1.default.SyntaxKind.EqualsToken && | ||
typescript_1.default.isIdentifier(node.expression.left) && | ||
node.expression.left.text === KEYNAME.exports) { | ||
// exports = { a: 'a' } | ||
if (typescript_1.default.isObjectLiteralExpression(node.expression.right)) { | ||
const expressions = []; | ||
for (const property of node.expression.right.properties) { | ||
if ((typescript_1.default.isPropertyAssignment(property) || | ||
typescript_1.default.isShorthandPropertyAssignment(property)) && | ||
typescript_1.default.isIdentifier(property.name)) { | ||
expressions.push(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier(property.name.text)), node.expression.operatorToken, typescript_1.default.isShorthandPropertyAssignment(property) | ||
? property.name | ||
: property.initializer))); | ||
} | ||
} | ||
return expressions; | ||
} | ||
// exports = Something | ||
else { | ||
return typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(KEYNAME.exports), typescript_1.default.factory.createIdentifier('default')), node.expression.operatorToken, node.expression.right)); | ||
} | ||
} | ||
return typescript_1.default.visitEachChild(node, visit, this.context); | ||
}; | ||
return typescript_1.default.visitNode(sourceFile, visit); | ||
} | ||
// Move all require-calls to top-scope (const hello = require('hello.js')) | ||
@@ -319,49 +232,2 @@ requireTopScope(sourceFile) { | ||
} | ||
// export { hello as Hello } -> export default { something: hello } | ||
createSyntheticDefaultExport(sourceFile) { | ||
const exportSpecifiers = []; | ||
const visit = (node) => { | ||
if (typescript_1.default.isExportDeclaration(node) && | ||
node.exportClause && | ||
typescript_1.default.isNamedExports(node.exportClause)) { | ||
exportSpecifiers.push(...node.exportClause.elements); | ||
return undefined; | ||
} | ||
return node; | ||
}; | ||
const changedSourceFile = typescript_1.default.visitEachChild(sourceFile, visit, this.context); | ||
if (exportSpecifiers.length === 0) { | ||
return sourceFile; | ||
} | ||
const propertyAssignments = []; | ||
for (const exportSpecifier of exportSpecifiers) { | ||
if (exportSpecifier.propertyName && | ||
typescript_1.default.isIdentifier(exportSpecifier.propertyName)) { | ||
propertyAssignments.push(typescript_1.default.factory.createPropertyAssignment(exportSpecifier.name, exportSpecifier.propertyName)); | ||
} | ||
} | ||
const newStatements = [ | ||
...changedSourceFile.statements, | ||
typescript_1.default.factory.createExportDeclaration(undefined, undefined, false, typescript_1.default.factory.createNamedExports(exportSpecifiers)), | ||
]; | ||
// If there is not export default already -> add synthentic | ||
if (sourceFile.statements.every((node) => !typescript_1.default.isExportAssignment(node))) { | ||
newStatements.push(typescript_1.default.factory.createExportAssignment(undefined, undefined, undefined, typescript_1.default.factory.createObjectLiteralExpression(propertyAssignments))); | ||
} | ||
return typescript_1.default.factory.updateSourceFile(changedSourceFile, [ | ||
...newStatements, | ||
]); | ||
} | ||
// Add const exports = {}, module = { exports: exports }; | ||
addModuleToScope(sourceFile) { | ||
return typescript_1.default.factory.updateSourceFile(sourceFile, [ | ||
typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ | ||
typescript_1.default.factory.createVariableDeclaration('exports', undefined, undefined, typescript_1.default.factory.createObjectLiteralExpression()), | ||
typescript_1.default.factory.createVariableDeclaration('module', undefined, undefined, typescript_1.default.factory.createObjectLiteralExpression([ | ||
typescript_1.default.factory.createPropertyAssignment('exports', typescript_1.default.factory.createIdentifier('exports')), | ||
])), | ||
], typescript_1.default.NodeFlags.Let)), | ||
...sourceFile.statements, | ||
]); | ||
} | ||
getAllCommonJsExportedNames(sourceFile) { | ||
@@ -368,0 +234,0 @@ const exportedNames = new Set(); |
@@ -8,3 +8,3 @@ import TypeScript from 'typescript'; | ||
resolveFilePath(fileName: string): Promise<string>; | ||
fileExists(path: string): Promise<boolean>; | ||
private fileExists; | ||
} |
@@ -56,3 +56,3 @@ "use strict"; | ||
try { | ||
await fs_1.default.promises.readFile(path); | ||
await fs_1.default.promises.stat(path); | ||
} | ||
@@ -59,0 +59,0 @@ catch (error) { |
@@ -26,11 +26,8 @@ "use strict"; | ||
const relativeDir = path_1.default.relative(path_1.default.dirname(parentPath), pathObj.dir) || '.'; | ||
const result = relativeDir + '/' + pathObj.name + pathObj.ext; | ||
if (result.startsWith('.')) { | ||
return result.replace(/\.(ts|tsx|jsx|json)$/, '.js'); | ||
} | ||
return './' + result.replace(/\.(ts|tsx|jsx|json)$/, '.js'); | ||
const result = relativeDir + '/' + pathObj.name + '.js'; | ||
const posixResult = path_1.default.posix.normalize(result).replace(/\\/g, '/'); | ||
return posixResult.startsWith('.') ? posixResult : './' + posixResult; | ||
} | ||
// Return an aboslute path e.g. /tmp/a-apath/node_modules/hello/module.js | ||
resolveDependencyPath(parentPath, dependencyName) { | ||
const extension = path_1.default.extname(dependencyName); | ||
const directory = path_1.default.dirname(parentPath); | ||
@@ -37,0 +34,0 @@ const result = this.resolver({}, directory, dependencyName); |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.0.32", | ||
"version": "0.1.0", | ||
"license": "ISC", | ||
@@ -26,3 +26,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "78cde4c251a682cad33bf51e5590eea687027917" | ||
"gitHead": "d33899b423e79b4fc662e92b8a81ae7436985c29" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
83539
999