@ts-liveserver/ts-transpiler
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -6,1 +6,3 @@ export { default as TsTranspiler } from './TsTranspiler'; | ||
export { default as CodeOptimizerTransformer } from './transformers/CodeOptimizerTransformer'; | ||
export { default as DefaultResolveAlias } from './DefaultResolveAlias'; | ||
export { default as DependencyResolver } from './utils/DependencyResolver'; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CodeOptimizerTransformer = exports.ResolveTransformer = exports.NodeEnvTransformer = exports.CommonJsTransformer = exports.TsTranspiler = void 0; | ||
exports.DependencyResolver = exports.DefaultResolveAlias = exports.CodeOptimizerTransformer = exports.ResolveTransformer = exports.NodeEnvTransformer = exports.CommonJsTransformer = exports.TsTranspiler = void 0; | ||
var TsTranspiler_1 = require("./TsTranspiler"); | ||
@@ -18,2 +18,6 @@ Object.defineProperty(exports, "TsTranspiler", { enumerable: true, get: function () { return __importDefault(TsTranspiler_1).default; } }); | ||
Object.defineProperty(exports, "CodeOptimizerTransformer", { enumerable: true, get: function () { return __importDefault(CodeOptimizerTransformer_1).default; } }); | ||
var DefaultResolveAlias_1 = require("./DefaultResolveAlias"); | ||
Object.defineProperty(exports, "DefaultResolveAlias", { enumerable: true, get: function () { return __importDefault(DefaultResolveAlias_1).default; } }); | ||
var DependencyResolver_1 = require("./utils/DependencyResolver"); | ||
Object.defineProperty(exports, "DependencyResolver", { enumerable: true, get: function () { return __importDefault(DependencyResolver_1).default; } }); | ||
//# sourceMappingURL=index.js.map |
import TypeScript from 'typescript'; | ||
interface Resolver { | ||
resolveDependencyName(parentFilePath: string, dependencyName: string): string; | ||
} | ||
export default class ResolveTransformer implements TypeScript.CustomTransformer { | ||
private context; | ||
constructor(context: TypeScript.TransformationContext); | ||
private resolver; | ||
constructor(context: TypeScript.TransformationContext, resolver: Resolver); | ||
transformSourceFile(sourceFile: TypeScript.SourceFile): TypeScript.SourceFile; | ||
@@ -9,3 +13,4 @@ transformBundle(node: TypeScript.Bundle): TypeScript.Bundle; | ||
private resolveDynamicImport; | ||
private visit; | ||
private getDependencyName; | ||
} | ||
export {}; |
@@ -7,6 +7,6 @@ "use strict"; | ||
const typescript_1 = __importDefault(require("typescript")); | ||
const DependencyResolver_1 = __importDefault(require("../utils/DependencyResolver")); | ||
class ResolveTransformer { | ||
constructor(context) { | ||
constructor(context, resolver) { | ||
this.context = context; | ||
this.resolver = resolver; | ||
} | ||
@@ -24,3 +24,3 @@ transformSourceFile(sourceFile) { | ||
typescript_1.default.isStringLiteral(node.moduleSpecifier)) { | ||
const resolvedName = new DependencyResolver_1.default(sourceFile.fileName).resolveRelativeDependency(node.moduleSpecifier.text); | ||
const resolvedName = this.getDependencyName(sourceFile.fileName, node.moduleSpecifier.text); | ||
return typescript_1.default.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, typescript_1.default.factory.createStringLiteral(resolvedName)); | ||
@@ -31,3 +31,3 @@ } | ||
typescript_1.default.isStringLiteral(node.moduleSpecifier)) { | ||
const resolvedName = new DependencyResolver_1.default(sourceFile.fileName).resolveRelativeDependency(node.moduleSpecifier.text); | ||
const resolvedName = this.getDependencyName(sourceFile.fileName, node.moduleSpecifier.text); | ||
return typescript_1.default.factory.updateExportDeclaration(node, undefined, undefined, node.isTypeOnly, undefined, typescript_1.default.factory.createStringLiteral(resolvedName)); | ||
@@ -46,3 +46,3 @@ } | ||
node === node.parent.arguments[0]) { | ||
return typescript_1.default.factory.createStringLiteral(new DependencyResolver_1.default(sourceFile.fileName).resolveRelativeDependency(node.text)); | ||
return typescript_1.default.factory.createStringLiteral(this.getDependencyName(sourceFile.fileName, node.text)); | ||
} | ||
@@ -53,16 +53,4 @@ return typescript_1.default.visitEachChild(node, visit, this.context); | ||
} | ||
visit(node) { | ||
if ((typescript_1.default.isImportDeclaration(node) || | ||
typescript_1.default.isExportDeclaration(node)) && | ||
node.moduleSpecifier) { | ||
return typescript_1.default.visitEachChild(node, this.visit.bind(this), this.context); | ||
} | ||
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).resolveRelativeDependency(node.text)); | ||
} | ||
return node; | ||
getDependencyName(parentFilePath, dependencyName) { | ||
return this.resolver.resolveDependencyName(parentFilePath, dependencyName); | ||
} | ||
@@ -69,0 +57,0 @@ } |
import TypeScript from 'typescript'; | ||
declare type Options = { | ||
compilerOptions?: TypeScript.CompilerOptions; | ||
resolveAlias?: Record<string, string>; | ||
}; | ||
export default class TsTranspiler { | ||
private compilerOptions; | ||
constructor(compilerOptions?: TypeScript.CompilerOptions); | ||
private transformers; | ||
constructor(options?: Options); | ||
transformCode(code: string, fileName: string): Promise<TypeScript.TranspileOutput>; | ||
@@ -10,1 +15,2 @@ transformFile(fileName: string): Promise<TypeScript.TranspileOutput>; | ||
} | ||
export {}; |
@@ -10,8 +10,25 @@ "use strict"; | ||
const CompilerOptions_1 = __importDefault(require("./CompilerOptions")); | ||
const AllTransformers_1 = __importDefault(require("./AllTransformers")); | ||
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 EnsureExportDefaultTransformer_1 = __importDefault(require("./transformers/EnsureExportDefaultTransformer")); | ||
const InternalDependencyReducer_1 = __importDefault(require("./transformers/InternalDependencyReducer")); | ||
const DependencyResolver_1 = __importDefault(require("./utils/DependencyResolver")); | ||
const RESOLVE_EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.json']; | ||
class TsTranspiler { | ||
constructor(compilerOptions) { | ||
constructor(options) { | ||
this.compilerOptions = CompilerOptions_1.default; | ||
Object.assign(this.compilerOptions, compilerOptions); | ||
const dependencyResolver = new DependencyResolver_1.default(options?.resolveAlias); | ||
Object.assign(this.compilerOptions, options?.compilerOptions); | ||
this.transformers = { | ||
before: [ | ||
(context) => new NodeEnvTransformer_1.default(context), | ||
(context) => new InternalDependencyReducer_1.default(context), | ||
(context) => new CodeOptimizerTransformer_1.default(context), | ||
(context) => new CommonJsTransformer_1.default(context), | ||
(context) => new ResolveTransformer_1.default(context, dependencyResolver), | ||
], | ||
after: [(context) => new EnsureExportDefaultTransformer_1.default(context)], | ||
}; | ||
} | ||
@@ -23,3 +40,3 @@ async transformCode(code, fileName) { | ||
reportDiagnostics: true, | ||
transformers: AllTransformers_1.default, | ||
transformers: this.transformers, | ||
}); | ||
@@ -26,0 +43,0 @@ if (results.diagnostics?.length) { |
export default class DependencyResolver { | ||
private resolver; | ||
private fileName; | ||
constructor(fileName: string); | ||
resolveRelativeDependency(dependencyFileName: string): string; | ||
private resolveDependencyName; | ||
private resolveDependencyPath; | ||
private alias?; | ||
constructor(alias?: Record<string, string>); | ||
resolveDependencyName(parentFilePath: string, dependencyName: string): string; | ||
resolveDependencyPath(parentFilePath: string, dependencyName: string): string; | ||
} |
@@ -10,3 +10,3 @@ "use strict"; | ||
class DependencyResolver { | ||
constructor(fileName) { | ||
constructor(alias = {}) { | ||
this.resolver = enhanced_resolve_1.default.create.sync({ | ||
@@ -17,12 +17,9 @@ mainFields: ['browser', 'module', 'main'], | ||
}); | ||
this.fileName = fileName; | ||
this.alias = alias; | ||
} | ||
resolveRelativeDependency(dependencyFileName) { | ||
return this.resolveDependencyName(this.fileName, dependencyFileName); | ||
} | ||
// Return e.g. ./hello/module.js | ||
resolveDependencyName(parentPath, dependencyName) { | ||
const absolutePath = this.resolveDependencyPath(parentPath, dependencyName); | ||
resolveDependencyName(parentFilePath, dependencyName) { | ||
const absolutePath = this.resolveDependencyPath(parentFilePath, dependencyName); | ||
const pathObj = path_1.default.parse(absolutePath); | ||
const relativeDir = path_1.default.relative(path_1.default.dirname(parentPath), pathObj.dir) || '.'; | ||
const relativeDir = path_1.default.relative(path_1.default.dirname(parentFilePath), pathObj.dir) || '.'; | ||
const result = relativeDir + '/' + pathObj.name + '.js'; | ||
@@ -33,7 +30,10 @@ const posixResult = path_1.default.posix.normalize(result).replace(/\\/g, '/'); | ||
// Return an aboslute path e.g. /tmp/a-apath/node_modules/hello/module.js | ||
resolveDependencyPath(parentPath, dependencyName) { | ||
const directory = path_1.default.dirname(parentPath); | ||
resolveDependencyPath(parentFilePath, dependencyName) { | ||
if (this.alias && dependencyName in this.alias) { | ||
return this.resolveDependencyPath(parentFilePath, this.alias[dependencyName]); | ||
} | ||
const directory = path_1.default.dirname(parentFilePath); | ||
const result = this.resolver({}, directory, dependencyName); | ||
if (result === false) { | ||
throw new Error('Could not resolve ' + dependencyName + ' from ' + parentPath); | ||
throw new Error('Could not resolve ' + dependencyName + ' from ' + parentFilePath); | ||
} | ||
@@ -40,0 +40,0 @@ return result; |
{ | ||
"name": "@ts-liveserver/ts-transpiler", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"license": "ISC", | ||
@@ -16,2 +16,8 @@ "main": "dist/index.js", | ||
}, | ||
"peerDependencies": { | ||
"buffer": "^6.0.3", | ||
"events": "^3.3.0", | ||
"stream-browserify": "^3.0.0", | ||
"util": "^0.12.3" | ||
}, | ||
"devDependencies": { | ||
@@ -27,3 +33,3 @@ "@types/express": "^4.17.9", | ||
}, | ||
"gitHead": "d33899b423e79b4fc662e92b8a81ae7436985c29" | ||
"gitHead": "a8809802be8740447259cb8f743d8c38b120ea7b" | ||
} |
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
83747
1005
6