@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -52,2 +52,13 @@ import CommonJsTransformer from '../../src/transformers/CommonJsTransformer' | ||
}) | ||
it('Should move exports to top scope', async () => { | ||
const input = '{ exports = Hello }' | ||
const output = ` | ||
var GENERATED_VAR_BY_TRANSFORMER_1; | ||
{ GENERATED_VAR_BY_TRANSFORMER_1 = Hello} | ||
export default GENERATED_VAR_BY_TRANSFORMER_1; | ||
` | ||
expect(await transformWithPlugin(input)).toBe( | ||
await transformWithoutPlugin(output), | ||
) | ||
}) | ||
it('Should convert exports.name with same name as the parameter', async () => { | ||
@@ -54,0 +65,0 @@ const input = 'const hello = null; exports.hello = hello' |
@@ -1,2 +0,5 @@ | ||
import TsTranspiler from './TsTranspiler'; | ||
export { TsTranspiler }; | ||
export { default as TsTranspiler } from './TsTranspiler'; | ||
export { default as CommonJsTransformer } from './transformers/CommonJsTransformer'; | ||
export { default as NodeEnvTransformer } from './transformers/NodeEnvTransformer'; | ||
export { default as ResolveTransformer } from './transformers/ResolveTransformer'; | ||
export { default as CodeOptimizerTransformer } from './transformers/CodeOptimizerTransformer'; |
@@ -6,5 +6,13 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TsTranspiler = void 0; | ||
const TsTranspiler_1 = __importDefault(require("./TsTranspiler")); | ||
exports.TsTranspiler = TsTranspiler_1.default; | ||
exports.CodeOptimizerTransformer = exports.ResolveTransformer = exports.NodeEnvTransformer = exports.CommonJsTransformer = exports.TsTranspiler = void 0; | ||
var TsTranspiler_1 = require("./TsTranspiler"); | ||
Object.defineProperty(exports, "TsTranspiler", { enumerable: true, get: function () { return __importDefault(TsTranspiler_1).default; } }); | ||
var CommonJsTransformer_1 = require("./transformers/CommonJsTransformer"); | ||
Object.defineProperty(exports, "CommonJsTransformer", { enumerable: true, get: function () { return __importDefault(CommonJsTransformer_1).default; } }); | ||
var NodeEnvTransformer_1 = require("./transformers/NodeEnvTransformer"); | ||
Object.defineProperty(exports, "NodeEnvTransformer", { enumerable: true, get: function () { return __importDefault(NodeEnvTransformer_1).default; } }); | ||
var ResolveTransformer_1 = require("./transformers/ResolveTransformer"); | ||
Object.defineProperty(exports, "ResolveTransformer", { enumerable: true, get: function () { return __importDefault(ResolveTransformer_1).default; } }); | ||
var CodeOptimizerTransformer_1 = require("./transformers/CodeOptimizerTransformer"); | ||
Object.defineProperty(exports, "CodeOptimizerTransformer", { enumerable: true, get: function () { return __importDefault(CodeOptimizerTransformer_1).default; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -13,3 +13,4 @@ import TypeScript from 'typescript'; | ||
private stripModule; | ||
private exportsTopScope; | ||
private requireTopScope; | ||
} |
@@ -23,3 +23,4 @@ "use strict"; | ||
const withoutDefineProperty = this.convertDefinePropery(withoutModule); | ||
const esmExport = this.convertToEsmExport(withoutDefineProperty); | ||
const exportsTopScope = this.exportsTopScope(withoutDefineProperty); | ||
const esmExport = this.convertToEsmExport(exportsTopScope); | ||
return this.convertToEsmImport(esmExport); | ||
@@ -213,2 +214,29 @@ } | ||
// Move all require-calls to top-scope | ||
exportsTopScope(sourceFile) { | ||
const newTopStatements = []; | ||
const newBottomStatements = []; | ||
const visit = (node) => { | ||
const inRootScope = node?.parent?.parent && typescript_1.default.isSourceFile(node.parent.parent); | ||
if (inRootScope === false && | ||
typescript_1.default.isBinaryExpression(node) && | ||
typescript_1.default.isIdentifier(node.left) && | ||
node.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken) { | ||
const newIdentifierName = this.generateUniqueName(); | ||
const newIdentifier = typescript_1.default.factory.createIdentifier(newIdentifierName); | ||
newTopStatements.push(typescript_1.default.factory.createVariableStatement(undefined, [ | ||
typescript_1.default.factory.createVariableDeclaration(newIdentifierName, undefined, undefined, undefined), | ||
])); | ||
newBottomStatements.push(typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createBinaryExpression(node.left, node.operatorToken, newIdentifier))); | ||
return typescript_1.default.factory.createBinaryExpression(newIdentifier, node.operatorToken, node.right); | ||
} | ||
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, | ||
]); | ||
} | ||
// Move all require-calls to top-scope | ||
requireTopScope(sourceFile) { | ||
@@ -215,0 +243,0 @@ const newStatements = []; |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -1,2 +0,5 @@ | ||
import TsTranspiler from './TsTranspiler' | ||
export { TsTranspiler } | ||
export { default as TsTranspiler } from './TsTranspiler' | ||
export { default as CommonJsTransformer } from './transformers/CommonJsTransformer' | ||
export { default as NodeEnvTransformer } from './transformers/NodeEnvTransformer' | ||
export { default as ResolveTransformer } from './transformers/ResolveTransformer' | ||
export { default as CodeOptimizerTransformer } from './transformers/CodeOptimizerTransformer' |
@@ -14,3 +14,3 @@ import TypeScript from 'typescript' | ||
} | ||
transformBundle(): TypeScript.Bundle { | ||
public transformBundle(): TypeScript.Bundle { | ||
throw new Error('Method not implemented.') | ||
@@ -24,3 +24,4 @@ } | ||
const withoutDefineProperty = this.convertDefinePropery(withoutModule) | ||
const esmExport = this.convertToEsmExport(withoutDefineProperty) | ||
const exportsTopScope = this.exportsTopScope(withoutDefineProperty) | ||
const esmExport = this.convertToEsmExport(exportsTopScope) | ||
return this.convertToEsmImport(esmExport) | ||
@@ -364,2 +365,55 @@ } | ||
// Move all require-calls to top-scope | ||
private exportsTopScope( | ||
sourceFile: TypeScript.SourceFile, | ||
): TypeScript.SourceFile { | ||
const newTopStatements: TypeScript.VariableStatement[] = [] | ||
const newBottomStatements: TypeScript.Statement[] = [] | ||
const visit = (node: TypeScript.Node): TypeScript.Node => { | ||
const inRootScope = | ||
node?.parent?.parent && TypeScript.isSourceFile(node.parent.parent) | ||
if ( | ||
inRootScope === false && | ||
TypeScript.isBinaryExpression(node) && | ||
TypeScript.isIdentifier(node.left) && | ||
node.operatorToken.kind === TypeScript.SyntaxKind.EqualsToken | ||
) { | ||
const newIdentifierName = this.generateUniqueName() | ||
const newIdentifier = TypeScript.factory.createIdentifier( | ||
newIdentifierName, | ||
) | ||
newTopStatements.push( | ||
TypeScript.factory.createVariableStatement(undefined, [ | ||
TypeScript.factory.createVariableDeclaration( | ||
newIdentifierName, | ||
undefined, | ||
undefined, | ||
undefined, | ||
), | ||
]), | ||
) | ||
newBottomStatements.push( | ||
TypeScript.factory.createExpressionStatement( | ||
TypeScript.factory.createBinaryExpression( | ||
node.left, | ||
node.operatorToken, | ||
newIdentifier, | ||
), | ||
), | ||
) | ||
return TypeScript.factory.createBinaryExpression( | ||
newIdentifier, | ||
node.operatorToken, | ||
node.right, | ||
) | ||
} | ||
return TypeScript.visitEachChild(node, visit, this.context) | ||
} | ||
const changedSourceFile = TypeScript.visitNode(sourceFile, visit) | ||
return TypeScript.factory.updateSourceFile(changedSourceFile, [ | ||
...newTopStatements, | ||
...changedSourceFile.statements, | ||
...newBottomStatements, | ||
]) | ||
} | ||
// Move all require-calls to top-scope | ||
private requireTopScope( | ||
@@ -366,0 +420,0 @@ sourceFile: TypeScript.SourceFile, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
105269
2037
0