Skip to content
/ ncc Public

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.

License

Notifications You must be signed in to change notification settings

vercel/ncc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Sep 9, 2019
a3d2860 · Sep 9, 2019
Mar 18, 2019
Aug 29, 2019
Feb 7, 2019
Jul 15, 2019
Jul 2, 2019
Sep 9, 2019
Sep 9, 2019
Mar 14, 2019
Jan 27, 2019
May 1, 2019
Dec 4, 2018
Dec 7, 2018
Jun 18, 2019
Sep 9, 2019
Aug 19, 2019
Aug 29, 2019

Repository files navigation

ncc

Build Status codecov

Simple CLI for compiling a Node.js module into a single file, together with all its dependencies, gcc-style.

Motivation

  • Publish minimal packages to npm
  • Only ship relevant app code to serverless environments
  • Don't waste time configuring bundlers
  • Generally faster bootup time and less I/O overhead
  • Compiled language-like experience (e.g.: go)

Design goals

  • Zero configuration
  • TypeScript built-in
  • Only supports Node.js programs as input / output
  • Support all Node.js patterns and npm modules

Usage

Installation

npm i -g @zeit/ncc

Usage

$ ncc build input.js -o dist

Outputs the Node.js compact build of input.js into dist/index.js.

Execution Testing

For testing and debugging, a file can be built into a temporary directory and executed with full source maps support with the command:

$ ncc run input.js

With TypeScript

The only requirement is to point ncc to .ts or .tsx files. A tsconfig.json file is necessary. Most likely you want to indicate es2015 support:

{
  "compilerOptions": {
    "target": "es2015",
    "moduleResolution": "node"
  }
}

Package Support

Some packages may need some extra options for ncc support in order to better work with the static analysis.

See package-support.md for some common packages and their usage with ncc.

Programmatically From Node.js

require('@zeit/ncc')('/path/to/input', {
  // provide a custom cache path or disable caching
  cache: "./custom/cache/path" | false,
  // externals to leave as requires of the build
  externals: ["externalpackage"],
  // directory outside of which never to emit assets
  filterAssetBase: process.cwd(), // default
  minify: false, // default
  sourceMap: false, // default
  sourceMapBasePrefix: '../', // default treats sources as output-relative
  // when outputting a sourcemap, automatically include
  // source-map-support in the output file (increases output by 32kB).
  sourceMapRegister: true, // default
  watch: false, // default
  v8cache: false, // default
  quiet: false, // default
  debugLog: false // default
}).then(({ code, map, assets }) => {
  console.log(code);
  // Assets is an object of asset file names to { source, permissions, symlinks }
  // expected relative to the output code (if any)
})

When watch: true is set, the build object is not a promise, but has the following signature:

{
  // handler re-run on each build completion
  // watch errors are reported on "err"
  handler (({ err, code, map, assets }) => { ... })
  // handler re-run on each rebuild start
  rebuild (() => {})
  // close the watcher
  void close ();
}

Caveats

  • Files / assets are relocated based on a static evaluator. Dynamic non-statically analyzable asset loads may not work out correctly

About

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Languages