GithubHelp home page GithubHelp logo

jeremyben / tsc-prog Goto Github PK

View Code? Open in Web Editor NEW
51.0 2.0 3.0 565 KB

Create a tsconfig and compile Typescript files programmatically.

Home Page: https://www.npmjs.com/package/tsc-prog

License: MIT License

TypeScript 99.81% JavaScript 0.08% HTML 0.11%
typescript tsconfig definitions tsc bundling

tsc-prog's Introduction

tsc-prog

Build your TypeScript projects programmatically.

tsc-prog offers flexiblity and convenient options for your more complex production builds (less suited for development builds).

Getting started

npm i -D tsc-prog
yarn add -D tsc-prog

tsc-prog has no dependency. You just need typescript as a peer dependency.

You simply need to build ๐Ÿ‘ทโ€

Use tsc.build. Specify the basePath first, and either inherit from a tsconfig file or create a config from scratch.

const tsc = require('tsc-prog')

tsc.build({
	basePath: __dirname, // always required, used for relative paths
	configFilePath: 'tsconfig.json', // config to inherit from (optional)
	compilerOptions: {
		rootDir: 'src',
		outDir: 'dist',
		declaration: true,
		skipLibCheck: true,
	},
	include: ['src/**/*'],
	exclude: ['**/*.test.ts', '**/*.spec.ts'],
})

You can have a look at all the parameters here.

You need more access ๐Ÿ‘จโ€๐Ÿญ

The tsc.build function is made of the two following steps, which you can have access to :

  • Program creation with tsc.createProgramFromConfig.
  • Emitting files from program with tsc.emit.
const tsc = require('tsc-prog')

// Create the program
const program = tsc.createProgramFromConfig({
	basePath: process.cwd(),
	configFilePath: 'tsconfig.json',
})

// Do what you want with the program

// Actually compile typescript files
tsc.emit(program, { copyOtherToOutDir: true })

Addons

Clean ๐Ÿงน

Helps to address this issue.

We frequently need to delete the emitted files from a previous build, so a clean option recursively removes folders and files :

tsc.build({
	basePath: __dirname,
	configFilePath: 'tsconfig.json',
	clean: ['dist'], // accepts relative paths to `basePath` or absolute paths
})

You can also directly specify common targets from your compiler options :

tsc.build({
	basePath: __dirname,
	configFilePath: 'tsconfig.json',
	clean: { outDir: true, declarationDir: true },
})
Protections

The clean option protects you against deleting the following folders :

  • the specified basePath and all its parents (up to the root folder).
  • the current working directory and all its parents (up to the root folder).
  • the rootDir path if specified in the compiler options and all its parents (up to the root folder).

Copy non-typescript files ๐Ÿ—‚๏ธ

Helps to address this issue.

The copyOtherToOutDir option allows you to copy other files to outDir (well it says so) :

tsc.build({
	basePath: __dirname,
	configFilePath: 'tsconfig.json',
	compilerOptions: {
		outDir: 'dist', // must be set
	},
	copyOtherToOutDir: true,
	exclude: ['**/somedir'], // taken into account
})

This option is protected against overwriting files emitted by the compiler, like same name .js files (could happen).

Bundle type definitions ๐Ÿ›๏ธ

Helps to address this issue.

Rollup your emitted .d.ts files into a single one with bundleDeclaration option.

tsc.build({
	basePath: __dirname,
	configFilePath: 'tsconfig.json',
	compilerOptions: {
		rootDir: 'src',
		outDir: 'dist',
		declaration: true // must be set
	},
	bundleDeclaration: {
		entryPoint: 'index.d.ts', // relative to the OUTPUT directory ('dist' here)
	},
})

Bundling options

tsc.build({
	// ...
	bundleDeclaration: {
		entryPoint: 'index.d.ts',
		fallbackOnError: false, // default: true
		globals: false // default: true
		augmentations: false // default: true
	}
})
  • fallbackOnError option is a safety mecanism that generates the original unbundled definition files if any error happens during the bundling process.

  • globals option can be switched to false to discard global declarations.

  • augmentations option can be switched to false to discard external library augmentations.

Notes on bundling ๐Ÿ—ฃ๏ธ

I recommend you still check the final .d.ts output, declaration bundling being very complex, with a lot of edge cases and issues such as name conflict and handling of external libraries.

tsc-prog does its best to acknowledge every edge case. It covers ones that similar tools don't and probably vice versa. Don't hesitate to review API Extractor to see if it works better with your program.

tsc-prog's People

Contributors

jeremyben avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tsc-prog's Issues

Resolve external libraries

Thanks for the great work!
When using the rollup feature, I still have a lot of import statements, that import from external libraries. I would like those imported types to basically get "pulled in" and resolved on spot.

Is something like that possible?

This is an example index.d.ts generated with tsc-prog:

import { default as debugLib } from "@prisma/debug";
import { Engine } from "@prisma/engine-core";
import { PrismaClientKnownRequestError } from "@prisma/engine-core";
import { PrismaClientUnknownRequestError } from "@prisma/engine-core";
import { PrismaClientInitializationError } from "@prisma/engine-core";
import { PrismaClientRustPanicError } from "@prisma/engine-core";
import { GeneratorConfig } from "@prisma/generator-helper/dist/types";
import { DatasourceOverwrite } from "@prisma/engine-core/dist/NodeEngine";
import { RawValue } from "sql-template-tag";
import { Sql } from "sql-template-tag";
import { Value } from "sql-template-tag";
import { empty } from "sql-template-tag";
import { join } from "sql-template-tag";
import { raw } from "sql-template-tag";
import { sqltag } from "sql-template-tag";

declare type FieldKind = 'scalar' | 'object' | 'enum';

interface FieldDefault {
        name: string;
        args: any[];
    }

interface Field {
        kind: FieldKind;
        name: string;
        isRequired: boolean;
        isList: boolean;
        isUnique: boolean;
        isId: boolean;
        type: string;
        dbNames: string[] | null;
        isGenerated: boolean;
        hasDefaultValue: boolean;
        default?: FieldDefault | string | boolean | number;
        relationToFields?: any[];
        relationOnDelete?: string;
        relationName?: string;
        documentation?: string;
        [key: string]: any;
    }

interface uniqueIndex {
        name: string;
        fields: string[];
    }

Remove `console.log()` statements

I'm using this in a wholly programmatic context, on our backend. Spurious console.log() statements clutter up our log files. It'd be nice to just remove these altogether, or log them conditionally with something like the debug module.

Programatic access to diagnostic errors

Looking at the source, it doesn't appear as if there's any way to hook into the diagnostics logging.

Maybe allow caller to provide a logger function that is used in place of logDiagnostics()?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.