@ffras4vnpm/esse-laborum-consectetur

1.0.0 • Public • Published

Typescript to Javascript compiler

This Typescript to Javascript compiler only drops the Typescript specific keywords. No other changes are made to the code.

Usually, when you compile typescript code with the official Typescript compiler, the outputted Javascript code looks something like this:

"use strict";
var __assign = (this && this.__assign) || function () {
	__assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
        t[p] = s[p];
    }
    return t;
	};
	return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
	return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = void 0;
var abstract_syntax_tree_1 = __importDefault(require("abstract-syntax-tree"));
var parser_1 = require("@typescript-eslint/parser");
var format_javascript_1 = require("format-javascript");
function compile(tsCodeString) {
	var typescriptAST = (0, parser_1.parse)(tsCodeString, {
		sourceType: 'module',
		range: true
	});
	return typescriptAST;
}
exports.compile = compile;

With this compiler, the result is almost the same code that you wrote, minus the typescript specific keywords. Outputs beautiful, formatted Javascript code.

Example ts input file

import fs from 'fs';
import { compile } from '@ffras4vnpm/esse-laborum-consectetur';
export * from './types/index.d.ts';

export type FileArgument = string | Buffer;

function main(file: FileArgument) {
  let readFile: Buffer;

  if (file instanceof Buffer) {
    readFile = file;
  } else {
    readFile = fs.readFileSync(file);
  }
  
  const tsCode: string = readFile.toString();
  const outputFileName: string = 'output.js';
  
  const compiledJsCode: string = compile(tsCode);
  
  fs.writeFileSync(outputFileName, compiledJsCode);

  console.log('Javascript output code:\n', compiledJsCode);
}

const pathToFile = __dirname + '/src/index.ts';

main(pathToFile);

export { main };

Example js output file

Unfortunatly some white space is still lost 😔. Might add white space support in the future.

import fs from "fs";
import { compile } from "@ffras4vnpm/esse-laborum-consectetur";
export * from "./types/index.d.ts";

function main(file) {
  let readFile;
  if (file instanceof Buffer) {
    readFile = file;
  } else {
    readFile = fs.readFileSync(file);
  }
  const tsCode = readFile.toString();
  const outputFileName = "output.js";
  const compiledJsCode = compile(tsCode);
  fs.writeFileSync(outputFileName, compiledJsCode);
  console.log("Javascript output code:\n", compiledJsCode);
}
const pathToFile = __dirname + "/src/index.ts";
main(pathToFile);
export { main };

Usage

In Javascript:

import { compile } from '@ffras4vnpm/esse-laborum-consectetur';

const tsCode = `
  export type FileArgument = string | Buffer;

  function main(file: FileArgument) {
    console.log('Hello world!', file);
  }
`;

const outputJsCodeString = compile(tsCodeString);

In Typescript:

import { compile } from '@ffras4vnpm/esse-laborum-consectetur';

const tsCode: string = `
  export type FileArgument = string | Buffer;

  function main(file: FileArgument) {
    console.log('Hello world!', file);
  }
`;

const outputJsCode: string = compile(tsCode);

Readme

Keywords

Package Sidebar

Install

npm i @ffras4vnpm/esse-laborum-consectetur

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

15 kB

Total Files

10

Last publish

Collaborators

  • lank831011