• Stars
    star
    125
  • Rank 286,335 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Bundling Tool for TypeScript

TypeScript-Bundle

A bundling tool for TypeScript.

logo

NPM package Build Status

$ npm install typescript-bundle -g
$ tsc-bundle ./index.ts

Overview

TypeScript-Bundle is a build tool for the TypeScript programming language. It layers the TypeScript compiler cli to enable direct bundling of modular TypeScript source code for immediate consumption in a browser.

This tool provides:

  • The ability to bundle directly from .ts, .tsx or tsconfig.json.
  • The ability to embed text, json, base64 and buffer assets as modules.
  • Support for custom pre-processing pipelines.
  • Supports TypeScript compiler versions as low as 1.8.0.

This tool is offered as is for anyone who finds it useful.

Docs

Options

$ tsc-bundle script.ts | script.tsx | tsconfig.json

  Examples: tsc-bundle index.ts
            tsc-bundle tsconfig.json
            tsc-bundle script.ts --exportAs Foo
            tsc-bundle index.ts --outFile bundle.js
            tsc-bundle index.ts --transform script.js

  Options:
    --outFile         Outputs the bundle with the give filepath.
    --target          Sets the ES Target for the bundle.
    --exportAs        Exports bundle exports as a global variable.
    --importAs        Imports global variable as a module (namespace).
    --importAsDefault Imports global variable as a module (default).
    --entryPoint      Overrides the default entry point for the bundle.
    --transform       Applies a transform to the bundle.
    --watch           Starts the compiler in watch mode.
    --debug           Prints debug information.

Usage

Bundle from source file

Point TypeScript-Bundle at a TypeScript source file to have it bundle that file and everything it imports. Will use the default compiler settings.

# output: ./script.js
$ tsc-bundle ./script.ts

# output: ./bundle.js
$ tsc-bundle ./script.ts --outFile ./bundle.js

Bundle from tsconfig.json

Point TypeScript-Bundle at a tsconfig.json to have it use the compiler settings within.

{
  "compilerOptions": {
    "strict": true,
    "target": "esnext",
    "lib": ["dom", "esnext"],
  },
  "files": ["script.ts"]
}
$ tsc-bundle ./src/tsconfig.json

# output: ./script.js
$ tsc-bundle ./src/tsconfig.json --outFile ./bundle.js

# output: ./bundle.js

Assets

Supported for TypeScript compiler versions 2.2.0 and higher.
import Content from 'text!./file.txt'

TypeScript-Bundle automatically bundles files with a special import scheme similar to WebPack's ts-loader. It supports text, json, base64, buffer and css directives that inform the bundler how to embed the asset.

import Text   from 'text!./file.txt'    // as 'string'
import Base64 from 'base64!./image.png' // as 'string | base64 encoded'
import Obj    from 'json!./file.json'   // as 'any'
import Buf    from 'buffer!./file.dat'  // as 'Uint8Array'
import Css    from 'css!./file.css'     // as 'string | @import concat'

Declarations

To import assets without compiler warnings, TypeScript requires you define an additional declaration file .d.ts that describes the file extension to type mappings. The following is an example of such a declaration.

declare module '*.txt' {
 const value: string
  export default value
}
declare module '*.json' {
  const value: any
  export default value
}
declare module '*.b64' {
 const value: string
  export default value
}
declare module '*.buf' {
 const value: Uint8Array
  export default value
}
declare module '*.css' {
 const value: string
  export default value
}

You can either add this declaration to your tsconfig.json or as a /// <reference path='extensions.d.ts' /> directive if bundling from script.

ExportAs

--exportAs GLOBAL

Assigns a global variable name to the bundle.

Example

// script.ts

export function add(a: number, b: number) { return a + b }
$ tsc-bundle script.ts --exportAs Foo
var Foo = (function() {  /* bundled code here */  })()

Now available to the page.

<script src='./script.js'></script>

<script> console.log(Foo.add(10, 20)) </script>

CommonJS

If you are bundling for nodejs, you can pass --exportAs commonjs that will make the bundle a nodejs module. Useful for keeping the internals of the bundle private while exposing a surface level API.

$ tsc-bundle ./script.ts --exportAs commonjs
module.exports = (function() {  /* bundled code here */  })()
// node: app.js
const Foo = require('./script')

ImportAs

--importAs GLOBAL=module

--importAsDefault GLOBAL=module

Adds global variables (such as those added to a web page when loading scripts via CDN) as internal modules.

Import React

# install react declaration files.

$ npm install @types/react
$ npm install @types/react-dom
// index.ts

import * as React    from 'react'
import * as ReactDOM from 'react-dom'
# bundle with --importAs. The convention is GLOBAL_NAME=MODULE_NAME.

$ tsc-bundle index.ts --importAs React=react --importAs ReactDOM=react-dom
<!-- adds global name React -->
<script src="./react.js"></script>
<!-- adds global name ReactDOM -->
<script src="./react-dom.js"></script>
<!-- the bundle -->
<script src="./index.js"></script>

It is possible to import as many global names as necessary.

importAs vs importAsDefault

TypeScript-Bundle provides two importAs schemes for importing global variables. The decision to select one over the other is purely down to the import semantics provided by @types/* declaration for that library.

importAs

--importAs THREE=three
import * as THREE from 'three'

importAsDefault

--importAsDefault THREE=three
import THREE from 'three'

Select the most appropriate based on the library you're importing.

EntryPoint

--entryPoint index2

Will override the default entry point for the bundle. By default, the last module within the bundle is treated as the default entry point and will be evaluated first. If this is undesirable, the --entryPoint option allows for the selection of any other module located within the bundle to be evaluated first. Useful when compiling with glob patterns such as "include": [ "src/**/*" ].

Transforms

--transform ./preprocessor.js

Allows for custom preprocessing of the bundle.

// ./preprocessor.js

// run on typescript AMD output.
module.exports.typescriptOutput = function(javascript_code) {
  // apply transformations here.

  return javascript_code
}

// run on typescript-bundle output.
module.exports.bundleOutput = function(javascript_code) {
  // apply transformations here.

  return javascript_code
}

Multiple preprocessing stages can be stacked if required.

$ tsc-bundle ./index.ts --transform ./a.js --transform ./b.js --transform ./c.js

Tasks

The following tasks are provided by this project.

$ npm run clean       # cleans this project.
$ npm run amd-loaders # rebuilds es loader templates. 
$ npm run build       # builds the bundler.
$ npm run pack        # packs the bundler.
$ npm run spec        # builds and runs the spec project.
$ npm run install-cli # installs the bundler cli globally.

More Repositories

1

typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
TypeScript
4,648
star
2

zero

A 3D renderer written in JavaScript and rendered to the terminal.
TypeScript
2,413
star
3

smoke

Run Web Servers in Web Browsers over WebRTC
TypeScript
520
star
4

hammer

Build Tool for Browser and Node Applications
TypeScript
234
star
5

threadbox

Recursive Worker Threads in NodeJS
TypeScript
229
star
6

linqbox

Language Integrated Query for JavaScript
TypeScript
130
star
7

typebox-codegen

Code Generation for TypeBox Types
TypeScript
120
star
8

sidewinder

Type Safe Micro Services for Node
TypeScript
59
star
9

blender-node

NodeJS binding to Blenders Python Scripting Environment
TypeScript
59
star
10

typebox-workbench

Type Transform Tool for Runtime Type Systems
TypeScript
46
star
11

reactor

Asynchronous Event Driven IO for .NET
C#
44
star
12

ts-8-bit

Using TypeScript's Type System to do 8-bit Arithmetic
TypeScript
37
star
13

tesseract

WebGL 2.0 GPGPU compute library for JavaScript.
TypeScript
32
star
14

fastify-typebox

Enhanced TypeBox support for Fastify
TypeScript
31
star
15

typescript.api

A typescript 0.9 compiler as a service api for nodejs.
TypeScript
27
star
16

black

A Software Rasterizer written in Rust
Rust
25
star
17

servicebox

Typed Web Services for NodeJS
TypeScript
22
star
18

esbuild-wasm-resolve

File Resolution for Esbuild running in the Browser
TypeScript
20
star
19

appex

develop nodejs web applications with typescript
TypeScript
16
star
20

carbon

Compatibility Layer for Node Deno and Bun
TypeScript
16
star
21

drift

Run Chrome from the Terminal
TypeScript
15
star
22

fs-effects

A library for composing various file, folder, shell and watch operations in node.
TypeScript
10
star
23

smoke-task

Runs JavaScript functions from a terminal
TypeScript
9
star
24

neuron

Neural network implemented in JavaScript
TypeScript
9
star
25

corsa

Asynchronous uni-directional channels in node using async iteration.
TypeScript
9
star
26

smoke-rs

lightweight async task and stream library for Rust
Rust
8
star
27

stream-cortex

real-time live video streaming experiments with node + ffmpeg
TypeScript
8
star
28

magnum

general purpose template engine for nodejs.
TypeScript
7
star
29

runtime-type-benchmarks

High Performance Validation Benchmarks for JavaScript
TypeScript
6
star
30

vector-cs

.NET opengl graphics library
C#
6
star
31

tasksmith

Task automation library for node.
TypeScript
5
star
32

phantom-network-service

run phantomjs as a network service
TypeScript
5
star
33

neuron-render

An experiment using neural networks to approximate various stages of graphics pipeline for the purpose of creating interesting things.
TypeScript
5
star
34

neuron-gpgpu

GPGPU based implementation of a multi layer perceptron network for the browser.
TypeScript
5
star
35

fpv32

Benchmarks for fast 32-bit floating point vector math for JavaScript.
TypeScript
5
star
36

statebox

An observable JavaScript state container
TypeScript
4
star
37

hexagon

WebGL 2.0 graphics renderer written in TypeScript
TypeScript
4
star
38

smoke-run

Runs shell commands on file system watch events.
TypeScript
4
star
39

crimson-rust

CSP experiments in the rust programming language
Rust
4
star
40

fsweb

Static HTTP development server with live reload on save.
TypeScript
4
star
41

pubsub-rs

simple tcp based pubsub for rust
Rust
3
star
42

crimson

Actor system in JavaScript
TypeScript
3
star
43

merc

blender scene renderer demo
TypeScript
3
star
44

bayes

An implementation of a naive bayes classifier in TypeScript
JavaScript
3
star
45

three-instanced-mesh

A reference project enabling geometry instancing for threejs materials
TypeScript
3
star
46

signature

Overloaded function signatures in JavaScript.
TypeScript
2
star
47

smoke-web

A static file server that live reloads on file change.
TypeScript
2
star
48

smoke-hub-appengine

messaging hub for webrtc targeting the google app engine standard environment.
Go
2
star
49

fsrun

Restart OS processes on file system watch events.
JavaScript
2
star
50

smoke-pack

A npm project provisioning and build system for browser, electron, node and library projects.
TypeScript
1
star
51

vlc.web.stream

Example and documentation about streaming from VLC to a browser.
JavaScript
1
star
52

taxman

simple book keeping application for nodejs
JavaScript
1
star
53

neuron-function-approximation

An experiment using neural networks to approximate pure functions
TypeScript
1
star
54

nx-transform

angular + threejs + css experiment
HTML
1
star
55

deno-minifb

Render 32-bit RGBA Buffers to Desktop Windows
Rust
1
star
56

vector-rs

vector math library and utilities for Rust.
Rust
1
star
57

brainfuck-rs

A brainfuck interpreter implemented in Rust.
Rust
1
star
58

pang

A simple dependency injection library for node
TypeScript
1
star