@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.0.20 to 0.0.21
@@ -278,16 +278,19 @@ "use strict"; | ||
} | ||
// Move all require-calls to top-scope | ||
// Move all require-calls to top-scope (const hello = require('hello.js')) | ||
requireTopScope(sourceFile) { | ||
const newStatements = []; | ||
const visit = (node) => { | ||
const inRootScope = node?.parent?.parent?.parent?.parent && | ||
// No need to convert already correct statements (const hello = require('hello.js')) | ||
if (node?.parent?.parent?.parent?.parent && | ||
typescript_1.default.isVariableDeclaration(node.parent) && | ||
typescript_1.default.isVariableDeclarationList(node.parent.parent) && | ||
typescript_1.default.isVariableStatement(node.parent.parent.parent) && | ||
typescript_1.default.isSourceFile(node.parent.parent.parent.parent); | ||
typescript_1.default.isSourceFile(node.parent.parent.parent.parent)) { | ||
return typescript_1.default.visitEachChild(node, visit, this.context); | ||
} | ||
if (typescript_1.default.isCallExpression(node) && | ||
typescript_1.default.isIdentifier(node.expression) && | ||
node.arguments.length === 1 && | ||
node.expression.text === 'require' && | ||
inRootScope === false) { | ||
node.expression.text === 'require') { | ||
// require() without a reference | ||
if (node.parent && typescript_1.default.isExpressionStatement(node.parent)) { | ||
@@ -298,2 +301,3 @@ newStatements.push(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(node.expression, undefined, node.arguments))); | ||
else { | ||
// require() with a reference (hello = require('hello.js')) | ||
const newIdentifierName = this.generateUniqueName(); | ||
@@ -300,0 +304,0 @@ newStatements.push(typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ |
@@ -23,3 +23,4 @@ "use strict"; | ||
typescript_1.default.isStringLiteral(node.moduleSpecifier)) { | ||
return typescript_1.default.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, typescript_1.default.factory.createStringLiteral(new DependencyResolver_1.default(sourceFile.fileName, this.context).resolveRelativeDependency(node.moduleSpecifier.text))); | ||
const resolvedName = new DependencyResolver_1.default(sourceFile.fileName, this.context).resolveRelativeDependency(node.moduleSpecifier.text); | ||
return typescript_1.default.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, typescript_1.default.factory.createStringLiteral(resolvedName)); | ||
} | ||
@@ -29,3 +30,4 @@ if (typescript_1.default.isExportDeclaration(node) && | ||
typescript_1.default.isStringLiteral(node.moduleSpecifier)) { | ||
return typescript_1.default.factory.updateExportDeclaration(node, undefined, undefined, node.isTypeOnly, undefined, node.moduleSpecifier); | ||
const resolvedName = new DependencyResolver_1.default(sourceFile.fileName, this.context).resolveRelativeDependency(node.moduleSpecifier.text); | ||
return typescript_1.default.factory.updateExportDeclaration(node, undefined, undefined, node.isTypeOnly, undefined, typescript_1.default.factory.createStringLiteral(resolvedName)); | ||
} | ||
@@ -32,0 +34,0 @@ return node; |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
94010
44
1102