Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ts-liveserver/ts-transpiler

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ts-liveserver/ts-transpiler - npm Package Compare versions

Comparing version 0.0.11 to 0.0.12

2

__tests__/outputData/ModuleA.js

@@ -1,2 +0,2 @@

import ModuleB from "./ModuleB.ts";
import ModuleB from "./ModuleB.js";
const component = React.createElement("div", null, "Hello");

@@ -3,0 +3,0 @@ console.log(component);

@@ -34,3 +34,3 @@ "use strict";

isCommonJsModule(sourceFile) {
return /require|exports/.test(sourceFile.text);
return /require|exports/gm.test(sourceFile.text);
}

@@ -37,0 +37,0 @@ // Generate a file-unique variable name

@@ -7,4 +7,4 @@ import TypeScript from 'typescript';

private visit;
transformSourceFile(node: TypeScript.SourceFile): TypeScript.SourceFile;
transformSourceFile(sourceFile: TypeScript.SourceFile): TypeScript.SourceFile;
transformBundle(node: TypeScript.Bundle): TypeScript.Bundle;
}

@@ -33,4 +33,7 @@ "use strict";

}
transformSourceFile(node) {
return typescript_1.default.visitNode(node, this.visit.bind(this));
transformSourceFile(sourceFile) {
if (!/NODE_ENV/gm.test(sourceFile.text)) {
return sourceFile;
}
return typescript_1.default.visitNode(sourceFile, this.visit.bind(this));
}

@@ -37,0 +40,0 @@ transformBundle(node) {

@@ -23,5 +23,5 @@ "use strict";

if (result.startsWith('.')) {
return result;
return result.replace(/\.(ts|tsx|jsx)$/, '.js');
}
return './' + result;
return './' + result.replace(/\.(ts|tsx|jsx)$/, '.js');
}

@@ -28,0 +28,0 @@ // Return an aboslute path e.g. /tmp/a-apath/node_modules/hello/module.js

import TypeScript from 'typescript';
export default class TsTranspiler {
private compilerOptions;
constructor(compilerOptions?: TypeScript.CompilerOptions);
transformCode(code: string, fileName: string): Promise<TypeScript.TranspileOutput>;
transformFile(fileName: string): Promise<TypeScript.TranspileOutput>;
transformFile(fileName: string): Promise<TypeScript.TranspileOutput & {
resolvedFilePath: string;
}>;
resolveFilePath(fileName: string): Promise<string>;
fileExists(path: string): Promise<boolean>;
}

@@ -7,2 +7,3 @@ "use strict";

const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const typescript_1 = __importDefault(require("typescript"));

@@ -12,5 +13,9 @@ const CompilerOptions_1 = __importDefault(require("./CompilerOptions"));

class TsTranspiler {
constructor(compilerOptions) {
this.compilerOptions = CompilerOptions_1.default;
Object.assign(this.compilerOptions, compilerOptions);
}
async transformCode(code, fileName) {
const results = typescript_1.default.transpileModule(code, {
compilerOptions: CompilerOptions_1.default,
compilerOptions: this.compilerOptions,
fileName: fileName,

@@ -27,7 +32,36 @@ reportDiagnostics: true,

async transformFile(fileName) {
const buffer = await fs_1.default.promises.readFile(fileName);
return this.transformCode(buffer.toString(), fileName);
const resolvedFilePath = await this.resolveFilePath(fileName);
const buffer = await fs_1.default.promises.readFile(resolvedFilePath);
return {
...(await this.transformCode(buffer.toString(), resolvedFilePath)),
resolvedFilePath: resolvedFilePath,
};
}
async resolveFilePath(fileName) {
if (await this.fileExists(path_1.default.resolve(fileName))) {
return path_1.default.resolve(fileName);
}
const parsedFileName = path_1.default.parse(fileName);
if (await this.fileExists(path_1.default.resolve(parsedFileName.dir, parsedFileName.name + '.js'))) {
return path_1.default.resolve(parsedFileName.dir, parsedFileName.name + '.js');
}
if (await this.fileExists(path_1.default.resolve(parsedFileName.dir, parsedFileName.name + '.tsx'))) {
return path_1.default.resolve(parsedFileName.dir, parsedFileName.name + '.tsx');
}
if (await this.fileExists(path_1.default.resolve(parsedFileName.dir, parsedFileName.name + '.ts'))) {
return path_1.default.resolve(parsedFileName.dir, parsedFileName.name + '.ts');
}
throw new Error('Could not find file' + fileName);
}
async fileExists(path) {
try {
await fs_1.default.promises.readFile(path);
}
catch (error) {
return false;
}
return true;
}
}
exports.default = TsTranspiler;
//# sourceMappingURL=TsTranspiler.js.map
{
"name": "@ts-liveserver/ts-transpiler",
"version": "0.0.11",
"version": "0.0.12",
"main": "dist/index.js",

@@ -5,0 +5,0 @@ "scripts": {

@@ -38,3 +38,3 @@ import TypeScript from 'typescript'

private isCommonJsModule(sourceFile: TypeScript.SourceFile): boolean {
return /require|exports/.test(sourceFile.text)
return /require|exports/gm.test(sourceFile.text)
}

@@ -41,0 +41,0 @@ // Generate a file-unique variable name

@@ -35,4 +35,9 @@ import TypeScript from 'typescript'

}
transformSourceFile(node: TypeScript.SourceFile): TypeScript.SourceFile {
return TypeScript.visitNode(node, this.visit.bind(this))
transformSourceFile(
sourceFile: TypeScript.SourceFile,
): TypeScript.SourceFile {
if (!/NODE_ENV/gm.test(sourceFile.text)) {
return sourceFile
}
return TypeScript.visitNode(sourceFile, this.visit.bind(this))
}

@@ -39,0 +44,0 @@ transformBundle(node: TypeScript.Bundle): TypeScript.Bundle {

@@ -26,5 +26,5 @@ import TypeScript from 'typescript'

if (result.startsWith('.')) {
return result
return result.replace(/\.(ts|tsx|jsx)$/, '.js')
}
return './' + result
return './' + result.replace(/\.(ts|tsx|jsx)$/, '.js')
}

@@ -31,0 +31,0 @@ // Return an aboslute path e.g. /tmp/a-apath/node_modules/hello/module.js

import Fs from 'fs'
import Path from 'path'
import TypeScript from 'typescript'

@@ -6,2 +7,6 @@ import CompilerOptions from './CompilerOptions'

export default class TsTranspiler {
private compilerOptions: TypeScript.CompilerOptions = CompilerOptions
constructor(compilerOptions?: TypeScript.CompilerOptions) {
Object.assign(this.compilerOptions, compilerOptions)
}
async transformCode(

@@ -12,3 +17,3 @@ code: string,

const results = TypeScript.transpileModule(code, {
compilerOptions: CompilerOptions,
compilerOptions: this.compilerOptions,
fileName: fileName,

@@ -24,6 +29,48 @@ reportDiagnostics: true,

}
async transformFile(fileName: string): Promise<TypeScript.TranspileOutput> {
const buffer = await Fs.promises.readFile(fileName)
return this.transformCode(buffer.toString(), fileName)
public async transformFile(
fileName: string,
): Promise<TypeScript.TranspileOutput & { resolvedFilePath: string }> {
const resolvedFilePath = await this.resolveFilePath(fileName)
const buffer = await Fs.promises.readFile(resolvedFilePath)
return {
...(await this.transformCode(buffer.toString(), resolvedFilePath)),
resolvedFilePath: resolvedFilePath,
}
}
public async resolveFilePath(fileName: string): Promise<string> {
if (await this.fileExists(Path.resolve(fileName))) {
return Path.resolve(fileName)
}
const parsedFileName = Path.parse(fileName)
if (
await this.fileExists(
Path.resolve(parsedFileName.dir, parsedFileName.name + '.js'),
)
) {
return Path.resolve(parsedFileName.dir, parsedFileName.name + '.js')
}
if (
await this.fileExists(
Path.resolve(parsedFileName.dir, parsedFileName.name + '.tsx'),
)
) {
return Path.resolve(parsedFileName.dir, parsedFileName.name + '.tsx')
}
if (
await this.fileExists(
Path.resolve(parsedFileName.dir, parsedFileName.name + '.ts'),
)
) {
return Path.resolve(parsedFileName.dir, parsedFileName.name + '.ts')
}
throw new Error('Could not find file' + fileName)
}
public async fileExists(path: string) {
try {
await Fs.promises.readFile(path)
} catch (error) {
return false
}
return true
}
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc