@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -6,2 +6,1 @@ import ModuleB from "./ModuleB.ts"; | ||
export default ModuleA; | ||
//# sourceMappingURL=ModuleA.js.map |
@@ -13,6 +13,4 @@ import CommonJsTransformer from '../../src/transformers/CommonJsTransformer' | ||
it('Should convert default import to ES6', async () => { | ||
const input = 'var Hello = require("./hello.ts")' | ||
const input = 'let Hello = require("./hello.ts")' | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = 'import * as Hello from "./hello.ts";' | ||
expect(await transformWithPlugin(input)).toBe(output) | ||
}) | ||
@@ -42,6 +40,2 @@ it('Should convert named import', async () => { | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
/* | ||
const output = 'export * from "./hello.js";' | ||
expect(await transformWithPlugin(input)).toBe(output) | ||
*/ | ||
}) | ||
@@ -57,3 +51,3 @@ it('Should convert redirects', async () => { | ||
const output = ` | ||
var GENERATED_VAR_BY_TRANSFORMER_1; | ||
let GENERATED_VAR_BY_TRANSFORMER_1; | ||
import * as WrenchIcon from './icons/WrenchIcon.js'; | ||
@@ -63,5 +57,3 @@ GENERATED_VAR_BY_TRANSFORMER_1 = WrenchIcon; | ||
` | ||
expect(await transformWithPlugin(input)).toBe( | ||
await transformWithoutPlugin(output), | ||
) | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
}) | ||
@@ -73,10 +65,2 @@ }) | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = ` | ||
var GENERATED_VAR_BY_TRANSFORMER_1; | ||
GENERATED_VAR_BY_TRANSFORMER_1 = Hello; | ||
export { GENERATED_VAR_BY_TRANSFORMER_1 as hello }; | ||
` | ||
expect(await transformWithPlugin(input)).toBe( | ||
await transformWithoutPlugin(output), | ||
) | ||
}) | ||
@@ -86,3 +70,3 @@ it('Should convert to default export to ES6', async () => { | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = `var GENERATED_VAR_BY_TRANSFORMER_1; GENERATED_VAR_BY_TRANSFORMER_1 = Hello; export default GENERATED_VAR_BY_TRANSFORMER_1;` | ||
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( | ||
@@ -133,13 +117,2 @@ await transformWithoutPlugin(output), | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = ` | ||
var GENERATED_VAR_BY_TRANSFORMER_1; | ||
var GENERATED_VAR_BY_TRANSFORMER_2; | ||
GENERATED_VAR_BY_TRANSFORMER_1 = b; | ||
GENERATED_VAR_BY_TRANSFORMER_2 = d; | ||
export { GENERATED_VAR_BY_TRANSFORMER_1 as a }; | ||
export { GENERATED_VAR_BY_TRANSFORMER_2 as c }; | ||
` | ||
expect(await transformWithPlugin(input)).toBe( | ||
await transformWithoutPlugin(output), | ||
) | ||
}) | ||
@@ -152,14 +125,2 @@ }) | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = ` | ||
var GENERATED_VAR_BY_TRANSFORMER_1; | ||
var GENERATED_VAR_BY_TRANSFORMER_2; | ||
var GENERATED_VAR_BY_TRANSFORMER_3; | ||
GENERATED_VAR_BY_TRANSFORMER_1 = GENERATED_VAR_BY_TRANSFORMER_2 = GENERATED_VAR_BY_TRANSFORMER_3 = void 0; | ||
export { GENERATED_VAR_BY_TRANSFORMER_1 as lastAttributeNameRegex }; | ||
export { GENERATED_VAR_BY_TRANSFORMER_2 as createMarker }; | ||
export { GENERATED_VAR_BY_TRANSFORMER_3 as isTemplatePartActive }; | ||
` | ||
expect(await transformWithPlugin(input)).toBe( | ||
await transformWithoutPlugin(output), | ||
) | ||
}) | ||
@@ -169,11 +130,2 @@ it('Should red from internal export "hello"', async () => { | ||
expect(await transformWithPlugin(input)).toMatchSnapshot() | ||
const output = ` | ||
var GENERATED_VAR_BY_TRANSFORMER_1 | ||
GENERATED_VAR_BY_TRANSFORMER_1 = "Hello" | ||
console.log(GENERATED_VAR_BY_TRANSFORMER_1) | ||
export { GENERATED_VAR_BY_TRANSFORMER_1 as hello } | ||
` | ||
expect(await transformWithPlugin(input)).toBe( | ||
await transformWithoutPlugin(output), | ||
) | ||
}) | ||
@@ -180,0 +132,0 @@ }) |
@@ -14,3 +14,3 @@ import TsTranspiler from '../src/TsTranspiler' | ||
).outputText | ||
expect(results + '\n').toBe( | ||
expect(results).toBe( | ||
Fs.readFileSync( | ||
@@ -17,0 +17,0 @@ Path.resolve(__dirname, 'outputData', 'ModuleA.js'), |
@@ -7,31 +7,25 @@ "use strict"; | ||
const CommonJsTransformer_1 = __importDefault(require("./transformers/CommonJsTransformer")); | ||
const ResolveTransformer_1 = __importDefault(require("./transformers/ResolveTransformer")); | ||
const CompilerOptions_1 = __importDefault(require("./CompilerOptions")); | ||
const typescript_1 = __importDefault(require("typescript")); | ||
const fs_1 = __importDefault(require("fs")); | ||
const filePath = './packages/test-environment/src/Test.js'; | ||
// const filePath = './packages/test-environment/src/ReactIndex.js' | ||
// const filePath = './node_modules/react-dom/index.js' | ||
// const filePath = './node_modules/react-dom/cjs/react-dom.development.js' // './lzString.js' | ||
const transformers = { | ||
before: [ | ||
(context) => new CommonJsTransformer_1.default(context), | ||
(context) => new ResolveTransformer_1.default(context), | ||
], | ||
}; | ||
async function main() { | ||
const content = (await fs_1.default.promises.readFile('./lzString.js')).toString(); | ||
const content = (await fs_1.default.promises.readFile(filePath)).toString(); | ||
await fs_1.default.promises.writeFile('./tmp/out.js', await transformWithPlugin(content)); | ||
} | ||
main(); | ||
const compilerOptions = { | ||
allowJs: true, | ||
jsx: typescript_1.default.JsxEmit.React, | ||
checkJs: false, | ||
noResolve: true, | ||
esModuleInterop: true, | ||
skipLibCheck: true, | ||
pretty: true, | ||
allowUnreachableCode: true, | ||
target: typescript_1.default.ScriptTarget.ES2020, | ||
declaration: false, | ||
module: typescript_1.default.ModuleKind.ES2020, | ||
moduleResolution: typescript_1.default.ModuleResolutionKind.NodeJs, | ||
sourceMap: false, | ||
}; | ||
const transformers = { | ||
after: [(context) => new CommonJsTransformer_1.default(context)], | ||
}; | ||
async function transformWithPlugin(code) { | ||
const results = await typescript_1.default.transpileModule(code, { | ||
compilerOptions: compilerOptions, | ||
fileName: 'hello.ts', | ||
compilerOptions: CompilerOptions_1.default, | ||
fileName: filePath, | ||
transformers: transformers, | ||
@@ -43,4 +37,4 @@ }); | ||
const results = await typescript_1.default.transpileModule(code, { | ||
compilerOptions: compilerOptions, | ||
fileName: 'hello.ts', | ||
compilerOptions: CompilerOptions_1.default, | ||
fileName: filePath, | ||
}); | ||
@@ -47,0 +41,0 @@ return results.outputText.trim(); |
@@ -16,2 +16,3 @@ import TypeScript from 'typescript'; | ||
private requireTopScope; | ||
private createSyntheticDefaultExport; | ||
} |
@@ -22,5 +22,5 @@ "use strict"; | ||
const requireInTopScope = this.requireTopScope(withoutWildcardExports); | ||
// const exportsTopScope = this.exportsTopScope(requireInTopScope) | ||
const esmExport = this.convertToEsmExport(requireInTopScope); | ||
return this.convertToEsmImport(esmExport); | ||
const withEsmImport = this.convertToEsmImport(esmExport); | ||
return this.createSyntheticDefaultExport(withEsmImport); | ||
} | ||
@@ -116,3 +116,3 @@ transformBundle() { | ||
typescript_1.default.isIdentifier(variableDeclaration.name)) { | ||
importDeclarations.push(typescript_1.default.factory.createImportDeclaration(undefined, undefined, typescript_1.default.factory.createImportClause(false, undefined, typescript_1.default.factory.createNamespaceImport(variableDeclaration.name)), argument)); | ||
importDeclarations.push(typescript_1.default.factory.createImportDeclaration(undefined, undefined, typescript_1.default.factory.createImportClause(false, variableDeclaration.name, undefined), argument)); | ||
} | ||
@@ -159,5 +159,5 @@ else if (typescript_1.default.isStringLiteral(argument) && | ||
const identifier = typescript_1.default.factory.createIdentifier(newIdentifierName); | ||
newTopStatements.push(typescript_1.default.factory.createVariableStatement(undefined, [ | ||
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; | ||
@@ -256,5 +256,5 @@ if (node.name.text === 'default') { | ||
const newIdentifierName = this.generateUniqueName(); | ||
newStatements.push(typescript_1.default.factory.createVariableStatement(undefined, [ | ||
newStatements.push(typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([ | ||
typescript_1.default.factory.createVariableDeclaration(newIdentifierName, undefined, undefined, node), | ||
])); | ||
], typescript_1.default.NodeFlags.Let))); | ||
return typescript_1.default.factory.createIdentifier(newIdentifierName); | ||
@@ -270,4 +270,39 @@ } | ||
} | ||
// 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, | ||
]); | ||
} | ||
} | ||
exports.default = CommonJsTransformer; | ||
//# sourceMappingURL=CommonJsTransformer.js.map |
@@ -21,13 +21,10 @@ "use strict"; | ||
const visit = (node) => { | ||
if ((typescript_1.default.isImportDeclaration(node) || | ||
typescript_1.default.isExportDeclaration(node)) && | ||
node.moduleSpecifier) { | ||
return typescript_1.default.visitEachChild(node, visit, this.context); | ||
if (typescript_1.default.isImportDeclaration(node) && | ||
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))); | ||
} | ||
if (typescript_1.default.isStringLiteral(node) && | ||
node.parent && | ||
(typescript_1.default.isExportDeclaration(node.parent) || | ||
typescript_1.default.isImportDeclaration(node.parent)) && | ||
node.parent.moduleSpecifier) { | ||
return typescript_1.default.factory.createStringLiteral(new DependencyResolver_1.default(node.getSourceFile().fileName, this.context).resolveRelativeDependency(node.text)); | ||
if (typescript_1.default.isExportDeclaration(node) && | ||
node.moduleSpecifier && | ||
typescript_1.default.isStringLiteral(node.moduleSpecifier)) { | ||
return typescript_1.default.factory.updateExportDeclaration(node, undefined, undefined, node.isTypeOnly, undefined, node.moduleSpecifier); | ||
} | ||
@@ -45,3 +42,3 @@ return node; | ||
node === node.parent.arguments[0]) { | ||
return typescript_1.default.factory.createStringLiteral(new DependencyResolver_1.default(node.getSourceFile().fileName, this.context).resolveRelativeDependency(node.text)); | ||
return typescript_1.default.factory.createStringLiteral(new DependencyResolver_1.default(sourceFile.fileName, this.context).resolveRelativeDependency(node.text)); | ||
} | ||
@@ -48,0 +45,0 @@ return typescript_1.default.visitEachChild(node, visit, this.context); |
@@ -21,3 +21,7 @@ "use strict"; | ||
const relativeDir = path_1.default.relative(path_1.default.dirname(parentPath), pathObj.dir) || '.'; | ||
return relativeDir + '/' + pathObj.name + pathObj.ext; | ||
const result = relativeDir + '/' + pathObj.name + pathObj.ext; | ||
if (result.startsWith('.')) { | ||
return result; | ||
} | ||
return './' + result; | ||
} | ||
@@ -24,0 +28,0 @@ // Return an aboslute path e.g. /tmp/a-apath/node_modules/hello/module.js |
@@ -8,45 +8,15 @@ "use strict"; | ||
const typescript_1 = __importDefault(require("typescript")); | ||
const ResolveTransformer_1 = __importDefault(require("./transformers/ResolveTransformer")); | ||
const CommonJsTransformer_1 = __importDefault(require("./transformers/CommonJsTransformer")); | ||
const NodeEnvTransformer_1 = __importDefault(require("./transformers/NodeEnvTransformer")); | ||
const CodeOptimizerTransformer_1 = __importDefault(require("./transformers/CodeOptimizerTransformer")); | ||
const compilerOptions = { | ||
allowJs: true, | ||
jsx: typescript_1.default.JsxEmit.React, | ||
/* | ||
jsx: TypeScript.JsxEmit.React, | ||
jsxImportSource: 'react', | ||
jsxFragmentFactory: 'React.Fragment', | ||
jsxFactory: 'React.createElement', | ||
*/ | ||
checkJs: false, | ||
noResolve: true, | ||
esModuleInterop: true, | ||
skipLibCheck: false, | ||
target: typescript_1.default.ScriptTarget.ES2020, | ||
declaration: false, | ||
module: typescript_1.default.ModuleKind.ES2020, | ||
moduleResolution: typescript_1.default.ModuleResolutionKind.NodeJs, | ||
sourceMap: true, | ||
}; | ||
const transformers = { | ||
before: [ | ||
(context) => new NodeEnvTransformer_1.default(context), | ||
(context) => new CodeOptimizerTransformer_1.default(context), | ||
], | ||
after: [ | ||
(context) => new CommonJsTransformer_1.default(context), | ||
(context) => new ResolveTransformer_1.default(context), | ||
], | ||
}; | ||
const CompilerOptions_1 = __importDefault(require("./CompilerOptions")); | ||
const AllTransformers_1 = __importDefault(require("./AllTransformers")); | ||
class TsTranspiler { | ||
async transformCode(code, fileName) { | ||
const results = typescript_1.default.transpileModule(code, { | ||
compilerOptions: compilerOptions, | ||
compilerOptions: CompilerOptions_1.default, | ||
fileName: fileName, | ||
reportDiagnostics: true, | ||
transformers: transformers, | ||
transformers: AllTransformers_1.default, | ||
}); | ||
if (results.diagnostics?.length) { | ||
console.log(results.diagnostics); | ||
// eslint-disable-next-line no-console | ||
console.log('diagnostics:', results.diagnostics); | ||
} | ||
@@ -53,0 +23,0 @@ return results; |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
import CommonJsTransformer from './transformers/CommonJsTransformer' | ||
import ResolveTransformer from './transformers/ResolveTransformer' | ||
import CompilerOptions from './CompilerOptions' | ||
import TypeScript from 'typescript' | ||
import Fs from 'fs' | ||
const filePath = './packages/test-environment/src/Test.js' | ||
// const filePath = './packages/test-environment/src/ReactIndex.js' | ||
// const filePath = './node_modules/react-dom/index.js' | ||
// const filePath = './node_modules/react-dom/cjs/react-dom.development.js' // './lzString.js' | ||
const transformers: TypeScript.CustomTransformers = { | ||
before: [ | ||
(context) => new CommonJsTransformer(context), | ||
(context) => new ResolveTransformer(context), | ||
], | ||
} | ||
async function main() { | ||
const content = (await Fs.promises.readFile('./lzString.js')).toString() | ||
const content = (await Fs.promises.readFile(filePath)).toString() | ||
await Fs.promises.writeFile( | ||
@@ -15,26 +30,6 @@ './tmp/out.js', | ||
const compilerOptions: TypeScript.CompilerOptions = { | ||
allowJs: true, | ||
jsx: TypeScript.JsxEmit.React, | ||
checkJs: false, | ||
noResolve: true, | ||
esModuleInterop: true, | ||
skipLibCheck: true, | ||
pretty: true, | ||
allowUnreachableCode: true, | ||
target: TypeScript.ScriptTarget.ES2020, | ||
declaration: false, | ||
module: TypeScript.ModuleKind.ES2020, | ||
moduleResolution: TypeScript.ModuleResolutionKind.NodeJs, | ||
sourceMap: false, | ||
} | ||
const transformers: TypeScript.CustomTransformers = { | ||
after: [(context) => new CommonJsTransformer(context)], | ||
} | ||
async function transformWithPlugin(code: string): Promise<string> { | ||
const results = await TypeScript.transpileModule(code, { | ||
compilerOptions: compilerOptions, | ||
fileName: 'hello.ts', | ||
compilerOptions: CompilerOptions, | ||
fileName: filePath, | ||
transformers: transformers, | ||
@@ -47,6 +42,6 @@ }) | ||
const results = await TypeScript.transpileModule(code, { | ||
compilerOptions: compilerOptions, | ||
fileName: 'hello.ts', | ||
compilerOptions: CompilerOptions, | ||
fileName: filePath, | ||
}) | ||
return results.outputText.trim() | ||
} |
@@ -24,5 +24,5 @@ import TypeScript from 'typescript' | ||
const requireInTopScope = this.requireTopScope(withoutWildcardExports) | ||
// const exportsTopScope = this.exportsTopScope(requireInTopScope) | ||
const esmExport = this.convertToEsmExport(requireInTopScope) | ||
return this.convertToEsmImport(esmExport) | ||
const withEsmImport = this.convertToEsmImport(esmExport) | ||
return this.createSyntheticDefaultExport(withEsmImport) | ||
} | ||
@@ -182,6 +182,4 @@ public transformBundle(): TypeScript.Bundle { | ||
false, | ||
variableDeclaration.name, | ||
undefined, | ||
TypeScript.factory.createNamespaceImport( | ||
variableDeclaration.name, | ||
), | ||
), | ||
@@ -269,10 +267,16 @@ argument, | ||
newTopStatements.push( | ||
TypeScript.factory.createVariableStatement(undefined, [ | ||
TypeScript.factory.createVariableDeclaration( | ||
newIdentifierName, | ||
undefined, | ||
undefined, | ||
undefined, | ||
TypeScript.factory.createVariableStatement( | ||
undefined, | ||
TypeScript.factory.createVariableDeclarationList( | ||
[ | ||
TypeScript.factory.createVariableDeclaration( | ||
newIdentifierName, | ||
undefined, | ||
undefined, | ||
undefined, | ||
), | ||
], | ||
TypeScript.NodeFlags.Let, | ||
), | ||
]), | ||
), | ||
) | ||
@@ -316,3 +320,2 @@ // exports.default = something; | ||
} | ||
return TypeScript.visitEachChild(node, visit, this.context) | ||
@@ -428,10 +431,16 @@ } | ||
newStatements.push( | ||
TypeScript.factory.createVariableStatement(undefined, [ | ||
TypeScript.factory.createVariableDeclaration( | ||
newIdentifierName, | ||
undefined, | ||
undefined, | ||
node, | ||
TypeScript.factory.createVariableStatement( | ||
undefined, | ||
TypeScript.factory.createVariableDeclarationList( | ||
[ | ||
TypeScript.factory.createVariableDeclaration( | ||
newIdentifierName, | ||
undefined, | ||
undefined, | ||
node, | ||
), | ||
], | ||
TypeScript.NodeFlags.Let, | ||
), | ||
]), | ||
), | ||
) | ||
@@ -448,2 +457,68 @@ return TypeScript.factory.createIdentifier(newIdentifierName) | ||
} | ||
// export { hello as Hello } -> export default { something: hello } | ||
private createSyntheticDefaultExport( | ||
sourceFile: TypeScript.SourceFile, | ||
): TypeScript.SourceFile { | ||
const exportSpecifiers: TypeScript.ExportSpecifier[] = [] | ||
const visit = (node: TypeScript.Node): TypeScript.Node | undefined => { | ||
if ( | ||
TypeScript.isExportDeclaration(node) && | ||
node.exportClause && | ||
TypeScript.isNamedExports(node.exportClause) | ||
) { | ||
exportSpecifiers.push(...node.exportClause.elements) | ||
return undefined | ||
} | ||
return node | ||
} | ||
const changedSourceFile = TypeScript.visitEachChild( | ||
sourceFile, | ||
visit, | ||
this.context, | ||
) | ||
if (exportSpecifiers.length === 0) { | ||
return sourceFile | ||
} | ||
const propertyAssignments: TypeScript.PropertyAssignment[] = [] | ||
for (const exportSpecifier of exportSpecifiers) { | ||
if ( | ||
exportSpecifier.propertyName && | ||
TypeScript.isIdentifier(exportSpecifier.propertyName) | ||
) { | ||
propertyAssignments.push( | ||
TypeScript.factory.createPropertyAssignment( | ||
exportSpecifier.name, | ||
exportSpecifier.propertyName, | ||
), | ||
) | ||
} | ||
} | ||
const newStatements = [ | ||
...changedSourceFile.statements, | ||
TypeScript.factory.createExportDeclaration( | ||
undefined, | ||
undefined, | ||
false, | ||
TypeScript.factory.createNamedExports(exportSpecifiers), | ||
), | ||
] | ||
// If there is not export default already -> add synthentic | ||
if ( | ||
sourceFile.statements.every( | ||
(node) => !TypeScript.isExportAssignment(node), | ||
) | ||
) { | ||
newStatements.push( | ||
TypeScript.factory.createExportAssignment( | ||
undefined, | ||
undefined, | ||
undefined, | ||
TypeScript.factory.createObjectLiteralExpression(propertyAssignments), | ||
), | ||
) | ||
} | ||
return TypeScript.factory.updateSourceFile(changedSourceFile, [ | ||
...newStatements, | ||
]) | ||
} | ||
} |
@@ -24,21 +24,30 @@ import TypeScript from 'typescript' | ||
if ( | ||
(TypeScript.isImportDeclaration(node) || | ||
TypeScript.isExportDeclaration(node)) && | ||
node.moduleSpecifier | ||
TypeScript.isImportDeclaration(node) && | ||
TypeScript.isStringLiteral(node.moduleSpecifier) | ||
) { | ||
return TypeScript.visitEachChild(node, visit, this.context) | ||
return TypeScript.factory.updateImportDeclaration( | ||
node, | ||
node.decorators, | ||
node.modifiers, | ||
node.importClause, | ||
TypeScript.factory.createStringLiteral( | ||
new DependencyResolver( | ||
sourceFile.fileName, | ||
this.context, | ||
).resolveRelativeDependency(node.moduleSpecifier.text), | ||
), | ||
) | ||
} | ||
if ( | ||
TypeScript.isStringLiteral(node) && | ||
node.parent && | ||
(TypeScript.isExportDeclaration(node.parent) || | ||
TypeScript.isImportDeclaration(node.parent)) && | ||
node.parent.moduleSpecifier | ||
TypeScript.isExportDeclaration(node) && | ||
node.moduleSpecifier && | ||
TypeScript.isStringLiteral(node.moduleSpecifier) | ||
) { | ||
return TypeScript.factory.createStringLiteral( | ||
new DependencyResolver( | ||
node.getSourceFile().fileName, | ||
this.context, | ||
).resolveRelativeDependency(node.text), | ||
// this.resolveDependencyName(node.getSourceFile().fileName, node.text), | ||
return TypeScript.factory.updateExportDeclaration( | ||
node, | ||
undefined, | ||
undefined, | ||
node.isTypeOnly, | ||
undefined, | ||
node.moduleSpecifier, | ||
) | ||
@@ -63,3 +72,3 @@ } | ||
new DependencyResolver( | ||
node.getSourceFile().fileName, | ||
sourceFile.fileName, | ||
this.context, | ||
@@ -66,0 +75,0 @@ ).resolveRelativeDependency(node.text), |
@@ -24,3 +24,7 @@ import TypeScript from 'typescript' | ||
Path.relative(Path.dirname(parentPath), pathObj.dir) || '.' | ||
return relativeDir + '/' + pathObj.name + pathObj.ext | ||
const result = relativeDir + '/' + pathObj.name + pathObj.ext | ||
if (result.startsWith('.')) { | ||
return result | ||
} | ||
return './' + result | ||
} | ||
@@ -27,0 +31,0 @@ // Return an aboslute path e.g. /tmp/a-apath/node_modules/hello/module.js |
import Fs from 'fs' | ||
import TypeScript from 'typescript' | ||
import ResolveTransformer from './transformers/ResolveTransformer' | ||
import CommonJsTransformer from './transformers/CommonJsTransformer' | ||
import NodeEnvTransformer from './transformers/NodeEnvTransformer' | ||
import CodeOptimizerTransformer from './transformers/CodeOptimizerTransformer' | ||
const compilerOptions: TypeScript.CompilerOptions = { | ||
allowJs: true, | ||
jsx: TypeScript.JsxEmit.React, | ||
/* | ||
jsx: TypeScript.JsxEmit.React, | ||
jsxImportSource: 'react', | ||
jsxFragmentFactory: 'React.Fragment', | ||
jsxFactory: 'React.createElement', | ||
*/ | ||
checkJs: false, | ||
noResolve: true, | ||
esModuleInterop: true, | ||
skipLibCheck: false, | ||
target: TypeScript.ScriptTarget.ES2020, | ||
declaration: false, | ||
module: TypeScript.ModuleKind.ES2020, | ||
moduleResolution: TypeScript.ModuleResolutionKind.NodeJs, | ||
sourceMap: true, | ||
} | ||
const transformers: TypeScript.CustomTransformers = { | ||
before: [ | ||
(context) => new NodeEnvTransformer(context), | ||
(context) => new CodeOptimizerTransformer(context), | ||
], | ||
after: [ | ||
(context) => new CommonJsTransformer(context), | ||
(context) => new ResolveTransformer(context), | ||
], | ||
} | ||
import CompilerOptions from './CompilerOptions' | ||
import AllTransformers from './AllTransformers' | ||
export default class TsTranspiler { | ||
@@ -45,9 +11,10 @@ async transformCode( | ||
const results = TypeScript.transpileModule(code, { | ||
compilerOptions: compilerOptions, | ||
compilerOptions: CompilerOptions, | ||
fileName: fileName, | ||
reportDiagnostics: true, | ||
transformers: transformers, | ||
transformers: AllTransformers, | ||
}) | ||
if (results.diagnostics?.length) { | ||
console.log(results.diagnostics) | ||
// eslint-disable-next-line no-console | ||
console.log('diagnostics:', results.diagnostics) | ||
} | ||
@@ -54,0 +21,0 @@ return results |
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
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
122454
66
2265