GithubHelp home page GithubHelp logo

skymakerolof / dts-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nomaed/dts-builder

0.0 1.0 0.0 72 KB

Builds a single d.ts file library from generated d.ts sources

License: MIT License

TypeScript 83.05% JavaScript 16.95%

dts-builder's Introduction

DTS-Builder

Modern NodeJS TypeScript Modern JavaScript MIT License

Project status

Note: This project is in mainteinance mode.

This tool was created to help with a TS library that I was writing. However, I am no longer part of this project and also it seems that there are actively maintained tools out there that do the job in a smarter way than concanetation + regexp replacements.

If you are using dts-builder and find bugs, I'd be happy to fix them. If you want to improve, please create pull requests or fork and hack away :)

Rollup

If you are using Rollup to bundle your code, consider using https://github.com/Swatinem/rollup-plugin-dts plugin, it looks quite promising.

Synopsis

Assembles a singe library definition file by concatenating and cleaning up generated .d.ts files.

Returns a promise with the names of bundles that have been exported.

Code Example

const path = require('path');
const dtsBuilder = require('dts-builder');

const projectRoot = path.resolve(__dirname, '../..');

dtsBuilder.generateBundles([
  {
    name: 'myLib',
    sourceDir: `${projectRoot}/ts-built/`,
    destDir: `${projectRoot}/dist`,
    externals: [
      `${projectRoot}/src/ext/external-lib.d.ts`,
      `${projectRoot}/src/lib/types.d.ts`
    ]
  }
]);

Running this script will read all TS declaration files from sourceDir (${sourceDir}/**/*.d.ts), concatenate them and clean up the result. It will then be wrapped with a name module declaration (declare module ${name} { ... }). The result will be saved as ${name}.d.ts in destDir.

Additionally, if externals array is provided, all the files that are referenced by it will be prepended to the top of the resulting library file as /// <reference path="${basename(externals[i])}" /> and these files will be copied to destDir alongside the library file.

Motivation

A project that I am involved in has a large library that is being build using concatenation of ES5 files into a single .js package. The team moved to ES6 with modules, and I wanted to take it one notch further, and convert the 12k lines of code to TypeScript, for all the benefits.

However, I didn't find a good way to generate a single definition file that will act as a library. I started by playing with the generated d.ts files for each transpiled file to see how this can be achieved, and I created this simple script to remove and modify the result so it will be parsed well by tsc and will allow importing of the namespace.

Installation

npm install --save-dev dts-builder

API Reference

module dtsBuilder {
    function generateBundles(bundles: Array<Bundle>): Promise<Array<string>>;

    interface Bundle {
        /**
          * Bundle name. Used for d.ts filename and the wrapping namespace.
          * @type {string}
          */
        name: string;

        /**
          * Location in which to search for *.d.ts files
          * @type {string}
          */
        sourceDir: string;

        /**
          * Location in which to save the output, under the name: `${name}.d.ts`
          * @type {string}
          */
        destDir: string;

        /**
          * List of external d.ts files that will be copied to the destination
          * directory, and will be referenced from withing the main d.ts file
          * @type {Array<string>}
          */
        externals?: Array<string>;

        /**
          * Wrap resulting definitions in a namespace. Set to false if files are already wrapped
          * by namespaces.
          * Default: true
          * @type {boolean}
          */
        wrap?: boolean;

        /**
          * If specified, library will exported as the given value, as an alias together
          * with the original name.
          * @type {string}
          */
        alias?: string;
    }
}

Tests

Currently test is basic, checking for required module and builds the generate-self package under two names.

Contributors

Feel free to fork, improve and send PRs if it's generic in nature.

License

MIT © Boris Aranovič

dts-builder's People

Contributors

nomaed avatar skymakerolof avatar ramana-chavakula avatar

Watchers

James Cloos avatar

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.