ncc

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

README

ncc

CI 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

  1. ```bash
  2. npm i -g @vercel/ncc
  3. ```

Usage


  1. ```bash
  2. $ ncc <cmd> <opts>
  3. ```
Eg:
  1. ```bash
  2. $ ncc build input.js -o dist
  3. ```

If building an .mjs or .js module inside a "type": "module" package boundary, an ES module output will be created automatically.

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

Note: If the input file is using a .cjs extension, then so will the corresponding output file.

This is useful for packages that want to use .js files as modules in native Node.js using

a "type": "module" in the package.json file.


Commands:

  1. ```
  2.   build <input-file> [opts]
  3.   run <input-file> [opts]
  4.   cache clean|dir|size
  5.   help
  6.   version
  7. ```

Options:

  1. ```
  2.   -o, --out [dir]          Output directory for build (defaults to dist)
  3.   -m, --minify             Minify output
  4.   -C, --no-cache           Skip build cache population
  5.   -s, --source-map         Generate source map
  6.   -a, --asset-builds       Build nested JS assets recursively, useful for
  7.                            when code is loaded as an asset eg for workers.
  8.   --no-source-map-register Skip source-map-register source map support
  9.   -e, --external [mod]     Skip bundling 'mod'. Can be used many times
  10.   -q, --quiet              Disable build summaries / non-error outputs
  11.   -w, --watch              Start a watched build
  12.   -t, --transpile-only     Use transpileOnly option with the ts-loader
  13.   --v8-cache               Emit a build using the v8 compile cache
  14.   --license [file]         Adds a file containing licensing information to the output
  15.   --stats-out [file]       Emit webpack stats as json to the specified output file
  16.   --target [es]            ECMAScript target to use for output (default: es2015)
  17.                            Learn more: https://webpack.js.org/configuration/target
  18.   -d, --debug              Show debug logs
  19. ```

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:

  1. ```bash
  2. $ ncc run input.js
  3. ```

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:

  1. ```json
  2. {
  3.   "compilerOptions": {
  4.     "target": "es2015",
  5.     "moduleResolution": "node"
  6.   }
  7. }
  8. ```

If typescript is found in devDependencies, that version will be used.

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


  1. ```js
  2. require('@vercel/ncc')('/path/to/input', {
  3.   // provide a custom cache path or disable caching
  4.   cache: "./custom/cache/path" | false,
  5.   // externals to leave as requires of the build
  6.   externals: ["externalpackage"],
  7.   // directory outside of which never to emit assets
  8.   filterAssetBase: process.cwd(), // default
  9.   minify: false, // default
  10.   sourceMap: false, // default
  11.   assetBuilds: false, // default
  12.   sourceMapBasePrefix: '../', // default treats sources as output-relative
  13.   // when outputting a sourcemap, automatically include
  14.   // source-map-support in the output file (increases output by 32kB).
  15.   sourceMapRegister: true, // default
  16.   watch: false, // default
  17.   license: '', // default does not generate a license file
  18.   v8cache: false, // default
  19.   quiet: false, // default
  20.   debugLog: false // default
  21. }).then(({ code, map, assets }) => {
  22.   console.log(code);
  23.   // Assets is an object of asset file names to { source, permissions, symlinks }
  24.   // expected relative to the output code (if any)
  25. })
  26. ```

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

  1. ```js
  2. {
  3.   // handler re-run on each build completion
  4.   // watch errors are reported on "err"
  5.   handler (({ err, code, map, assets }) => { ... })
  6.   // handler re-run on each rebuild start
  7.   rebuild (() => {})
  8.   // close the watcher
  9.   void close ();
  10. }
  11. ```

Caveats


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