@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -9,4 +9,2 @@ import CommonJsTransformer from '../../src/transformers/CommonJsTransformer' | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = 'import "./hello.ts";' | ||
expect(await transformWithPlugin(input)).toBe(output) | ||
}) | ||
@@ -20,6 +18,2 @@ it('Should convert default import to ES6', async () => { | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
/* | ||
const output = 'import { Hello } from "./hello.ts";' | ||
expect(await transformWithPlugin(input)).toBe(output) | ||
*/ | ||
}) | ||
@@ -29,8 +23,2 @@ it('Should convert require in sub-scope', async () => { | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
/* | ||
'import * as GENERATED_VAR_BY_TRANSFORMER_1 from "hello.js"; { const hello = GENERATED_VAR_BY_TRANSFORMER_1; }' | ||
const output = | ||
expect(await transformWithPlugin(input)).toBe( | ||
await transformWithoutPlugin(output), | ||
) */ | ||
}) | ||
@@ -46,14 +34,5 @@ }) | ||
const WrenchIcon = require('./icons/WrenchIcon.js'); | ||
module.exports = { | ||
WrenchIcon, | ||
} | ||
module.exports = { WrenchIcon } | ||
` | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = ` | ||
let GENERATED_VAR_BY_TRANSFORMER_1; | ||
import * as WrenchIcon from './icons/WrenchIcon.js'; | ||
GENERATED_VAR_BY_TRANSFORMER_1 = WrenchIcon; | ||
export { GENERATED_VAR_BY_TRANSFORMER_1 as WrenchIcon }; | ||
` | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
}) | ||
@@ -69,6 +48,2 @@ }) | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = `let 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), | ||
) | ||
}) | ||
@@ -75,0 +50,0 @@ it('Should convert exports.name', async () => { |
@@ -9,2 +9,3 @@ import TypeScript from 'typescript'; | ||
transformBundle(): TypeScript.Bundle; | ||
private isCommonJsModule; | ||
private generateUniqueName; | ||
@@ -18,2 +19,3 @@ private convertDefinePropery; | ||
private createSyntheticDefaultExport; | ||
private addModuleToScope; | ||
} |
@@ -18,2 +18,5 @@ "use strict"; | ||
transformSourceFile(sourceFile) { | ||
if (this.isCommonJsModule(sourceFile) === false) { | ||
return sourceFile; | ||
} | ||
const withoutModule = this.stripModule(sourceFile); | ||
@@ -25,3 +28,4 @@ const withoutDefineProperty = this.convertDefinePropery(withoutModule); | ||
const withEsmImport = this.convertToEsmImport(esmExport); | ||
return this.createSyntheticDefaultExport(withEsmImport); | ||
const withSyntheticDefaultExport = this.createSyntheticDefaultExport(withEsmImport); | ||
return this.addModuleToScope(withSyntheticDefaultExport); | ||
} | ||
@@ -31,2 +35,5 @@ transformBundle() { | ||
} | ||
isCommonJsModule(sourceFile) { | ||
return /require|exports/.test(sourceFile.text); | ||
} | ||
// Generate a file-unique variable name | ||
@@ -304,4 +311,16 @@ generateUniqueName() { | ||
} | ||
// 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.Const)), | ||
...sourceFile.statements, | ||
]); | ||
} | ||
} | ||
exports.default = CommonJsTransformer; | ||
//# sourceMappingURL=CommonJsTransformer.js.map |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -18,2 +18,5 @@ import TypeScript from 'typescript' | ||
): TypeScript.SourceFile { | ||
if (this.isCommonJsModule(sourceFile) === false) { | ||
return sourceFile | ||
} | ||
const withoutModule = this.stripModule(sourceFile) | ||
@@ -27,3 +30,6 @@ const withoutDefineProperty = this.convertDefinePropery(withoutModule) | ||
const withEsmImport = this.convertToEsmImport(esmExport) | ||
return this.createSyntheticDefaultExport(withEsmImport) | ||
const withSyntheticDefaultExport = this.createSyntheticDefaultExport( | ||
withEsmImport, | ||
) | ||
return this.addModuleToScope(withSyntheticDefaultExport) | ||
} | ||
@@ -33,2 +39,5 @@ public transformBundle(): TypeScript.Bundle { | ||
} | ||
private isCommonJsModule(sourceFile: TypeScript.SourceFile): boolean { | ||
return /require|exports/.test(sourceFile.text) | ||
} | ||
// Generate a file-unique variable name | ||
@@ -521,2 +530,35 @@ private generateUniqueName() { | ||
} | ||
// Add const exports = {}, module = { exports: exports }; | ||
private addModuleToScope( | ||
sourceFile: TypeScript.SourceFile, | ||
): TypeScript.SourceFile { | ||
return TypeScript.factory.updateSourceFile(sourceFile, [ | ||
TypeScript.factory.createVariableStatement( | ||
undefined, | ||
TypeScript.factory.createVariableDeclarationList( | ||
[ | ||
TypeScript.factory.createVariableDeclaration( | ||
'exports', | ||
undefined, | ||
undefined, | ||
TypeScript.factory.createObjectLiteralExpression(), | ||
), | ||
TypeScript.factory.createVariableDeclaration( | ||
'module', | ||
undefined, | ||
undefined, | ||
TypeScript.factory.createObjectLiteralExpression([ | ||
TypeScript.factory.createPropertyAssignment( | ||
'exports', | ||
TypeScript.factory.createIdentifier('exports'), | ||
), | ||
]), | ||
), | ||
], | ||
TypeScript.NodeFlags.Const, | ||
), | ||
), | ||
...sourceFile.statements, | ||
]) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
125371
2303